1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Random;
|
---|
25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.LinearLinkageEncoding.Tests {
|
---|
28 | [TestClass()]
|
---|
29 | public class ConversionsTest {
|
---|
30 | private readonly int[][] validForwardEncoding = new[]{
|
---|
31 | new int[0], new[] {0},
|
---|
32 | new[] {0, 1}, new[] {1, 1},
|
---|
33 | new[] {0, 1, 2}, new[] {1, 1, 2}, new[] {2, 1, 2},
|
---|
34 | new[] {0, 2, 2}, new[] {1, 2, 2},
|
---|
35 | new[] {0, 1, 2, 3}, new[] {1, 1, 2, 3}, new[] {2, 1, 2, 3}, new[] {3, 1, 2, 3},
|
---|
36 | new[] {0, 1, 3, 3}, new[] {1, 2, 2, 3}, new[] {2, 1, 3, 3}, new[] {3, 2, 2, 3},
|
---|
37 | new[] {0, 2, 2, 3}, new[] {1, 3, 2, 3},
|
---|
38 | new[] {0, 2, 3, 3}
|
---|
39 | };
|
---|
40 | private readonly int[][] invalidForwardEncoding = new[]{
|
---|
41 | new [] {-1}, new [] {1}, new [] {1, 0},
|
---|
42 | new [] {0, 0, 0}, new [] {0, 0, 1}, new [] {0, 0, 2},
|
---|
43 | new [] {0, 1, 0},
|
---|
44 | new [] {0, 2, 0}, new [] {0, 2, 1},
|
---|
45 | new [] {1, 0, 0}, new [] {1, 0, 1}, new [] {1, 0, 2},
|
---|
46 | new [] {1, 1, 0}, new [] {1, 1, 1},
|
---|
47 | new [] {1, 2, 0}, new [] {1, 2, 1},
|
---|
48 | new [] {2, 0, 0}, new [] {2, 0, 1}, new [] {2, 0, 2},
|
---|
49 | new [] {2, 1, 0}, new [] {2, 1, 1},
|
---|
50 | new [] {2, 2, 0}, new [] {2, 2, 1}, new [] {2, 2, 2},
|
---|
51 | };
|
---|
52 | private readonly int[][] invalidEndEncoding = new[]{
|
---|
53 | new [] {-1}, new [] {1}, new [] {1, 0},
|
---|
54 | new [] {0, 0, 1}, new [] {0, 0, 2},
|
---|
55 | new [] {0, 1, 0},
|
---|
56 | new [] {0, 2, 0}, new [] {0, 2, 1},
|
---|
57 | new [] {1, 0, 0}, new [] {1, 0, 1}, new [] {1, 0, 2},
|
---|
58 | new [] {1, 1, 0}, new [] {1, 1, 1},
|
---|
59 | new [] {1, 2, 0}, new [] {1, 2, 1},
|
---|
60 | new [] {2, 0, 0}, new [] {2, 0, 1}, new [] {2, 0, 2},
|
---|
61 | new [] {2, 1, 0}, new [] {2, 1, 1},
|
---|
62 | new [] {2, 2, 0}, new [] {2, 2, 1},
|
---|
63 | };
|
---|
64 |
|
---|
65 | [TestMethod()]
|
---|
66 | [TestCategory("Encodings.LinearLinkage")]
|
---|
67 | [TestProperty("Time", "short")]
|
---|
68 | public void EndLinksRoundtripTest() {
|
---|
69 | foreach (var values in validForwardEncoding) {
|
---|
70 | var expected = LinearLinkage.FromForwardLinks(values);
|
---|
71 | var llee = expected.ToEndLinks();
|
---|
72 | var actual = LinearLinkage.FromEndLinks(llee);
|
---|
73 | Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, actual),
|
---|
74 | "[{0}] did not roundtrip successfully!", string.Join(", ", values));
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | [TestMethod()]
|
---|
79 | [TestCategory("Encodings.LinearLinkage")]
|
---|
80 | [TestProperty("Time", "short")]
|
---|
81 | public void GroupsRoundtripTest() {
|
---|
82 | foreach (var values in validForwardEncoding) {
|
---|
83 | var expected = LinearLinkage.FromForwardLinks(values);
|
---|
84 | var groups = expected.GetGroups();
|
---|
85 | var actual = LinearLinkage.FromGroups(expected.Length, groups);
|
---|
86 | Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, actual),
|
---|
87 | "[{0}] did not roundtrip successfully!", string.Join(", ", values));
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | [TestMethod()]
|
---|
92 | [TestCategory("Encodings.LinearLinkage")]
|
---|
93 | [TestProperty("Time", "short")]
|
---|
94 | public void InvalidForwardLinksTest() {
|
---|
95 | foreach (var values in invalidForwardEncoding) {
|
---|
96 | var isValid = true;
|
---|
97 | try {
|
---|
98 | var lle = LinearLinkage.FromForwardLinks(values);
|
---|
99 | } catch (ArgumentException e) {
|
---|
100 | isValid = false;
|
---|
101 | }
|
---|
102 | Assert.IsFalse(isValid, "[{0}] is invalid and should throw ArgumentException!", string.Join(", ", values));
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | [TestMethod()]
|
---|
107 | [TestCategory("Encodings.LinearLinkage")]
|
---|
108 | [TestProperty("Time", "short")]
|
---|
109 | public void InvalidEndLinksTest() {
|
---|
110 | foreach (var values in invalidEndEncoding) {
|
---|
111 | var isValid = true;
|
---|
112 | try {
|
---|
113 | var lle = LinearLinkage.FromEndLinks(values);
|
---|
114 | } catch (ArgumentException e) {
|
---|
115 | isValid = false;
|
---|
116 | }
|
---|
117 | Assert.IsFalse(isValid, "{0} should be invalid", string.Join(", ", values));
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | [TestMethod]
|
---|
122 | [TestCategory("Encodings.LinearLinkage")]
|
---|
123 | [TestProperty("Time", "short")]
|
---|
124 | public void ConvertFromLLEtoLLEe() {
|
---|
125 | var random = new MersenneTwister(42);
|
---|
126 | var lle = MaxGroupsLinearLinkageCreator.Apply(random, 32, 8);
|
---|
127 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromEndLinks(lle.ToEndLinks())));
|
---|
128 | }
|
---|
129 |
|
---|
130 | [TestMethod]
|
---|
131 | [TestCategory("Encodings.LinearLinkage")]
|
---|
132 | [TestProperty("Time", "short")]
|
---|
133 | public void ConvertFromLLEtoLLEb() {
|
---|
134 | var random = new MersenneTwister(42);
|
---|
135 | var lle = MaxGroupsLinearLinkageCreator.Apply(random, 32, 8);
|
---|
136 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromBackLinks(lle.ToBackLinks())));
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|