DotNet Reference

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