From a7fdb95bd0c07762b57d900dfbd1ef61a4cfe3ab Mon Sep 17 00:00:00 2001 From: "lperron@google.com" Date: Fri, 27 Jan 2012 14:52:12 +0000 Subject: [PATCH] remove most of the factory calls --- csharp/coins_grid.cs | 4 ++-- csharp/golomb_ruler.cs | 16 ++++++++-------- csharp/send_more_money2.cs | 21 +++------------------ csharp/send_most_money.cs | 4 ++-- csharp/set_covering.cs | 10 +++++----- csharp/set_covering2.cs | 9 ++++----- csharp/set_covering3.cs | 12 ++++++------ csharp/set_covering4.cs | 2 +- csharp/set_covering_deployment.cs | 14 +++++++------- 9 files changed, 38 insertions(+), 54 deletions(-) diff --git a/csharp/coins_grid.cs b/csharp/coins_grid.cs index 00162a8326..36bc9916ce 100644 --- a/csharp/coins_grid.cs +++ b/csharp/coins_grid.cs @@ -21,7 +21,7 @@ public class CoinsGrid /** * - * Solves the Coins Grid problm. + * Solves the Coins Grid problm. * See http://www.hakank.org/google_or_tools/coins_grid.py * */ @@ -70,7 +70,7 @@ public class CoinsGrid // // Objective // - OptimizeVar obj = solver.MakeMinimize(obj_var, 1); + OptimizeVar obj = obj_var.Minimize(1); // // Search diff --git a/csharp/golomb_ruler.cs b/csharp/golomb_ruler.cs index 621f3dd5fa..f7cbb9a8a9 100644 --- a/csharp/golomb_ruler.cs +++ b/csharp/golomb_ruler.cs @@ -27,7 +27,7 @@ public class GolombRuler * * Golomb Ruler problem. * - * This C# implementation is based on Charles Prud'homme's + * This C# implementation is based on Charles Prud'homme's * or-tools/Java model: * http://code.google.com/p/or-tools/source/browse/trunk/com/google/ortools/constraintsolver/samples/GolombRuler.java * @@ -40,9 +40,9 @@ public class GolombRuler // // Decision variables // - IntVar[] ticks = solver.MakeIntVarArray(m, - 0, - ((m < 31) ? (1 << (m + 1)) - 1 : 9999), + IntVar[] ticks = solver.MakeIntVarArray(m, + 0, + ((m < 31) ? (1 << (m + 1)) - 1 : 9999), "ticks"); IntVar[] diff = new IntVar[(m * m - m) / 2]; @@ -50,14 +50,14 @@ public class GolombRuler // // Constraints - // + // solver.Add(ticks[0] == 0); for(int i = 0; i < ticks.Length - 1; i++) { solver.Add(ticks[i] < ticks[i+1]); } - + for (int k = 0, i = 0; i < m - 1; i++) { for (int j = i + 1; j < m; j++, k++) { diff[k] = (ticks[j]-ticks[i]).Var(); @@ -76,8 +76,8 @@ public class GolombRuler // // Optimization // - OptimizeVar opt = solver.MakeMinimize(ticks[m-1], 1); - + OptimizeVar opt = ticks[m - 1].Minimize(1); + // // Search diff --git a/csharp/send_more_money2.cs b/csharp/send_more_money2.cs index 79e64d2e34..2141d5ce94 100644 --- a/csharp/send_more_money2.cs +++ b/csharp/send_more_money2.cs @@ -54,26 +54,11 @@ public class SendMoreMoney */ // Here we use scalar product instead. - /* - solver.Add( - solver.MakeSum( - solver.MakeScalProd(new IntVar[] {S, E, N, D}, - new int[] {1000,100,10,1}), - solver.MakeScalProd(new IntVar[] {M, O, R, E}, - new int[] {1000,100,10,1})) - == - solver.MakeScalProd(new IntVar[] {M,O,N,E,Y}, - new int[] {10000, 1000, 100, 10, 1} - ) - ); - */ - // Alternative int[] s1 = new int[] {1000,100,10,1}; int[] s2 = new int[] {10000,1000,100,10,1}; - solver.Add(solver.MakeSum(new IntVar[] {S,E,N,D}.ScalProd(s1), - new IntVar[] {M,O,R,E}.ScalProd(s1)) - == new IntVar[] {M,O,N,E,Y}.ScalProd(s2)); - + solver.Add(new IntVar[] {S,E,N,D}.ScalProd(s1) + + new IntVar[] {M,O,R,E}.ScalProd(s1) == + new IntVar[] {M,O,N,E,Y}.ScalProd(s2)); solver.Add(S > 0); solver.Add(M > 0); diff --git a/csharp/send_most_money.cs b/csharp/send_most_money.cs index deb041a4b0..1fdf31edc2 100644 --- a/csharp/send_most_money.cs +++ b/csharp/send_most_money.cs @@ -20,7 +20,7 @@ public class SendMostMoney { /** * - * Solve the SEND+MOST=MONEY problem + * Solve the SEND+MOST=MONEY problem * where the object is to maximize MONEY * See http://www.hakank.org/google_or_tools/send_most_money.py * @@ -76,7 +76,7 @@ public class SendMostMoney Solver.ASSIGN_MIN_VALUE); if (MONEY == 0) { - OptimizeVar obj = solver.MakeMaximize(money, 1); + OptimizeVar obj = money.Maximize(1); solver.NewSearch(db, obj); } else { solver.NewSearch(db); diff --git a/csharp/set_covering.cs b/csharp/set_covering.cs index c909c4dae5..3c2de49f9d 100644 --- a/csharp/set_covering.cs +++ b/csharp/set_covering.cs @@ -38,7 +38,7 @@ public class SetCovering // data // - // Placing of firestations, from Winston 'Operations Research', + // Placing of firestations, from Winston 'Operations Research', // page 486. int min_distance = 15; int num_cities = 6; @@ -49,7 +49,7 @@ public class SetCovering {30,35,15, 0,15,25}, {30,20,30,15, 0,14}, {20,10,20,25,14, 0}}; - + // // Decision variables // @@ -58,11 +58,11 @@ public class SetCovering // // Constraints - // + // // ensure that all cities are covered for(int i = 0; i < num_cities; i++) { - IntVar[] b = (from j in Enumerable.Range(0, num_cities) + IntVar[] b = (from j in Enumerable.Range(0, num_cities) where distance[i,j] <= min_distance select x[j]).ToArray(); solver.Add(b.Sum() >= 1); @@ -72,7 +72,7 @@ public class SetCovering // // objective // - OptimizeVar objective = solver.MakeMinimize(z, 1); + OptimizeVar objective = z.Minimize(1); // diff --git a/csharp/set_covering2.cs b/csharp/set_covering2.cs index 12b605cd15..8899c046ac 100644 --- a/csharp/set_covering2.cs +++ b/csharp/set_covering2.cs @@ -59,7 +59,7 @@ public class SetCovering2 {2,4}, {5,8}, {3,5}}; - + // // Decision variables // @@ -69,18 +69,17 @@ public class SetCovering2 // // Constraints - // + // // ensure that all streets are covered for(int i = 0; i < num_streets; i++) { solver.Add(x[corner[i,0] - 1] + x[corner[i,1] - 1] >= 1); } - + // // objective // - OptimizeVar objective = solver.MakeMinimize(z, 1); - + OptimizeVar objective = z.Minimize(1); // // Search diff --git a/csharp/set_covering3.cs b/csharp/set_covering3.cs index 5d1edf55de..012a496c8d 100644 --- a/csharp/set_covering3.cs +++ b/csharp/set_covering3.cs @@ -38,7 +38,7 @@ public class SetCovering3 // // Set covering problem from - // Katta G. Murty: 'Optimization Models for Decision Making', + // Katta G. Murty: 'Optimization Models for Decision Making', // page 302f // http://ioe.engin.umich.edu/people/fac/books/murty/opti_model/junior-7.pdf int num_groups = 6; @@ -52,7 +52,7 @@ public class SetCovering3 {0, 0, 1, 1, 1, 1, 1, 0, 1, 0}, // 5 democrats {1, 1, 0, 0, 0, 0, 0, 1, 0, 1}}; // 6 republicans - + // // Decision variables // @@ -62,7 +62,7 @@ public class SetCovering3 // // Constraints - // + // // ensure that each group is covered by at least // one senator @@ -74,11 +74,11 @@ public class SetCovering3 solver.Add(b.Sum() >= 1); } - + // // objective // - OptimizeVar objective = solver.MakeMinimize(z, 1); + OptimizeVar objective = z.Minimize(1); // @@ -101,7 +101,7 @@ public class SetCovering3 // More details for(int j = 0; j < num_senators; j++) { if (x[j].Value() == 1) { - Console.Write("Senator " + (1 + j) + + Console.Write("Senator " + (1 + j) + " belongs to these groups: "); for(int i = 0; i < num_groups; i++) { if (belongs[i,j] == 1) { diff --git a/csharp/set_covering4.cs b/csharp/set_covering4.cs index 317c96e4db..1fcc4435f9 100644 --- a/csharp/set_covering4.cs +++ b/csharp/set_covering4.cs @@ -91,7 +91,7 @@ public class SetCovering4 // // objective // - OptimizeVar objective = solver.MakeMinimize(z, 1); + OptimizeVar objective = z.Minimize(1); // diff --git a/csharp/set_covering_deployment.cs b/csharp/set_covering_deployment.cs index 3631733bf0..3135fb402a 100644 --- a/csharp/set_covering_deployment.cs +++ b/csharp/set_covering_deployment.cs @@ -49,7 +49,7 @@ public class SetCoveringDeployment "Tunis"}; int n = countries.Length; - + // the incidence matrix (neighbours) int[,] mat = {{0, 1, 0, 1, 0, 0, 1, 1}, {1, 0, 0, 1, 0, 0, 0, 0}, @@ -59,7 +59,7 @@ public class SetCoveringDeployment {0, 0, 1, 0, 1, 0, 1, 1}, {1, 0, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 0}}; - + // // Decision variables // @@ -77,7 +77,7 @@ public class SetCoveringDeployment // // Constraints // - + // // Constraint 1: There is always an army in a city // (+ maybe a backup) @@ -89,7 +89,7 @@ public class SetCoveringDeployment } // - // Constraint 2: There should always be an backup + // Constraint 2: There should always be an backup // army near every city // for(int i = 0; i < n; i++) { @@ -101,12 +101,12 @@ public class SetCoveringDeployment solver.Add((x[i] + count_neighbours.Sum()) >= 1); } - - + + // // objective // - OptimizeVar objective = solver.MakeMinimize(num_armies, 1); + OptimizeVar objective = num_armies.Minimize(1); //