Skip to content

Commit e02bdd6

Browse files
committed
Add sort function test
1 parent 06d497d commit e02bdd6

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎mlp-ea-decentralized/common/mlp/tests/geneticOperators_test.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp-ea-decentralized/common/mlp"
99
"github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp/common/utils"
10+
"github.com/salvacorts/eaopt"
1011
mv "github.com/salvacorts/go-perceptron-go/validation"
1112

1213
//mn "github.com/salvacorts/go-perceptron-go/model/neural"
@@ -369,3 +370,54 @@ func TestCrossover(t *testing.T) {
369370
t.Errorf("Failed to create second offspring")
370371
}
371372
}
373+
374+
func TestSort(t *testing.T) {
375+
indis := []eaopt.Individual{
376+
eaopt.Individual{ // A: 99 fitness, 15 neurons
377+
ID: "A",
378+
Fitness: 99.991,
379+
Genome: &mlp.MultiLayerNetwork{
380+
NeuralLayers: []mlp.NeuralLayer{ // 15 neurons
381+
mlp.NeuralLayer{Length: 5},
382+
mlp.NeuralLayer{Length: 10},
383+
},
384+
},
385+
},
386+
387+
eaopt.Individual{ // B: 10 fitness, 5 neurons
388+
ID: "B",
389+
Fitness: 10.0,
390+
Genome: &mlp.MultiLayerNetwork{
391+
NeuralLayers: []mlp.NeuralLayer{ // 15 neurons
392+
mlp.NeuralLayer{Length: 2},
393+
mlp.NeuralLayer{Length: 3},
394+
},
395+
},
396+
},
397+
398+
eaopt.Individual{ // C: 99 fitness, 10 neurons
399+
ID: "C",
400+
Fitness: 99.999,
401+
Genome: &mlp.MultiLayerNetwork{
402+
NeuralLayers: []mlp.NeuralLayer{ // 10 neurons
403+
mlp.NeuralLayer{Length: 5},
404+
mlp.NeuralLayer{Length: 5},
405+
},
406+
},
407+
},
408+
}
409+
410+
// Before: [A, B, C]
411+
412+
// TODO: I think I'm overwritting the original individuals
413+
indis = mlp.SortByFitnessAndNeurons(indis, 100)
414+
415+
// Expected: [B, C, A]
416+
expected := []string{"B", "C", "A"}
417+
418+
for i, in := range indis {
419+
if in.ID != expected[i] {
420+
t.Errorf("Not sorted properly. Expected (%s), got (%s)", expected[i], in.ID)
421+
}
422+
}
423+
}

0 commit comments

Comments
 (0)