You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Genetic/README.markdown
+14-16Lines changed: 14 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ The randomization that allows for organisms to change over time. In GAs we build
29
29
### Problem
30
30
For this quick and dirty example, we are going to produce an optimized string using a simple genetic algorithm. More specifically we are trying to take a randomly generated origin string of a fixed length and evolve it into the most optimized string of our choosing.
31
31
32
-
We will be creating a bio-inspired world where the absolute existence is the string `Hello, World!`. Nothing in this universe is better and it's our goal to get as close to it as possible to ensure survival.
32
+
We will be creating a bio-inspired world where the absolute existence is the string `Hello, World!`. Nothing in this universe is better and it's our goal to get as close to it as possible to ensure survival.
33
33
34
34
### Define the Universe
35
35
@@ -73,8 +73,8 @@ let MUTATION_CHANCE = 100
73
73
return lexicon[rand]
74
74
}
75
75
```
76
-
77
-
**Note**: `arc4random_uniform` is strickly used in this example. It would be fun to play around with some of the [randomization in GameKit](https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html)
76
+
77
+
**Note**: `arc4random_uniform` is strictly used in this example. It would be fun to play around with some of the [randomization in GameKit](https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html)
78
78
79
79
### Population Zero
80
80
@@ -83,8 +83,6 @@ Before selecting, crossover and mutation, we need a population to start with. No
let total = items.reduce(0.0) { return$0+$1.weight}
130
128
131
129
var n =Double(arc4random_uniform(UInt32(total *1000000.0))) /1000000.0
132
130
133
-
foritemTuplein items {
134
-
if n <itemTuple.weight {
135
-
returnitemTuple
131
+
foritemin items {
132
+
if n <item.weight {
133
+
returnitem
136
134
}
137
-
n = n -itemTuple.weight
135
+
n = n -item.weight
138
136
}
139
137
return items[1]
140
138
}
@@ -144,7 +142,7 @@ The above function takes a list of individuals with their calculated fitness. Th
144
142
145
143
## Mutation
146
144
147
-
The all powerful mutation, the thing that introduces otherwise non exisitant fitness variance. It can either hurt of improve a individuals fitness but over time it will cause evolution towards more fit populations. Imagine if our initial random population was missing the charachter `H`, in that case we need to rely on mutation to introduce that character into the population in order to achive the optimal solution.
145
+
The all powerful mutation, the thing that introduces otherwise non existent fitness variance. It can either hurt of improve a individuals fitness but over time it will cause evolution towards more fit populations. Imagine if our initial random population was missing the charachter `H`, in that case we need to rely on mutation to introduce that character into the population in order to achieve the optimal solution.
@@ -184,7 +182,7 @@ The above is used to generate a completely new generation based on the current g
184
182
185
183
## Putting it all together -- Running the Genetic Algorithm
186
184
187
-
We now have all the functions we need to kick off the algorthim. Let's start from the beginning, first we need a random population to serve as a starting point. We will also initialize a fittest variable to hold the fittest individual, we will initialize it with the first individual of our random population.
185
+
We now have all the functions we need to kick off the algorithm. Let's start from the beginning, first we need a random population to serve as a starting point. We will also initialize a fittest variable to hold the fittest individual, we will initialize it with the first individual of our random population.
188
186
189
187
```swift
190
188
var population:[[UInt8]] =randomPopulation(from: lex, populationSize: POP_SIZE, dnaSize: DNA_SIZE)
@@ -202,7 +200,7 @@ for generation in 0...GENERATIONS {
202
200
Now, for each individual in the population, we need to calculate its fitness and weighted value. Since 0 is the best value we will use `1/fitness` to represent the weight. Note this is not a percent, but just how much more likely the value is to be selected over others. If the highest number was the most fit, the weight calculation would be `fitness/totalFitness`, which would be a percent.
203
201
204
202
```swift
205
-
var weightedPopulation = [(item:[UInt8], weight:Double)]()
203
+
var weightedPopulation = [(dna:[UInt8], weight:Double)]()
206
204
207
205
for individual in population {
208
206
let fitnessValue =calculateFitness(dna: individual, optimal: OPTIMAL)
@@ -224,7 +222,7 @@ The below loop is where we pull everything together. We loop for `POP_SIZE`, sel
224
222
let ind1 =weightedChoice(items: weightedPopulation)
225
223
let ind2 =weightedChoice(items: weightedPopulation)
226
224
227
-
let offspring =crossover(dna1: ind1.item, dna2: ind2.item, dnaSize: DNA_SIZE)
225
+
let offspring =crossover(dna1: ind1.dna, dna2: ind2.dna, dnaSize: DNA_SIZE)
0 commit comments