DotNet Reference

DotNet Reference

SimpleSatProgram.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 // [START program]
15 using System;
16 using Google.OrTools.Sat;
17 
18 public class SimpleSatProgram
19 {
20  static void Main()
21  {
22  // Creates the model.
23  // [START model]
24  CpModel model = new CpModel();
25  // [END model]
26 
27  // Creates the variables.
28  // [START variables]
29  int num_vals = 3;
30 
31  IntVar x = model.NewIntVar(0, num_vals - 1, "x");
32  IntVar y = model.NewIntVar(0, num_vals - 1, "y");
33  IntVar z = model.NewIntVar(0, num_vals - 1, "z");
34  // [END variables]
35 
36  // Creates the constraints.
37  // [START constraints]
38  model.Add(x != y);
39  // [END constraints]
40 
41  // Creates a solver and solves the model.
42  // [START solve]
43  CpSolver solver = new CpSolver();
44  CpSolverStatus status = solver.Solve(model);
45  // [END solve]
46 
47  if (status == CpSolverStatus.Feasible)
48  {
49  Console.WriteLine("x = " + solver.Value(x));
50  Console.WriteLine("y = " + solver.Value(y));
51  Console.WriteLine("z = " + solver.Value(z));
52  }
53  }
54 }
55 // [END program]
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