1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Text;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition {
|
---|
29 | [Item("PermutationWithRepetitionEncoding", "Represents a encoding for a standard JobShop Scheduling Problem.")]
|
---|
30 | [StorableClass]
|
---|
31 | public class PWREncoding : Item, IScheduleEncoding {
|
---|
32 | [Storable]
|
---|
33 | public IntegerVector PermutationWithRepetition { get; set; }
|
---|
34 |
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | protected PWREncoding(bool deserializing) : base(deserializing) { }
|
---|
38 | protected PWREncoding(PWREncoding original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | this.PermutationWithRepetition = cloner.Clone(original.PermutationWithRepetition);
|
---|
41 | }
|
---|
42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
43 | return new PWREncoding(this, cloner);
|
---|
44 | }
|
---|
45 | public PWREncoding()
|
---|
46 | : base() {
|
---|
47 | PermutationWithRepetition = new IntegerVector();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public PWREncoding(int nrOfJobs, int nrOfResources, IRandom random)
|
---|
51 | : base() {
|
---|
52 | PermutationWithRepetition = new IntegerVector(nrOfJobs * nrOfResources);
|
---|
53 | int[] lookUpTable = new int[nrOfJobs];
|
---|
54 |
|
---|
55 | for (int i = 0; i < PermutationWithRepetition.Length; i++) {
|
---|
56 | int newValue = random.Next(nrOfJobs);
|
---|
57 | while (lookUpTable[newValue] >= nrOfResources)
|
---|
58 | newValue = random.Next(nrOfJobs);
|
---|
59 |
|
---|
60 | PermutationWithRepetition[i] = newValue;
|
---|
61 |
|
---|
62 | lookUpTable[newValue]++;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override string ToString() {
|
---|
67 | StringBuilder sb = new StringBuilder();
|
---|
68 | sb.Append("[ ");
|
---|
69 | foreach (int i in PermutationWithRepetition) {
|
---|
70 | sb.Append(i + " ");
|
---|
71 | }
|
---|
72 | sb.Append("]");
|
---|
73 |
|
---|
74 | return sb.ToString();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override bool Equals(object obj) {
|
---|
78 | if (obj.GetType() == typeof(PWREncoding))
|
---|
79 | return AreEqual(this, obj as PWREncoding);
|
---|
80 | else
|
---|
81 | return base.Equals(obj);
|
---|
82 | }
|
---|
83 | public override int GetHashCode() {
|
---|
84 | if (PermutationWithRepetition.Length == 1)
|
---|
85 | return PermutationWithRepetition[0].GetHashCode();
|
---|
86 | if (PermutationWithRepetition.Length == 2)
|
---|
87 | return PermutationWithRepetition[0].GetHashCode() ^ PermutationWithRepetition[1].GetHashCode();
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | private bool AreEqual(PWREncoding pWREncoding1, PWREncoding pWREncoding2) {
|
---|
91 | if (pWREncoding1.PermutationWithRepetition.Length != pWREncoding2.PermutationWithRepetition.Length)
|
---|
92 | return false;
|
---|
93 | for (int i = 0; i < pWREncoding1.PermutationWithRepetition.Length; i++) {
|
---|
94 | if (pWREncoding1.PermutationWithRepetition[i] != pWREncoding2.PermutationWithRepetition[i])
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | }
|
---|
102 | }
|
---|