Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14728 was 13436, checked in by mkommend, 9 years ago

#2521: Minor improvements in SchedulingEncoding.

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.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
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, ISchedule {
34
35    #region Properties
36    [Storable]
37    private ItemList<Resource> resources;
38    public ItemList<Resource> Resources {
39      get { return resources; }
40    }
41    [Storable]
42    private double quality;
43    public double Quality {
44      get { return quality; }
45      set {
46        if (quality == value) return;
47        quality = value;
48        OnQualityChanged();
49      }
50    }
51    [Storable]
52    private Dictionary<int, ScheduledTask> lastScheduledTaskOfJob;
53    #endregion
54
55    [StorableConstructor]
56    protected Schedule(bool deserializing) : base(deserializing) { }
57    protected Schedule(Schedule original, Cloner cloner)
58      : base(original, cloner) {
59      this.resources = cloner.Clone(original.Resources);
60      this.quality = original.Quality;
61      this.lastScheduledTaskOfJob = original.lastScheduledTaskOfJob.ToDictionary(kvp => kvp.Key, kvp => cloner.Clone(kvp.Value));
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.