Rework LinearSumAssignment sample
This commit is contained in:
@@ -415,6 +415,9 @@ test_cc_algorithms_samples: \
|
||||
|
||||
.PHONY: test_cc_graph_samples # Build and Run all C++ Graph Samples (located in ortools/graph/samples)
|
||||
test_cc_graph_samples: \
|
||||
rcc_assignment_linear_sum_assignment \
|
||||
rcc_assignment_min_flow \
|
||||
rcc_balance_min_flow \
|
||||
rcc_simple_max_flow_program \
|
||||
rcc_simple_min_cost_flow_program
|
||||
|
||||
|
||||
@@ -774,6 +774,9 @@ test_dotnet_constraint_solver_samples: \
|
||||
|
||||
.PHONY: test_dotnet_graph_samples # Build and Run all .Net LP Samples (located in ortools/graph/samples)
|
||||
test_dotnet_graph_samples: \
|
||||
rdotnet_AssignmentLinearSumAssignment \
|
||||
rdotnet_AssignmentMinFlow \
|
||||
rdotnet_BalanceMinFlow \
|
||||
rdotnet_SimpleMaxFlowProgram \
|
||||
rdotnet_SimpleMinCostFlowProgram \
|
||||
|
||||
|
||||
@@ -704,6 +704,11 @@ test_java_constraint_solver_samples: \
|
||||
|
||||
.PHONY: test_java_graph_samples # Build and Run all Java Graph Samples (located in ortools/graph/samples)
|
||||
test_java_graph_samples: \
|
||||
rjava_AssignmentLinearSumAssignment \
|
||||
rjava_AssignmentMinFlow \
|
||||
rjava_BalanceMinFlow \
|
||||
rjava_SimpleMaxFlowProgram \
|
||||
rjava_SimpleMinCostFlowProgram
|
||||
|
||||
.PHONY: test_java_linear_solver_samples # Build and Run all Java LP Samples (located in ortools/linear_solver/samples)
|
||||
test_java_linear_solver_samples: \
|
||||
|
||||
@@ -634,6 +634,9 @@ test_python_constraint_solver_samples: \
|
||||
|
||||
.PHONY: test_python_graph_samples # Run all Python Graph Samples (located in ortools/graph/samples)
|
||||
test_python_graph_samples: \
|
||||
rpy_assignment_linear_sum_assignment \
|
||||
rpy_assignment_min_flow \
|
||||
rpy_balance_min_flow \
|
||||
rpy_simple_max_flow_program \
|
||||
rpy_simple_min_cost_flow_program
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ using System.Linq;
|
||||
using Google.OrTools.Graph;
|
||||
// [END import]
|
||||
|
||||
public class AssignmentLinearAssignment
|
||||
public class AssignmentLinearSumAssignment
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
@@ -29,10 +29,10 @@ public class AssignmentLinearAssignment
|
||||
|
||||
// [START data]
|
||||
int[,] costs = {
|
||||
{90, 76, 75, 70},
|
||||
{35, 85, 55, 65},
|
||||
{125, 95, 90, 105},
|
||||
{45, 110, 95, 115},
|
||||
{ 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();
|
||||
@@ -44,13 +44,13 @@ public class AssignmentLinearAssignment
|
||||
// Add each arc.
|
||||
foreach (int w in allWorkers)
|
||||
{
|
||||
foreach (int t in allTasks)
|
||||
{
|
||||
if (costs[w,t] != 0)
|
||||
foreach (int t in allTasks)
|
||||
{
|
||||
assignment.AddArcWithCost(w, t, costs[w, t]);
|
||||
if (costs[w, t] != 0)
|
||||
{
|
||||
assignment.AddArcWithCost(w, t, costs[w, t]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// [END constraints]
|
||||
|
||||
@@ -64,9 +64,8 @@ public class AssignmentLinearAssignment
|
||||
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)}.");
|
||||
Console.WriteLine($"Worker {worker} assigned to task {assignment.RightMate(worker)}. " +
|
||||
$"Cost: {assignment.AssignmentCost(worker)}.");
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -19,9 +19,9 @@ 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 {
|
||||
/** Minimal Linear Sum Assignment problem. */
|
||||
public class AssignmentLinearSumAssignment {
|
||||
public static void main(String[] args) {
|
||||
Loader.loadNativeLibraries();
|
||||
// [START solver]
|
||||
LinearSumAssignment assignment = new LinearSumAssignment();
|
||||
@@ -29,10 +29,10 @@ public class AssignmentLinearAssignment {
|
||||
|
||||
// [START data]
|
||||
final int[][] costs = {
|
||||
{90, 76, 75, 70},
|
||||
{35, 85, 55, 65},
|
||||
{125, 95, 90, 105},
|
||||
{45, 110, 95, 115},
|
||||
{90, 76, 75, 70},
|
||||
{35, 85, 55, 65},
|
||||
{125, 95, 90, 105},
|
||||
{45, 110, 95, 115},
|
||||
};
|
||||
final int numWorkers = 4;
|
||||
final int numTasks = 4;
|
||||
@@ -60,9 +60,8 @@ public class AssignmentLinearAssignment {
|
||||
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));
|
||||
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.");
|
||||
@@ -71,6 +70,6 @@ public class AssignmentLinearAssignment {
|
||||
// [END print_solution]
|
||||
}
|
||||
|
||||
private AssignmentLinearAssignment() {}
|
||||
private AssignmentLinearSumAssignment() {}
|
||||
}
|
||||
// [END program]
|
||||
@@ -1,5 +1,11 @@
|
||||
load(":code_samples.bzl", "code_sample_cc")
|
||||
|
||||
code_sample_cc(name = "assignment_linear_sum_assignment")
|
||||
|
||||
code_sample_cc(name = "assignment_min_flow")
|
||||
|
||||
code_sample_cc(name = "balance_min_flow")
|
||||
|
||||
code_sample_cc(name = "simple_max_flow_program")
|
||||
|
||||
code_sample_cc(name = "simple_min_cost_flow_program")
|
||||
|
||||
@@ -13,17 +13,17 @@
|
||||
|
||||
// [START program]
|
||||
// [START import]
|
||||
#include "ortools/graph/assignment.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
|
||||
#include "ortools/graph/assignment.h"
|
||||
// [END import]
|
||||
|
||||
namespace operations_research {
|
||||
// MinCostFlow simple interface example.
|
||||
void AssignmentLinearAssignment() {
|
||||
void AssignmentLinearSumAssignment() {
|
||||
// [START solver]
|
||||
SimpleLinearSumAssignment assignment;
|
||||
// [END solver]
|
||||
@@ -38,16 +38,16 @@ void AssignmentLinearAssignment() {
|
||||
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
|
||||
{{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) {
|
||||
for (int w : all_workers) {
|
||||
for (int t : all_tasks) {
|
||||
if (costs[w][t]) {
|
||||
assignment.AddArcWithCost(w, t, costs[w][t]);
|
||||
}
|
||||
@@ -62,11 +62,10 @@ void AssignmentLinearAssignment() {
|
||||
// [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)) << ".";
|
||||
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.";
|
||||
@@ -77,7 +76,7 @@ void AssignmentLinearAssignment() {
|
||||
} // namespace operations_research
|
||||
|
||||
int main() {
|
||||
operations_research::AssignmentLinearAssignment();
|
||||
operations_research::AssignmentLinearSumAssignment();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
// [END program]
|
||||
Reference in New Issue
Block a user