1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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.Linq;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
|
---|
27 | using HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition;
|
---|
28 | using HeuristicLab.Tests;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.ScheduleEncoding.Tests {
|
---|
31 | public class TestUtils {
|
---|
32 | public static JSMEncoding CreateTestJSM1() {
|
---|
33 | JSMEncoding result = new JSMEncoding();
|
---|
34 | ItemList<Permutation> jsm = new ItemList<Permutation>();
|
---|
35 | for (int i = 0; i < 6; i++)
|
---|
36 | jsm.Add(new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 2, 3, 4, 5 }));
|
---|
37 | result.JobSequenceMatrix = jsm;
|
---|
38 | return result;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static JSMEncoding CreateTestJSM2() {
|
---|
42 | JSMEncoding result = new JSMEncoding();
|
---|
43 | ItemList<Permutation> jsm = new ItemList<Permutation>();
|
---|
44 | for (int i = 0; i < 6; i++)
|
---|
45 | jsm.Add(new Permutation(PermutationTypes.Absolute, new int[] { 5, 4, 3, 2, 1, 0 }));
|
---|
46 | result.JobSequenceMatrix = jsm;
|
---|
47 | return result;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | public static PWREncoding CreateTestPWR1() {
|
---|
52 | PWREncoding result = new PWREncoding();
|
---|
53 | IntegerVector pwr = new IntegerVector(new int[] { 1, 0, 1, 1, 2, 0, 2, 2, 0 });
|
---|
54 | result.PermutationWithRepetition = pwr;
|
---|
55 | return result;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static PWREncoding CreateTestPWR2() {
|
---|
59 | PWREncoding result = new PWREncoding();
|
---|
60 | IntegerVector pwr = new IntegerVector(new int[] { 0, 1, 1, 0, 2, 0, 1, 2, 2 });
|
---|
61 | result.PermutationWithRepetition = pwr;
|
---|
62 | return result;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | public static ItemList<Job> CreateJobData() {
|
---|
67 | Job j1 = new Job(0, 0);
|
---|
68 | j1.Tasks = new ItemList<Task> {
|
---|
69 | new Task (0, 0, j1.Index, 1),
|
---|
70 | new Task (1, 1, j1.Index, 2),
|
---|
71 | new Task (2, 2, j1.Index, 1)
|
---|
72 | };
|
---|
73 |
|
---|
74 | Job j2 = new Job(1, 0);
|
---|
75 | j2.Tasks = new ItemList<Task> {
|
---|
76 | new Task (3, 2, j2.Index, 2),
|
---|
77 | new Task (4, 1, j2.Index, 1),
|
---|
78 | new Task (5, 0, j2.Index, 2)
|
---|
79 | };
|
---|
80 |
|
---|
81 | Job j3 = new Job(2, 0);
|
---|
82 | j3.Tasks = new ItemList<Task> {
|
---|
83 | new Task (6, 0, j3.Index, 1),
|
---|
84 | new Task (7, 2, j3.Index, 2),
|
---|
85 | new Task (8, 1, j3.Index, 1)
|
---|
86 | };
|
---|
87 |
|
---|
88 | return new ItemList<Job> { j1, j2, j3 };
|
---|
89 | }
|
---|
90 |
|
---|
91 | public static Schedule CreateTestSchedule1() {
|
---|
92 | Schedule result = DirectScheduleRandomCreator.Apply(3, 3, new PWREncoding(3, 3, new TestRandom(new int[] { 1, 0, 1, 1, 2, 0, 2, 2, 0 }, null)), CreateJobData());
|
---|
93 | return result;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static Schedule CreateTestSchedule2() {
|
---|
97 | Schedule result = DirectScheduleRandomCreator.Apply(3, 3, new PWREncoding(3, 3, new TestRandom(new int[] { 0, 1, 1, 0, 2, 0, 1, 2, 2 }, null)), CreateJobData());
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 | public static bool ScheduleEquals(Schedule actual, Schedule expected) {
|
---|
101 | return actual.Resources.Count == expected.Resources.Count &&
|
---|
102 | actual.Resources.Zip(expected.Resources, (a, e) => ResourceEquals(a, e)).All(_ => _);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static bool ResourceEquals(Resource actual, Resource expected) {
|
---|
106 | return actual.Index == expected.Index &&
|
---|
107 | actual.TotalDuration == expected.TotalDuration &&
|
---|
108 | actual.Tasks.Count == expected.Tasks.Count &&
|
---|
109 | actual.Tasks.Zip(expected.Tasks, (a, e) => TaskEquals(a, e)).All(_ => _);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static bool TaskEquals(ScheduledTask actual, ScheduledTask expected) {
|
---|
113 | return
|
---|
114 | actual.StartTime == expected.StartTime &&
|
---|
115 | actual.EndTime == expected.EndTime &&
|
---|
116 | actual.Duration == expected.Duration &&
|
---|
117 | actual.ResourceNr == expected.ResourceNr &&
|
---|
118 | actual.JobNr == expected.JobNr &&
|
---|
119 | actual.TaskNr == expected.TaskNr;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public static bool JSMEncodingEquals(JSMEncoding expected, JSMEncoding actual) {
|
---|
123 | if (expected.JobSequenceMatrix.Count != actual.JobSequenceMatrix.Count)
|
---|
124 | return false;
|
---|
125 | for (int i = 0; i < expected.JobSequenceMatrix.Count; i++) {
|
---|
126 | if (!PermutationEquals(expected.JobSequenceMatrix[i], actual.JobSequenceMatrix[i]))
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 | private static bool PermutationEquals(Permutation p1, Permutation p2) {
|
---|
132 | if (p1.Length != p2.Length)
|
---|
133 | return false;
|
---|
134 | for (int i = 0; i < p1.Length; i++) {
|
---|
135 | if (p1[i] != p2[i])
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public static bool PRWEncodingEquals(PWREncoding expected, PWREncoding actual) {
|
---|
142 | if (expected.PermutationWithRepetition.Length != actual.PermutationWithRepetition.Length)
|
---|
143 | return false;
|
---|
144 | for (int i = 0; i < expected.PermutationWithRepetition.Length; i++) {
|
---|
145 | if (expected.PermutationWithRepetition[i] != actual.PermutationWithRepetition[i])
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|