Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Resource.cs @ 7116

Last change on this file since 7116 was 7116, checked in by gkronber, 12 years ago

#1329: added a solution file, added missing overrides for GetHashCode(), removed tautological if-conditions, adapted prebuildevent script and added the build script

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