1 | using HeuristicLab.Permutation;
|
---|
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Permutation.Tests
|
---|
6 | {
|
---|
7 |
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | ///This is a test class for OrderCrossoverTest and is intended
|
---|
11 | ///to contain all OrderCrossoverTest Unit Tests
|
---|
12 | ///</summary>
|
---|
13 | [TestClass()]
|
---|
14 | public class OrderCrossoverTest {
|
---|
15 |
|
---|
16 |
|
---|
17 | private TestContext testContextInstance;
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | ///Gets or sets the test context which provides
|
---|
21 | ///information about and functionality for the current test run.
|
---|
22 | ///</summary>
|
---|
23 | public TestContext TestContext {
|
---|
24 | get {
|
---|
25 | return testContextInstance;
|
---|
26 | }
|
---|
27 | set {
|
---|
28 | testContextInstance = value;
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | #region Additional test attributes
|
---|
33 | //
|
---|
34 | //You can use the following additional attributes as you write your tests:
|
---|
35 | //
|
---|
36 | //Use ClassInitialize to run code before running the first test in the class
|
---|
37 | //[ClassInitialize()]
|
---|
38 | //public static void MyClassInitialize(TestContext testContext)
|
---|
39 | //{
|
---|
40 | //}
|
---|
41 | //
|
---|
42 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
43 | //[ClassCleanup()]
|
---|
44 | //public static void MyClassCleanup()
|
---|
45 | //{
|
---|
46 | //}
|
---|
47 | //
|
---|
48 | //Use TestInitialize to run code before running each test
|
---|
49 | //[TestInitialize()]
|
---|
50 | //public void MyTestInitialize()
|
---|
51 | //{
|
---|
52 | //}
|
---|
53 | //
|
---|
54 | //Use TestCleanup to run code after each test has run
|
---|
55 | //[TestCleanup()]
|
---|
56 | //public void MyTestCleanup()
|
---|
57 | //{
|
---|
58 | //}
|
---|
59 | //
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | ///A test for Cross
|
---|
65 | ///</summary>
|
---|
66 | [TestMethod()]
|
---|
67 | [DeploymentItem("HeuristicLab.Permutation-3.3.dll")]
|
---|
68 | public void OrderCrossoverCrossTest() {
|
---|
69 | TestRandom random = new TestRandom();
|
---|
70 | Permutation parent1, parent2, expected, actual;
|
---|
71 | ItemArray<Permutation> parents;
|
---|
72 | OrderCrossover_Accessor target = new OrderCrossover_Accessor(new PrivateObject(typeof(OrderCrossover)));
|
---|
73 | // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 55-56
|
---|
74 | random.Reset();
|
---|
75 | random.IntNumbers = new int[] { 3, 6 };
|
---|
76 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
77 | Assert.IsTrue(parent1.Validate());
|
---|
78 | parent2 = new Permutation(new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 });
|
---|
79 | Assert.IsTrue(parent2.Validate());
|
---|
80 | parents = new ItemArray<Permutation>(new Permutation[] { parent1, parent2 });
|
---|
81 | expected = new Permutation(new int[] { 2, 7, 1, 3, 4, 5, 6, 0, 8 });
|
---|
82 | Assert.IsTrue(expected.Validate());
|
---|
83 | actual = target.Cross(random, parents);
|
---|
84 | Assert.IsTrue(actual.Validate());
|
---|
85 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
86 | // The following test is based on an example from Larranaga, P. et al. 1999. Genetic Algorithms for the Travelling Salesman Problem: A Review of Representations and Operators. Artificial Intelligence Review, 13, pp. 129-170.
|
---|
87 | random.Reset();
|
---|
88 | random.IntNumbers = new int[] { 2, 4 };
|
---|
89 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
|
---|
90 | Assert.IsTrue(parent1.Validate());
|
---|
91 | parent2 = new Permutation(new int[] { 1, 3, 5, 7, 6, 4, 2, 0 });
|
---|
92 | Assert.IsTrue(parent2.Validate());
|
---|
93 | parents = new ItemArray<Permutation>(new Permutation[] { parent1, parent2 });
|
---|
94 | expected = new Permutation(new int[] { 7, 6, 2, 3, 4, 0, 1, 5 });
|
---|
95 | actual = target.Cross(random, parents);
|
---|
96 | Assert.IsTrue(actual.Validate());
|
---|
97 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
98 | // The following test is based on an example from Talbi, E.G. 2009. Metaheuristics - From Design to Implementation. Wiley, p. 218.
|
---|
99 | random.Reset();
|
---|
100 | random.IntNumbers = new int[] { 2, 5 };
|
---|
101 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
102 | Assert.IsTrue(parent1.Validate());
|
---|
103 | parent2 = new Permutation(new int[] { 7, 3, 0, 4, 8, 2, 5, 1, 6 });
|
---|
104 | Assert.IsTrue(parent2.Validate());
|
---|
105 | parents = new ItemArray<Permutation>(new Permutation[] { parent1, parent2 });
|
---|
106 | expected = new Permutation(new int[] { 0, 8, 2, 3, 4, 5, 1, 6, 7 });
|
---|
107 | Assert.IsTrue(expected.Validate());
|
---|
108 | actual = target.Cross(random, parents);
|
---|
109 | Assert.IsTrue(actual.Validate());
|
---|
110 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
111 | // The following test is not based on published examples
|
---|
112 | random.Reset();
|
---|
113 | random.IntNumbers = new int[] { 0, 5 };
|
---|
114 | parent1 = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
115 | Assert.IsTrue(parent1.Validate());
|
---|
116 | parent2 = new Permutation(new int[] { 5, 3, 4, 0, 9, 8, 2, 7, 1, 6 });
|
---|
117 | Assert.IsTrue(parent2.Validate());
|
---|
118 | parents = new ItemArray<Permutation>(new Permutation[] { parent1, parent2 });
|
---|
119 | expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 5, 0, 9 });
|
---|
120 | Assert.IsTrue(expected.Validate());
|
---|
121 | actual = target.Cross(random, parents);
|
---|
122 | Assert.IsTrue(actual.Validate());
|
---|
123 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
124 | // based on the previous with changed breakpoints
|
---|
125 | random.Reset();
|
---|
126 | random.IntNumbers = new int[] { 6, 9 };
|
---|
127 | expected = new Permutation(new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
|
---|
128 | Assert.IsTrue(expected.Validate());
|
---|
129 | actual = target.Cross(random, parents);
|
---|
130 | Assert.IsTrue(actual.Validate());
|
---|
131 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
132 | // another one based on the previous with changed breakpoints
|
---|
133 | random.Reset();
|
---|
134 | random.IntNumbers = new int[] { 0, 9 };
|
---|
135 | expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
136 | Assert.IsTrue(expected.Validate());
|
---|
137 | actual = target.Cross(random, parents);
|
---|
138 | Assert.IsTrue(actual.Validate());
|
---|
139 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
140 | // perform a test with more than two parents
|
---|
141 | random.Reset();
|
---|
142 | bool exceptionFired = false;
|
---|
143 | try {
|
---|
144 | target.Cross(random, new ItemArray<Permutation>(new Permutation[] { new Permutation(4), new Permutation(4), new Permutation(4)}));
|
---|
145 | } catch (System.InvalidOperationException) {
|
---|
146 | exceptionFired = true;
|
---|
147 | }
|
---|
148 | Assert.IsTrue(exceptionFired);
|
---|
149 | // perform a test when two permutations are of unequal length
|
---|
150 | random.Reset();
|
---|
151 | exceptionFired = false;
|
---|
152 | try {
|
---|
153 | target.Cross(random, new ItemArray<Permutation>(new Permutation[] { new Permutation(8), new Permutation(6) }));
|
---|
154 | } catch (System.ArgumentException) {
|
---|
155 | exceptionFired = true;
|
---|
156 | }
|
---|
157 | Assert.IsTrue(exceptionFired);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | ///A test for Apply
|
---|
162 | ///</summary>
|
---|
163 | [TestMethod()]
|
---|
164 | public void OrderCrossoverApplyTest() {
|
---|
165 | TestRandom random = new TestRandom();
|
---|
166 | Permutation parent1, parent2, expected, actual;
|
---|
167 | // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 55-56
|
---|
168 | random.Reset();
|
---|
169 | random.IntNumbers = new int[] { 3, 6 };
|
---|
170 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
171 | Assert.IsTrue(parent1.Validate());
|
---|
172 | parent2 = new Permutation(new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 });
|
---|
173 | Assert.IsTrue(parent2.Validate());
|
---|
174 | expected = new Permutation(new int[] { 2, 7, 1, 3, 4, 5, 6, 0, 8 });
|
---|
175 | Assert.IsTrue(expected.Validate());
|
---|
176 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
177 | Assert.IsTrue(actual.Validate());
|
---|
178 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
179 | // The following test is based on an example from Larranaga, P. et al. 1999. Genetic Algorithms for the Travelling Salesman Problem: A Review of Representations and Operators. Artificial Intelligence Review, 13, pp. 129-170.
|
---|
180 | random.Reset();
|
---|
181 | random.IntNumbers = new int[] { 2, 4 };
|
---|
182 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
|
---|
183 | Assert.IsTrue(parent1.Validate());
|
---|
184 | parent2 = new Permutation(new int[] { 1, 3, 5, 7, 6, 4, 2, 0 });
|
---|
185 | Assert.IsTrue(parent2.Validate());
|
---|
186 | expected = new Permutation(new int[] { 7, 6, 2, 3, 4, 0, 1, 5 });
|
---|
187 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
188 | Assert.IsTrue(actual.Validate());
|
---|
189 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
190 | // The following test is based on an example from Talbi, E.G. 2009. Metaheuristics - From Design to Implementation. Wiley, p. 218.
|
---|
191 | random.Reset();
|
---|
192 | random.IntNumbers = new int[] { 2, 5 };
|
---|
193 | parent1 = new Permutation(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
|
---|
194 | Assert.IsTrue(parent1.Validate());
|
---|
195 | parent2 = new Permutation(new int[] { 7, 3, 0, 4, 8, 2, 5, 1, 6 });
|
---|
196 | Assert.IsTrue(parent2.Validate());
|
---|
197 | expected = new Permutation(new int[] { 0, 8, 2, 3, 4, 5, 1, 6, 7 });
|
---|
198 | Assert.IsTrue(expected.Validate());
|
---|
199 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
200 | Assert.IsTrue(actual.Validate());
|
---|
201 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
202 | // The following test is not based on published examples
|
---|
203 | random.Reset();
|
---|
204 | random.IntNumbers = new int[] { 0, 5 };
|
---|
205 | parent1 = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
206 | Assert.IsTrue(parent1.Validate());
|
---|
207 | parent2 = new Permutation(new int[] { 5, 3, 4, 0, 9, 8, 2, 7, 1, 6 });
|
---|
208 | Assert.IsTrue(parent2.Validate());
|
---|
209 | expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 5, 0, 9 });
|
---|
210 | Assert.IsTrue(expected.Validate());
|
---|
211 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
212 | Assert.IsTrue(actual.Validate());
|
---|
213 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
214 | // based on the previous with changed breakpoints
|
---|
215 | random.Reset();
|
---|
216 | random.IntNumbers = new int[] { 6, 9 };
|
---|
217 | expected = new Permutation(new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
|
---|
218 | Assert.IsTrue(expected.Validate());
|
---|
219 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
220 | Assert.IsTrue(actual.Validate());
|
---|
221 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
222 | // another one based on the previous with changed breakpoints
|
---|
223 | random.Reset();
|
---|
224 | random.IntNumbers = new int[] { 0, 9 };
|
---|
225 | expected = new Permutation(new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
|
---|
226 | Assert.IsTrue(expected.Validate());
|
---|
227 | actual = OrderCrossover.Apply(random, parent1, parent2);
|
---|
228 | Assert.IsTrue(actual.Validate());
|
---|
229 | Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
|
---|
230 | // perform a test when the two permutations are of unequal length
|
---|
231 | random.Reset();
|
---|
232 | bool exceptionFired = false;
|
---|
233 | try {
|
---|
234 | OrderCrossover.Apply(random, new Permutation(8), new Permutation(6));
|
---|
235 | } catch (System.ArgumentException) {
|
---|
236 | exceptionFired = true;
|
---|
237 | }
|
---|
238 | Assert.IsTrue(exceptionFired);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /// <summary>
|
---|
242 | ///A test for OrderCrossover Constructor
|
---|
243 | ///</summary>
|
---|
244 | [TestMethod()]
|
---|
245 | public void OrderCrossoverConstructorTest() {
|
---|
246 | OrderCrossover target = new OrderCrossover();
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|