Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Tests/HeuristicLab.Encodings.ScheduleEncoding-3.3/TestUtils.cs @ 14877

Last change on this file since 14877 was 13469, checked in by mkommend, 9 years ago

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

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