Java Reference

Java Reference

ModelSolver.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 package com.google.ortools.modelbuilder;
15 
16 import java.time.Duration;
17 import java.util.function.Consumer;
18 
20 public 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 
35  public SolveStatus solve(ModelBuilder model) {
36  if (logCallback == null) {
37  helper.clearLogCallback();
38  } else {
39  helper.setLogCallback(logCallback);
40  }
41  helper.solve(model.getHelper());
42  if (!helper.hasResponse()) {
43  return SolveStatus.UNKNOWN_STATUS;
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 }
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
int getIndex()
Returns the index of the constraint in the model.
ModelBuilderHelper getHelper()
Returns the model builder helper.
double getDualValue(LinearConstraint ct)
Checks that the solver has found a solution, and returns the dual value of the given constraint.
double getReducedCost(Variable var)
Checks that the solver has found a solution, and returns the reduced cost of the given variable.
boolean interruptSolve()
Tries to interrupt the solve.
double getObjectiveValue()
Checks that the solver has found a solution, and returns the objective value.
void enableOutput(boolean enable)
Enables or disables the underlying solver output.
SolveStatus solve(ModelBuilder model)
Solves given model, and returns the status of the response.
boolean hasSolution()
Returns true if solve() was called, and a solution was returned.
double getValue(Variable var)
Checks that the solver has found a solution, and returns the value of the given variable.
double getBestObjectiveBound()
Checks that the solver has found a solution, and returns the objective value.
boolean hasResponse()
Returns true if solve() was called, and a response was returned.
void setLogCallback(Consumer< String > cb)
Sets the log callback for the solver.
ModelSolver(String solverName)
Creates the solver with the supplied solver backend.
double getWallTime()
Returns the elapsed time since the creation of the solver.
double getActivity(LinearConstraint ct)
Checks that the solver has found a solution, and returns the activity of the given constraint.
void setTimeLimit(Duration limit)
Sets the time limit for the solve in seconds.
void setSolverSpecificParameters(String parameters)
Sets solver specific parameters as string.
double getUserTime()
Returns the user time since the creation of the solver.
boolean solverIsSupported()
Returns whether solver specified during the ctor was found and correctly installed.
int getIndex()
Returns the index of the variable in the underlying ModelBuilderHelper.
Definition: Variable.java:33