DotNet Reference

DotNet Reference

SimpleCpProgram.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 // [START import]
16 using System;
18 // [END import]
19 
23 public class SimpleCpProgram {
24  public static void Main(String[] args) {
25  // Instantiate the solver.
26  // [START solver]
27  Solver solver = new Solver("CpSimple");
28  // [END solver]
29 
30  // Create the variables.
31  // [START variables]
32  const long numVals = 3;
33  IntVar x = solver.MakeIntVar(0, numVals - 1, "x");
34  IntVar y = solver.MakeIntVar(0, numVals - 1, "y");
35  IntVar z = solver.MakeIntVar(0, numVals - 1, "z");
36  // [END variables]
37 
38  // Constraint 0: x != y..
39  // [START constraints]
40  solver.Add(solver.MakeAllDifferent(new IntVar[]{x, y}));
41  Console.WriteLine($"Number of constraints: {solver.Constraints()}");
42  // [END constraints]
43 
44  // Solve the problem.
45  // [START solve]
46  DecisionBuilder db = solver.MakePhase(
47  new IntVar[]{x, y, z},
50  // [END solve]
51 
52  // Print solution on console.
53  // [START print_solution]
54  int count = 0;
55  solver.NewSearch(db);
56  while (solver.NextSolution()) {
57  ++count;
58  Console.WriteLine($"Solution: {count}\n x={x.Value()} y={y.Value()} z={z.Value()}");
59  }
60  solver.EndSearch();
61  Console.WriteLine($"Number of solutions found: {solver.Solutions()}");
62  // [END print_solution]
63 
64  // [START advanced]
65  Console.WriteLine("Advanced usage:");
66  Console.WriteLine($"Problem solved in {solver.WallTime()}ms");
67  Console.WriteLine($"Memory usage: {Solver.MemoryUsage()}bytes");
68  // [END advanced]
69  }
70 }
71 // [END program]
Definition: IntVar.cs:18
void Add(Constraint c)
static readonly int ASSIGN_MIN_VALUE
Definition: Assignment.cs:11
Constraint MakeAllDifferent(IntVarVector vars)
DecisionBuilder MakePhase(IntVarVector vars, int var_str, int val_str)
void EndSearch()
void NewSearch(DecisionBuilder db)
bool NextSolution()
static void Main(String[] args)
This is a simple CP program.
IntVar MakeIntVar(long min, long max, string name)
static readonly int CHOOSE_FIRST_UNBOUND