DotNet Reference

DotNet Reference

EarlinessTardinessCostSampleSat.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  long earliness_date = 5;
44  long earliness_cost = 8;
45  long lateness_date = 15;
46  long lateness_cost = 12;
47 
48  // Create the CP-SAT model.
49  CpModel model = new CpModel();
50 
51  // Declare our primary variable.
52  IntVar x = model.NewIntVar(0, 20, "x");
53 
54  // Create the expression variable and implement the piecewise linear
55  // function.
56  //
57  // \ /
58  // \______/
59  // ed ld
60  //
61  long large_constant = 1000;
62  IntVar expr = model.NewIntVar(0, large_constant, "expr");
63 
64  // First segment.
65  IntVar s1 = model.NewIntVar(-large_constant, large_constant, "s1");
66  model.Add(s1 == earliness_cost * (earliness_date - x));
67 
68  // Second segment.
69  IntVar s2 = model.NewConstant(0);
70 
71  // Third segment.
72  IntVar s3 = model.NewIntVar(-large_constant, large_constant, "s3");
73  model.Add(s3 == lateness_cost * (x - lateness_date));
74 
75  // Link together expr and x through s1, s2, and s3.
76  model.AddMaxEquality(expr, new IntVar[] {s1, s2, s3});
77 
78  // Search for x values in increasing order.
79  model.AddDecisionStrategy(
80  new IntVar[] {x},
83 
84  // Create the solver.
85  CpSolver solver = new CpSolver();
86 
87  // Force solver to follow the decision strategy exactly.
88  solver.StringParameters = "search_branching:FIXED_SEARCH";
89 
91  new VarArraySolutionPrinter(new IntVar[] {x, expr});
92  solver.SearchAllSolutions(model, cb);
93  }
94 }
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()
Constraint AddMaxEquality(IntVar target, IEnumerable< IntVar > vars)
Definition: CpModel.cs:454
void AddDecisionStrategy(IEnumerable< IntVar > vars, DecisionStrategyProto.Types.VariableSelectionStrategy var_str, DecisionStrategyProto.Types.DomainReductionStrategy dom_str)
Definition: CpModel.cs:629
IntVar NewConstant(long value)
Definition: CpModel.cs:57
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
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
Define the strategy to follow when the solver needs to take a new decision.
Definition: CpModel.pb.cs:5083
Definition: CpModel.pb.cs:12