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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "random" for Random

var target = "METHINKS IT IS LIKE A WEASEL"
var set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
var rand = Random.new()

// fitness:  0 is perfect fit.  greater numbers indicate worse fit.
var fitness = Fn.new { |a| (0...target.count).count { |i| a[i] != target[i] } }

// set m to mutation of p, with each character of p mutated with probability r
var mutate = Fn.new { |p, m, r|
    var i = 0
    for (ch in p) {
        if (rand.float(1) < r) {
            m[i] = set[rand.int(set.count)]
        } else {
            m[i] = ch
        }
        i = i + 1
    }
}

var parent = List.filled(target.count, "")
(0...parent.count).each { |i| parent[i] = set[rand.int(set.count)] }
var c = 20 // number of times to copy and mutate parent
var copies = List.filled(c, null)
for (i in 0...c) copies[i] = List.filled(parent.count, 0)
System.print(parent.join())
var best = fitness.call(parent)
while (best > 0) {
    for (cp in copies) mutate.call(parent, cp, 0.05)
    for (cp in copies) {
        var fm = fitness.call(cp)
        if (fm < best) {
            best = fm
            parent = cp.toList
            System.print(parent.join())
        }
    }
}


  

You may also check:How to resolve the algorithm Approximate equality step by step in the Groovy programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the SPL programming language
You may also check:How to resolve the algorithm Loops/While step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Empty directory step by step in the MS-DOS programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the XPL0 programming language