DotNet Reference

DotNet Reference

CpSolver.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 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 using System;
15 using System.Collections.Generic;
16 
17 namespace Google.OrTools.Sat
18 {
19  public class CpSolver
20  {
21 
23  {
24  if (string_parameters_ != null)
25  {
26  response_ = SatHelper.SolveWithStringParameters(model.Model,
27  string_parameters_);
28  }
29  else
30  {
31  response_ = SatHelper.Solve(model.Model);
32  }
33  return response_.Status;
34  }
35 
38  {
39  if (string_parameters_ != null)
40  {
42  model.Model, string_parameters_, cb);
43  }
44  else
45  {
47  model.Model, "", cb);
48  }
49  return response_.Status;
50  }
51 
53  {
54  if (string_parameters_ != null)
55  {
56  string extra_parameters = " enumerate_all_solutions:true";
57  response_ =
59  model.Model, string_parameters_ + extra_parameters, cb);
60  }
61  else
62  {
63  string parameters = "enumerate_all_solutions:true";
65  model.Model, parameters, cb);
66  }
67  return response_.Status;
68  }
69 
70  public String ResponseStats()
71  {
72  return SatHelper.SolverResponseStats(response_);
73  }
74 
75  public double ObjectiveValue
76  {
77  get { return response_.ObjectiveValue; }
78  }
79 
80  public double BestObjectiveBound
81  {
82  get { return response_.BestObjectiveBound; }
83  }
84 
85  public string StringParameters
86  {
87  get { return string_parameters_; }
88  set { string_parameters_ = value; }
89  }
90 
92  {
93  get { return response_; }
94  }
95 
96  public long Value(LinearExpr e)
97  {
98  List<LinearExpr> exprs = new List<LinearExpr>();
99  List<long> coeffs = new List<long>();
100  exprs.Add(e);
101  coeffs.Add(1L);
102  long constant = 0;
103 
104  while (exprs.Count > 0)
105  {
106  LinearExpr expr = exprs[0];
107  exprs.RemoveAt(0);
108  long coeff = coeffs[0];
109  coeffs.RemoveAt(0);
110  if (coeff == 0) continue;
111 
112  if (expr is ProductCst)
113  {
114  ProductCst p = (ProductCst)expr;
115  if (p.Coeff != 0)
116  {
117  exprs.Add(p.Expr);
118  coeffs.Add(p.Coeff * coeff);
119  }
120  }
121  else if (expr is SumArray)
122  {
123  SumArray a = (SumArray)expr;
124  constant += coeff * a.Constant;
125  foreach (LinearExpr sub in a.Expressions)
126  {
127  exprs.Add(sub);
128  coeffs.Add(coeff);
129  }
130  }
131  else if (expr is IntVar)
132  {
133  int index = expr.Index;
134  long value = index >= 0 ? response_.Solution[index]
135  : -response_.Solution[-index - 1];
136  constant += coeff * value;
137  }
138  else if (expr is NotBooleanVariable)
139  {
140  throw new ArgumentException(
141  "Cannot evaluate a literal in an integer expression.");
142  }
143  else
144  {
145  throw new ArgumentException("Cannot evaluate '" + expr.ToString() +
146  "' in an integer expression");
147  }
148  }
149  return constant;
150  }
151 
152  public Boolean BooleanValue(ILiteral literal)
153  {
154  if (literal is IntVar || literal is NotBooleanVariable)
155  {
156  int index = literal.GetIndex();
157  if (index >= 0)
158  {
159  return response_.Solution[index] != 0;
160  }
161  else
162  {
163  return response_.Solution[-index - 1] == 0;
164  }
165  }
166  else
167  {
168  throw new ArgumentException("Cannot evaluate '" + literal.ToString() +
169  "' as a boolean literal");
170  }
171  }
172 
173  public long NumBranches()
174  {
175  return response_.NumBranches;
176  }
177 
178  public long NumConflicts()
179  {
180  return response_.NumConflicts;
181  }
182 
183  public double WallTime()
184  {
185  return response_.WallTime;
186  }
187 
188 
189  private CpModelProto model_;
190  private CpSolverResponse response_;
191  string string_parameters_;
192  }
193 
194 } // namespace Google.OrTools.Sat
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
Boolean BooleanValue(ILiteral literal)
Definition: CpSolver.cs:152
The response returned by a solver trying to solve a CpModelProto.
Definition: CpModel.pb.cs:5997
CpModelProto Model
Definition: CpModel.cs:34
static Google.OrTools.Sat.CpSolverResponse SolveWithStringParametersAndSolutionCallback(Google.OrTools.Sat.CpModelProto model_proto, string parameters, SolutionCallback callback)
Definition: SatHelper.cs:89
long NumConflicts
Definition: CpModel.pb.cs:6226
long Constant
LinearExpr Expr
long Coeff
pbc::RepeatedField< long > Solution
A feasible solution to the given problem.
Definition: CpModel.pb.cs:6075
Definition: SatHelper.cs:15
double BestObjectiveBound
Only make sense for an optimization problem.
Definition: CpModel.pb.cs:6105
global::Google.OrTools.Sat.CpSolverStatus Status
The status of the solve.
Definition: CpModel.pb.cs:6056
CpSolverStatus
The status returned by a solver trying to solve a CpModelProto.
Definition: CpModel.pb.cs:186
String ResponseStats()
Definition: CpSolver.cs:70
double WallTime()
Definition: CpSolver.cs:183
static Google.OrTools.Sat.CpSolverResponse SolveWithStringParameters(Google.OrTools.Sat.CpModelProto model_proto, string parameters)
Definition: SatHelper.cs:69
A constraint programming problem.
Definition: CpModel.pb.cs:5663
long NumBranches()
Definition: CpSolver.cs:173
CpSolverStatus SearchAllSolutions(CpModel model, SolutionCallback cb)
Definition: CpSolver.cs:52
string StringParameters
Definition: CpSolver.cs:86
static Google.OrTools.Sat.CpSolverResponse Solve(Google.OrTools.Sat.CpModelProto model_proto)
Definition: SatHelper.cs:49
double ObjectiveValue
Definition: CpSolver.cs:76
double WallTime
Definition: CpModel.pb.cs:6270
long NumConflicts()
Definition: CpSolver.cs:178
CpSolverStatus SolveWithSolutionCallback(CpModel model, SolutionCallback cb)
Definition: CpSolver.cs:36
CpSolverStatus Solve(CpModel model)
Definition: CpSolver.cs:22
Definition: CpSolver.cs:20
long Value(LinearExpr e)
Definition: CpSolver.cs:96
double ObjectiveValue
Only make sense for an optimization problem.
Definition: CpModel.pb.cs:6089
long NumBranches
Definition: CpModel.pb.cs:6237
int Index
double BestObjectiveBound
Definition: CpSolver.cs:81
int GetIndex()
static string SolverResponseStats(Google.OrTools.Sat.CpSolverResponse response)
Definition: SatHelper.cs:114
CpSolverResponse Response
Definition: CpSolver.cs:92
Definition: CpModel.pb.cs:12