OR-Tools  7.1
NoOverlapSampleSat.java
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 
17 import com.google.ortools.sat.IntVar;
19 
24 public class NoOverlapSampleSat {
25  static {
26  System.loadLibrary("jniortools");
27  }
28 
29  public static void main(String[] args) throws Exception {
30  CpModel model = new CpModel();
31  // Three weeks.
32  int horizon = 21;
33 
34  // Task 0, duration 2.
35  IntVar start0 = model.newIntVar(0, horizon, "start0");
36  int duration0 = 2;
37  IntVar end0 = model.newIntVar(0, horizon, "end0");
38  IntervalVar task0 = model.newIntervalVar(start0, duration0, end0, "task0");
39 
40  // Task 1, duration 4.
41  IntVar start1 = model.newIntVar(0, horizon, "start1");
42  int duration1 = 4;
43  IntVar end1 = model.newIntVar(0, horizon, "end1");
44  IntervalVar task1 = model.newIntervalVar(start1, duration1, end1, "task1");
45 
46  // Task 2, duration 3.
47  IntVar start2 = model.newIntVar(0, horizon, "start2");
48  int duration2 = 3;
49  IntVar end2 = model.newIntVar(0, horizon, "end2");
50  IntervalVar task2 = model.newIntervalVar(start2, duration2, end2, "task2");
51 
52  // Weekends.
53  IntervalVar weekend0 = model.newFixedInterval(5, 2, "weekend0");
54  IntervalVar weekend1 = model.newFixedInterval(12, 2, "weekend1");
55  IntervalVar weekend2 = model.newFixedInterval(19, 2, "weekend2");
56 
57  // No Overlap constraint. This constraint enforces that no two intervals can overlap.
58  // In this example, as we use 3 fixed intervals that span over weekends, this constraint makes
59  // sure that all tasks are executed on weekdays.
60  model.addNoOverlap(new IntervalVar[] {task0, task1, task2, weekend0, weekend1, weekend2});
61 
62  // Makespan objective.
63  IntVar obj = model.newIntVar(0, horizon, "makespan");
64  model.addMaxEquality(obj, new IntVar[] {end0, end1, end2});
65  model.minimize(obj);
66 
67  // Creates a solver and solves the model.
68  CpSolver solver = new CpSolver();
69  CpSolverStatus status = solver.solve(model);
70 
71  if (status == CpSolverStatus.OPTIMAL) {
72  System.out.println("Optimal Schedule Length: " + solver.objectiveValue());
73  System.out.println("Task 0 starts at " + solver.value(start0));
74  System.out.println("Task 1 starts at " + solver.value(start1));
75  System.out.println("Task 2 starts at " + solver.value(start2));
76  }
77  }
78 }
We want to schedule 3 tasks on 3 weeks excluding weekends, making the final day as early as possible.
Constraint addNoOverlap(IntervalVar[] intervalVars)
Adds.
Definition: CpModel.java:823
Constraint addMaxEquality(IntVar target, IntVar[] vars)
Adds.
Definition: CpModel.java:635
Wrapper around the SAT solver.
Definition: CpSolver.java:26
IntVar newIntVar(long lb, long ub, String name)
Creates an integer variable with domain [lb, ub].
Definition: CpModel.java:69
double objectiveValue()
Returns the best objective value found during search.
Definition: CpSolver.java:66
IntervalVar newFixedInterval(long start, long size, String name)
Creates a fixed interval from its start and its size.
Definition: CpModel.java:747
long value(IntVar var)
Returns the value of a variable in the last solution found.
Definition: CpSolver.java:79
Main modeling class.
Definition: CpModel.java:40
void minimize(LinearExpr expr)
Adds a minimization objective of a linear expression.
Definition: CpModel.java:958
IntervalVar newIntervalVar(IntVar start, IntVar size, IntVar end, String name)
Creates an interval variable from start, size, and end.
Definition: CpModel.java:712
static void main(String[] args)
CpSolverStatus solve(CpModel model)
Solves the given module, and returns the solve status.
Definition: CpSolver.java:33