Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Schedule.cs @ 14763

Last change on this file since 14763 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 sealed 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    private Schedule(bool deserializing) : base(deserializing) { }
66    private 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      Name = "Schedule";
74
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    public override IDeepCloneable Clone(Cloner cloner) {
83      return new Schedule(this, cloner);
84    }
85
86    #region Events
87    public event EventHandler QualityChanged;
88    private void OnQualityChanged() {
89      var changed = QualityChanged;
90      if (changed != null)
91        changed(this, EventArgs.Empty);
92    }
93    private void RegisterQualityEvents() {
94      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
95    }
96    private void DeregisterQualityEvents() {
97      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
98    }
99    private void Quality_ValueChanged(object sender, EventArgs e) {
100      OnQualityChanged();
101    }
102
103    public event EventHandler ResourcesChanged;
104    private void OnResourcesChanged() {
105      var changed = ResourcesChanged;
106      if (changed != null)
107        changed(this, EventArgs.Empty);
108    }
109    private void RegisterResourcesEvents() {
110      Resources.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
111    }
112    private void DeregisterResourcesEvents() {
113      Resources.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
114    }
115    private void Resources_PropertyChanged(object sender, EventArgs e) {
116      OnResourcesChanged();
117    }
118    #endregion
119
120    public void ScheduleTask(int resNr, double startTime, double duration, int jobNr) {
121      ScheduledTask task = new ScheduledTask(resNr, startTime, duration, jobNr);
122      Resource affectedResource = resources[task.ResourceNr];
123      int i = 0;
124      while (i < affectedResource.Tasks.Count && affectedResource.Tasks[i].StartTime < task.StartTime)
125        i++;
126
127      if (!lastScheduledTaskOfJob.ContainsKey(jobNr)) {
128        lastScheduledTaskOfJob.Add(jobNr, task);
129        task.TaskNr = 0;
130      } else {
131        task.TaskNr = lastScheduledTaskOfJob[jobNr].TaskNr + 1;
132        lastScheduledTaskOfJob[jobNr] = task;
133      }
134
135      if (i >= affectedResource.Tasks.Count)
136        affectedResource.Tasks.Add(task);
137      else
138        affectedResource.Tasks.Insert(i, task);
139
140    }
141
142    public ScheduledTask GetLastScheduledTaskForJobNr(int jobNr) {
143      if (lastScheduledTaskOfJob.ContainsKey(jobNr))
144        return lastScheduledTaskOfJob[jobNr];
145      else
146        return null;
147    }
148
149    public override string ToString() {
150      StringBuilder sb = new StringBuilder();
151      sb.Append("[ ");
152      foreach (Resource r in Resources) {
153        sb.AppendLine(r.ToString());
154      }
155      sb.Append("]");
156      return sb.ToString();
157    }
158
159    public double CalculateMakespan() {
160      double quality = 0;
161      foreach (Resource r in Resources) {
162        if (r.TotalDuration > quality) {
163          quality = r.TotalDuration;
164        }
165      }
166      return quality;
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.