DotNet Reference

DotNet Reference

RabbitsAndPheasantsSat.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 
18 {
19  static void Main()
20  {
21  // Creates the model.
22  CpModel model = new CpModel();
23  // Creates the variables.
24  IntVar r = model.NewIntVar(0, 100, "r");
25  IntVar p = model.NewIntVar(0, 100, "p");
26  // 20 heads.
27  model.Add(r + p == 20);
28  // 56 legs.
29  model.Add(4 * r + 2 * p == 56);
30 
31  // Creates a solver and solves the model.
32  CpSolver solver = new CpSolver();
33  CpSolverStatus status = solver.Solve(model);
34 
35  if (status == CpSolverStatus.Feasible)
36  {
37  Console.WriteLine(solver.Value(r) + " rabbits, and " +
38  solver.Value(p) + " pheasants");
39  }
40  }
41 }
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
CpSolverStatus
The status returned by a solver trying to solve a CpModelProto.
Definition: CpModel.pb.cs:186
IntVar NewIntVar(long lb, long ub, string name)
Definition: CpModel.cs:45
Constraint Add(BoundedLinearExpression lin)
Definition: CpModel.cs:104
CpSolverStatus Solve(CpModel model)
Definition: CpSolver.cs:22
Definition: CpSolver.cs:20
long Value(LinearExpr e)
Definition: CpSolver.cs:96
Definition: CpModel.pb.cs:12