1 | using System;
|
---|
2 | using HeuristicLab.Random;
|
---|
3 | using HeuristicLab.Tests;
|
---|
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Encodings.LinearLinkageEncoding.Tests {
|
---|
7 | [TestClass()]
|
---|
8 | public class GroupCrossoverTest {
|
---|
9 | [TestMethod()]
|
---|
10 | [TestCategory("Encodings.LinearLinkage")]
|
---|
11 | [TestProperty("Time", "short")]
|
---|
12 | public void EqualParentsTest() {
|
---|
13 | var random = new FastRandom(0);
|
---|
14 | var parent = LinearLinkage.SingleElementGroups(10);
|
---|
15 | var child = GroupCrossover.Apply(random, parent, parent);
|
---|
16 | Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(parent, child));
|
---|
17 |
|
---|
18 | parent = RandomLinearLinkageCreator.Apply(random, 10);
|
---|
19 | child = GroupCrossover.Apply(random, parent, parent);
|
---|
20 | Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(parent, child));
|
---|
21 | }
|
---|
22 |
|
---|
23 | // Example from paper:
|
---|
24 | // Multi-objective Genetic Algorithms for grouping problems. Korkmaz, E.E. Applied Intelligence (2010) 33: 179.
|
---|
25 | [TestMethod()]
|
---|
26 | [TestCategory("Encodings.LinearLinkage")]
|
---|
27 | [TestProperty("Time", "short")]
|
---|
28 | public void ExampleFromPaperTest() {
|
---|
29 | var parent1 = LinearLinkage.FromEndLinks(new[] { 3, 4, 3, 3, 4 });
|
---|
30 |
|
---|
31 | var parent2 = LinearLinkage.FromEndLinks(new[] { 2, 2, 2, 4, 4 });
|
---|
32 |
|
---|
33 | CheckGroupCrossover(parent1, parent2, new[] { 3, 4, 2, 3, 4 }, new[] { 0.0, 0.0, 0.0, 0.0 }); // (i)
|
---|
34 | CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 3, 4 }, new[] { 0.0, 0.0, 0.0, 0.9 }); // (iv)
|
---|
35 | CheckGroupCrossover(parent1, parent2, new[] { 3, 2, 2, 3, 4 }, new[] { 0.0, 0.0, 0.9, 0.0 }); // (iii)
|
---|
36 | CheckGroupCrossover(parent1, parent2, new[] { 2, 2, 2, 3, 4 }, new[] { 0.0, 0.0, 0.9, 0.9 }); // (ii)
|
---|
37 | CheckGroupCrossover(parent1, parent2, new[] { 3, 4, 3, 3, 4 }, new[] { 0.0, 0.9 });
|
---|
38 | CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 4, 4 }, new[] { 0.9, 0.0, 0.0 });
|
---|
39 | CheckGroupCrossover(parent1, parent2, new[] { 2, 2, 2, 4, 4 }, new[] { 0.9, 0.0, 0.9 });
|
---|
40 | CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 4, 4, 4 }, new[] { 0.9, 0.9, 0.0, 0.0 });
|
---|
41 | CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 4, 4, 4 }, new[] { 0.9, 0.9, 0.0, 0.9 });
|
---|
42 | CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 4, 4 }, new[] { 0.9, 0.9, 0.9, 0.0 });
|
---|
43 | CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 2, 4, 4 }, new[] { 0.9, 0.9, 0.9, 0.9 });
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void CheckGroupCrossover(LinearLinkage parent1, LinearLinkage parent2, int[] expectedllee, double[] randomNumbers) {
|
---|
47 | var expected = LinearLinkage.FromEndLinks(expectedllee);
|
---|
48 | var random = new TestRandom() { DoubleNumbers = randomNumbers };
|
---|
49 | var child = GroupCrossover.Apply(random, parent1, parent2);
|
---|
50 | Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, child), "Expected [{0}] but was [{1}]!", string.Join(", ", expected), string.Join(", ", child));
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|