Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,44 @@
|
|||
···
|
||||
http://rosettacode.org/wiki/Evolutionary_algorithm
|
||||
···
|
||||
■ Evolutionary
|
||||
§ static
|
||||
target⦂ String: "METHINKS IT IS LIKE A WEASEL"
|
||||
letter⦂ char[]: "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".toCharArray°
|
||||
parent⦂ String
|
||||
random⦂ java.util.Random°
|
||||
rate⦂ double: 0.5
|
||||
C⦂ int: 1000
|
||||
|
||||
▶ fittness⦂ int · computes the 'closeness' of its
|
||||
• argument⦂ String · to the target string
|
||||
closeness⦂ int: 0
|
||||
∀ i ∈ 0 … target.length°
|
||||
closeness◥ if target.charAt i = argument.charAt i
|
||||
return closeness
|
||||
|
||||
▶ mutate⦂ String · returns a copy of the
|
||||
• given⦂ String · with some characters probably mutated
|
||||
• rate⦂ double
|
||||
copy⦂ char[]: given.toCharArray°
|
||||
∀ i ∈ 0 … given.length°
|
||||
copy[i]: letter[random.nextInt letter.length] if rate > random.nextDouble°
|
||||
return String.valueOf copy
|
||||
|
||||
▶ main
|
||||
• args⦂ String[]
|
||||
ancest⦂ StringBuilder°
|
||||
∀ i ∈ 0 … target.length°
|
||||
ancest.append letter[random.nextInt letter.length]
|
||||
parent: ancest.toString°
|
||||
currentFittness⦂ int: fittness parent
|
||||
generation⦂ int: 0
|
||||
🔁 until the parent ≈ target
|
||||
if fittness parent > currentFittness
|
||||
currentFittness: fittness parent
|
||||
System.out.println "Fittness of generation #⸨generation⸩ is ⸨currentFittness⸩"
|
||||
for each time from 1 to C
|
||||
mutation⦂ String: mutate parent, rate
|
||||
parent: mutation if fittness parent < fittness mutation
|
||||
generation◥
|
||||
System.out.println "Target reached by generation #⸨generation⸩"
|
||||
Loading…
Add table
Add a link
Reference in a new issue