forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobshopFt06Sat.cs
134 lines (118 loc) · 3.89 KB
/
JobshopFt06Sat.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
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using Google.OrTools.Sat;
public class JobshopFt06Sat
{
public struct Task
{
public Task(IntVar s, IntVar e, IntervalVar i)
{
start = s;
end = e;
interval = i;
}
public IntVar start;
public IntVar end;
public IntervalVar interval;
}
static void Main()
{
int[,] durations = new int[,] { {1, 3, 6, 7, 3, 6},
{8, 5, 10, 10, 10, 4},
{5, 4, 8, 9, 1, 7},
{5, 5, 5, 3, 8, 9},
{9, 3, 5, 4, 3, 1},
{3, 3, 9, 10, 4, 1} };
int[,] machines = new int[,] { {2, 0, 1, 3, 5, 4},
{1, 2, 4, 5, 0, 3},
{2, 3, 5, 0, 1, 4},
{1, 0, 2, 3, 4, 5},
{2, 1, 4, 5, 0, 3},
{1, 3, 5, 0, 4, 2} };
int num_jobs = durations.GetLength(0);
int num_machines = durations.GetLength(1);
var all_jobs = Enumerable.Range(0, num_jobs);
var all_machines = Enumerable.Range(0, num_machines);
int horizon = 0;
foreach (int j in all_jobs)
{
foreach (int m in all_machines)
{
horizon += durations[j, m];
}
}
// Creates the model.
CpModel model = new CpModel();
// Creates jobs.
Task[,] all_tasks = new Task[num_jobs, num_machines];
foreach (int j in all_jobs)
{
foreach (int m in all_machines)
{
IntVar start_var = model.NewIntVar(
0, horizon, String.Format("start_{0}_{1}", j, m));
int duration = durations[j, m];
IntVar end_var = model.NewIntVar(
0, horizon, String.Format("end_{0}_{1}", j, m));
IntervalVar interval_var = model.NewIntervalVar(
start_var, duration, end_var,
String.Format("interval_{0}_{1}", j, m));
all_tasks[j, m] = new Task(start_var, end_var, interval_var);
}
}
// Create disjuctive constraints.
List<IntervalVar>[] machine_to_jobs = new List<IntervalVar>[num_machines];
foreach (int m in all_machines)
{
machine_to_jobs[m] = new List<IntervalVar>();
}
foreach (int j in all_jobs)
{
foreach (int m in all_machines)
{
machine_to_jobs[machines[j, m]].Add(all_tasks[j, m].interval);
}
}
foreach (int m in all_machines)
{
model.AddNoOverlap(machine_to_jobs[m]);
}
// Precedences inside a job.
foreach (int j in all_jobs)
{
for (int k = 0; k < num_machines - 1; ++k)
{
model.Add(all_tasks[j, k + 1].start >= all_tasks[j, k].end);
}
}
// Makespan objective.
IntVar[] all_ends = new IntVar[num_jobs];
foreach (int j in all_jobs)
{
all_ends[j] = all_tasks[j, num_machines - 1].end;
}
IntVar makespan = model.NewIntVar(0, horizon, "makespan");
model.AddMaxEquality(makespan, all_ends);
model.Minimize(makespan);
Console.WriteLine(model.ModelStats());
// Creates the solver and solve.
CpSolver solver = new CpSolver();
// Display a few solutions picked at random.
solver.Solve(model);
// Statistics.
Console.WriteLine(solver.ResponseStats());
}
}