Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.ScheduleEncoding/3.3/JobSequenceMatrix/JSMEncoding.cs @ 6475

Last change on this file since 6475 was 6406, checked in by jhelm, 13 years ago

#1329: Applied suggestions from codereview. Added unit-tests. Renamed encoding-project.

File size: 3.0 KB
Line 
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
22using System.Text;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix {
29  [Item("JobSequenceMatrixEncoding", "Represents an encoding for a scheduling Problem using a list of job sequences to deliver scheduleinformation.")]
30  [StorableClass]
31  public class JSMEncoding : Item, IScheduleEncoding {
32    [Storable]
33    public ItemList<Permutation> JobSequenceMatrix { get; set; }
34
35    [StorableConstructor]
36    protected JSMEncoding(bool deserializing) : base(deserializing) { }
37    protected JSMEncoding(JSMEncoding original, Cloner cloner)
38      : base(original, cloner) {
39      this.JobSequenceMatrix = cloner.Clone(original.JobSequenceMatrix);
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new JSMEncoding(this, cloner);
43    }
44    public JSMEncoding()
45      : base() {
46      JobSequenceMatrix = new ItemList<Permutation>();
47    }
48
49    public override string ToString() {
50      StringBuilder sb = new StringBuilder();
51      sb.Append("[ ");
52
53      foreach (Permutation p in JobSequenceMatrix) {
54        sb.Append(p.ToString() + " \n");
55      }
56
57      sb.Append("]");
58      return sb.ToString();
59    }
60
61
62    public override bool Equals(object obj) {
63      if (obj.GetType() == typeof(JSMEncoding))
64        return AreEqual(this, obj as JSMEncoding);
65
66      return false;
67    }
68
69    private static bool AreEqual(JSMEncoding jSMEncoding1, JSMEncoding jSMEncoding2) {
70      if (jSMEncoding1.JobSequenceMatrix.Count != jSMEncoding2.JobSequenceMatrix.Count)
71        return false;
72      for (int i = 0; i < jSMEncoding1.JobSequenceMatrix.Count; i++) {
73        if (!AreEqual(jSMEncoding1.JobSequenceMatrix[i], jSMEncoding2.JobSequenceMatrix[i]))
74          return false;
75      }
76      return true;
77    }
78
79    private static bool AreEqual(Permutation p1, Permutation p2) {
80      if (p1.Length != p2.Length)
81        return false;
82      for (int i = 0; i < p1.Length; i++) {
83        if (p1[i] != p2[i])
84          return false;
85      }
86      return true;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.