OR-Tools 7.2
Main Page
Namespaces
Classes
Files
File List
File Members
ortools
sat
samples
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
18
public
class
VarArraySolutionPrinter
:
CpSolverSolutionCallback
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},
66
DecisionStrategyProto
.
Types
.
VariableSelectionStrategy
.ChooseFirst,
67
DecisionStrategyProto
.
Types
.
DomainReductionStrategy
.SelectMinValue);
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
75
VarArraySolutionPrinter
cb =
76
new
VarArraySolutionPrinter
(
new
IntVar
[] {x, y, b});
77
solver.
SearchAllSolutions
(model, cb);
78
}
79
}
Google.OrTools.Sat.DecisionStrategyProto
Define the strategy to follow when the solver needs to take a new decision.
Definition:
CpModel.pb.cs:4619
Google.OrTools.Sat.CpSolverSolutionCallback
Definition:
SearchHelpers.cs:20
Google.OrTools.Util
Definition:
Domain.cs:11
VarArraySolutionPrinter.VarArraySolutionPrinter
VarArraySolutionPrinter(IntVar[] variables)
Definition:
ChannelingSampleSat.cs:20
Google.OrTools.Sat.IntVar
Definition:
IntegerExpressions.cs:491
Google
Definition:
KInt64Vector.cs:11
Google.OrTools.Sat.DecisionStrategyProto.Types
Container for nested types declared in the DecisionStrategyProto message type.
Definition:
CpModel.pb.cs:4823
Google.OrTools.Sat.DecisionStrategyProto.Types.DomainReductionStrategy
DomainReductionStrategy
Once a variable has been chosen, this enum describe what decision is taken on its domain.
Definition:
CpModel.pb.cs:4844
ChannelingSampleSat
Definition:
ChannelingSampleSat.cs:39
Google.OrTools.Sat.CpSolver.StringParameters
string StringParameters
Definition:
CpSolver.cs:86
Google.OrTools.Sat.CpModel.Add
Constraint Add(BoundedLinearExpression lin)
Definition:
CpModel.cs:104
Google.OrTools.Sat.CpSolverSolutionCallback.Value
long Value(LinearExpr e)
Definition:
SearchHelpers.cs:22
VarArraySolutionPrinter
Definition:
ChannelingSampleSat.cs:18
Google.OrTools.Sat.CpSolver.SearchAllSolutions
CpSolverStatus SearchAllSolutions(CpModel model, SolutionCallback cb)
Definition:
CpSolver.cs:52
Google.OrTools.Sat.IntVar.Not
ILiteral Not()
Definition:
IntegerExpressions.cs:542
Google.OrTools
Definition:
KInt64Vector.cs:11
Google.OrTools.Sat.CpModel
Wrapper class around the cp_model proto.
Definition:
CpModel.cs:23
Google.OrTools.Sat.CpModel.NewBoolVar
IntVar NewBoolVar(string name)
Definition:
CpModel.cs:67
Google.OrTools.Sat.CpModel.AddDecisionStrategy
void AddDecisionStrategy(IEnumerable< IntVar > vars, DecisionStrategyProto.Types.VariableSelectionStrategy var_str, DecisionStrategyProto.Types.DomainReductionStrategy dom_str)
Definition:
CpModel.cs:624
VarArraySolutionPrinter.OnSolutionCallback
override void OnSolutionCallback()
Definition:
ChannelingSampleSat.cs:25
Google.OrTools.Sat.IntVar.ShortString
override string ShortString()
Definition:
IntegerExpressions.cs:525
Google.OrTools.Sat.CpModel.NewIntVar
IntVar NewIntVar(long lb, long ub, string name)
Definition:
CpModel.cs:45
Google.OrTools.Sat.DecisionStrategyProto.Types.VariableSelectionStrategy
VariableSelectionStrategy
The order in which the variables above should be considered.
Definition:
CpModel.pb.cs:4830
Google.OrTools.Sat
Definition:
CpModel.pb.cs:12
Google.OrTools.Sat.CpSolver
Definition:
CpSolver.cs:19
Google.OrTools.Sat.Constraint.OnlyEnforceIf
void OnlyEnforceIf(ILiteral lit)
Definition:
Constraints.cs:28