Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7116 was 7116, checked in by gkronber, 13 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: 6.3 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;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ScheduleEncoding {
31  [Item("Schedule", "Represents the general solution for scheduling problems.")]
32  [StorableClass]
33  public class Schedule : NamedItem, IScheduleEncoding {
34    #region Properties
35    [Storable]
36    private ItemList<Resource> resources;
37    public ItemList<Resource> Resources {
38      get { return resources; }
39      set {
40        if (resources != null) DeregisterResourcesEvents();
41        resources = value;
42        if (resources != null) RegisterResourcesEvents();
43        OnResourcesChanged();
44      }
45    }
46    [Storable]
47    private DoubleValue quality;
48    public DoubleValue Quality {
49      get { return quality; }
50      set {
51        if (quality != value) {
52          if (quality != null) DeregisterQualityEvents();
53          quality = value;
54          if (quality != null) RegisterQualityEvents();
55          OnQualityChanged();
56        }
57      }
58    }
59    [Storable]
60    private Dictionary<int, ScheduledTask> lastScheduledTaskOfJob;
61    #endregion
62
63    [StorableConstructor]
64    protected Schedule(bool deserializing) : base(deserializing) { }
65    protected Schedule(Schedule original, Cloner cloner)
66      : base(original, cloner) {
67      this.Resources = cloner.Clone(original.Resources);
68      this.Quality = cloner.Clone(original.Quality);
69      this.lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>(original.lastScheduledTaskOfJob);
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new Schedule(this, cloner);
73    }
74    public Schedule(int nrOfResources) {
75      Resources = new ItemList<Resource>();
76      for (int i = 0; i < nrOfResources; i++) {
77        Resources.Add(new Resource(i));
78      }
79      lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>();
80    }
81
82
83
84    #region Events
85    public event EventHandler QualityChanged;
86    private void OnQualityChanged() {
87      var changed = QualityChanged;
88      if (changed != null)
89        changed(this, EventArgs.Empty);
90    }
91    private void RegisterQualityEvents() {
92      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
93    }
94    private void DeregisterQualityEvents() {
95      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
96    }
97    private void Quality_ValueChanged(object sender, EventArgs e) {
98      OnQualityChanged();
99    }
100
101    public event EventHandler ResourcesChanged;
102    private void OnResourcesChanged() {
103      var changed = ResourcesChanged;
104      if (changed != null)
105        changed(this, EventArgs.Empty);
106    }
107    private void RegisterResourcesEvents() {
108      Resources.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
109    }
110    private void DeregisterResourcesEvents() {
111      Resources.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
112    }
113    private void Resources_PropertyChanged(object sender, EventArgs e) {
114      OnResourcesChanged();
115    }
116    #endregion
117
118    public void ScheduleTask(int resNr, double startTime, double duration, int jobNr) {
119      ScheduledTask task = new ScheduledTask(resNr, startTime, duration, jobNr);
120      Resource affectedResource = resources[task.ResourceNr];
121      int i = 0;
122      while (i < affectedResource.Tasks.Count && affectedResource.Tasks[i].StartTime < task.StartTime)
123        i++;
124
125      if (!lastScheduledTaskOfJob.ContainsKey(jobNr)) {
126        lastScheduledTaskOfJob.Add(jobNr, task);
127        task.TaskNr = 0;
128      } else {
129        task.TaskNr = lastScheduledTaskOfJob[jobNr].TaskNr + 1;
130        lastScheduledTaskOfJob[jobNr] = task;
131      }
132
133      if (i >= affectedResource.Tasks.Count)
134        affectedResource.Tasks.Add(task);
135      else
136        affectedResource.Tasks.Insert(i, task);
137
138    }
139
140    public ScheduledTask GetLastScheduledTaskForJobNr(int jobNr) {
141      if (lastScheduledTaskOfJob.ContainsKey(jobNr))
142        return lastScheduledTaskOfJob[jobNr];
143      else
144        return null;
145    }
146
147    public override string ToString() {
148      StringBuilder sb = new StringBuilder();
149      sb.Append("[ ");
150      foreach (Resource r in Resources) {
151        sb.Append(r.ToString() + " \n");
152      }
153      sb.Append("]");
154      return sb.ToString();
155    }
156
157    public double CalculateMakespan() {
158      double quality = 0;
159      foreach (Resource r in Resources) {
160        if (r.TotalDuration > quality) {
161          quality = r.TotalDuration;
162        }
163      }
164      return quality;
165    }
166
167    public override bool Equals(object obj) {
168      if (obj.GetType() == typeof(Schedule))
169        return AreEqual(this, obj as Schedule);
170      else
171        return false;
172    }
173    public override int GetHashCode() {
174      if (Resources.Count == 1)
175        return Resources[0].GetHashCode();
176      if (Resources.Count == 2)
177        return Resources[0].GetHashCode() ^ Resources[1].GetHashCode();
178      return 0;
179    }
180
181    private static bool AreEqual(Schedule schedule1, Schedule schedule2) {
182      if (schedule1.Resources.Count != schedule2.Resources.Count)
183        return false;
184      for (int i = 0; i < schedule1.Resources.Count; i++) {
185        if (!schedule1.Resources[i].Equals(schedule2.Resources[i]))
186          return false;
187      }
188
189      return true;
190    }
191
192  }
193}
Note: See TracBrowser for help on using the repository browser.