Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Schedule.cs @ 10476

Last change on this file since 10476 was 10476, checked in by gkronber, 11 years ago

#2127: removed Equals() and GetHashCode() overrides as well as related static methods.
Changed ctors of Task and Job to prevent calling virtual members.

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
35    #region Properties
36    [Storable]
37    private ItemList<Resource> resources;
38    public ItemList<Resource> Resources {
39      get { return resources; }
40      set {
41        if (resources != null) DeregisterResourcesEvents();
42        resources = value;
43        if (resources != null) RegisterResourcesEvents();
44        OnResourcesChanged();
45      }
46    }
47    [Storable]
48    private DoubleValue quality;
49    public DoubleValue Quality {
50      get { return quality; }
51      set {
52        if (quality != value) {
53          if (quality != null) DeregisterQualityEvents();
54          quality = value;
55          if (quality != null) RegisterQualityEvents();
56          OnQualityChanged();
57        }
58      }
59    }
60    [Storable]
61    private Dictionary<int, ScheduledTask> lastScheduledTaskOfJob;
62    #endregion
63
64    [StorableConstructor]
65    protected Schedule(bool deserializing) : base(deserializing) { }
66    protected Schedule(Schedule original, Cloner cloner)
67      : base(original, cloner) {
68      this.Resources = cloner.Clone(original.Resources);
69      this.Quality = cloner.Clone(original.Quality);
70      this.lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>(original.lastScheduledTaskOfJob);
71    }
72    public Schedule(int nrOfResources) {
73      Resources = new ItemList<Resource>();
74      for (int i = 0; i < nrOfResources; i++) {
75        Resources.Add(new Resource(i));
76      }
77      lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>();
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new Schedule(this, cloner);
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.AppendLine(r.ToString());
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}
Note: See TracBrowser for help on using the repository browser.