How to resolve the algorithm Evolutionary algorithm step by step in the Ceylon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Evolutionary algorithm step by step in the Ceylon 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 Ceylon programming language

Source code in the ceylon programming language

import ceylon.random {
	
	DefaultRandom
}

shared void run() {
	
	value mutationRate = 0.05;
	value childrenPerGeneration = 100;
	value target = "METHINKS IT IS LIKE A WEASEL";
	value alphabet = {' ', *('A'..'Z')};
	value random = DefaultRandom();
	
	value randomLetter => random.nextElement(alphabet);
	
	function fitness(String a, String b) =>
			count {for([c1, c2] in zipPairs(a, b)) c1 == c2};
	
	function mutate(String string) =>
			String {
				for(letter in string) 
				if(random.nextFloat() < mutationRate) 
				then randomLetter 
				else letter
			};
	
	function makeCopies(String string) =>
			{for(i in 1..childrenPerGeneration) mutate(string)};
	
	function chooseFittest(String+ children) =>
			children
			.map((String element) => element->fitness(element, target))
			.max(increasingItem)
			.key;
	
	variable value parent = String {for(i in 1..target.size) randomLetter};
	variable value generationCount = 0;
	function display() => print("``generationCount``: ``parent``");
	
	display();
	while(parent != target) {
		parent = chooseFittest(parent, *makeCopies(parent));
		generationCount++;
		display();
	}
	
	print("mutated into target in ``generationCount`` generations!");
	
}


  

You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Raven programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the Python programming language
You may also check:How to resolve the algorithm Filter step by step in the Go programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Seed7 programming language