forked from heal-research/TreesearchLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulingProblem.cs
185 lines (164 loc) · 6.84 KB
/
SchedulingProblem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using TreesearchLib;
namespace SampleApp
{
public class SchedulingProblem : IMutableState<SchedulingProblem, ScheduleChoice, Minimize>
{
public enum ObjectiveType { Makespan, Delay, TotalCompletionTime }
public ObjectiveType Objective { get; }
public bool IsTerminal => remainingJobs.Count == 0;
public Minimize Bound =>
Objective switch
{
ObjectiveType.Makespan => new Minimize((int)Math.Max(makespan.TotalSeconds, (maxJobEndDate - baseDate).TotalSeconds)),
ObjectiveType.Delay => new Minimize((int)delay.TotalSeconds),
ObjectiveType.TotalCompletionTime => new Minimize((int)(totalCompletionTime + totalCompletionBound).TotalSeconds),
_ => throw new NotImplementedException(),
};
public Minimize? Quality => IsTerminal ? (
Objective switch
{
ObjectiveType.Makespan => new Minimize((int)makespan.TotalSeconds),
ObjectiveType.Delay => new Minimize((int)delay.TotalSeconds),
ObjectiveType.TotalCompletionTime => new Minimize((int)totalCompletionTime.TotalSeconds),
_ => throw new NotImplementedException(),
}
) : (Minimize?)null;
private DateTime[] nextAvailableTime;
public IReadOnlyList<DateTime> NextAvailableTime => nextAvailableTime;
public IEnumerable<ScheduleChoice> Choices => choices.Reverse();
private DateTime baseDate;
private HashSet<Job> remainingJobs;
private List<Machine> machines;
private Stack<ScheduleChoice> choices;
private TimeSpan makespan, delay, totalCompletionTime;
public TimeSpan Makespan => makespan;
public TimeSpan Delay => Delay;
private DateTime maxJobEndDate;
private TimeSpan totalCompletionBound;
public SchedulingProblem(ObjectiveType objective, List<Job> jobs, List<Machine> machines)
{
Objective = objective;
this.machines = machines;
remainingJobs = new HashSet<Job>(jobs);
choices = new Stack<ScheduleChoice>();
makespan = TimeSpan.Zero;
delay = TimeSpan.Zero;
totalCompletionTime = TimeSpan.Zero;
nextAvailableTime = new DateTime[machines.Max(m => m.Id) + 1];
baseDate = DateTime.MaxValue;
foreach (var m in machines)
{
nextAvailableTime[m.Id] = m.Start;
if (m.Start < baseDate)
{
baseDate = m.Start;
}
}
maxJobEndDate = jobs.Max(x => x.ReadyDate + x.Duration);
totalCompletionBound = TimeSpan.FromSeconds(jobs.Sum(j => ((j.ReadyDate + j.Duration) - baseDate).TotalSeconds));
}
public SchedulingProblem(SchedulingProblem other)
{
this.Objective = other.Objective;
this.baseDate = other.baseDate;
this.machines = other.machines;
this.remainingJobs = new HashSet<Job>(other.remainingJobs);
this.choices = new Stack<ScheduleChoice>(other.choices.Reverse());
this.makespan = other.makespan;
this.delay = other.delay;
this.totalCompletionTime = other.totalCompletionTime;
this.nextAvailableTime = (DateTime[])other.nextAvailableTime.Clone();
this.maxJobEndDate = other.maxJobEndDate;
this.totalCompletionBound = other.totalCompletionBound;
}
public void Apply(ScheduleChoice choice)
{
remainingJobs.Remove(choice.Job);
choices.Push(choice);
var endDate = choice.ScheduledDate + choice.Job.Duration;
nextAvailableTime[choice.Machine.Id] = endDate;
if (endDate - baseDate > makespan)
{
makespan = endDate - baseDate;
}
delay += (choice.ScheduledDate - choice.Job.ReadyDate);
totalCompletionTime += (endDate - baseDate);
totalCompletionBound -= (choice.Job.ReadyDate + choice.Job.Duration) - baseDate;
}
public void UndoLast()
{
var choice = choices.Pop();
remainingJobs.Add(choice.Job);
nextAvailableTime[choice.Machine.Id] = choice.PreviousAvailableTime;
makespan = choice.PreviousMakespan;
delay -= (choice.ScheduledDate - choice.Job.ReadyDate);
totalCompletionTime -= (choice.ScheduledDate + choice.Job.Duration) - baseDate;
totalCompletionBound += (choice.Job.ReadyDate + choice.Job.Duration) - baseDate;
}
public object Clone()
{
return new SchedulingProblem(this);
}
public IEnumerable<ScheduleChoice> GetChoices()
{
foreach (var job in remainingJobs.OrderBy(x => x.ReadyDate))
{
foreach (var machine in machines.OrderBy(x => nextAvailableTime[x.Id]))
{
yield return new ScheduleChoice(this, job, machine);
}
}
}
}
public class Machine
{
public int Id { get; internal set; }
public string Name { get; internal set; }
public DateTime Start { get; internal set; }
public override string ToString() => Name;
}
public class Job
{
public int Id { get; internal set; }
public string Name { get; internal set; }
public DateTime ReadyDate { get; internal set; }
public TimeSpan Duration { get; internal set; }
public override string ToString() => Name;
}
public class ScheduleChoice
{
public Job Job { get; }
public Machine Machine { get; }
public DateTime ScheduledDate { get; }
public DateTime PreviousAvailableTime { get; }
public TimeSpan PreviousMakespan { get; }
public ScheduleChoice(SchedulingProblem state, Job job, Machine machine)
{
Job = job;
Machine = machine;
var availTime = state.NextAvailableTime[machine.Id];
PreviousAvailableTime = availTime;
if (job.ReadyDate < availTime)
{
ScheduledDate = availTime;
} else
{
ScheduledDate = job.ReadyDate;
}
PreviousMakespan = state.Makespan;
}
public override bool Equals(object obj)
{
if (obj is ScheduleChoice other)
{
return Job.Id == other.Job.Id && Machine.Id == other.Machine.Id
&& ScheduledDate == other.ScheduledDate && PreviousAvailableTime == other.PreviousAvailableTime
&& PreviousMakespan == other.PreviousMakespan;
}
return false;
}
}
}