Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelSolver.cs
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
15{
16
18using System;
19using System.Collections;
20using System.Collections.Generic;
21using System.Linq;
22using System.Runtime.CompilerServices;
23using Google.Protobuf.Collections;
24
28public class Solver
29{
30 class SolverException : Exception
31 {
32 public SolverException(String methodName, String msg) : base(methodName + ": " + msg)
33 {
34 }
35 }
36
41 public Solver(String solverName)
42 {
43 this.helper_ = new ModelSolverHelper(solverName);
44 this.logCallback_ = null;
45 }
46
52 public SolveStatus Solve(Model model)
53 {
54 if (logCallback_ == null)
55 {
56 helper_.ClearLogCallback();
57 }
58 else
59 {
60 helper_.SetLogCallbackFromDirectorClass(logCallback_);
61 }
62 helper_.Solve(model.Helper);
63 if (!helper_.HasResponse())
64 {
65 return SolveStatus.UNKNOWN_STATUS;
66 }
67 return helper_.Status();
68 }
69
74 public void EnableOutput(bool enable)
75 {
76 helper_.EnableOutput(enable);
77 }
78
80 public void SetTimeLimitInSeconds(double limit)
81 {
82 helper_.SetTimeLimitInSeconds(limit);
83 }
84
86 public void SetSolverSpecificParameters(String parameters)
87 {
88 helper_.SetSolverSpecificParameters(parameters);
89 }
90
95 public bool SolverIsSupported()
96 {
97 return helper_.SolverIsSupported();
98 }
99
104 public bool InterruptSolve()
105 {
106 return helper_.InterruptSolve();
107 }
108
113 public bool HasResponse()
114 {
115 return helper_.HasResponse();
116 }
117
122 public bool HasSolution()
123 {
124 return helper_.HasSolution();
125 }
126
131 public double ObjectiveValue
132 {
133 get {
134 if (!helper_.HasSolution())
135 {
136 throw new SolverException("Solver.ObjectiveValue", "Solve() was not called or no solution was found");
137 }
138 return helper_.ObjectiveValue();
139 }
140 }
141
146 public double BestObjectiveBound
147 {
148 get {
149 if (!helper_.HasSolution())
150 {
151 throw new SolverException("Solver.BestObjectiveBound",
152 "Solve() was not called or no solution was found");
153 }
154 return helper_.BestObjectiveBound();
155 }
156 }
157
162 public double Value(Variable var)
163 {
164 if (!helper_.HasSolution())
165 {
166 throw new SolverException("Solver.Value())", "Solve() was not called or no solution was found");
167 }
168 return helper_.VariableValue(var.Index);
169 }
174 public double ReducedCost(Variable var)
175 {
176 if (!helper_.HasSolution())
177 {
178 throw new SolverException("Solver.ReducedCost())", "Solve() was not called or no solution was found");
179 }
180 return helper_.ReducedCost(var.Index);
181 }
182
187 public double DualValue(LinearConstraint ct)
188 {
189 if (!helper_.HasSolution())
190 {
191 throw new SolverException("Solver.DualValue())", "Solve() was not called or no solution was found");
192 }
193 return helper_.DualValue(ct.Index);
194 }
195
200 public double Activity(LinearConstraint ct)
201 {
202 if (!helper_.HasSolution())
203 {
204 throw new SolverException("Solver.Activity())", "Solve() was not called or no solution was found");
205 }
206 return helper_.Activity(ct.Index);
207 }
208
213 {
214 get {
215 return logCallback_;
216 }
217 set {
218 logCallback_ = value;
219 }
220 }
221
225 public double WallTime
226 {
227 get {
228 return helper_.WallTime();
229 }
230 }
231
235 public double UserTime
236 {
237 get {
238 return helper_.UserTime();
239 }
240 }
241
242 private ModelSolverHelper helper_;
243 private MbLogCallback logCallback_;
244}
245
246} // namespace Google.OrTools.ModelBuilder
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
int Index
Returns the index of the constraint in the model.
void SetSolverSpecificParameters(string solver_specific_parameters)
void SetLogCallbackFromDirectorClass(MbLogCallback log_callback)
ModelBuilderHelper Helper
The model builder helper.
MbLogCallback LogCallback
Sets the log callback for the solver.
double Value(Variable var)
The value of a variable in the current solution. This raises a SolverException if no solution has bee...
double BestObjectiveBound
The best objective bound found during search. This raises a SolverException if no solution has been f...
double DualValue(LinearConstraint ct)
The dual value of a linear constraint in the current solution. This raises a SolverException if no so...
void SetTimeLimitInSeconds(double limit)
double Activity(LinearConstraint ct)
The activity of a constraint in the current solution. This raises a SolverException if no solution ha...
double ReducedCost(Variable var)
The reduced cost of a variable in the current solution. This raises a SolverException if no solution ...
double ObjectiveValue
The best objective value found during search. This raises a SolverException if no solution has been f...
void SetSolverSpecificParameters(String parameters)
SolveStatus Solve(Model model)
Solves given model, and returns the status of the response.
double UserTime
Returns the user time since the creation of the solver.
bool SolverIsSupported()
Returns whether solver specified during the ctor was found and correctly installed.
bool InterruptSolve()
Tries to interrupt the solve. Returns true if the feature is supported.
void EnableOutput(bool enable)
Enables or disables the underlying solver output.
bool HasSolution()
Returns true if solve() was called, and a solution was returned.
bool HasResponse()
Returns true if solve() was called, and a response was returned.
Solver(String solverName)
Creates the solver with the supplied solver backend.
double WallTime
Returns the elapsed time since the creation of the solver.