Skip to content

Commit 9334035

Browse files
committed
Update README.md
1 parent e824a66 commit 9334035

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,45 @@ A genetic algorithm written in swift, limited to Foundation libraries in the hop
33

44
## Ussage
55

6-
See main.swift for a sample implementation.
6+
```swift
7+
8+
class TestFitnessCalculator : FitnessCalculator {
9+
10+
let solution = [1,5,2,1,6,3,1,7,8,2,5,7,3,8,1,2,5,7,3,8,9,2,4,6]
11+
12+
func calculateFitness(candidate: Candidate) -> Double {
13+
var sum = -24.0
14+
var rawGenome = candidate.getGenome().getGenome()
15+
for var index = 0; index < candidate.getGenome().length; index += 1 {
16+
let genomeValueAtIndex = rawGenome[index]
17+
18+
if Double(solution[index]) == (Double(Int(genomeValueAtIndex * 1000))/100) {
19+
sum += 1
20+
}
21+
22+
}
23+
24+
return sum
25+
}
26+
27+
}
28+
29+
var population = DefaultPopulation(model: DefaultPopulationModel(), candidateBreeder: DefaultCandidateBreeder(), fitnessCalculator: TestFitnessCalculator())
30+
31+
var itteration = 0
32+
33+
population.evolveUntilFit({ (fittest: Candidate) -> () in
34+
35+
itteration += 1
36+
print(itteration)
37+
print(fittest.getFitness())
38+
39+
}) { (fittest) -> () in
40+
print(fittest)
41+
}
42+
43+
44+
```
745

846
### Notes
9-
This is a port of an old Java library I wrote and has some relics of java programming patterns in it.
47+
This is a port of an old Java library I wrote and has some relics of java programming patterns in it.

0 commit comments

Comments
 (0)