DotNet Reference

DotNet Reference

SolveWithTimeLimitSampleSat.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  int num_vals = 3;
25 
26  IntVar x = model.NewIntVar(0, num_vals - 1, "x");
27  IntVar y = model.NewIntVar(0, num_vals - 1, "y");
28  IntVar z = model.NewIntVar(0, num_vals - 1, "z");
29  // Adds a different constraint.
30  model.Add(x != y);
31 
32  // Creates a solver and solves the model.
33  CpSolver solver = new CpSolver();
34 
35  // Adds a time limit. Parameters are stored as strings in the solver.
36  solver.StringParameters = "max_time_in_seconds:10.0";
37 
38  CpSolverStatus status = solver.Solve(model);
39 
40  if (status == CpSolverStatus.Feasible)
41  {
42  Console.WriteLine("x = " + solver.Value(x));
43  Console.WriteLine("y = " + solver.Value(y));
44  Console.WriteLine("z = " + solver.Value(z));
45  }
46  }
47 }
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
string StringParameters
Definition: CpSolver.cs:86
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