2022-06-17 08:40:20 +02:00
|
|
|
// Copyright 2010-2022 Google LLC
|
2021-12-20 15:49:07 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
// [START program]
|
|
|
|
|
// [START import]
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Google.OrTools.Graph;
|
|
|
|
|
// [END import]
|
|
|
|
|
|
2021-12-22 11:05:53 +01:00
|
|
|
public class AssignmentLinearSumAssignment
|
2021-12-20 15:49:07 +01:00
|
|
|
{
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
|
|
|
|
// [START solver]
|
|
|
|
|
LinearSumAssignment assignment = new LinearSumAssignment();
|
|
|
|
|
// [END solver]
|
|
|
|
|
|
|
|
|
|
// [START data]
|
|
|
|
|
int[,] costs = {
|
2021-12-22 11:05:53 +01:00
|
|
|
{ 90, 76, 75, 70 },
|
|
|
|
|
{ 35, 85, 55, 65 },
|
|
|
|
|
{ 125, 95, 90, 105 },
|
|
|
|
|
{ 45, 110, 95, 115 },
|
2021-12-20 15:49:07 +01:00
|
|
|
};
|
|
|
|
|
int numWorkers = 4;
|
|
|
|
|
int[] allWorkers = Enumerable.Range(0, numWorkers).ToArray();
|
|
|
|
|
int numTasks = 4;
|
|
|
|
|
int[] allTasks = Enumerable.Range(0, numTasks).ToArray();
|
|
|
|
|
// [END data]
|
|
|
|
|
|
|
|
|
|
// [START constraints]
|
|
|
|
|
// Add each arc.
|
|
|
|
|
foreach (int w in allWorkers)
|
|
|
|
|
{
|
2021-12-22 11:05:53 +01:00
|
|
|
foreach (int t in allTasks)
|
2021-12-20 15:49:07 +01:00
|
|
|
{
|
2021-12-22 11:05:53 +01:00
|
|
|
if (costs[w, t] != 0)
|
|
|
|
|
{
|
|
|
|
|
assignment.AddArcWithCost(w, t, costs[w, t]);
|
|
|
|
|
}
|
2021-12-20 15:49:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// [END constraints]
|
|
|
|
|
|
|
|
|
|
// [START solve]
|
|
|
|
|
LinearSumAssignment.Status status = assignment.Solve();
|
|
|
|
|
// [END solve]
|
|
|
|
|
|
|
|
|
|
// [START print_solution]
|
|
|
|
|
if (status == LinearSumAssignment.Status.OPTIMAL)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Total cost: {assignment.OptimalCost()}.");
|
|
|
|
|
foreach (int worker in allWorkers)
|
|
|
|
|
{
|
2021-12-22 11:05:53 +01:00
|
|
|
Console.WriteLine($"Worker {worker} assigned to task {assignment.RightMate(worker)}. " +
|
|
|
|
|
$"Cost: {assignment.AssignmentCost(worker)}.");
|
2021-12-20 15:49:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Solving the linear assignment problem failed.");
|
|
|
|
|
Console.WriteLine($"Solver status: {status}.");
|
|
|
|
|
}
|
|
|
|
|
// [END print_solution]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// [END program]
|