Add AssignmentLinearAssignment in all supported languages

This commit is contained in:
Corentin Le Molgat
2021-12-20 15:49:07 +01:00
parent b9dd3e47d4
commit 701b00914b
4 changed files with 239 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
// Copyright 2010-2021 Google LLC
// 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]
public class AssignmentLinearAssignment
{
static void Main()
{
// [START solver]
LinearSumAssignment assignment = new LinearSumAssignment();
// [END solver]
// [START data]
int[,] costs = {
{90, 76, 75, 70},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115},
};
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)
{
foreach (int t in allTasks)
{
if (costs[w,t] != 0)
{
assignment.AddArcWithCost(w, t, costs[w, t]);
}
}
}
// [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)
{
Console.WriteLine(
$"Worker {worker} assigned to task {assignment.RightMate(worker)}. " +
$"Cost: {assignment.AssignmentCost(worker)}.");
}
}
else
{
Console.WriteLine("Solving the linear assignment problem failed.");
Console.WriteLine($"Solver status: {status}.");
}
// [END print_solution]
}
}
// [END program]

View File

@@ -0,0 +1,76 @@
// Copyright 2010-2021 Google LLC
// 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]
package com.google.ortools.graph.samples;
// [START import]
import com.google.ortools.Loader;
import com.google.ortools.graph.LinearSumAssignment;
import java.util.stream.IntStream;
// [END import]
/** Minimal Linear Sum Assignment. */
public class AssignmentLinearAssignment {
public static void main(String[] args) throws Exception {
Loader.loadNativeLibraries();
// [START solver]
LinearSumAssignment assignment = new LinearSumAssignment();
// [END solver]
// [START data]
final int[][] costs = {
{90, 76, 75, 70},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115},
};
final int numWorkers = 4;
final int numTasks = 4;
final int[] allWorkers = IntStream.range(0, numWorkers).toArray();
final int[] allTasks = IntStream.range(0, numTasks).toArray();
// [END data]
// [START constraints]
// Add each arc.
for (int w : allWorkers) {
for (int t : allTasks) {
if (costs[w][t] != 0) {
assignment.addArcWithCost(w, t, costs[w][t]);
}
}
}
// [END constraints]
// [START solve]
LinearSumAssignment.Status status = assignment.solve();
// [END solve]
// [START print_solution]
if (status == LinearSumAssignment.Status.OPTIMAL) {
System.out.println("Total cost: " + assignment.getOptimalCost());
for (int worker : allWorkers) {
System.out.println("Worker " + worker +
" assigned to task " + assignment.getRightMate(worker) +
". Cost: " + assignment.getAssignmentCost(worker));
}
} else {
System.out.println("Solving the min cost flow problem failed.");
System.out.println("Solver status: " + status);
}
// [END print_solution]
}
private AssignmentLinearAssignment() {}
}
// [END program]

View File

@@ -0,0 +1,83 @@
// Copyright 2010-2021 Google LLC
// 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]
#include <cstdint>
#include <string>
#include <vector>
#include <numeric>
#include "ortools/graph/assignment.h"
// [END import]
namespace operations_research {
// MinCostFlow simple interface example.
void AssignmentLinearAssignment() {
// [START solver]
SimpleLinearSumAssignment assignment;
// [END solver]
// [START data]
const int num_workers = 4;
std::vector<int> all_workers(num_workers);
std::iota(all_workers.begin(), all_workers.end(), 0);
const int num_tasks = 4;
std::vector<int> all_tasks(num_tasks);
std::iota(all_tasks.begin(), all_tasks.end(), 0);
const std::vector<std::vector<int>> costs = {{
{{90, 76, 75, 70}}, // Worker 0
{{35, 85, 55, 65}}, // Worker 1
{{125, 95, 90, 105}}, // Worker 2
{{45, 110, 95, 115}}, // Worker 3
}};
// [END data]
// [START constraints]
for (int w: all_workers) {
for (int t: all_tasks) {
if (costs[w][t]) {
assignment.AddArcWithCost(w, t, costs[w][t]);
}
}
}
// [END constraints]
// [START solve]
SimpleLinearSumAssignment::Status status = assignment.Solve();
// [END solve]
// [START print_solution]
if (status == SimpleLinearSumAssignment::OPTIMAL) {
LOG(INFO) << "Total cost: " << assignment.OptimalCost();
for (int worker: all_workers) {
LOG(INFO)
<< "Worker " << std::to_string(worker)
<< " assigned to task " << std::to_string(assignment.RightMate(worker))
<< ". Cost: " << std::to_string(assignment.AssignmentCost(worker)) << ".";
}
} else {
LOG(INFO) << "Solving the linear assignment problem failed.";
}
// [END print_solution]
}
} // namespace operations_research
int main() {
operations_research::AssignmentLinearAssignment();
return EXIT_SUCCESS;
}
// [END program]

0
ortools/graph/samples/assignment_linear_assignment.py Normal file → Executable file
View File