2025-01-10 11:33:35 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2018-08-02 17:00:44 -07:00
|
|
|
// 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.
|
|
|
|
|
|
2025-09-15 10:24:48 +02:00
|
|
|
// [START program]
|
2020-05-26 09:30:42 +02:00
|
|
|
package com.google.ortools.sat.samples;
|
|
|
|
|
|
2020-09-11 03:07:18 +02:00
|
|
|
import com.google.ortools.Loader;
|
2018-08-03 16:42:45 -07:00
|
|
|
import com.google.ortools.sat.CpModel;
|
|
|
|
|
import com.google.ortools.sat.CpSolver;
|
2018-11-15 11:38:24 -08:00
|
|
|
import com.google.ortools.sat.CpSolverStatus;
|
2018-08-03 16:42:45 -07:00
|
|
|
import com.google.ortools.sat.IntVar;
|
|
|
|
|
import com.google.ortools.sat.IntervalVar;
|
2021-09-09 06:41:35 +02:00
|
|
|
import com.google.ortools.sat.LinearExpr;
|
2018-08-02 17:00:44 -07:00
|
|
|
|
2018-08-28 11:19:49 +02:00
|
|
|
/**
|
2018-11-15 11:38:24 -08:00
|
|
|
* We want to schedule 3 tasks on 3 weeks excluding weekends, making the final day as early as
|
|
|
|
|
* possible.
|
2018-08-28 11:19:49 +02:00
|
|
|
*/
|
2018-11-15 11:38:24 -08:00
|
|
|
public class NoOverlapSampleSat {
|
2018-08-03 16:42:45 -07:00
|
|
|
public static void main(String[] args) throws Exception {
|
2020-09-11 03:07:18 +02:00
|
|
|
Loader.loadNativeLibraries();
|
2018-08-02 17:00:44 -07:00
|
|
|
CpModel model = new CpModel();
|
|
|
|
|
// Three weeks.
|
|
|
|
|
int horizon = 21;
|
|
|
|
|
|
|
|
|
|
// Task 0, duration 2.
|
2018-08-03 16:42:45 -07:00
|
|
|
IntVar start0 = model.newIntVar(0, horizon, "start0");
|
|
|
|
|
int duration0 = 2;
|
2022-01-03 09:43:59 +01:00
|
|
|
IntervalVar task0 = model.newFixedSizeIntervalVar(start0, duration0, "task0");
|
2018-08-02 17:00:44 -07:00
|
|
|
|
|
|
|
|
// Task 1, duration 4.
|
2018-08-03 16:42:45 -07:00
|
|
|
IntVar start1 = model.newIntVar(0, horizon, "start1");
|
|
|
|
|
int duration1 = 4;
|
2022-01-03 09:43:59 +01:00
|
|
|
IntervalVar task1 = model.newFixedSizeIntervalVar(start1, duration1, "task1");
|
2018-08-02 17:00:44 -07:00
|
|
|
|
|
|
|
|
// Task 2, duration 3.
|
2018-08-03 16:42:45 -07:00
|
|
|
IntVar start2 = model.newIntVar(0, horizon, "start2");
|
|
|
|
|
int duration2 = 3;
|
2022-01-03 09:43:59 +01:00
|
|
|
IntervalVar task2 = model.newFixedSizeIntervalVar(start2, duration2, "task2");
|
2018-08-02 17:00:44 -07:00
|
|
|
|
|
|
|
|
// Weekends.
|
2018-08-03 16:42:45 -07:00
|
|
|
IntervalVar weekend0 = model.newFixedInterval(5, 2, "weekend0");
|
|
|
|
|
IntervalVar weekend1 = model.newFixedInterval(12, 2, "weekend1");
|
|
|
|
|
IntervalVar weekend2 = model.newFixedInterval(19, 2, "weekend2");
|
2018-08-02 17:00:44 -07:00
|
|
|
|
2018-11-15 11:38:24 -08:00
|
|
|
// No Overlap constraint. This constraint enforces that no two intervals can overlap.
|
|
|
|
|
// In this example, as we use 3 fixed intervals that span over weekends, this constraint makes
|
|
|
|
|
// sure that all tasks are executed on weekdays.
|
2018-11-10 23:56:52 +01:00
|
|
|
model.addNoOverlap(new IntervalVar[] {task0, task1, task2, weekend0, weekend1, weekend2});
|
2018-08-02 17:00:44 -07:00
|
|
|
|
|
|
|
|
// Makespan objective.
|
|
|
|
|
IntVar obj = model.newIntVar(0, horizon, "makespan");
|
2022-01-03 09:43:59 +01:00
|
|
|
model.addMaxEquality(obj,
|
|
|
|
|
new LinearExpr[] {LinearExpr.newBuilder().add(start0).add(duration0).build(),
|
|
|
|
|
LinearExpr.newBuilder().add(start1).add(duration1).build(),
|
|
|
|
|
LinearExpr.newBuilder().add(start2).add(duration2).build()});
|
2018-08-02 17:00:44 -07:00
|
|
|
model.minimize(obj);
|
|
|
|
|
|
|
|
|
|
// Creates a solver and solves the model.
|
|
|
|
|
CpSolver solver = new CpSolver();
|
|
|
|
|
CpSolverStatus status = solver.solve(model);
|
|
|
|
|
|
2018-08-03 16:42:45 -07:00
|
|
|
if (status == CpSolverStatus.OPTIMAL) {
|
2018-08-02 17:00:44 -07:00
|
|
|
System.out.println("Optimal Schedule Length: " + solver.objectiveValue());
|
2018-08-03 16:42:45 -07:00
|
|
|
System.out.println("Task 0 starts at " + solver.value(start0));
|
|
|
|
|
System.out.println("Task 1 starts at " + solver.value(start1));
|
|
|
|
|
System.out.println("Task 2 starts at " + solver.value(start2));
|
2018-08-02 17:00:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-15 10:24:48 +02:00
|
|
|
// [END program]
|