EarlinessTardinessCostSampleSat.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 
18 import com.google.ortools.sat.IntVar;
21 
24  static {
25  System.loadLibrary("jniortools");
26  }
27 
28  public static void main(String[] args) throws Exception {
29  long earlinessDate = 5;
30  long earlinessCost = 8;
31  long latenessDate = 15;
32  long latenessCost = 12;
33 
34  // Create the CP-SAT model.
35  CpModel model = new CpModel();
36 
37  // Declare our primary variable.
38  IntVar x = model.newIntVar(0, 20, "x");
39 
40  // Create the expression variable and implement the piecewise linear function.
41  //
42  // \ /
43  // \______/
44  // ed ld
45  //
46  long largeConstant = 1000;
47  IntVar expr = model.newIntVar(0, largeConstant, "expr");
48 
49  // First segment: s1 == earlinessCost * (earlinessDate - x).
50  IntVar s1 = model.newIntVar(-largeConstant, largeConstant, "s1");
51  model.addEquality(LinearExpr.scalProd(new IntVar[] {s1, x}, new long[] {1, earlinessCost}),
52  earlinessCost * earlinessDate);
53 
54  // Second segment.
55  IntVar s2 = model.newConstant(0);
56 
57  // Third segment: s3 == latenessCost * (x - latenessDate).
58  IntVar s3 = model.newIntVar(-largeConstant, largeConstant, "s3");
59  model.addEquality(LinearExpr.scalProd(new IntVar[] {s3, x}, new long[] {1, -latenessCost}),
60  -latenessCost * latenessDate);
61 
62  // Link together expr and x through s1, s2, and s3.
63  model.addMaxEquality(expr, new IntVar[] {s1, s2, s3});
64 
65  // Search for x values in increasing order.
66  model.addDecisionStrategy(new IntVar[] {x},
69 
70  // Create the solver.
71  CpSolver solver = new CpSolver();
72 
73  // Force the solver to follow the decision strategy exactly.
74  solver.getParameters().setSearchBranching(SatParameters.SearchBranching.FIXED_SEARCH);
75 
76  // Solve the problem with the printer callback.
77  solver.searchAllSolutions(model, new CpSolverSolutionCallback() {
78  public CpSolverSolutionCallback init(IntVar[] variables) {
79  variableArray = variables;
80  return this;
81  }
82 
83  @Override
84  public void onSolutionCallback() {
85  for (IntVar v : variableArray) {
86  System.out.printf("%s=%d ", v.getName(), value(v));
87  }
88  System.out.println();
89  }
90 
91  private IntVar[] variableArray;
92  }.init(new IntVar[] {x, expr}));
93  }
94 }
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Constraint addEquality(LinearExpr expr, long value)
Adds.
Definition: CpModel.java:153
IntVar newConstant(long value)
Creates a constant variable.
Definition: CpModel.java:90
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
SatParameters.Builder getParameters()
Returns the builder of the parameters of the SAT solver for modification.
Definition: CpSolver.java:119
Main modeling class.
Definition: CpModel.java:40
static LinearExpr scalProd(IntVar[] variables, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:33
Encode the piecewise linear expression.
void addDecisionStrategy(IntVar[] variables, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr)
Adds.
Definition: CpModel.java:979
CpSolverStatus searchAllSolutions(CpModel model, CpSolverSolutionCallback cb)
Searches for all solutions of a satisfiability problem.
Definition: CpSolver.java:57
Parent class to create a callback called at each solution.