Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6414 was 6412, checked in by jhelm, 13 years ago

#1329: Did some minor changes affecting datatypes.

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