diff --git a/ortools/graph/samples/AssignmentMinFlow.cs b/ortools/graph/samples/AssignmentMinFlow.cs index 5523f4521a..4e5b39644a 100644 --- a/ortools/graph/samples/AssignmentMinFlow.cs +++ b/ortools/graph/samples/AssignmentMinFlow.cs @@ -10,6 +10,7 @@ // 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; @@ -28,27 +29,16 @@ public class AssignmentMinFlow // [START data] // Define four parallel arrays: sources, destinations, capacities, and unit costs // between each pair. - int[] startNodes = { - 0, 0, 0, 0, - 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, - 5, 6, 7, 8}; - int[] endNodes = { - 1, 2, 3, 4, - 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, - 9, 9, 9, 9}; - int[] 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}; - int[] unitCosts = { - 0, 0, 0, 0, - 90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, - 0, 0, 0, 0}; + int[] startNodes = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8 }; + int[] endNodes = { 1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 9, 9, 9, 9 }; + int[] 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 }; + int[] unitCosts = { 0, 0, 0, 0, 90, 76, 75, 70, 35, 85, 55, 65, + 125, 95, 90, 105, 45, 110, 95, 115, 0, 0, 0, 0 }; int source = 0; int sink = 9; // Define an array of supplies at each node. - int[] supplies = {4, 0, 0, 0, 0, 0, 0, 0, 0, -4}; + int[] supplies = { 4, 0, 0, 0, 0, 0, 0, 0, 0, -4 }; // [END data] // [START constraints] @@ -80,16 +70,17 @@ public class AssignmentMinFlow Console.WriteLine(""); for (int i = 0; i < minCostFlow.NumArcs(); ++i) { - // Can ignore arcs leading out of source or into sink. - if (minCostFlow.Tail(i) != source && 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. - if (minCostFlow.Flow(i) > 0) { - Console.WriteLine("Worker " + minCostFlow.Tail(i) + - " assigned to task " + minCostFlow.Head(i) + - " Cost: " + minCostFlow.UnitCost(i)); + // Can ignore arcs leading out of source or into sink. + if (minCostFlow.Tail(i) != source && 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. + if (minCostFlow.Flow(i) > 0) + { + Console.WriteLine("Worker " + minCostFlow.Tail(i) + " assigned to task " + minCostFlow.Head(i) + + " Cost: " + minCostFlow.UnitCost(i)); + } } - } } } else diff --git a/ortools/graph/samples/AssignmentMinFlow.csproj b/ortools/graph/samples/AssignmentMinFlow.csproj new file mode 100644 index 0000000000..e0a64c2bd4 --- /dev/null +++ b/ortools/graph/samples/AssignmentMinFlow.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.AssignmentMinFlow + true + + + + full + true + true + + + + + + + diff --git a/ortools/graph/samples/AssignmentMinFlow.java b/ortools/graph/samples/AssignmentMinFlow.java index ab7a015841..22b9100774 100644 --- a/ortools/graph/samples/AssignmentMinFlow.java +++ b/ortools/graph/samples/AssignmentMinFlow.java @@ -16,11 +16,10 @@ package com.google.ortools.graph.samples; // [START import] import com.google.ortools.Loader; import com.google.ortools.graph.MinCostFlow; -import java.util.ArrayList; -import java.util.List; +import com.google.ortools.graph.MinCostFlowBase; // [END import] -/** Minimal Assignment Min Flow.*/ +/** Minimal Assignment Min Flow. */ public class AssignmentMinFlow { public static void main(String[] args) throws Exception { Loader.loadNativeLibraries(); @@ -32,22 +31,14 @@ public class AssignmentMinFlow { // [START data] // Define four parallel arrays: sources, destinations, capacities, and unit costs // between each pair. - int[] startNodes = new int[] { - 0, 0, 0, 0, - 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, - 5, 6, 7, 8}; - int[] endNodes = new int[] { - 1, 2, 3, 4, - 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, - 9, 9, 9, 9}; - int[] capacities = new int[] { - 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1}; + int[] startNodes = + new int[] {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8}; + int[] endNodes = + new int[] {1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 9, 9, 9, 9}; + int[] capacities = + new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; int[] unitCosts = new int[] { - 0, 0, 0, 0, - 90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, - 0, 0, 0, 0}; + 0, 0, 0, 0, 90, 76, 75, 70, 35, 85, 55, 65, 125, 95, 90, 105, 45, 110, 95, 115, 0, 0, 0, 0}; int source = 0; int sink = 9; @@ -60,8 +51,9 @@ public class AssignmentMinFlow { for (int i = 0; i < startNodes.length; ++i) { int arc = minCostFlow.addArcWithCapacityAndUnitCost( startNodes[i], endNodes[i], capacities[i], unitCosts[i]); - if (arc != i) + if (arc != i) { throw new Exception("Internal error"); + } } // Add node supplies. @@ -72,23 +64,21 @@ public class AssignmentMinFlow { // [START solve] // Find the min cost flow. - MinCostFlow.Status status = minCostFlow.solve(); + MinCostFlowBase.Status status = minCostFlow.solve(); // [END solve] // [START print_solution] if (status == MinCostFlow.Status.OPTIMAL) { System.out.println("Total cost: " + minCostFlow.getOptimalCost()); - System.out.println(""); + System.out.println(); for (int i = 0; i < minCostFlow.getNumArcs(); ++i) { // Can ignore arcs leading out of source or into sink. if (minCostFlow.getTail(i) != source && 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) { - System.out.println( - "Worker " + minCostFlow.getTail(i) + - " assigned to task " + minCostFlow.getHead(i) + - " Cost: " + minCostFlow.getUnitCost(i)); + System.out.println("Worker " + minCostFlow.getTail(i) + " assigned to task " + + minCostFlow.getHead(i) + " Cost: " + minCostFlow.getUnitCost(i)); } } } diff --git a/ortools/graph/samples/BalanceMinFlow.cs b/ortools/graph/samples/BalanceMinFlow.cs index c3535d4dd7..41b699bda7 100644 --- a/ortools/graph/samples/BalanceMinFlow.cs +++ b/ortools/graph/samples/BalanceMinFlow.cs @@ -10,6 +10,7 @@ // 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; @@ -27,40 +28,24 @@ public class BalanceMinFlow // [START data] // Define the directed graph for the flow. - int[] teamA = {1, 3, 5}; - int[] teamB = {2, 4, 6}; + int[] teamA = { 1, 3, 5 }; + int[] teamB = { 2, 4, 6 }; // Define four parallel arrays: sources, destinations, capacities, and unit costs // between each pair. - int[] startNodes = { - 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}; - int[] endNodes = { - 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}; - int[] 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}; - int[] unitCosts = { - 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[] startNodes = { 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 }; + int[] endNodes = { 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 }; + int[] 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 }; + int[] unitCosts = { 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; // 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 = { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4 }; // [END data] // [START constraints] @@ -92,19 +77,18 @@ public class BalanceMinFlow Console.WriteLine(""); 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) { - // Arcs in the solution have a flow value of 1. Their start and end nodes - // give an assignment of worker to task. - if (minCostFlow.Flow(i) > 0) { - Console.WriteLine("Worker " + minCostFlow.Tail(i) + - " assigned to task " + minCostFlow.Head(i) + - " Cost: " + minCostFlow.UnitCost(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) + { + // Arcs in the solution have a flow value of 1. Their start and end nodes + // give an assignment of worker to task. + if (minCostFlow.Flow(i) > 0) + { + Console.WriteLine("Worker " + minCostFlow.Tail(i) + " assigned to task " + minCostFlow.Head(i) + + " Cost: " + minCostFlow.UnitCost(i)); + } } - } } } else @@ -116,4 +100,3 @@ public class BalanceMinFlow } } // [END program] - diff --git a/ortools/graph/samples/BalanceMinFlow.csproj b/ortools/graph/samples/BalanceMinFlow.csproj new file mode 100644 index 0000000000..e45db5acf0 --- /dev/null +++ b/ortools/graph/samples/BalanceMinFlow.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.BalanceMinFlow + true + + + + full + true + true + + + + + + + diff --git a/ortools/graph/samples/BalanceMinFlow.java b/ortools/graph/samples/BalanceMinFlow.java index 70e97199cf..cdaf096da1 100644 --- a/ortools/graph/samples/BalanceMinFlow.java +++ b/ortools/graph/samples/BalanceMinFlow.java @@ -16,11 +16,10 @@ package com.google.ortools.graph.samples; // [START import] import com.google.ortools.Loader; import com.google.ortools.graph.MinCostFlow; -import java.util.ArrayList; -import java.util.List; +import com.google.ortools.graph.MinCostFlowBase; // [END import] -/** Minimal Assignment Min Flow.*/ +/** Minimal Assignment Min Flow. */ public class BalanceMinFlow { public static void main(String[] args) throws Exception { Loader.loadNativeLibraries(); @@ -31,38 +30,21 @@ public class BalanceMinFlow { // [START data] // Define the directed graph for the flow. - int[] teamA = new int[] {1, 3, 5}; - int[] teamB = new int[] {2, 4, 6}; + // int[] teamA = new int[] {1, 3, 5}; + // int[] teamB = new int[] {2, 4, 6}; - int[] startNodes = new int[] { - 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}; - int[] endNodes = new int[] { - 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}; - int[] capacities = new int[] { - 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}; - 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[] startNodes = new int[] {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}; + int[] endNodes = new int[] {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}; + int[] capacities = new int[] {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}; + 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[] {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4}; + int[] supplies = new int[] {tasks, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; // [END data] // [START constraints] @@ -70,8 +52,9 @@ public class BalanceMinFlow { for (int i = 0; i < startNodes.length; ++i) { int arc = minCostFlow.addArcWithCapacityAndUnitCost( startNodes[i], endNodes[i], capacities[i], unitCosts[i]); - if (arc != i) + if (arc != i) { throw new Exception("Internal error"); + } } // Add node supplies. @@ -82,26 +65,22 @@ public class BalanceMinFlow { // [START solve] // Find the min cost flow. - MinCostFlow.Status status = minCostFlow.solve(); + MinCostFlowBase.Status status = minCostFlow.solve(); // [END solve] // [START print_solution] if (status == MinCostFlow.Status.OPTIMAL) { System.out.println("Total cost: " + minCostFlow.getOptimalCost()); - System.out.println(""); + 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) != 0 && minCostFlow.getTail(i) != 11 + && minCostFlow.getTail(i) != 12 && minCostFlow.getHead(i) != 13) { // 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) { - System.out.println( - "Worker " + minCostFlow.getTail(i) + - " assigned to task " + minCostFlow.getHead(i) + - " Cost: " + minCostFlow.getUnitCost(i)); + System.out.println("Worker " + minCostFlow.getTail(i) + " assigned to task " + + minCostFlow.getHead(i) + " Cost: " + minCostFlow.getUnitCost(i)); } } } diff --git a/ortools/graph/samples/SimpleMaxFlowProgram.cs b/ortools/graph/samples/SimpleMaxFlowProgram.cs index 565f569661..8dfa4a4d78 100644 --- a/ortools/graph/samples/SimpleMaxFlowProgram.cs +++ b/ortools/graph/samples/SimpleMaxFlowProgram.cs @@ -10,6 +10,7 @@ // 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] // From Taha 'Introduction to Operations Research', example 6.4-2. // [START import] diff --git a/ortools/graph/samples/SimpleMaxFlowProgram.java b/ortools/graph/samples/SimpleMaxFlowProgram.java index 90a9a33053..3fde527609 100644 --- a/ortools/graph/samples/SimpleMaxFlowProgram.java +++ b/ortools/graph/samples/SimpleMaxFlowProgram.java @@ -16,11 +16,9 @@ package com.google.ortools.graph.samples; // [START import] import com.google.ortools.Loader; import com.google.ortools.graph.MaxFlow; -import java.util.ArrayList; -import java.util.List; // [END import] -/** Minimal MaxFlow program.*/ +/** Minimal MaxFlow program. */ public final class SimpleMaxFlowProgram { public static void main(String[] args) throws Exception { Loader.loadNativeLibraries(); @@ -44,8 +42,9 @@ public final class SimpleMaxFlowProgram { // Add each arc. for (int i = 0; i < startNodes.length; ++i) { int arc = maxFlow.addArcWithCapacity(startNodes[i], endNodes[i], capacities[i]); - if (arc != i) + if (arc != i) { throw new Exception("Internal error"); + } } // [END constraints] @@ -57,7 +56,7 @@ public final class SimpleMaxFlowProgram { // [START print_solution] if (status == MaxFlow.Status.OPTIMAL) { System.out.println("Max. flow: " + maxFlow.getOptimalFlow()); - System.out.println(""); + System.out.println(); System.out.println(" Arc Flow / Capacity"); for (int i = 0; i < maxFlow.getNumArcs(); ++i) { System.out.println(maxFlow.getTail(i) + " -> " + maxFlow.getHead(i) + " " diff --git a/ortools/graph/samples/SimpleMinCostFlowProgram.cs b/ortools/graph/samples/SimpleMinCostFlowProgram.cs index a8ad1f2d1b..26e92e4830 100644 --- a/ortools/graph/samples/SimpleMinCostFlowProgram.cs +++ b/ortools/graph/samples/SimpleMinCostFlowProgram.cs @@ -10,6 +10,7 @@ // 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] // From Bradley, Hax, and Magnanti, 'Applied Mathematical Programming', figure 8.1. // [START import] diff --git a/ortools/graph/samples/SimpleMinCostFlowProgram.java b/ortools/graph/samples/SimpleMinCostFlowProgram.java index 6d313ab7f9..80521296f4 100644 --- a/ortools/graph/samples/SimpleMinCostFlowProgram.java +++ b/ortools/graph/samples/SimpleMinCostFlowProgram.java @@ -16,11 +16,10 @@ package com.google.ortools.graph.samples; // [START import] import com.google.ortools.Loader; import com.google.ortools.graph.MinCostFlow; -import java.util.ArrayList; -import java.util.List; +import com.google.ortools.graph.MinCostFlowBase; // [END import] -/** Minimal MinCostFlow program.*/ +/** Minimal MinCostFlow program. */ public class SimpleMinCostFlowProgram { public static void main(String[] args) throws Exception { Loader.loadNativeLibraries(); @@ -49,8 +48,9 @@ public class SimpleMinCostFlowProgram { for (int i = 0; i < startNodes.length; ++i) { int arc = minCostFlow.addArcWithCapacityAndUnitCost( startNodes[i], endNodes[i], capacities[i], unitCosts[i]); - if (arc != i) + if (arc != i) { throw new Exception("Internal error"); + } } // Add node supplies. @@ -61,13 +61,13 @@ public class SimpleMinCostFlowProgram { // [START solve] // Find the min cost flow. - MinCostFlow.Status status = minCostFlow.solve(); + MinCostFlowBase.Status status = minCostFlow.solve(); // [END solve] // [START print_solution] if (status == MinCostFlow.Status.OPTIMAL) { System.out.println("Minimum cost: " + minCostFlow.getOptimalCost()); - System.out.println(""); + System.out.println(); System.out.println(" Edge Flow / Capacity Cost"); for (int i = 0; i < minCostFlow.getNumArcs(); ++i) { long cost = minCostFlow.getFlow(i) * minCostFlow.getUnitCost(i); diff --git a/ortools/graph/samples/assignment_min_flow.cc b/ortools/graph/samples/assignment_min_flow.cc index eb69ed136a..2aead41968 100644 --- a/ortools/graph/samples/assignment_min_flow.cc +++ b/ortools/graph/samples/assignment_min_flow.cc @@ -10,6 +10,7 @@ // 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 @@ -29,27 +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}; + 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}; int64_t source = 0; int64_t sink = 9; + int64_t tasks = 4; // Define an array of supplies at each node. - std::vector supplies = {4, 0, 0, 0, 0, 0, 0, 0, 0, -4}; + std::vector supplies = {tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks}; // [END data] // [START constraints] @@ -78,8 +73,8 @@ void AssignmentMinFlow() { for (std::size_t i = 0; i < min_cost_flow.NumArcs(); ++i) { // Can ignore arcs leading out of source or into sink. if (min_cost_flow.Tail(i) != source && 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. + // 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) { LOG(INFO) << "Worker " << min_cost_flow.Tail(i) << " assigned to task " << min_cost_flow.Head(i) diff --git a/ortools/graph/samples/assignment_min_flow.py b/ortools/graph/samples/assignment_min_flow.py index 655e8d8dba..e58957427e 100755 --- a/ortools/graph/samples/assignment_min_flow.py +++ b/ortools/graph/samples/assignment_min_flow.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # [START program] +"""Linear assignment example.""" # [START import] from ortools.graph import pywrapgraph # [END import] @@ -29,9 +30,8 @@ def main(): 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] - 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] + 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] 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] @@ -42,15 +42,15 @@ def main(): source = 0 sink = 9 tasks = 4 - supplies = [4, 0, 0, 0, 0, 0, 0, 0, 0, -4] + supplies = [tasks, 0, 0, 0, 0, 0, 0, 0, 0, -tasks] # [END data] # [START constraints] # Add each arc. for i in range(len(start_nodes)): min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i], - end_nodes[i], - capacities[i], costs[i]) + end_nodes[i], capacities[i], + costs[i]) # Add node supplies. for i in range(len(supplies)): min_cost_flow.SetNodeSupply(i, supplies[i]) diff --git a/ortools/graph/samples/balance_min_flow.cc b/ortools/graph/samples/balance_min_flow.cc index b30dfedc6d..e018bd06b5 100644 --- a/ortools/graph/samples/balance_min_flow.cc +++ b/ortools/graph/samples/balance_min_flow.cc @@ -10,6 +10,7 @@ // 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 @@ -31,32 +32,19 @@ void BalanceMinFlow() { std::vector team_B = {2, 4, 6}; 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}; + 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}; - int64_t source = 0; - int64_t sink = 13; // 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}; // [END data] @@ -85,13 +73,12 @@ void BalanceMinFlow() { LOG(INFO) << "Total cost: " << min_cost_flow.OptimalCost(); LOG(INFO) << ""; 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) { - // Arcs in the solution have a flow value of 1. Their start and end nodes - // give an assignment of worker to task. + // 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) { + // 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) { LOG(INFO) << "Worker " << min_cost_flow.Tail(i) << " assigned to task " << min_cost_flow.Head(i) @@ -113,4 +100,3 @@ int main() { return EXIT_SUCCESS; } // [END program] - diff --git a/ortools/graph/samples/balance_min_flow.py b/ortools/graph/samples/balance_min_flow.py index 789a791a50..885a1fecde 100755 --- a/ortools/graph/samples/balance_min_flow.py +++ b/ortools/graph/samples/balance_min_flow.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # [START program] +"""Assignment with teams of workers.""" # [START import] from ortools.graph import pywrapgraph # [END import] @@ -25,13 +26,13 @@ def main(): # [START data] # Define the directed graph for the flow. - team_A = [1, 3, 5] - team_B = [2, 4, 6] + team_a = [1, 3, 5] + team_b = [2, 4, 6] 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]) - end_nodes = ([11, 12] + team_A + team_B + [ + end_nodes = ([11, 12] + team_a + team_b + [ 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]) @@ -45,16 +46,14 @@ def main(): # Define an array of supplies at each node. supplies = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4] - source = 0 - sink = 13 # [END data] # [START constraints] # Add each arc. for i in range(0, len(start_nodes)): min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i], - end_nodes[i], - capacities[i], costs[i]) + end_nodes[i], capacities[i], + costs[i]) # Add node supplies. for i in range(0, len(supplies)): @@ -72,13 +71,14 @@ def main(): print('Total cost = ', min_cost_flow.OptimalCost()) print() for arc in range(min_cost_flow.NumArcs()): - # Can ignore arcs leading out of source or intermediate nodes, or into sink. - if (min_cost_flow.Tail(arc) != 0 and min_cost_flow.Tail(arc) != 11 - and min_cost_flow.Tail(arc) != 12 - and min_cost_flow.Head(arc) != 13): + # Can ignore arcs leading out of source or intermediate, or into sink. + if (min_cost_flow.Tail(arc) != 0 and + min_cost_flow.Tail(arc) != 11 and + min_cost_flow.Tail(arc) != 12 and + min_cost_flow.Head(arc) != 13): - # Arcs in the solution will have a flow value of 1. There start and end nodes - # give an assignment of worker to task. + # Arcs in the solution will have a flow value of 1. + # There start and end nodes give an assignment of worker to task. if min_cost_flow.Flow(arc) > 0: print('Worker %d assigned to task %d. Cost = %d' % (min_cost_flow.Tail(arc), min_cost_flow.Head(arc), diff --git a/ortools/graph/samples/simple_max_flow_program.py b/ortools/graph/samples/simple_max_flow_program.py index dacbddaa92..525e51c7a3 100755 --- a/ortools/graph/samples/simple_max_flow_program.py +++ b/ortools/graph/samples/simple_max_flow_program.py @@ -42,7 +42,7 @@ def main(): # [START solve] # Find the maximum flow between node 0 and node 4. - status = max_flow.Solve(0, 4) + status = max_flow.Solve(0, 4) # [END solve] # [START print_solution] @@ -55,8 +55,8 @@ def main(): print(' Arc Flow / Capacity') for i in range(max_flow.NumArcs()): print('%1s -> %1s %3s / %3s' % - (max_flow.Tail(i), max_flow.Head(i), - max_flow.Flow(i), max_flow.Capacity(i))) + (max_flow.Tail(i), max_flow.Head(i), max_flow.Flow(i), + max_flow.Capacity(i))) print('Source side min-cut:', max_flow.GetSourceSideMinCut()) print('Sink side min-cut:', max_flow.GetSinkSideMinCut()) # [END print_solution] diff --git a/ortools/sat/samples/MinimalJobshopSat.cs b/ortools/sat/samples/MinimalJobshopSat.cs index 54610b9ffd..6c071a3e70 100644 --- a/ortools/sat/samples/MinimalJobshopSat.cs +++ b/ortools/sat/samples/MinimalJobshopSat.cs @@ -10,6 +10,7 @@ // 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; @@ -128,7 +129,7 @@ public class ScheduleRequestsSat allTasks[key] = Tuple.Create(start, end, interval); if (!machineToIntervals.ContainsKey(task.machine)) { - machineToIntervals.Add(task.machine, new List()); + machineToIntervals.Add(task.machine, new List()); } machineToIntervals[task.machine].Add(interval); } @@ -193,7 +194,7 @@ public class ScheduleRequestsSat int start = (int)solver.Value(allTasks[key].Item1); if (!assignedJobs.ContainsKey(task.machine)) { - assignedJobs.Add(task.machine, new List()); + assignedJobs.Add(task.machine, new List()); } assignedJobs[task.machine].Add(new AssignedTask(jobID, taskID, start, task.duration)); } diff --git a/ortools/sat/samples/MinimalJobshopSat.csproj b/ortools/sat/samples/MinimalJobshopSat.csproj new file mode 100644 index 0000000000..7950dc5db8 --- /dev/null +++ b/ortools/sat/samples/MinimalJobshopSat.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.MinimalJobshopSat + true + + + + full + true + true + + + + + + + diff --git a/ortools/sat/samples/MinimalJobshopSat.java b/ortools/sat/samples/MinimalJobshopSat.java index ffbefc1ae5..54efe44f16 100644 --- a/ortools/sat/samples/MinimalJobshopSat.java +++ b/ortools/sat/samples/MinimalJobshopSat.java @@ -10,13 +10,15 @@ // 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.sat.samples; // [START import] +import static java.lang.Math.max; + import com.google.ortools.Loader; import com.google.ortools.sat.CpModel; import com.google.ortools.sat.CpSolver; -import com.google.ortools.sat.CpSolverSolutionCallback; import com.google.ortools.sat.CpSolverStatus; import com.google.ortools.sat.IntVar; import com.google.ortools.sat.IntervalVar; @@ -31,12 +33,14 @@ import java.util.Map; import java.util.stream.IntStream; // [END import] +/** Minimal Jobshop problem. */ public class MinimalJobshopSat { - public static void main(String[] args) throws Exception { + public static void main(String[] args) { Loader.loadNativeLibraries(); // [START data] class Task { - int machine, duration; + int machine; + int duration; Task(int machine, int duration) { this.machine = machine; this.duration = duration; @@ -52,7 +56,7 @@ public class MinimalJobshopSat { int numMachines = 1; for (List job : allJobs) { for (Task task : job) { - numMachines = Math.max(numMachines, 1 + task.machine); + numMachines = max(numMachines, 1 + task.machine); } } final int[] allMachines = IntStream.range(0, numMachines).toArray(); @@ -94,9 +98,7 @@ public class MinimalJobshopSat { List key = Arrays.asList(jobID, taskID); allTasks.put(key, taskType); - if (!machineToIntervals.containsKey(task.machine)) { - machineToIntervals.put(task.machine, new ArrayList<>()); - } + machineToIntervals.computeIfAbsent(task.machine, (Integer k) -> new ArrayList<>()); machineToIntervals.get(task.machine).add(taskType.interval); } } @@ -106,7 +108,7 @@ public class MinimalJobshopSat { // Create and add disjunctive constraints. for (int machine : allMachines) { List list = machineToIntervals.get(machine); - model.addNoOverlap(list.toArray(new IntervalVar[list.size()])); + model.addNoOverlap(list.toArray(new IntervalVar[0])); } // Precedences inside a job. @@ -129,7 +131,7 @@ public class MinimalJobshopSat { List key = Arrays.asList(jobID, job.size() - 1); ends.add(allTasks.get(key).end); } - model.addMaxEquality(objVar, ends.toArray(new IntVar[ends.size()])); + model.addMaxEquality(objVar, ends.toArray(new IntVar[0])); model.minimize(objVar); // [END objective] @@ -142,7 +144,10 @@ public class MinimalJobshopSat { // [START print_solution] if (status == CpSolverStatus.OPTIMAL || status == CpSolverStatus.FEASIBLE) { class AssignedTask { - int jobID, taskID, start, duration; + int jobID; + int taskID; + int start; + int duration; // Ctor AssignedTask(int jobID, int taskID, int start, int duration) { this.jobID = jobID; @@ -151,12 +156,14 @@ public class MinimalJobshopSat { this.duration = duration; } } - class sortTasks implements Comparator { + class SortTasks implements Comparator { + @Override public int compare(AssignedTask a, AssignedTask b) { - if (a.start != b.start) + if (a.start != b.start) { return a.start - b.start; - else + } else { return a.duration - b.duration; + } } } System.out.println("Solution:"); @@ -169,9 +176,7 @@ public class MinimalJobshopSat { List key = Arrays.asList(jobID, taskID); AssignedTask assignedTask = new AssignedTask( jobID, taskID, (int) solver.value(allTasks.get(key).start), task.duration); - if (!assignedJobs.containsKey(task.machine)) { - assignedJobs.put(task.machine, new ArrayList<>()); - } + assignedJobs.computeIfAbsent(task.machine, (Integer k) -> new ArrayList<>()); assignedJobs.get(task.machine).add(assignedTask); } } @@ -180,7 +185,7 @@ public class MinimalJobshopSat { String output = ""; for (int machine : allMachines) { // Sort by starting time. - Collections.sort(assignedJobs.get(machine), new sortTasks()); + Collections.sort(assignedJobs.get(machine), new SortTasks()); String solLineTasks = "Machine " + machine + ": "; String solLine = " "; diff --git a/ortools/sat/samples/NursesSat.cs b/ortools/sat/samples/NursesSat.cs index db5463493c..a9a6f4a1c0 100644 --- a/ortools/sat/samples/NursesSat.cs +++ b/ortools/sat/samples/NursesSat.cs @@ -10,6 +10,7 @@ // 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; diff --git a/ortools/sat/samples/NursesSat.csproj b/ortools/sat/samples/NursesSat.csproj new file mode 100644 index 0000000000..4290bdc570 --- /dev/null +++ b/ortools/sat/samples/NursesSat.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.NursesSat + true + + + + full + true + true + + + + + + + diff --git a/ortools/sat/samples/NursesSat.java b/ortools/sat/samples/NursesSat.java index 1f37fb0289..35eb34338e 100644 --- a/ortools/sat/samples/NursesSat.java +++ b/ortools/sat/samples/NursesSat.java @@ -10,6 +10,7 @@ // 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.sat.samples; // [START import] @@ -23,8 +24,9 @@ import com.google.ortools.sat.LinearExpr; import java.util.stream.IntStream; // [END import] +/** Nurses problem. */ public class NursesSat { - public static void main(String[] args) throws Exception { + public static void main(String[] args) { Loader.loadNativeLibraries(); // [START data] final int numNurses = 4; @@ -169,6 +171,7 @@ public class NursesSat { // Creates a solver and solves the model. // [START solve] CpSolverStatus status = solver.solve(model, cb); + System.out.println("Status: " + status); System.out.println(cb.getSolutionCount() + " solutions found."); // [END solve] diff --git a/ortools/sat/samples/ScheduleRequestsSat.cs b/ortools/sat/samples/ScheduleRequestsSat.cs index b5c3a01d7e..e2e8329c9f 100644 --- a/ortools/sat/samples/ScheduleRequestsSat.cs +++ b/ortools/sat/samples/ScheduleRequestsSat.cs @@ -10,6 +10,7 @@ // 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; diff --git a/ortools/sat/samples/ScheduleRequestsSat.csproj b/ortools/sat/samples/ScheduleRequestsSat.csproj new file mode 100644 index 0000000000..a2e2564f0b --- /dev/null +++ b/ortools/sat/samples/ScheduleRequestsSat.csproj @@ -0,0 +1,24 @@ + + + Exe + 7.3 + netcoreapp3.1 + false + + LatestMajor + ../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json + Google.OrTools.ScheduleRequestsSat + true + + + + full + true + true + + + + + + + diff --git a/ortools/sat/samples/ScheduleRequestsSat.java b/ortools/sat/samples/ScheduleRequestsSat.java index e67e9e803b..52f6b4f851 100644 --- a/ortools/sat/samples/ScheduleRequestsSat.java +++ b/ortools/sat/samples/ScheduleRequestsSat.java @@ -10,21 +10,22 @@ // 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.sat.samples; // [START import] import com.google.ortools.Loader; import com.google.ortools.sat.CpModel; import com.google.ortools.sat.CpSolver; -import com.google.ortools.sat.CpSolverSolutionCallback; import com.google.ortools.sat.CpSolverStatus; import com.google.ortools.sat.IntVar; import com.google.ortools.sat.LinearExpr; import java.util.stream.IntStream; // [END import] +/** Nurses problem with schedule requests. */ public class ScheduleRequestsSat { - public static void main(String[] args) throws Exception { + public static void main(String[] args) { Loader.loadNativeLibraries(); // [START data] final int numNurses = 5; diff --git a/ortools/sat/samples/minimal_jobshop_sat.py b/ortools/sat/samples/minimal_jobshop_sat.py index a60fc453f5..545d789a2f 100755 --- a/ortools/sat/samples/minimal_jobshop_sat.py +++ b/ortools/sat/samples/minimal_jobshop_sat.py @@ -18,6 +18,7 @@ import collections from ortools.sat.python import cp_model # [END import] + def main(): """Minimal jobshop problem.""" # Data. @@ -102,11 +103,11 @@ def main(): for task_id, task in enumerate(job): machine = task[0] assigned_jobs[machine].append( - assigned_task_type( - start=solver.Value(all_tasks[job_id, task_id].start), - job=job_id, - index=task_id, - duration=task[1])) + assigned_task_type(start=solver.Value( + all_tasks[job_id, task_id].start), + job=job_id, + index=task_id, + duration=task[1])) # Create per machine output lines. output = '' @@ -117,7 +118,8 @@ def main(): sol_line = ' ' for assigned_task in assigned_jobs[machine]: - name = 'job_%i_task_%i' % (assigned_task.job, assigned_task.index) + name = 'job_%i_task_%i' % (assigned_task.job, + assigned_task.index) # Add spaces to output to align columns. sol_line_tasks += '%-15s' % name diff --git a/ortools/sat/samples/nurses_sat.cc b/ortools/sat/samples/nurses_sat.cc index b4f70b0d92..8a03ec2c70 100644 --- a/ortools/sat/samples/nurses_sat.cc +++ b/ortools/sat/samples/nurses_sat.cc @@ -1,8 +1,16 @@ +// 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] // Example of a simple nurse scheduling problem. // [START import] diff --git a/ortools/sat/samples/nurses_sat.py b/ortools/sat/samples/nurses_sat.py index 2d8523e66c..dd0ae07126 100755 --- a/ortools/sat/samples/nurses_sat.py +++ b/ortools/sat/samples/nurses_sat.py @@ -17,6 +17,7 @@ from ortools.sat.python import cp_model # [END import] + def main(): # Data. # [START data] @@ -40,7 +41,8 @@ def main(): for n in all_nurses: for d in all_days: for s in all_shifts: - shifts[(n, d, s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s)) + shifts[(n, d, + s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s)) # [END variables] # Each shift is assigned to exactly one nurse in the schedule period. @@ -117,7 +119,6 @@ def main(): def solution_count(self): return self._solution_count - # Display the first five solutions. solution_limit = 5 solution_printer = NursesPartialSolutionPrinter(shifts, num_nurses, diff --git a/ortools/sat/samples/schedule_requests_sat.cc b/ortools/sat/samples/schedule_requests_sat.cc index 463dfb6298..b9b3773f7d 100644 --- a/ortools/sat/samples/schedule_requests_sat.cc +++ b/ortools/sat/samples/schedule_requests_sat.cc @@ -1,8 +1,16 @@ +// 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] // Nurse scheduling problem with shift requests. // [START import] @@ -166,7 +174,7 @@ void ScheduleRequestsSat() { for (int s : all_shifts) { if (shift_requests[n][d][s] == 1) { auto key = std::make_tuple(n, d, s); - //tmp.push_back(shifts[key]); + // tmp.push_back(shifts[key]); tmp.push_back(LinearExpr::ScalProd({shifts[key]}, {shift_requests[n][d][s]}).Var()); } } diff --git a/ortools/sat/samples/schedule_requests_sat.py b/ortools/sat/samples/schedule_requests_sat.py index b69475eced..ba9c1a2f5f 100755 --- a/ortools/sat/samples/schedule_requests_sat.py +++ b/ortools/sat/samples/schedule_requests_sat.py @@ -17,6 +17,7 @@ from ortools.sat.python import cp_model # [END import] + def main(): # This program tries to find an optimal assignment of nurses to shifts # (3 shifts per day, for 7 days), subject to some constraints (see below). @@ -53,7 +54,8 @@ def main(): for n in all_nurses: for d in all_days: for s in all_shifts: - shifts[(n, d, s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s)) + shifts[(n, d, + s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s)) # [END variables] # Each shift is assigned to exactly one nurse in . @@ -113,7 +115,8 @@ def main(): if shift_requests[n][d][s] == 1: print('Nurse', n, 'works shift', s, '(requested).') else: - print('Nurse', n, 'works shift', s, '(not requested).') + print('Nurse', n, 'works shift', s, + '(not requested).') print() print(f'Number of shift requests met = {solver.ObjectiveValue()}', f'(out of {num_nurses * min_shifts_per_nurse})')