DotNet Reference

DotNet Reference

ChannelingSampleSat.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 
39 public class ChannelingSampleSat
40 {
41  static void Main()
42  {
43  // Create the CP-SAT model.
44  CpModel model = new CpModel();
45 
46  // Declare our two primary variables.
47  IntVar x = model.NewIntVar(0, 10, "x");
48  IntVar y = model.NewIntVar(0, 10, "y");
49 
50  // Declare our intermediate boolean variable.
51  IntVar b = model.NewBoolVar("b");
52 
53  // Implement b == (x >= 5).
54  model.Add(x >= 5).OnlyEnforceIf(b);
55  model.Add(x < 5).OnlyEnforceIf(b.Not());
56 
57  // Create our two half-reified constraints.
58  // First, b implies (y == 10 - x).
59  model.Add(y == 10 - x).OnlyEnforceIf(b);
60  // Second, not(b) implies y == 0.
61  model.Add(y == 0).OnlyEnforceIf(b.Not());
62 
63  // Search for x values in increasing order.
64  model.AddDecisionStrategy(
65  new IntVar[] {x},
68 
69  // Create the solver.
70  CpSolver solver = new CpSolver();
71 
72  // Force solver to follow the decision strategy exactly.
73  solver.StringParameters = "search_branching:FIXED_SEARCH";
74 
76  new VarArraySolutionPrinter(new IntVar[] {x, y, b});
77  solver.SearchAllSolutions(model, cb);
78  }
79 }
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()
void OnlyEnforceIf(ILiteral lit)
Definition: Constraints.cs:28
void AddDecisionStrategy(IEnumerable< IntVar > vars, DecisionStrategyProto.Types.VariableSelectionStrategy var_str, DecisionStrategyProto.Types.DomainReductionStrategy dom_str)
Definition: CpModel.cs:629
VariableSelectionStrategy
The order in which the variables above should be considered.
Definition: CpModel.pb.cs:5294
override void OnSolutionCallback()
IntVar NewIntVar(long lb, long ub, string name)
Definition: CpModel.cs:45
Constraint Add(BoundedLinearExpression lin)
Definition: CpModel.cs:104
ILiteral Not()
CpSolverStatus SearchAllSolutions(CpModel model, SolutionCallback cb)
Definition: CpSolver.cs:52
string StringParameters
Definition: CpSolver.cs:86
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