Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
CpSolver.java
Go to the documentation of this file.
1// Copyright 2010-2024 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
14package com.google.ortools.sat;
15
16import com.google.ortools.sat.CpSolverResponse;
17import com.google.ortools.sat.CpSolverStatus;
18import com.google.ortools.sat.SatParameters;
19import java.util.List;
20import java.util.function.Consumer;
21
28public final class CpSolver {
30 public CpSolver() {
31 this.solveParameters = SatParameters.newBuilder();
32 this.logCallback = null;
33 this.bestBoundCallback = null;
34 this.solveWrapper = null;
35 }
36
39 return solve(model, null);
40 }
41
47 // Setup search.
48 createSolveWrapper(); // Synchronized.
49 solveWrapper.setParameters(solveParameters.build());
50 if (cb != null) {
51 solveWrapper.addSolutionCallback(cb);
52 }
53 if (logCallback != null) {
54 solveWrapper.addLogCallback(logCallback);
55 }
56 if (bestBoundCallback != null) {
57 solveWrapper.addBestBoundCallback(bestBoundCallback);
58 }
59
60 solveResponse = solveWrapper.solve(model.model());
61
62 // Cleanup search.
63 if (cb != null) {
64 solveWrapper.clearSolutionCallback(cb);
65 }
66 releaseSolveWrapper(); // Synchronized.
67
68 return solveResponse.getStatus();
69 }
70
77 @Deprecated
79 return solve(model, cb);
80 }
81
96 @Deprecated
98 boolean oldValue = solveParameters.getEnumerateAllSolutions();
99 solveParameters.setEnumerateAllSolutions(true);
100 CpSolverStatus status = solve(model, cb);
101 solveParameters.setEnumerateAllSolutions(oldValue);
102 return status;
103 }
104
105 private synchronized void createSolveWrapper() {
106 solveWrapper = new SolveWrapper();
107 }
108
109 private synchronized void releaseSolveWrapper() {
110 solveWrapper = null;
111 }
112
114 public synchronized void stopSearch() {
115 if (solveWrapper != null) {
116 solveWrapper.stopSearch();
117 }
118 }
119
121 public double objectiveValue() {
122 return solveResponse.getObjectiveValue();
123 }
124
129 public double bestObjectiveBound() {
130 return solveResponse.getBestObjectiveBound();
131 }
132
134 public long value(LinearArgument expr) {
135 final LinearExpr e = expr.build();
136 long result = e.getOffset();
137 for (int i = 0; i < e.numElements(); ++i) {
138 result += solveResponse.getSolution(e.getVariableIndex(i)) * e.getCoefficient(i);
139 }
140 return result;
141 }
142
144 public Boolean booleanValue(Literal var) {
145 int index = var.getIndex();
146 if (index >= 0) {
147 return solveResponse.getSolution(index) != 0;
148 } else {
149 return solveResponse.getSolution(-index - 1) == 0;
150 }
151 }
152
155 return solveResponse;
156 }
157
159 public long numBranches() {
160 return solveResponse.getNumBranches();
161 }
162
164 public long numConflicts() {
165 return solveResponse.getNumConflicts();
166 }
167
169 public double wallTime() {
170 return solveResponse.getWallTime();
171 }
172
174 public double userTime() {
175 return solveResponse.getUserTime();
176 }
177
180 }
181
183 public SatParameters.Builder getParameters() {
184 return solveParameters;
185 }
186
188 public void setLogCallback(Consumer<String> cb) {
189 this.logCallback = cb;
190 }
191
193 public void clearLogCallback() {
194 this.logCallback = null;
195 }
196
198 public void setBestBoundCallback(Consumer<Double> cb) {
199 this.bestBoundCallback = cb;
200 }
201
204 this.bestBoundCallback = null;
205 }
206
208 public String responseStats() {
209 return CpSatHelper.solverResponseStats(solveResponse);
210 }
211
216 public String getSolutionInfo() {
217 return solveResponse.getSolutionInfo();
218 }
219
220 private CpSolverResponse solveResponse;
221 private final SatParameters.Builder solveParameters;
222 private Consumer<String> logCallback;
223 private Consumer<Double> bestBoundCallback;
224 private SolveWrapper solveWrapper;
225}
CpModelProto model()
Getters.
static String solverResponseStats(com.google.ortools.sat.CpSolverResponse response)
java.util.List< java.lang.Integer > getSufficientAssumptionsForInfeasibilityList()
com.google.ortools.sat.CpSolverStatus getStatus()
synchronized void stopSearch()
SatParameters.Builder getParameters()
long value(LinearArgument expr)
void setLogCallback(Consumer< String > cb)
CpSolverResponse response()
Boolean booleanValue(Literal var)
CpSolverStatus solve(CpModel model, CpSolverSolutionCallback cb)
Definition CpSolver.java:46
void setBestBoundCallback(Consumer< Double > cb)
CpSolverStatus solveWithSolutionCallback(CpModel model, CpSolverSolutionCallback cb)
Definition CpSolver.java:78
CpSolverStatus solve(CpModel model)
Definition CpSolver.java:38
List< Integer > sufficientAssumptionsForInfeasibility()
CpSolverStatus searchAllSolutions(CpModel model, CpSolverSolutionCallback cb)
Definition CpSolver.java:97
void addSolutionCallback(SolutionCallback callback)
void addBestBoundCallback(java.util.function.Consumer< Double > best_bound_callback)
void addLogCallback(java.util.function.Consumer< String > log_callback)
void clearSolutionCallback(SolutionCallback callback)
void setParameters(com.google.ortools.sat.SatParameters parameters)
com.google.ortools.sat.CpSolverResponse solve(com.google.ortools.sat.CpModelProto model_proto)