remove most of the factory calls
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
//
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -91,7 +91,7 @@ public class SetCovering4
|
||||
//
|
||||
// objective
|
||||
//
|
||||
OptimizeVar objective = solver.MakeMinimize(z, 1);
|
||||
OptimizeVar objective = z.Minimize(1);
|
||||
|
||||
|
||||
//
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user