How to resolve the algorithm Evolutionary algorithm step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Evolutionary algorithm step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Starting with:

Note: to aid comparison, try and ensure the variables and functions mentioned in the task description appear in solutions

A cursory examination of a few of the solutions reveals that the instructions have not been followed rigorously in some solutions. Specifically, Note that some of the the solutions given retain characters in the mutated string that are correct in the target string. However, the instruction above does not state to retain any of the characters while performing the mutation. Although some may believe to do so is implied from the use of "converges" Strictly speaking, the new parent should be selected from the new pool of mutations, and then the new parent used to generate the next set of mutations with parent characters getting retained only by not being mutated. It then becomes possible that the new set of mutations has no member that is fitter than the parent! As illustration of this error, the code for 8th has the following remark. NOTE: this has been changed, the 8th version is completely random now Clearly, this algo will be applying the mutation function only to the parent characters that don't match to the target characters! To ensure that the new parent is never less fit than the prior parent, both the parent and all of the latest mutations are subjected to the fitness test to select the next parent.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Evolutionary algorithm step by step in the PARI/GP programming language

Source code in the pari/gp programming language

target="METHINKS IT IS LIKE A WEASEL";
fitness(s)=-dist(Vec(s),Vec(target));
dist(u,v)=sum(i=1,min(#u,#v),u[i]!=v[i])+abs(#u-#v);
letter()=my(r=random(27)); if(r==26, " ", Strchr(r+65));
insert(v,x=letter())=
{
	my(r=random(#v+1));
	if(r==0, return(concat([x],v)));
	if(r==#v, return(concat(v,[x])));
	concat(concat(v[1..r],[x]),v[r+1..#v]);
}
delete(v)=
{
	if(#v<2, return([]));
	my(r=random(#v)+1);
	if(r==1, return(v[2..#v]));
	if(r==#v, return(v[1..#v-1]));
	concat(v[1..r-1],v[r+1..#v]);
}
mutate(s,rateM,rateI,rateD)=
{
	my(v=Vec(s));
	if(random(1.)
	if(random(1.)
	for(i=1,#v,
		if(random(1.)
	);
	concat(v);
}
evolve(C,rate)=
{
	my(parent=concat(vector(#target,i,letter())),ct=0);
	while(parent != target,
		print(parent" "fitness(parent));
		my(v=vector(C,i,mutate(parent,rate,0,0)),best,t);
		best=fitness(parent=v[1]);
		for(i=2,C,
			t=fitness(v[i]);
			if(t>best, best=t; parent=v[i])
		);
		ct++
	);
	print(parent" "fitness(parent));
	ct;
}
evolve(35,.05)

  

You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Qi programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm The Name Game step by step in the 11l programming language
You may also check:How to resolve the algorithm Combinations step by step in the Oz programming language