source: branches/ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Schedule.cs @ 13435

Last change on this file since 13435 was 13435, checked in by mkommend, 5 years ago

#2521: Intermediate version of schedule encoding refactoring.

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ScheduleEncoding {
30  [Item("Schedule", "Represents the general solution for scheduling problems.")]
31  [StorableClass]
32  public class Schedule : NamedItem, ISchedule {
33
34    #region Properties
35    [Storable]
36    private ItemList<Resource> resources;
37    public ItemList<Resource> Resources {
38      get { return resources; }
39    }
40    [Storable]
41    private double quality;
42    public double Quality {
43      get { return quality; }
44      set {
45        if (quality == value) return;
46        quality = value;
47        OnQualityChanged();
48      }
49    }
50    [Storable]
51    private Dictionary<int, ScheduledTask> lastScheduledTaskOfJob;
52    #endregion
53
54    [StorableConstructor]
55    protected Schedule(bool deserializing) : base(deserializing) { }
56    protected Schedule(Schedule original, Cloner cloner)
57      : base(original, cloner) {
58      this.resources = cloner.Clone(original.Resources);
59      this.quality = original.Quality;
60      //TODO clone
61      this.lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>(original.lastScheduledTaskOfJob);
62
63      RegisterResourcesEvents();
64    }
65    public Schedule(int nrOfResources) {
66      resources = new ItemList<Resource>();
67      for (int i = 0; i < nrOfResources; i++) {
68        Resources.Add(new Resource(i));
69      }
70      lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>();
71
72      RegisterResourcesEvents();
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new Schedule(this, cloner);
77    }
78
79    #region Events
80    public event EventHandler QualityChanged;
81    private void OnQualityChanged() {
82      var changed = QualityChanged;
83      if (changed != null)
84        changed(this, EventArgs.Empty);
85    }
86
87    public event EventHandler ResourcesChanged;
88    private void OnResourcesChanged() {
89      var changed = ResourcesChanged;
90      if (changed != null)
91        changed(this, EventArgs.Empty);
92    }
93    private void RegisterResourcesEvents() {
94      Resources.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
95    }
96    private void Resources_PropertyChanged(object sender, EventArgs e) {
97      OnResourcesChanged();
98    }
99    #endregion
100
101    public void ScheduleTask(int resNr, double startTime, double duration, int jobNr) {
102      ScheduledTask task = new ScheduledTask(resNr, startTime, duration, jobNr);
103      Resource affectedResource = resources[task.ResourceNr];
104      int i = 0;
105      while (i < affectedResource.Tasks.Count && affectedResource.Tasks[i].StartTime < task.StartTime)
106        i++;
107
108      if (!lastScheduledTaskOfJob.ContainsKey(jobNr)) {
109        lastScheduledTaskOfJob.Add(jobNr, task);
110        task.TaskNr = 0;
111      } else {
112        task.TaskNr = lastScheduledTaskOfJob[jobNr].TaskNr + 1;
113        lastScheduledTaskOfJob[jobNr] = task;
114      }
115
116      if (i >= affectedResource.Tasks.Count)
117        affectedResource.Tasks.Add(task);
118      else
119        affectedResource.Tasks.Insert(i, task);
120
121    }
122
123    public ScheduledTask GetLastScheduledTaskForJobNr(int jobNr) {
124      if (lastScheduledTaskOfJob.ContainsKey(jobNr))
125        return lastScheduledTaskOfJob[jobNr];
126      else
127        return null;
128    }
129
130    public override string ToString() {
131      StringBuilder sb = new StringBuilder();
132      sb.Append("[ ");
133      foreach (Resource r in Resources) {
134        sb.AppendLine(r.ToString());
135      }
136      sb.Append("]");
137      return sb.ToString();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.