Java Reference

Java Reference

CpSolver.java
Go to the documentation of this file.
1// Copyright 2010-2021 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.solveWrapper = null;
34 }
35
38 return solveWithSolutionCallback(model, null);
39 }
40
46 // Setup search.
47 createSolveWrapper(); // Synchronized.
48 solveWrapper.setParameters(solveParameters.build());
49 if (cb != null) {
50 solveWrapper.addSolutionCallback(cb);
51 }
52 if (logCallback != null) {
53 solveWrapper.addLogCallback(logCallback);
54 }
55
56 solveResponse = solveWrapper.solve(model.model());
57
58 // Cleanup search.
59 if (cb != null) {
60 solveWrapper.clearSolutionCallback(cb);
61 }
62 releaseSolveWrapper(); // Synchronized.
63
64 return solveResponse.getStatus();
65 }
66
73 @Deprecated
75 return solve(model, cb);
76 }
77
92 @Deprecated
94 boolean oldValue = solveParameters.getEnumerateAllSolutions();
95 solveParameters.setEnumerateAllSolutions(true);
96 solve(model, cb);
97 solveParameters.setEnumerateAllSolutions(oldValue);
98 return solveResponse.getStatus();
99 }
100
101 private synchronized void createSolveWrapper() {
102 solveWrapper = new SolveWrapper();
103 }
104
105 private synchronized void releaseSolveWrapper() {
106 solveWrapper = null;
107 }
108
110 public synchronized void stopSearch() {
111 if (solveWrapper != null) {
112 solveWrapper.stopSearch();
113 }
114 }
115
117 public double objectiveValue() {
118 return solveResponse.getObjectiveValue();
119 }
120
125 public double bestObjectiveBound() {
126 return solveResponse.getBestObjectiveBound();
127 }
128
130 public long value(IntVar var) {
131 return solveResponse.getSolution(var.getIndex());
132 }
133
135 public Boolean booleanValue(Literal var) {
136 int index = var.getIndex();
137 if (index >= 0) {
138 return solveResponse.getSolution(index) != 0;
139 } else {
140 return solveResponse.getSolution(-index - 1) == 0;
141 }
142 }
143
146 return solveResponse;
147 }
148
150 public long numBranches() {
151 return solveResponse.getNumBranches();
152 }
153
155 public long numConflicts() {
156 return solveResponse.getNumConflicts();
157 }
158
160 public double wallTime() {
161 return solveResponse.getWallTime();
162 }
163
165 public double userTime() {
166 return solveResponse.getUserTime();
167 }
168
171 }
172
175 return solveParameters;
176 }
177
179 public void setLogCallback(Consumer<String> cb) {
180 this.logCallback = cb;
181 }
182
184 public String responseStats() {
185 return CpSatHelper.solverResponseStats(solveResponse);
186 }
187
188 private CpSolverResponse solveResponse;
189 private final SatParameters.Builder solveParameters;
190 private Consumer<String> logCallback;
191 private SolveWrapper solveWrapper;
192}
Main modeling class.
Definition: CpModel.java:42
static String solverResponseStats(com.google.ortools.sat.CpSolverResponse response)
.lang.Override double getUserTime()
double user_time = 16;
.lang.Override java.util.List< java.lang.Integer > getSufficientAssumptionsForInfeasibilityList()
.lang.Override com.google.ortools.sat.CpSolverStatus getStatus()
.lang.Override long getNumConflicts()
int64 num_conflicts = 11;
.lang.Override double getWallTime()
double wall_time = 15;
.lang.Override long getNumBranches()
int64 num_branches = 12;
Parent class to create a callback called at each solution.
Wrapper around the SAT solver.
Definition: CpSolver.java:28
CpSolver()
Main construction of the CpSolver class.
Definition: CpSolver.java:30
double objectiveValue()
Returns the best objective value found during search.
Definition: CpSolver.java:117
double userTime()
Returns the user time of the search.
Definition: CpSolver.java:165
double wallTime()
Returns the wall time of the search.
Definition: CpSolver.java:160
CpSolverStatus solve(CpModel model, CpSolverSolutionCallback cb)
Solves the given model, calls the solution callback at each incumbent solution, and returns the solve...
Definition: CpSolver.java:45
double bestObjectiveBound()
Returns the best lower bound found when minimizing, of the best upper bound found when maximizing.
Definition: CpSolver.java:125
CpSolverResponse response()
Returns the internal response protobuf that is returned internally by the SAT solver.
Definition: CpSolver.java:145
CpSolverStatus solveWithSolutionCallback(CpModel model, CpSolverSolutionCallback cb)
Solves the given model, passes each incumber solution to the solution callback if not null,...
Definition: CpSolver.java:74
long value(IntVar var)
Returns the value of a variable in the last solution found.
Definition: CpSolver.java:130
long numBranches()
Returns the number of branches explored during search.
Definition: CpSolver.java:150
CpSolverStatus searchAllSolutions(CpModel model, CpSolverSolutionCallback cb)
Searches for all solutions of a satisfiability problem.
Definition: CpSolver.java:93
CpSolverStatus solve(CpModel model)
Solves the given model, and returns the solve status.
Definition: CpSolver.java:37
long numConflicts()
Returns the number of conflicts created during search.
Definition: CpSolver.java:155
SatParameters.Builder getParameters()
Returns the builder of the parameters of the SAT solver for modification.
Definition: CpSolver.java:174
void setLogCallback(Consumer< String > cb)
Sets the log callback for the solver.
Definition: CpSolver.java:179
synchronized void stopSearch()
Stops the search asynchronously.
Definition: CpSolver.java:110
Boolean booleanValue(Literal var)
Returns the Boolean value of a literal in the last solution found.
Definition: CpSolver.java:135
List< Integer > sufficientAssumptionsForInfeasibility()
Definition: CpSolver.java:169
String responseStats()
Returns some statistics on the solution found as a string.
Definition: CpSolver.java:184
int getIndex()
Internal, returns the index of the variable in the underlying CpModelProto.
.lang.Override boolean getEnumerateAllSolutions()
void addSolutionCallback(SolutionCallback callback)
void setParameters(com.google.ortools.sat.SatParameters parameters)
void addLogCallback(java.util.function.Consumer< String > log_callback)
void clearSolutionCallback(SolutionCallback callback)
com.google.ortools.sat.CpSolverResponse solve(com.google.ortools.sat.CpModelProto model_proto)
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17