Evolutionary algorithm: Difference between revisions

Added Easylang
(Added Easylang)
 
(4 intermediate revisions by 3 users not shown)
Line 685:
380: METHINKS ITBIS LIKE A WEASEL, fitness: 27
Final ( 384): METHINKS IT IS LIKE A WEASEL, fitness: 28</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">PUT "ABCDEFGHIJKLMNOPQRSTUVWXYZ " IN alphabet
 
HOW TO RETURN initial.state target:
SHARE alphabet
PUT "" IN state
FOR c IN target: PUT state^choice alphabet IN state
RETURN state
 
HOW TO RETURN state fitness target:
PUT #target IN score
FOR i IN {1..#target}:
IF state item i = target item i: PUT score-1 IN score
RETURN score
 
HOW TO RETURN chance mutate state:
SHARE alphabet
PUT "" IN mutated
FOR i IN {1..#state}:
SELECT:
random < chance: PUT choice alphabet IN next
ELSE: PUT state item i IN next
PUT mutated^next IN mutated
RETURN mutated
 
HOW TO EVOLVE TOWARD target:
PUT 0.1 IN mutation.rate
PUT 100 IN generation.size
PUT initial.state target IN state
WHILE state fitness target > 0:
WRITE (state fitness target)>>2, ": ", state/
PUT {} IN next.generation
FOR i IN {1..generation.size}:
PUT mutation.rate mutate state IN child
PUT child fitness target IN score
PUT child IN next.generation[score]
PUT next.generation[min keys next.generation] IN state
WRITE (state fitness target)>>2, ": ", state/
 
EVOLVE TOWARD "METHINKS IT IS LIKE A WEASEL"</syntaxhighlight>
{{out}}
<pre>27: CYPPYRQHMACPLQIZLPETRJVVPYLI
26: CYPPYRQHMICPLQIZLPEDRJVVPILI
25: CYKPYRQH ICPLWIZLPEDRJVJPILI
24: CWKPWWQH ICPLSIZLPEDRJVJPILI
23: CWKPWWQH ICPLSIZLPEDR BJPILI
21: CWKPWWQH ICPLSIZLSEDA BJA LI
20: JWKPWWKH ICPLSIZLSEDA BJA LK
19: WWKPIWKG ICPLSIZLSEDA BJA LK
18: WWKPIWKG ICPLSILLSEDA BJA LK
17: WWKPIWKG ICPLSILISEDA BJA LK
17: IWMPIWKG ICPLSILISEDA BJA LK
16: IWMPIWKG ICPLSILISEDA WJA LK
15: IWMPIWKG ICPLSILISEDA WEA LK
...
1: METHINKS IT IS LIKE A WEAS L
1: METHINKS IT IS LIKE A WEAS L
1: METHINKS IT IS LIKE A WEAS L
0: METHINKS IT IS LIKE A WEASEL</pre>
 
=={{header|Aime}}==
Line 2,521 ⟶ 2,581:
Generation 23, dist= 1: METHINKS IT IS LIKE A WEASEW
Generation 24, dist= 0: METHINKS IT IS LIKE A WEASEL</pre>
 
=={{header|Dart}}==
{{trans|Java}}
<syntaxhighlight lang="Dart">
import 'dart:math';
 
class EvoAlgo {
static final String target = "METHINKS IT IS LIKE A WEASEL";
static final List<String> possibilities = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".split('');
static int c = 100; // Number of spawn per generation
static double minMutateRate = 0.09;
static int perfectFitness = target.length;
static String parent = '';
static Random rand = Random();
 
static int fitness(String trial) {
int retVal = 0;
for (int i = 0; i < trial.length; i++) {
if (trial[i] == target[i]) retVal++;
}
return retVal;
}
 
static double newMutateRate() {
return (((perfectFitness - fitness(parent)) / perfectFitness) * (1 - minMutateRate));
}
 
static String mutate(String parent, double rate) {
String retVal = '';
for (int i = 0; i < parent.length; i++) {
retVal += (rand.nextDouble() <= rate)
? possibilities[rand.nextInt(possibilities.length)]
: parent[i];
}
return retVal;
}
 
static void main() {
parent = mutate(target, 1);
int iter = 0;
while (parent != target) {
double rate = newMutateRate();
iter++;
if (iter % 100 == 0) {
print('$iter: $parent, fitness: ${fitness(parent)}, rate: $rate');
}
String bestSpawn;
int bestFit = 0;
for (int i = 0; i < c; i++) {
String spawn = mutate(parent, rate);
int fit = fitness(spawn);
if (fit > bestFit) {
bestSpawn = spawn;
bestFit = fit;
}
}
if (bestFit > fitness(parent)) {
parent = bestSpawn;
}
}
print('$parent, $iter');
}
}
 
void main() {
EvoAlgo.main();
}
</syntaxhighlight>
{{out}}
<pre>
100: MFTHINFU IR N WLEKWKA WQZKYM, fitness: 13, rate: 0.4875
200: MXTHINPJ HR NNTLEKF A WDZHEH, fitness: 14, rate: 0.455
300: YNTHINPJ IK IS LEKNNA ADZHEL, fitness: 16, rate: 0.39
400: XETHI PS IKXIS LBKNDA QEUSEL, fitness: 18, rate: 0.325
500: NETHINPS I IS IBKLNA WEASEL, fitness: 21, rate: 0.2275
METHINKS IT IS LIKE A WEASEL, 551
 
</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Evolutionary_algorithm#Pascal Pascal].
Line 2,569 ⟶ 2,708:
 
weasel()</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
target$ = "METHINKS IT IS LIKE A WEASEL"
abc$[] = strchars " ABCDEFGHIJLKLMNOPQRSTUVWXYZ"
P = 0.05
C = 100
func fitness trial$ .
for i to len trial$
res += if substr trial$ i 1 <> substr target$ i 1
.
return res
.
func$ mutate parent$ .
for c$ in strchars parent$
if randomf < P
res$ &= abc$[randint len abc$[]]
else
res$ &= c$
.
.
return res$
.
for i to len target$
parent$ &= abc$[randint len abc$[]]
.
while fitness parent$ > 0
copies$[] = [ ]
for i to C
copies$[] &= mutate parent$
.
parent$ = copies$[1]
for s$ in copies$[]
if fitness s$ < fitness parent$
parent$ = s$
.
.
step += 1
print step & " " & parent$
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 4,445 ⟶ 4,625:
=={{header|jq}}==
{{Works with|jq}}
 
'''Works with gojq, the Go implementation of jq(*)'''
 
'''Works with jaq, the Rust implementation of jq(*)'''
 
In this entry, the "fitness" score is based on codepoint differences;
Line 4,457 ⟶ 4,641:
< /dev/random tr -cd '0-9' | fold -w 3 | $JQ -cnr -f evolutionary-algorithm.jq
</pre>
 
(*) For gojq and jaq, leading 0s must be stripped from the input, e.g. by
`sed -e '/^0./s/0//' -e '/^0./s/0//'`.
 
<syntaxhighlight lang="jq">
# Assumption: input consists of random three-digit numbers i.e. 000 to 999
Line 4,485 ⟶ 4,673:
# Output: a mutation of . such that each character is mutated with probability $r
def mutate($r):
(set|length) as $length
# Output: a pseudo-random character from set
# $n should be a random number drawn from range(0; N) inclusive where N > set|length
| def letter($n):
($n % $(set|length)) as $i
| set[$i:$i+1];
 
. as $p
Line 4,509 ⟶ 4,696:
| min_by( fitness($gold) );
 
# Evolve towards the target string provided as input, using $r as the mutation rate;
# `recurse` is used in order to show progress conveniently.
def evolve($r):
. as $gold
| (set|length * " ") as $strings
| (reduce range(0; $n) as $i (""; (rand % $s) as $j | . + set[$j:$j+1])) as $string
| {count: 0, $string }
| recurse (
Line 4,525 ⟶ 4,713:
{{output}}
<pre>
{"count":0,"string":"OIRSP AVNMTSSKOEIRBGDXZUBIQL"}
{"count":1,"string":"OIRSP AV E AE LE MTSSKOEIRBGDXZUBIQL"}
{"count":2,"string":"OIRSP AV MTSAKOEIRBGD JE G WEAE LE W ZUBIQL"}
{"count":3,"string":"OIRSPCAV MTSAWOEIRBGD ZJE G B WEAE QE WBZUBIQL"}
{"count":4,"string":"OIRJACAV MTEAWOEIRBGD WDJE G BF T WEME QE WBZUBIQL"}
{"count":5,"string":"OIRJACTV DWDJEIGMTEAWOEIRBGD BF NT WEME QE WIZGBIQL"}
{"count":6,"string":"OIRJBCTV IWDJEIAMTEAWAEIRBGD BF NT WEME QESQWIZGBIJL"}
{"count":7,"string":"JIWDJEIAOIRJBCTV BFMTEAWAEIRBGD NT WEME QESQWIZGBTJL"}
{"count":8,"string":"JIWDJEIAUIRJBCTV BFMT NTAWFEIRBGG WEME C QESQWIZGBTJL"}
{"count":9,"string":"JIWDJEIAUIRJBMTV BVMT NTAWFEIRBGG WEME C QESQBIZGBTJL"}
{"count":10,"string":"JIWDJEIAJIRJEMTV BVMT QTAWFEIKGGG WEME C QEBQBRZGBTJL"}
{"count":11,"string":"JIRJEMKV MT AWFEIKGGG XGBTJL"}
{"count":12,"string":"JIRJEMKV MT AWFEIKGGG XGBTAL"}
{"count":13,"string":"JIRJEMKV MT KWFEIKGGG XGBTAL"}
{"count":14,"string":"JFRJEMKP MT KWFEIKGGG XGBTAL"}
{"count":15,"string":"JFRJEMKP MT KWFJIKGGG XGBTAL"}
{"count":16,"string":"JFRJEMKT MT KQFJIKGGG XGBTAL"}
{"count":17,"string":"JFRJEMKT MT HQFJIKGGG XGBTAL"}
{"count":18,"string":"JFRJEMKT MT HQFJIKGGC XGBTAL"}
{"count":19,"string":"LFRJEMKT MT HQFJIIGGC XGATFL"}
{"count":20,"string":"LFRJIMKT MT HQFJIIGGC XGATFL"}
...
{"count":9061,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9162,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9263,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9364,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9465,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9566,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9667,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9768,"string":"METHJNKSMFTHINKS ITJT ISIR LIKE A WEASELWEAREL"}
{"count":9869,"string":"METHJNKSMFTHINKS IT ISIR LIKE A WEASELWEAREL"}
{"count":9970,"string":"METHJNKSMFTHINKS IT ISIR LIKE A WEASELWEAREL"}
{"count":10071,"string":"METHJNKSMFTHINKS IT IS LIKE A WEASELWEAREL"}
{"count":10172,"string":"METHJNKSMFTHINKS IT IS LIKE A WEASELWEAREL"}
{"count":10273,"string":"METHJNKSMFTHINKS IT IS LIKE A WEASELWEAREL"}
{"count":10374,"string":"METHJNKSMFTHINKS IT IS LIKE A WEASELWEAREL"}
{"count":10475,"string":"METHINKSMFTHINKS IT IS LIKE A WEASELWEAREL"}
{"count":76,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":77,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":78,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":79,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":80,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":81,"string":"METHINKS IT IS LIKE A WEAREL"}
{"count":82,"string":"METHINKS IT IS LIKE A WEASEL"}
</pre>
 
2,083

edits