Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding/Schedule.cs @ 17226

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

#2521: Merged trunk changes into problem refactoring branch.

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29
30namespace HeuristicLab.Encodings.ScheduleEncoding {
31  [Item("Schedule", "Represents the general solution for scheduling problems.")]
32  [StorableType("EA6D14A3-7FA6-4B4B-B7F6-40B42657D398")]
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    private Schedule(StorableConstructorFlag _) : base(_) { }
57    private 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      Name = "Schedule";
67
68      resources = new ItemList<Resource>();
69      for (int i = 0; i < nrOfResources; i++) {
70        Resources.Add(new Resource(i));
71      }
72      lastScheduledTaskOfJob = new Dictionary<int, ScheduledTask>();
73
74      RegisterResourcesEvents();
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new Schedule(this, cloner);
79    }
80
81    #region Events
82    public event EventHandler QualityChanged;
83    private void OnQualityChanged() {
84      var changed = QualityChanged;
85      if (changed != null)
86        changed(this, EventArgs.Empty);
87    }
88
89    public event EventHandler ResourcesChanged;
90    private void OnResourcesChanged() {
91      var changed = ResourcesChanged;
92      if (changed != null)
93        changed(this, EventArgs.Empty);
94    }
95    private void RegisterResourcesEvents() {
96      Resources.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Resources_PropertyChanged);
97    }
98    private void Resources_PropertyChanged(object sender, EventArgs e) {
99      OnResourcesChanged();
100    }
101    #endregion
102
103    public void ScheduleTask(int resNr, double startTime, double duration, int jobNr) {
104      ScheduledTask task = new ScheduledTask(resNr, startTime, duration, jobNr);
105      Resource affectedResource = resources[task.ResourceNr];
106      int i = 0;
107      while (i < affectedResource.Tasks.Count && affectedResource.Tasks[i].StartTime < task.StartTime)
108        i++;
109
110      if (!lastScheduledTaskOfJob.ContainsKey(jobNr)) {
111        lastScheduledTaskOfJob.Add(jobNr, task);
112        task.TaskNr = 0;
113      } else {
114        task.TaskNr = lastScheduledTaskOfJob[jobNr].TaskNr + 1;
115        lastScheduledTaskOfJob[jobNr] = task;
116      }
117
118      if (i >= affectedResource.Tasks.Count)
119        affectedResource.Tasks.Add(task);
120      else
121        affectedResource.Tasks.Insert(i, task);
122
123    }
124
125    public ScheduledTask GetLastScheduledTaskForJobNr(int jobNr) {
126      if (lastScheduledTaskOfJob.ContainsKey(jobNr))
127        return lastScheduledTaskOfJob[jobNr];
128      else
129        return null;
130    }
131
132    public override string ToString() {
133      StringBuilder sb = new StringBuilder();
134      sb.Append("[ ");
135      foreach (Resource r in Resources) {
136        sb.AppendLine(r.ToString());
137      }
138      sb.Append("]");
139      return sb.ToString();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.