DotNet Reference

DotNet Reference

StepFunctionSampleSat.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 Google.OrTools.Sat;
16 using Google.OrTools.Util;
17 
19 {
20  public VarArraySolutionPrinter(IntVar[] variables)
21  {
22  variables_ = variables;
23  }
24 
25  public override void OnSolutionCallback()
26  {
27  {
28  foreach (IntVar v in variables_)
29  {
30  Console.Write(String.Format("{0}={1} ", v.ShortString(), Value(v)));
31  }
32  Console.WriteLine();
33  }
34  }
35 
36  private IntVar[] variables_;
37 }
38 
40 {
41  static void Main()
42  {
43  // Create the CP-SAT model.
44  CpModel model = new CpModel();
45 
46  // Declare our primary variable.
47  IntVar x = model.NewIntVar(0, 20, "x");
48 
49  // Create the expression variable and implement the step function
50  // Note it is not defined for var == 2.
51  //
52  // - 3
53  // -- -- --------- 2
54  // 1
55  // -- --- 0
56  // 0 ================ 20
57  //
58  IntVar expr = model.NewIntVar(0, 3, "expr");
59 
60  // expr == 0 on [5, 6] U [8, 10]
61  ILiteral b0 = model.NewBoolVar("b0");
63  x,
64  Domain.FromValues(new long[] { 5, 6, 8, 9, 10 })).OnlyEnforceIf(b0);
65  model.Add(expr == 0).OnlyEnforceIf(b0);
66 
67  // expr == 2 on [0, 1] U [3, 4] U [11, 20]
68  ILiteral b2 = model.NewBoolVar("b2");
70  x,
72  new long[][] {new long[] {0, 1},
73  new long[] {3, 4},
74  new long[] {11, 20}})).OnlyEnforceIf(b2);
75  model.Add(expr == 2).OnlyEnforceIf(b2);
76 
77  // expr == 3 when x == 7
78  ILiteral b3 = model.NewBoolVar("b3");
79  model.Add(x == 7).OnlyEnforceIf(b3);
80  model.Add(expr == 3).OnlyEnforceIf(b3);
81 
82  // At least one bi is true. (we could use a sum == 1).
83  model.AddBoolOr(new ILiteral[] { b0, b2, b3 });
84 
85  // Search for x values in increasing order.
86  model.AddDecisionStrategy(
87  new IntVar[] { x },
90 
91  // Create the solver.
92  CpSolver solver = new CpSolver();
93 
94  // Force solver to follow the decision strategy exactly.
95  solver.StringParameters = "search_branching:FIXED_SEARCH";
96 
98  new VarArraySolutionPrinter(new IntVar[] { x, expr });
99  solver.SearchAllSolutions(model, cb);
100  }
101 }
Container for nested types declared in the DecisionStrategyProto message type.
Definition: CpModel.pb.cs:5287
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
override string ShortString()
Definition: Domain.cs:17
void OnlyEnforceIf(ILiteral lit)
Definition: Constraints.cs:28
static Domain FromValues(long[] values)
Definition: Domain.cs:65
VariableSelectionStrategy
The order in which the variables above should be considered.
Definition: CpModel.pb.cs:5294
override void OnSolutionCallback()
static Domain FromIntervals(long[][] intervals)
Definition: Domain.cs:70
IntVar NewIntVar(long lb, long ub, string name)
Definition: CpModel.cs:45
Constraint Add(BoundedLinearExpression lin)
Definition: CpModel.cs:104
CpSolverStatus SearchAllSolutions(CpModel model, SolutionCallback cb)
Definition: CpSolver.cs:52
string StringParameters
Definition: CpSolver.cs:86
Constraint AddLinearExpressionInDomain(LinearExpr linear_expr, Domain domain)
Definition: CpModel.cs:78
long Value(LinearExpr e)
DomainReductionStrategy
Once a variable has been chosen, this enum describe what decision is taken on its domain.
Definition: CpModel.pb.cs:5308
Definition: CpSolver.cs:20
VarArraySolutionPrinter(IntVar[] variables)
Definition: Domain.cs:11
IntVar NewBoolVar(string name)
Definition: CpModel.cs:67
Define the strategy to follow when the solver needs to take a new decision.
Definition: CpModel.pb.cs:5083
Definition: CpModel.pb.cs:12