|
7 | 7 |
|
8 | 8 | "github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp-ea-decentralized/common/mlp" |
9 | 9 | "github.com/salvacorts/TFG-Parasitic-Metaheuristics/mlp/common/utils" |
| 10 | + "github.com/salvacorts/eaopt" |
10 | 11 | mv "github.com/salvacorts/go-perceptron-go/validation" |
11 | 12 |
|
12 | 13 | //mn "github.com/salvacorts/go-perceptron-go/model/neural" |
@@ -369,3 +370,54 @@ func TestCrossover(t *testing.T) { |
369 | 370 | t.Errorf("Failed to create second offspring") |
370 | 371 | } |
371 | 372 | } |
| 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