DotNet Reference

.Net Reference

SearchHelpers.cs
Go to the documentation of this file.
1// Copyright 2010-2021 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
14using System;
15using System.Collections.Generic;
16
17namespace Google.OrTools.Sat
18{
19
25public class CpSolverSolutionCallback : SolutionCallback
26{
32 public long Value(LinearExpr e)
33 {
34 long constant = 0;
35 long coefficient = 1;
36 LinearExpr expr = e;
37 if (terms_ is null)
38 {
39 terms_ = new Queue<Term>();
40 }
41 else
42 {
43 terms_.Clear();
44 }
45
46 do
47 {
48 switch (expr)
49 {
50 case LinearExprBuilder a:
51 constant += coefficient * a.Offset;
52 if (coefficient == 1)
53 {
54 foreach (Term sub in a.Terms)
55 {
56 terms_.Enqueue(sub);
57 }
58 }
59 else
60 {
61 foreach (Term sub in a.Terms)
62 {
63 terms_.Enqueue(new Term(sub.expr, sub.coefficient * coefficient));
64 }
65 }
66 break;
67 case IntVar intVar:
68 int index = intVar.GetIndex();
69 long value = SolutionIntegerValue(index);
70 constant += coefficient * value;
71 break;
72 case NotBoolVar:
73 throw new ArgumentException("Cannot evaluate a literal in an integer expression.");
74 default:
75 throw new ArgumentException("Cannot evaluate '" + expr + "' in an integer expression");
76 }
77
78 if (!terms_.TryDequeue(out var term))
79 {
80 break;
81 }
82 expr = term.expr;
83 coefficient = term.coefficient;
84 } while (true);
85
86 return constant;
87 }
88
94 public long Value(IntVar intVar)
95 {
96 return SolutionIntegerValue(intVar.GetIndex());
97 }
98
104 public Boolean BooleanValue(ILiteral literal)
105 {
106 if (literal is BoolVar || literal is NotBoolVar)
107 {
108 int index = literal.GetIndex();
109 return SolutionBooleanValue(index);
110 }
111 else
112 {
113 throw new ArgumentException("Cannot evaluate '" + literal.ToString() + "' as a boolean literal");
114 }
115 }
116
117 private Queue<Term> terms_;
118}
119
126{
127 private DateTime _startTime;
128 private int _solutionCount;
129
131 {
132 _startTime = DateTime.Now;
133 }
134
135 public override void OnSolutionCallback()
136 {
137 var currentTime = DateTime.Now;
138 var objective = ObjectiveValue();
139 var objectiveBound = BestObjectiveBound();
140 var objLb = Math.Min(objective, objectiveBound);
141 var objUb = Math.Max(objective, objectiveBound);
142 var time = currentTime - _startTime;
143
144 Console.WriteLine(
145 value: $"Solution {_solutionCount}, time = {time.TotalSeconds} s, objective = [{objLb}, {objUb}]");
146
147 _solutionCount++;
148 }
149
150 public int solutionCount() => _solutionCount;
151}
152
153} // namespace Google.OrTools.Sat
Holds a Boolean variable.
Parent class to create a callback called at each solution.
long Value(LinearExpr e)
Returns the value of a linear expression in the current solution.
Boolean BooleanValue(ILiteral literal)
Returns the Boolean value of a literal in the current solution.
long Value(IntVar intVar)
Returns the value of an integer variable in the current solution.
Holds a integer variable with a discrete domain.
int GetIndex()
Returns the index of the variable in the underlying CpModelProto.
A builder class for linear expressions.
Holds a linear expression: sum (ai * xi) + b.
A specialized solution printer.
Holds a Boolean variable or its negation.
int GetIndex()
Returns the logical index of the literal.