-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
162a0c8
commit a886433
Showing
5 changed files
with
169 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package gago | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGenerateOffsprings(t *testing.T) { | ||
var ( | ||
N = []int{0, 1, 3, 10} | ||
indis = makeIndividuals(10, 2, rand.New(rand.NewSource(time.Now().UnixNano()))) | ||
sel = SelTournament{3} | ||
cross = CrossPoint{2} | ||
rng = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
) | ||
for _, n := range N { | ||
var offsprings = generateOffsprings(n, indis, sel, cross, rng) | ||
if len(offsprings) != n { | ||
t.Error("GenerateOffsprings didn't produce the expected number of offsprings") | ||
} | ||
} | ||
} | ||
|
||
func TestConstantSizeModels(t *testing.T) { | ||
var ( | ||
// Testing framework for each model | ||
nbIndis = 10 | ||
ff = Float64Function{func(X []float64) float64 { | ||
sum := 0.0 | ||
for _, x := range X { | ||
sum += x | ||
} | ||
return sum | ||
}} | ||
init = InitUniformF{ | ||
Lower: -1, | ||
Upper: 1, | ||
} | ||
// Model configurations | ||
models = []Model{ | ||
ModGenerational{ | ||
Selector: SelTournament{3}, | ||
Crossover: CrossPoint{2}, | ||
Mutator: MutNormalF{0.1, 1}, | ||
MutRate: 0.2, | ||
}, | ||
ModSteadyState{ | ||
Selector: SelTournament{3}, | ||
Crossover: CrossPoint{2}, | ||
KeepBest: false, | ||
Mutator: MutNormalF{0.1, 1}, | ||
MutRate: 0.2, | ||
}, | ||
ModSteadyState{ | ||
Selector: SelTournament{3}, | ||
Crossover: CrossPoint{2}, | ||
KeepBest: true, | ||
Mutator: MutNormalF{0.1, 1}, | ||
MutRate: 0.2, | ||
}, | ||
ModDownToSize{ | ||
NbrOffsprings: 5, | ||
SelectorA: SelTournament{3}, | ||
Crossover: CrossPoint{2}, | ||
SelectorB: SelElitism{}, | ||
Mutator: MutNormalF{0.1, 1}, | ||
MutRate: 0.2, | ||
}, | ||
ModRing{ | ||
Crossover: CrossPoint{2}, | ||
Selector: SelTournament{3}, | ||
Mutator: MutNormalF{0.1, 1}, | ||
MutRate: 0.2, | ||
}, | ||
ModSimAnn{ | ||
Mutator: MutNormalF{0.1, 1}, | ||
T: 10, | ||
Tmin: 1, | ||
Alpha: 0.3, | ||
}, | ||
ModMutationOnly{ | ||
NbrParents: 3, | ||
Selector: SelTournament{2}, | ||
KeepParents: false, | ||
NbrOffsprings: 2, | ||
Mutator: MutNormalF{0.1, 1}, | ||
}, | ||
ModMutationOnly{ | ||
NbrParents: 3, | ||
Selector: SelTournament{2}, | ||
KeepParents: true, | ||
NbrOffsprings: 2, | ||
Mutator: MutNormalF{0.1, 1}, | ||
}, | ||
} | ||
) | ||
for _, model := range models { | ||
// Check the model parameters are valid | ||
var err = model.Validate() | ||
if err != nil { | ||
t.Error("The model doesn't contain valid parameters") | ||
} | ||
// Check the number of individuals didn't change | ||
var pop = makePopulation(nbIndis, 4, ff, init) | ||
model.Apply(&pop) | ||
var size = len(pop.Individuals) | ||
// Check the size of the population doesn't change | ||
for i := 0; i < 5; i++ { | ||
model.Apply(&pop) | ||
if len(pop.Individuals) != size { | ||
t.Error("The size of the population was modified") | ||
} | ||
} | ||
} | ||
} |