DotNet Reference

DotNet Reference

SearchHelpers.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 
20  public class CpSolverSolutionCallback : SolutionCallback
21  {
22  public long Value(LinearExpr e)
23  {
24  List<LinearExpr> exprs = new List<LinearExpr>();
25  List<long> coeffs = new List<long>();
26  exprs.Add(e);
27  coeffs.Add(1L);
28  long constant = 0;
29 
30  while (exprs.Count > 0)
31  {
32  LinearExpr expr = exprs[0];
33  exprs.RemoveAt(0);
34  long coeff = coeffs[0];
35  coeffs.RemoveAt(0);
36  if (coeff == 0) continue;
37 
38  if (expr is ProductCst)
39  {
40  ProductCst p = (ProductCst)expr;
41  if (p.Coeff != 0)
42  {
43  exprs.Add(p.Expr);
44  coeffs.Add(p.Coeff * coeff);
45  }
46  }
47  else if (expr is SumArray)
48  {
49  SumArray a = (SumArray)expr;
50  constant += coeff * a.Constant;
51  foreach (LinearExpr sub in a.Expressions)
52  {
53  exprs.Add(sub);
54  coeffs.Add(coeff);
55  }
56  }
57  else if (expr is IntVar)
58  {
59  int index = expr.Index;
60  long value = SolutionIntegerValue(index);
61  constant += coeff * value;
62  }
63  else if (expr is NotBooleanVariable)
64  {
65  throw new ArgumentException(
66  "Cannot evaluate a literal in an integer expression.");
67  }
68  else
69  {
70  throw new ArgumentException("Cannot evaluate '" + expr.ToString() +
71  "' in an integer expression");
72  }
73  }
74  return constant;
75  }
76 
77  public Boolean BooleanValue(ILiteral literal)
78  {
79  if (literal is IntVar || literal is NotBooleanVariable)
80  {
81  int index = literal.GetIndex();
82  return SolutionBooleanValue(index);
83  }
84  else
85  {
86  throw new ArgumentException("Cannot evaluate '" + literal.ToString() +
87  "' as a boolean literal");
88  }
89  }
90  }
91 
93  {
94  private DateTime _startTime;
95  private int _solutionCount;
96 
98  {
99  _startTime = DateTime.Now;
100  }
101 
102  public override void OnSolutionCallback()
103  {
104  var currentTime = DateTime.Now;
105  var objective = ObjectiveValue();
106  var objectiveBound = BestObjectiveBound();
107  var objLb = Math.Min(objective, objectiveBound);
108  var objUb = Math.Max(objective, objectiveBound);
109  var time = currentTime - _startTime;
110 
111  Console.WriteLine(value: $"Solution {_solutionCount}, time = {time.TotalSeconds} s, objective = [{objLb}, {objUb}]");
112 
113  _solutionCount++;
114  }
115 
116  public int solutionCount() => _solutionCount;
117  }
118 
119 } // namespace Google.OrTools.Sat
int solutionCount()
long Constant
LinearExpr Expr
long Coeff
long Value(LinearExpr e)
Boolean BooleanValue(ILiteral literal)
int Index
int GetIndex()
override void OnSolutionCallback()
ObjectiveSolutionPrinter()
Definition: Constraints.cs:14