Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelSolver.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.modelbuilder;
15
16import java.time.Duration;
17import java.util.function.Consumer;
18
20public final class ModelSolver {
21 static class ModelSolverException extends RuntimeException {
22 public ModelSolverException(String methodName, String msg) {
23 // Call constructor of parent Exception
24 super(methodName + ": " + msg);
25 }
26 }
27
29 public ModelSolver(String solverName) {
30 this.helper = new ModelSolverHelper(solverName);
31 this.logCallback = null;
32 }
33
36 if (logCallback == null) {
37 helper.clearLogCallback();
38 } else {
39 helper.setLogCallback(logCallback);
40 }
41 helper.solve(model.getHelper());
42 if (!helper.hasResponse()) {
44 }
45 return helper.getStatus();
46 }
47
49 public void enableOutput(boolean enable) {
50 helper.enableOutput(enable);
51 }
52
54 public void setTimeLimit(Duration limit) {
55 helper.setTimeLimitInSeconds((double) limit.toMillis() / 1000.0);
56 }
57
59 public void setSolverSpecificParameters(String parameters) {
60 helper.setSolverSpecificParameters(parameters);
61 }
62
64 public boolean solverIsSupported() {
65 return helper.solverIsSupported();
66 }
67
69 public boolean interruptSolve() {
70 return helper.interruptSolve();
71 }
72
74 public boolean hasResponse() {
75 return helper.hasResponse();
76 }
77
79 public boolean hasSolution() {
80 return helper.hasSolution();
81 }
82
84 public double getObjectiveValue() {
85 if (!helper.hasSolution()) {
86 throw new ModelSolverException(
87 "ModelSolver.getObjectiveValue()", "solve() was not called or no solution was found");
88 }
89 return helper.getObjectiveValue();
90 }
91
93 public double getBestObjectiveBound() {
94 if (!helper.hasSolution()) {
95 throw new ModelSolverException(
96 "ModelSolver.getBestObjectiveBound()", "solve() was not called or no solution was found");
97 }
98 return helper.getBestObjectiveBound();
99 }
100
102 public double getValue(Variable var) {
103 if (!helper.hasSolution()) {
104 throw new ModelSolverException(
105 "ModelSolver.getValue())", "solve() was not called or no solution was found");
106 }
107 return helper.getVariableValue(var.getIndex());
108 }
113 public double getReducedCost(Variable var) {
114 if (!helper.hasSolution()) {
115 throw new ModelSolverException(
116 "ModelSolver.getReducedCost())", "solve() was not called or no solution was found");
117 }
118 return helper.getReducedCost(var.getIndex());
119 }
120
125 public double getDualValue(LinearConstraint ct) {
126 if (!helper.hasSolution()) {
127 throw new ModelSolverException(
128 "ModelSolver.getDualValue())", "solve() was not called or no solution was found");
129 }
130 return helper.getDualValue(ct.getIndex());
131 }
132
136 public double getActivity(LinearConstraint ct) {
137 if (!helper.hasSolution()) {
138 throw new ModelSolverException(
139 "ModelSolver.getActivity())", "solve() was not called or no solution was found");
140 }
141 return helper.getActivity(ct.getIndex());
142 }
143
145 public void setLogCallback(Consumer<String> cb) {
146 this.logCallback = cb;
147 }
148
150 public double getWallTime() {
151 return helper.getWallTime();
152 }
153
155 public double getUserTime() {
156 return helper.getUserTime();
157 }
158
159 private final ModelSolverHelper helper;
160 private Consumer<String> logCallback;
161}
void setLogCallback(java.util.function.Consumer< String > log_callback)
void setSolverSpecificParameters(String solver_specific_parameters)
void setSolverSpecificParameters(String parameters)
void setLogCallback(Consumer< String > cb)
double getDualValue(LinearConstraint ct)
SolveStatus solve(ModelBuilder model)
double getActivity(LinearConstraint ct)