DotNet Reference

DotNet Reference

AssignmentSat.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;
17 using Google.OrTools.Sat;
18 // [END import]
19 
20 public class AssignmentSat
21 {
22  static void Main()
23  {
24  // Data.
25  // [START data_model]
26  int[,] costs = {
27  {90, 80, 75, 70},
28  {35, 85, 55, 65},
29  {125, 95, 90, 95},
30  {45, 110, 95, 115},
31  {50, 100, 90, 100},
32  };
33  int numWorkers = costs.GetLength(0);
34  int numTasks = costs.GetLength(1);
35  // [END data_model]
36 
37  // Model.
38  // [START model]
39  CpModel model = new CpModel();
40  // [END model]
41 
42  // Variables.
43  // [START variables]
44  IntVar[,] x = new IntVar[numWorkers, numTasks];
45  // Variables in a 1-dim array.
46  IntVar[] xFlat = new IntVar[numWorkers * numTasks];
47  int[] costsFlat = new int[numWorkers * numTasks];
48  for (int i = 0; i < numWorkers; ++i)
49  {
50  for (int j = 0; j < numTasks; ++j)
51  {
52  x[i, j] = model.NewIntVar(0, 1, $"worker_{i}_task_{j}");
53  int k = i * numTasks + j;
54  xFlat[k] = x[i, j];
55  costsFlat[k] = costs[i, j];
56  }
57  }
58  // [END variables]
59 
60  // Constraints
61  // [START constraints]
62  // Each worker is assigned to at most one task.
63  for (int i = 0; i < numWorkers; ++i)
64  {
65  IntVar[] vars = new IntVar[numTasks];
66  for (int j = 0; j < numTasks; ++j)
67  {
68  vars[j] = x[i, j];
69  }
70  model.Add(LinearExpr.Sum(vars) <= 1);
71  }
72 
73  // Each task is assigned to exactly one worker.
74  for (int j = 0; j < numTasks; ++j)
75  {
76  IntVar[] vars = new IntVar[numWorkers];
77  for (int i = 0; i < numWorkers; ++i)
78  {
79  vars[i] = x[i, j];
80  }
81  model.Add(LinearExpr.Sum(vars) == 1);
82  }
83  // [END constraints]
84 
85  // Objective
86  // [START objective]
87  model.Minimize(LinearExpr.ScalProd(xFlat, costsFlat));
88  // [END objective]
89 
90  // Solve
91  // [START solve]
92  CpSolver solver = new CpSolver();
93  CpSolverStatus status = solver.Solve(model);
94  Console.WriteLine($"Solve status: {status}");
95  // [END solve]
96 
97  // Print solution.
98  // [START print_solution]
99  // Check that the problem has a feasible solution.
100  if (status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible) {
101  Console.WriteLine($"Total cost: {solver.ObjectiveValue}\n");
102  for (int i = 0; i < numWorkers; ++i)
103  {
104  for (int j = 0; j < numTasks; ++j)
105  {
106  if (solver.Value(x[i, j]) > 0.5)
107  {
108  Console.WriteLine($"Worker {i} assigned to task {j}. Cost: {costs[i, j]}");
109  }
110  }
111  }
112  } else {
113  Console.WriteLine("No solution found.");
114  }
115  // [END print_solution]
116 
117  Console.WriteLine("Statistics");
118  Console.WriteLine($" - conflicts : {solver.NumConflicts()}");
119  Console.WriteLine($" - branches : {solver.NumBranches()}");
120  Console.WriteLine($" - wall time : {solver.WallTime()}s");
121  }
122 }
123 // [END program]
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
void Minimize(LinearExpr obj)
Definition: CpModel.cs:588
static LinearExpr Sum(IEnumerable< IntVar > vars)
CpSolverStatus
The status returned by a solver trying to solve a CpModelProto.
Definition: CpModel.pb.cs:186
static LinearExpr ScalProd(IEnumerable< IntVar > vars, IEnumerable< int > coeffs)
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