From 9806a20832337afbd16acfda9c8ac4a61fea551d Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 3 Nov 2021 12:57:55 +0100 Subject: [PATCH] graph: Sync samples --- ortools/graph/samples/AssignmentMinFlow.cs | 3 +- ortools/graph/samples/AssignmentMinFlow.java | 3 +- ortools/graph/samples/BalanceMinFlow.cs | 7 ++-- ortools/graph/samples/BalanceMinFlow.java | 6 ++-- .../samples/SimpleMinCostFlowProgram.java | 1 + ortools/graph/samples/assignment_min_flow.cc | 26 +++++++------- ortools/graph/samples/assignment_min_flow.py | 1 + ortools/graph/samples/balance_min_flow.cc | 36 ++++++++++--------- ortools/graph/samples/balance_min_flow.py | 9 +++-- .../samples/simple_min_cost_flow_program.cc | 2 +- 10 files changed, 54 insertions(+), 40 deletions(-) diff --git a/ortools/graph/samples/AssignmentMinFlow.cs b/ortools/graph/samples/AssignmentMinFlow.cs index 4e5b39644a..20fe3a9948 100644 --- a/ortools/graph/samples/AssignmentMinFlow.cs +++ b/ortools/graph/samples/AssignmentMinFlow.cs @@ -37,8 +37,9 @@ public class AssignmentMinFlow int source = 0; int sink = 9; + int tasks = 4; // Define an array of supplies at each node. - int[] supplies = { 4, 0, 0, 0, 0, 0, 0, 0, 0, -4 }; + int[] supplies = { tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks }; // [END data] // [START constraints] diff --git a/ortools/graph/samples/AssignmentMinFlow.java b/ortools/graph/samples/AssignmentMinFlow.java index 22b9100774..e86b83d7f2 100644 --- a/ortools/graph/samples/AssignmentMinFlow.java +++ b/ortools/graph/samples/AssignmentMinFlow.java @@ -42,8 +42,9 @@ public class AssignmentMinFlow { int source = 0; int sink = 9; + int tasks = 4; // Define an array of supplies at each node. - int[] supplies = new int[] {4, 0, 0, 0, 0, 0, 0, 0, 0, -4}; + int[] supplies = new int[] {tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; // [END data] // [START constraints] diff --git a/ortools/graph/samples/BalanceMinFlow.cs b/ortools/graph/samples/BalanceMinFlow.cs index 41b699bda7..cefc158c92 100644 --- a/ortools/graph/samples/BalanceMinFlow.cs +++ b/ortools/graph/samples/BalanceMinFlow.cs @@ -44,8 +44,9 @@ public class BalanceMinFlow int source = 0; int sink = 13; + int tasks = 4; // Define an array of supplies at each node. - int[] supplies = { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4 }; + int[] supplies = { tasks, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -tasks }; // [END data] // [START constraints] @@ -78,8 +79,8 @@ public class BalanceMinFlow for (int i = 0; i < minCostFlow.NumArcs(); ++i) { // Can ignore arcs leading out of source or into sink. - if (minCostFlow.Tail(i) != 0 && minCostFlow.Tail(i) != 11 && minCostFlow.Tail(i) != 12 && - minCostFlow.Head(i) != 13) + if (minCostFlow.Tail(i) != source && minCostFlow.Tail(i) != 11 && minCostFlow.Tail(i) != 12 && + minCostFlow.Head(i) != sink) { // Arcs in the solution have a flow value of 1. Their start and end nodes // give an assignment of worker to task. diff --git a/ortools/graph/samples/BalanceMinFlow.java b/ortools/graph/samples/BalanceMinFlow.java index cdaf096da1..b0d908fcc1 100644 --- a/ortools/graph/samples/BalanceMinFlow.java +++ b/ortools/graph/samples/BalanceMinFlow.java @@ -42,6 +42,8 @@ public class BalanceMinFlow { int[] unitCosts = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, 60, 105, 80, 75, 45, 65, 110, 95, 0, 0, 0, 0}; + int source = 0; + int sink = 13; int tasks = 4; // Define an array of supplies at each node. int[] supplies = new int[] {tasks, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; @@ -74,8 +76,8 @@ public class BalanceMinFlow { System.out.println(); for (int i = 0; i < minCostFlow.getNumArcs(); ++i) { // Can ignore arcs leading out of source or intermediate nodes, or into sink. - if (minCostFlow.getTail(i) != 0 && minCostFlow.getTail(i) != 11 - && minCostFlow.getTail(i) != 12 && minCostFlow.getHead(i) != 13) { + if (minCostFlow.getTail(i) != source && minCostFlow.getTail(i) != 11 + && minCostFlow.getTail(i) != 12 && minCostFlow.getHead(i) != sink) { // Arcs in the solution have a flow value of 1. Their start and end nodes // give an assignment of worker to task. if (minCostFlow.getFlow(i) > 0) { diff --git a/ortools/graph/samples/SimpleMinCostFlowProgram.java b/ortools/graph/samples/SimpleMinCostFlowProgram.java index 80521296f4..56ef439565 100644 --- a/ortools/graph/samples/SimpleMinCostFlowProgram.java +++ b/ortools/graph/samples/SimpleMinCostFlowProgram.java @@ -12,6 +12,7 @@ // limitations under the License. // [START program] +// From Bradley, Hax, and Maganti, 'Applied Mathematical Programming', figure 8.1. package com.google.ortools.graph.samples; // [START import] import com.google.ortools.Loader; diff --git a/ortools/graph/samples/assignment_min_flow.cc b/ortools/graph/samples/assignment_min_flow.cc index 2aead41968..976c900263 100644 --- a/ortools/graph/samples/assignment_min_flow.cc +++ b/ortools/graph/samples/assignment_min_flow.cc @@ -30,21 +30,21 @@ void AssignmentMinFlow() { // Define four parallel arrays: sources, destinations, capacities, // and unit costs between each pair. For instance, the arc from node 0 // to node 1 has a capacity of 15. - std::vector start_nodes = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, - 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8}; - std::vector end_nodes = {1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, - 5, 6, 7, 8, 5, 6, 7, 8, 9, 9, 9, 9}; - std::vector capacities = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - std::vector unit_costs = {0, 0, 0, 0, 90, 76, 75, 70, - 35, 85, 55, 65, 125, 95, 90, 105, - 45, 110, 95, 115, 0, 0, 0, 0}; + const std::vector start_nodes = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8}; + const std::vector end_nodes = {1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, + 5, 6, 7, 8, 5, 6, 7, 8, 9, 9, 9, 9}; + const std::vector capacities = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + const std::vector unit_costs = {0, 0, 0, 0, 90, 76, 75, 70, + 35, 85, 55, 65, 125, 95, 90, 105, + 45, 110, 95, 115, 0, 0, 0, 0}; - int64_t source = 0; - int64_t sink = 9; - int64_t tasks = 4; + const int64_t source = 0; + const int64_t sink = 9; + const int64_t tasks = 4; // Define an array of supplies at each node. - std::vector supplies = {tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; + const std::vector supplies = {tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; // [END data] // [START constraints] diff --git a/ortools/graph/samples/assignment_min_flow.py b/ortools/graph/samples/assignment_min_flow.py index e58957427e..f77e1e9414 100755 --- a/ortools/graph/samples/assignment_min_flow.py +++ b/ortools/graph/samples/assignment_min_flow.py @@ -39,6 +39,7 @@ def main(): [0, 0, 0, 0] + [90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115] + [0, 0, 0, 0]) + source = 0 sink = 9 tasks = 4 diff --git a/ortools/graph/samples/balance_min_flow.cc b/ortools/graph/samples/balance_min_flow.cc index e018bd06b5..f4dc85cf13 100644 --- a/ortools/graph/samples/balance_min_flow.cc +++ b/ortools/graph/samples/balance_min_flow.cc @@ -28,25 +28,29 @@ void BalanceMinFlow() { // [START data] // Define the directed graph for the flow. - std::vector team_A = {1, 3, 5}; - std::vector team_B = {2, 4, 6}; + const std::vector team_A = {1, 3, 5}; + const std::vector team_B = {2, 4, 6}; - std::vector start_nodes = { + const std::vector start_nodes = { 0, 0, 11, 11, 11, 12, 12, 12, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 8, 9, 10}; - std::vector end_nodes = {11, 12, 1, 3, 5, 2, 4, 6, 7, 8, 9, 10, - 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, - 7, 8, 9, 10, 7, 8, 9, 10, 13, 13, 13, 13}; - std::vector capacities = {2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - std::vector unit_costs = {0, 0, 0, 0, 0, 0, 0, 0, 90, - 76, 75, 70, 35, 85, 55, 65, 125, 95, - 90, 105, 45, 110, 95, 115, 60, 105, 80, - 75, 45, 65, 110, 95, 0, 0, 0, 0}; + const std::vector end_nodes = { + 11, 12, 1, 3, 5, 2, 4, 6, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, + 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 7, 8, 9, 10, 13, 13, 13, 13}; + const std::vector capacities = {2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + const std::vector unit_costs = { + 0, 0, 0, 0, 0, 0, 0, 0, 90, 76, 75, 70, + 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, + 60, 105, 80, 75, 45, 65, 110, 95, 0, 0, 0, 0}; + const int64_t source = 0; + const int64_t sink = 13; + const int64_t tasks = 4; // Define an array of supplies at each node. - std::vector supplies = {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4}; + const std::vector supplies = {tasks, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, -tasks}; // [END data] // [START constraints] @@ -75,8 +79,8 @@ void BalanceMinFlow() { for (std::size_t i = 0; i < min_cost_flow.NumArcs(); ++i) { // Can ignore arcs leading out of source or intermediate nodes, or into // sink. - if (min_cost_flow.Tail(i) != 0 && min_cost_flow.Tail(i) != 11 && - min_cost_flow.Tail(i) != 12 && min_cost_flow.Head(i) != 13) { + if (min_cost_flow.Tail(i) != source && min_cost_flow.Tail(i) != 11 && + min_cost_flow.Tail(i) != 12 && min_cost_flow.Head(i) != sink) { // Arcs in the solution have a flow value of 1. Their start and end // nodes give an assignment of worker to task. if (min_cost_flow.Flow(i) > 0) { diff --git a/ortools/graph/samples/balance_min_flow.py b/ortools/graph/samples/balance_min_flow.py index 885a1fecde..3a7d5923c4 100755 --- a/ortools/graph/samples/balance_min_flow.py +++ b/ortools/graph/samples/balance_min_flow.py @@ -44,8 +44,11 @@ def main(): 105, 80, 75, 45, 65, 110, 95 ] + [0, 0, 0, 0]) + source = 0 + sink = 13 + tasks = 4 # Define an array of supplies at each node. - supplies = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4] + supplies = [tasks, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -tasks] # [END data] # [START constraints] @@ -72,10 +75,10 @@ def main(): print() for arc in range(min_cost_flow.NumArcs()): # Can ignore arcs leading out of source or intermediate, or into sink. - if (min_cost_flow.Tail(arc) != 0 and + if (min_cost_flow.Tail(arc) != source and min_cost_flow.Tail(arc) != 11 and min_cost_flow.Tail(arc) != 12 and - min_cost_flow.Head(arc) != 13): + min_cost_flow.Head(arc) != sink): # Arcs in the solution will have a flow value of 1. # There start and end nodes give an assignment of worker to task. diff --git a/ortools/graph/samples/simple_min_cost_flow_program.cc b/ortools/graph/samples/simple_min_cost_flow_program.cc index 8fb0fac509..1b8c624f43 100644 --- a/ortools/graph/samples/simple_min_cost_flow_program.cc +++ b/ortools/graph/samples/simple_min_cost_flow_program.cc @@ -12,7 +12,7 @@ // limitations under the License. // [START program] -// From Bradley, H., and M., 'Applied Mathematical Programming', figure 8.1. +// From Bradley, Hax and Maganti, 'Applied Mathematical Programming', figure 8.1 // [START import] #include