DotNet Reference

DotNet Reference

NoOverlapSampleSat.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 using System;
15 using Google.OrTools.Sat;
16 
17 public class NoOverlapSampleSat
18 {
19  static void Main()
20  {
21  CpModel model = new CpModel();
22  // Three weeks.
23  int horizon = 21;
24 
25  // Task 0, duration 2.
26  IntVar start_0 = model.NewIntVar(0, horizon, "start_0");
27  int duration_0 = 2;
28  IntVar end_0 = model.NewIntVar(0, horizon, "end_0");
29  IntervalVar task_0 =
30  model.NewIntervalVar(start_0, duration_0, end_0, "task_0");
31 
32  // Task 1, duration 4.
33  IntVar start_1 = model.NewIntVar(0, horizon, "start_1");
34  int duration_1 = 4;
35  IntVar end_1 = model.NewIntVar(0, horizon, "end_1");
36  IntervalVar task_1 =
37  model.NewIntervalVar(start_1, duration_1, end_1, "task_1");
38 
39  // Task 2, duration 3.
40  IntVar start_2 = model.NewIntVar(0, horizon, "start_2");
41  int duration_2 = 3;
42  IntVar end_2 = model.NewIntVar(0, horizon, "end_2");
43  IntervalVar task_2 =
44  model.NewIntervalVar(start_2, duration_2, end_2, "task_2");
45 
46  // Weekends.
47  IntervalVar weekend_0 = model.NewIntervalVar(5, 2, 7, "weekend_0");
48  IntervalVar weekend_1 = model.NewIntervalVar(12, 2, 14, "weekend_1");
49  IntervalVar weekend_2 = model.NewIntervalVar(19, 2, 21, "weekend_2");
50 
51  // No Overlap constraint.
52  model.AddNoOverlap(new IntervalVar[] {task_0, task_1, task_2, weekend_0,
53  weekend_1, weekend_2});
54 
55  // Makespan objective.
56  IntVar obj = model.NewIntVar(0, horizon, "makespan");
57  model.AddMaxEquality(obj, new IntVar[] {end_0, end_1, end_2});
58  model.Minimize(obj);
59 
60  // Creates a solver and solves the model.
61  CpSolver solver = new CpSolver();
62  CpSolverStatus status = solver.Solve(model);
63 
64  if (status == CpSolverStatus.Optimal)
65  {
66  Console.WriteLine("Optimal Schedule Length: " + solver.ObjectiveValue);
67  Console.WriteLine("Task 0 starts at " + solver.Value(start_0));
68  Console.WriteLine("Task 1 starts at " + solver.Value(start_1));
69  Console.WriteLine("Task 2 starts at " + solver.Value(start_2));
70  }
71  }
72 }
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
void Minimize(LinearExpr obj)
Definition: CpModel.cs:588
Constraint AddMaxEquality(IntVar target, IEnumerable< IntVar > vars)
Definition: CpModel.cs:454
Constraint AddNoOverlap(IEnumerable< IntervalVar > intervals)
Definition: CpModel.cs:538
CpSolverStatus
The status returned by a solver trying to solve a CpModelProto.
Definition: CpModel.pb.cs:186
IntVar NewIntVar(long lb, long ub, string name)
Definition: CpModel.cs:45
double ObjectiveValue
Definition: CpSolver.cs:76
CpSolverStatus Solve(CpModel model)
Definition: CpSolver.cs:22
Definition: CpSolver.cs:20
long Value(LinearExpr e)
Definition: CpSolver.cs:96
Definition: CpModel.pb.cs:12