1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.ScheduleEncoding {
|
---|
28 | [Item("ResourceClass", "Represents a resource used in scheduling problems.")]
|
---|
29 | [StorableClass]
|
---|
30 | public class Resource : Item {
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | public int Index {
|
---|
34 | get;
|
---|
35 | set;
|
---|
36 | }
|
---|
37 | [Storable]
|
---|
38 | public ItemList<ScheduledTask> Tasks {
|
---|
39 | get;
|
---|
40 | set;
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected Resource(bool deserializing) : base(deserializing) { }
|
---|
45 | protected Resource(Resource original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | this.Index = original.Index;
|
---|
48 | this.Tasks = cloner.Clone(original.Tasks);
|
---|
49 | }
|
---|
50 | public Resource(int index)
|
---|
51 | : base() {
|
---|
52 | Index = index;
|
---|
53 | Tasks = new ItemList<ScheduledTask>();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new Resource(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public double TotalDuration {
|
---|
61 | get {
|
---|
62 | double result = 0;
|
---|
63 | foreach (ScheduledTask t in Tasks) {
|
---|
64 | if (t.EndTime > result)
|
---|
65 | result = t.EndTime;
|
---|
66 | }
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override string ToString() {
|
---|
72 | StringBuilder sb = new StringBuilder();
|
---|
73 | sb.Append("Resource#" + Index + " [ ");
|
---|
74 | foreach (ScheduledTask t in Tasks) {
|
---|
75 | sb.Append(t.ToString() + " ");
|
---|
76 | }
|
---|
77 | sb.Append("]");
|
---|
78 | return sb.ToString();
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | public override bool Equals(object obj) {
|
---|
83 | if (obj.GetType() == typeof(Resource))
|
---|
84 | return AreEqual(this, obj as Resource);
|
---|
85 | else
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override int GetHashCode() {
|
---|
90 | if (Tasks.Count == 1)
|
---|
91 | return Tasks[0].GetHashCode();
|
---|
92 | if (Tasks.Count == 2)
|
---|
93 | return Tasks[0].GetHashCode() ^ Tasks[1].GetHashCode();
|
---|
94 | return 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private static bool AreEqual(Resource res1, Resource res2) {
|
---|
98 | if (res1.Tasks.Count != res2.Tasks.Count)
|
---|
99 | return false;
|
---|
100 | for (int i = 0; i < res1.Tasks.Count; i++) {
|
---|
101 | if (!res1.Tasks[i].Equals(res2.Tasks[i]))
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|