java: Format all files
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
---
|
||||
Language: Java
|
||||
BasedOnStyle: Google
|
||||
...
|
||||
|
||||
2
.github/workflows/clang_format.yml
vendored
2
.github/workflows/clang_format.yml
vendored
@@ -21,4 +21,4 @@ jobs:
|
||||
run: docker run --rm --init -v $(pwd):/repo linter:latest clang-format --help
|
||||
|
||||
- name: Check current commit
|
||||
run: docker run --rm --init -v $(pwd):/repo -w /repo linter:latest sh -c "git diff --name-only FETCH_HEAD | grep '\.c$\|\.h$\|\.cc$' | xargs clang-format --style=file --dry-run --Werror "
|
||||
run: docker run --rm --init -v $(pwd):/repo -w /repo linter:latest sh -c "git diff --name-only FETCH_HEAD | grep '\.c$\|\.h$\|\.cc$\|\.java$' | xargs clang-format --style=file --dry-run --Werror "
|
||||
|
||||
@@ -26,7 +26,6 @@ public class AllDifferentExcept0 {
|
||||
// alldifferent_except_0, i.e. all values
|
||||
// must be either distinct, or 0.
|
||||
public static void alldifferent_except_0(Solver solver, IntVar[] a) {
|
||||
|
||||
int n = a.length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
@@ -43,7 +42,6 @@ public class AllDifferentExcept0 {
|
||||
* http://www.hakank.org/google_or_tools/circuit.py
|
||||
*/
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("AllDifferentExcept0");
|
||||
|
||||
//
|
||||
|
||||
@@ -40,9 +40,8 @@ public class AllInterval {
|
||||
solver.addConstraint(solver.makeAllDifferent(diffs));
|
||||
|
||||
for (int k = 0; k < n - 1; k++) {
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(
|
||||
diffs[k], solver.makeAbs(solver.makeDifference(x[k + 1], x[k])).var()));
|
||||
solver.addConstraint(solver.makeEquality(
|
||||
diffs[k], solver.makeAbs(solver.makeDifference(x[k + 1], x[k])).var()));
|
||||
}
|
||||
|
||||
// symmetry breaking
|
||||
|
||||
@@ -30,7 +30,6 @@ public class Circuit {
|
||||
* <p>Note: The domain of x must be 0..n-1 (not 1..n) since Java is 0-based.
|
||||
*/
|
||||
public static void circuit(Solver solver, IntVar[] x) {
|
||||
|
||||
int n = x.length;
|
||||
IntVar[] z = solver.makeIntVarArray(n, 0, n - 1, "z");
|
||||
|
||||
@@ -57,7 +56,6 @@ public class Circuit {
|
||||
* http://www.hakank.org/google_or_tools/circuit.py
|
||||
*/
|
||||
private static void solve(int n) {
|
||||
|
||||
Solver solver = new Solver("Circuit");
|
||||
|
||||
//
|
||||
|
||||
@@ -27,8 +27,8 @@ public class CoinsGrid {
|
||||
Solver solver = new Solver("CoinsGrid");
|
||||
|
||||
// data
|
||||
int n = 5; //31;
|
||||
int c = 2; //14;
|
||||
int n = 5; // 31;
|
||||
int c = 2; // 14;
|
||||
|
||||
// variables
|
||||
IntVar[][] x = new IntVar[n][n];
|
||||
|
||||
3
examples/contrib/CoinsGridMIP.java
Executable file → Normal file
3
examples/contrib/CoinsGridMIP.java
Executable file → Normal file
@@ -51,7 +51,8 @@ public class CoinsGridMIP {
|
||||
System.out.println("---- CoinsGridMIP with " + solverType);
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(solverType);
|
||||
if (solver == null) return;
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
/** invariants */
|
||||
int n = 31;
|
||||
|
||||
29
examples/contrib/ColoringMIP.java
Executable file → Normal file
29
examples/contrib/ColoringMIP.java
Executable file → Normal file
@@ -40,7 +40,8 @@ public class ColoringMIP {
|
||||
System.out.println("---- CoinsGridMIP with " + solverType);
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(solverType);
|
||||
if (solver == null) return;
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
double infinity = MPSolver.infinity();
|
||||
|
||||
@@ -48,28 +49,10 @@ public class ColoringMIP {
|
||||
int noCols = 5; // variables number
|
||||
int noNodes = 11; // constraints number
|
||||
|
||||
Edge[] edges = {
|
||||
new Edge(1, 2),
|
||||
new Edge(1, 4),
|
||||
new Edge(1, 7),
|
||||
new Edge(1, 9),
|
||||
new Edge(2, 3),
|
||||
new Edge(2, 6),
|
||||
new Edge(2, 8),
|
||||
new Edge(3, 5),
|
||||
new Edge(3, 7),
|
||||
new Edge(3, 10),
|
||||
new Edge(4, 5),
|
||||
new Edge(4, 6),
|
||||
new Edge(4, 10),
|
||||
new Edge(5, 8),
|
||||
new Edge(5, 9),
|
||||
new Edge(6, 11),
|
||||
new Edge(7, 11),
|
||||
new Edge(8, 11),
|
||||
new Edge(9, 11),
|
||||
new Edge(10, 11)
|
||||
};
|
||||
Edge[] edges = {new Edge(1, 2), new Edge(1, 4), new Edge(1, 7), new Edge(1, 9), new Edge(2, 3),
|
||||
new Edge(2, 6), new Edge(2, 8), new Edge(3, 5), new Edge(3, 7), new Edge(3, 10),
|
||||
new Edge(4, 5), new Edge(4, 6), new Edge(4, 10), new Edge(5, 8), new Edge(5, 9),
|
||||
new Edge(6, 11), new Edge(7, 11), new Edge(8, 11), new Edge(9, 11), new Edge(10, 11)};
|
||||
|
||||
/** variables */
|
||||
MPVariable[][] x = new MPVariable[noNodes][noCols];
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.*;
|
||||
public class CoveringOpl {
|
||||
/** Solves a set covering problem. See http://www.hakank.org/google_or_tools/covering_opl.py */
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("CoveringOpl");
|
||||
|
||||
//
|
||||
@@ -35,28 +34,15 @@ public class CoveringOpl {
|
||||
|
||||
// Which worker is qualified for each task.
|
||||
// Note: This is 1-based and will be made 0-base below.
|
||||
int[][] qualified = {
|
||||
{1, 9, 19, 22, 25, 28, 31},
|
||||
{2, 12, 15, 19, 21, 23, 27, 29, 30, 31, 32},
|
||||
{3, 10, 19, 24, 26, 30, 32},
|
||||
{4, 21, 25, 28, 32},
|
||||
{5, 11, 16, 22, 23, 27, 31},
|
||||
{6, 20, 24, 26, 30, 32},
|
||||
{7, 12, 17, 25, 30, 31},
|
||||
{8, 17, 20, 22, 23},
|
||||
{9, 13, 14, 26, 29, 30, 31},
|
||||
{10, 21, 25, 31, 32},
|
||||
{14, 15, 18, 23, 24, 27, 30, 32},
|
||||
{18, 19, 22, 24, 26, 29, 31},
|
||||
{11, 20, 25, 28, 30, 32},
|
||||
{16, 19, 23, 31},
|
||||
{9, 18, 26, 28, 31, 32}
|
||||
};
|
||||
int[][] qualified = {{1, 9, 19, 22, 25, 28, 31}, {2, 12, 15, 19, 21, 23, 27, 29, 30, 31, 32},
|
||||
{3, 10, 19, 24, 26, 30, 32}, {4, 21, 25, 28, 32}, {5, 11, 16, 22, 23, 27, 31},
|
||||
{6, 20, 24, 26, 30, 32}, {7, 12, 17, 25, 30, 31}, {8, 17, 20, 22, 23},
|
||||
{9, 13, 14, 26, 29, 30, 31}, {10, 21, 25, 31, 32}, {14, 15, 18, 23, 24, 27, 30, 32},
|
||||
{18, 19, 22, 24, 26, 29, 31}, {11, 20, 25, 28, 30, 32}, {16, 19, 23, 31},
|
||||
{9, 18, 26, 28, 31, 32}};
|
||||
|
||||
int[] cost = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3,
|
||||
3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 8, 9
|
||||
};
|
||||
int[] cost = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6,
|
||||
6, 6, 7, 8, 9};
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -23,18 +23,13 @@ import java.util.*;
|
||||
public class Crossword {
|
||||
/** Solving a simple crossword. See http://www.hakank.org/google_or_tools/crossword2.py */
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("Crossword");
|
||||
|
||||
//
|
||||
// data
|
||||
//
|
||||
String[] alpha = {
|
||||
"_", "a", "b", "c", "d", "e", "f",
|
||||
"g", "h", "i", "j", "k", "l", "m",
|
||||
"n", "o", "p", "q", "r", "s", "t",
|
||||
"u", "v", "w", "x", "y", "z"
|
||||
};
|
||||
String[] alpha = {"_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
|
||||
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
|
||||
|
||||
int a = 1;
|
||||
int b = 2;
|
||||
@@ -66,39 +61,35 @@ public class Crossword {
|
||||
int num_words = 15;
|
||||
int word_len = 5;
|
||||
|
||||
int[][] AA = {
|
||||
{h, o, s, e, s}, // HOSES
|
||||
{l, a, s, e, r}, // LASER
|
||||
{s, a, i, l, s}, // SAILS
|
||||
{s, h, e, e, t}, // SHEET
|
||||
{s, t, e, e, r}, // STEER
|
||||
{h, e, e, l, 0}, // HEEL
|
||||
{h, i, k, e, 0}, // HIKE
|
||||
{k, e, e, l, 0}, // KEEL
|
||||
{k, n, o, t, 0}, // KNOT
|
||||
{l, i, n, e, 0}, // LINE
|
||||
{a, f, t, 0, 0}, // AFT
|
||||
{a, l, e, 0, 0}, // ALE
|
||||
{e, e, l, 0, 0}, // EEL
|
||||
{l, e, e, 0, 0}, // LEE
|
||||
{t, i, e, 0, 0}
|
||||
}; // TIE
|
||||
int[][] AA = {{h, o, s, e, s}, // HOSES
|
||||
{l, a, s, e, r}, // LASER
|
||||
{s, a, i, l, s}, // SAILS
|
||||
{s, h, e, e, t}, // SHEET
|
||||
{s, t, e, e, r}, // STEER
|
||||
{h, e, e, l, 0}, // HEEL
|
||||
{h, i, k, e, 0}, // HIKE
|
||||
{k, e, e, l, 0}, // KEEL
|
||||
{k, n, o, t, 0}, // KNOT
|
||||
{l, i, n, e, 0}, // LINE
|
||||
{a, f, t, 0, 0}, // AFT
|
||||
{a, l, e, 0, 0}, // ALE
|
||||
{e, e, l, 0, 0}, // EEL
|
||||
{l, e, e, 0, 0}, // LEE
|
||||
{t, i, e, 0, 0}}; // TIE
|
||||
|
||||
int num_overlapping = 12;
|
||||
int[][] overlapping = {
|
||||
{0, 2, 1, 0}, // s
|
||||
{0, 4, 2, 0}, // s
|
||||
{3, 1, 1, 2}, // i
|
||||
{3, 2, 4, 0}, // k
|
||||
{3, 3, 2, 2}, // e
|
||||
{6, 0, 1, 3}, // l
|
||||
{6, 1, 4, 1}, // e
|
||||
{6, 2, 2, 3}, // e
|
||||
{7, 0, 5, 1}, // l
|
||||
{7, 2, 1, 4}, // s
|
||||
{7, 3, 4, 2}, // e
|
||||
{7, 4, 2, 4}
|
||||
}; // r
|
||||
int[][] overlapping = {{0, 2, 1, 0}, // s
|
||||
{0, 4, 2, 0}, // s
|
||||
{3, 1, 1, 2}, // i
|
||||
{3, 2, 4, 0}, // k
|
||||
{3, 3, 2, 2}, // e
|
||||
{6, 0, 1, 3}, // l
|
||||
{6, 1, 4, 1}, // e
|
||||
{6, 2, 2, 3}, // e
|
||||
{7, 0, 5, 1}, // l
|
||||
{7, 2, 1, 4}, // s
|
||||
{7, 3, 4, 2}, // e
|
||||
{7, 4, 2, 4}}; // r
|
||||
|
||||
int N = 8;
|
||||
|
||||
@@ -135,26 +126,21 @@ public class Crossword {
|
||||
}
|
||||
|
||||
for (int I = 0; I < num_overlapping; I++) {
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(
|
||||
solver
|
||||
.makeElement(
|
||||
A_flat,
|
||||
solver
|
||||
.makeSum(
|
||||
solver.makeProd(E[overlapping[I][0]], word_len).var(),
|
||||
overlapping[I][1])
|
||||
.var())
|
||||
.var(),
|
||||
solver
|
||||
.makeElement(
|
||||
A_flat,
|
||||
solver
|
||||
.makeSum(
|
||||
solver.makeProd(E[overlapping[I][2]], word_len).var(),
|
||||
overlapping[I][3])
|
||||
.var())
|
||||
.var()));
|
||||
solver.addConstraint(solver.makeEquality(
|
||||
solver
|
||||
.makeElement(A_flat,
|
||||
solver
|
||||
.makeSum(
|
||||
solver.makeProd(E[overlapping[I][0]], word_len).var(), overlapping[I][1])
|
||||
.var())
|
||||
.var(),
|
||||
solver
|
||||
.makeElement(A_flat,
|
||||
solver
|
||||
.makeSum(
|
||||
solver.makeProd(E[overlapping[I][2]], word_len).var(), overlapping[I][3])
|
||||
.var())
|
||||
.var()));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -41,7 +41,6 @@ public class DeBruijn {
|
||||
* http://www.hakank.org/google_or_tools/debruijn_binary.py
|
||||
*/
|
||||
private static void solve(int base, int n, int m) {
|
||||
|
||||
Solver solver = new Solver("DeBruijn");
|
||||
|
||||
System.out.println("base: " + base + " n: " + n + " m: " + m);
|
||||
@@ -164,7 +163,6 @@ public class DeBruijn {
|
||||
}
|
||||
System.out.println();
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
solver.endSearch();
|
||||
|
||||
80
examples/contrib/DietMIP.java
Executable file → Normal file
80
examples/contrib/DietMIP.java
Executable file → Normal file
@@ -26,57 +26,57 @@ import com.google.ortools.linearsolver.MPSolver;
|
||||
import com.google.ortools.linearsolver.MPVariable;
|
||||
|
||||
public class DietMIP {
|
||||
private static void solve(String solverType) {
|
||||
MPSolver solver = MPSolver.createSolver(solverType);
|
||||
double infinity = MPSolver.infinity();
|
||||
private static void solve(String solverType) {
|
||||
MPSolver solver = MPSolver.createSolver(solverType);
|
||||
double infinity = MPSolver.infinity();
|
||||
|
||||
int n = 4; // variables number
|
||||
int m = 4; // constraints number
|
||||
int n = 4; // variables number
|
||||
int m = 4; // constraints number
|
||||
|
||||
int[] price = { 50, 20, 30, 80 };
|
||||
int[] price = {50, 20, 30, 80};
|
||||
|
||||
int[] limits = { 500, 6, 10, 8 };
|
||||
int[] limits = {500, 6, 10, 8};
|
||||
|
||||
int[] calories = { 400, 200, 150, 500 };
|
||||
int[] chocolate = { 3, 2, 0, 0 };
|
||||
int[] sugar = { 2, 2, 4, 4 };
|
||||
int[] fat = { 2, 4, 1, 5 };
|
||||
int[] calories = {400, 200, 150, 500};
|
||||
int[] chocolate = {3, 2, 0, 0};
|
||||
int[] sugar = {2, 2, 4, 4};
|
||||
int[] fat = {2, 4, 1, 5};
|
||||
|
||||
int[][] values = { calories, chocolate, sugar, fat };
|
||||
int[][] values = {calories, chocolate, sugar, fat};
|
||||
|
||||
MPVariable[] x = solver.makeIntVarArray(n, 0, 100, "x");
|
||||
MPObjective objective = solver.objective();
|
||||
MPConstraint[] targets = new MPConstraint[4];
|
||||
MPVariable[] x = solver.makeIntVarArray(n, 0, 100, "x");
|
||||
MPObjective objective = solver.objective();
|
||||
MPConstraint[] targets = new MPConstraint[4];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
objective.setCoefficient(x[i], price[i]);
|
||||
for (int i = 0; i < n; i++) {
|
||||
objective.setCoefficient(x[i], price[i]);
|
||||
|
||||
// constraints
|
||||
targets[i] = solver.makeConstraint(limits[i], infinity);
|
||||
for (int j = 0; j < m; j++) {
|
||||
targets[i].setCoefficient(x[j], values[i][j]);
|
||||
}
|
||||
}
|
||||
// constraints
|
||||
targets[i] = solver.makeConstraint(limits[i], infinity);
|
||||
for (int j = 0; j < m; j++) {
|
||||
targets[i].setCoefficient(x[j], values[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
final MPSolver.ResultStatus resultStatus = solver.solve();
|
||||
final MPSolver.ResultStatus resultStatus = solver.solve();
|
||||
|
||||
/** printing */
|
||||
if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
|
||||
System.err.println("The problem does not have an optimal solution!");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Optimal objective value = " + solver.objective().value());
|
||||
/** printing */
|
||||
if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
|
||||
System.err.println("The problem does not have an optimal solution!");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Optimal objective value = " + solver.objective().value());
|
||||
|
||||
System.out.print("Item quantities: ");
|
||||
System.out.print((int) x[0].solutionValue() + " ");
|
||||
System.out.print((int) x[1].solutionValue() + " ");
|
||||
System.out.print((int) x[2].solutionValue() + " ");
|
||||
System.out.print((int) x[3].solutionValue() + " ");
|
||||
}
|
||||
}
|
||||
System.out.print("Item quantities: ");
|
||||
System.out.print((int) x[0].solutionValue() + " ");
|
||||
System.out.print((int) x[1].solutionValue() + " ");
|
||||
System.out.print((int) x[2].solutionValue() + " ");
|
||||
System.out.print((int) x[3].solutionValue() + " ");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
solve("CBC");
|
||||
}
|
||||
solve("CBC");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ public class DivisibleBy9Through1 {
|
||||
* The ECLiPSe Prolog source code: http://www.hakank.org/eclipse/modulo_propagator.ecl
|
||||
*/
|
||||
public static void my_mod(Solver solver, IntVar x, IntVar y, IntVar r) {
|
||||
|
||||
long lbx = x.min();
|
||||
long ubx = x.max();
|
||||
long ubx_neg = -ubx;
|
||||
@@ -83,7 +82,6 @@ public class DivisibleBy9Through1 {
|
||||
* http://www.hakank.org/google_or_tools/divisible_by_9_through_1.py
|
||||
*/
|
||||
private static void solve(int base) {
|
||||
|
||||
Solver solver = new Solver("DivisibleBy9Through1");
|
||||
|
||||
//
|
||||
|
||||
@@ -8,7 +8,6 @@ import com.google.ortools.linearsolver.MPVariable;
|
||||
|
||||
public class Issue173 {
|
||||
public static void breakit() {
|
||||
|
||||
for (int i = 0; i < 50000; i++) {
|
||||
solveLP();
|
||||
}
|
||||
|
||||
13
examples/contrib/KnapsackMIP.java
Executable file → Normal file
13
examples/contrib/KnapsackMIP.java
Executable file → Normal file
@@ -44,15 +44,10 @@ public class KnapsackMIP {
|
||||
|
||||
int[] capacity = {18209, 7692, 1333, 924, 26638, 61188, 13360};
|
||||
int[] value = {96, 76, 56, 11, 86, 10, 66, 86, 83, 12, 9, 81};
|
||||
int[][] weights = {
|
||||
{19, 1, 10, 1, 1, 14, 152, 11, 1, 1, 1, 1},
|
||||
{0, 4, 53, 0, 0, 80, 0, 4, 5, 0, 0, 0},
|
||||
{4, 660, 3, 0, 30, 0, 3, 0, 4, 90, 0, 0},
|
||||
{7, 0, 18, 6, 770, 330, 7, 0, 0, 6, 0, 0},
|
||||
{0, 20, 0, 4, 52, 3, 0, 0, 0, 5, 4, 0},
|
||||
{0, 0, 40, 70, 4, 63, 0, 0, 60, 0, 4, 0},
|
||||
{0, 32, 0, 0, 0, 5, 0, 3, 0, 660, 0, 9}
|
||||
};
|
||||
int[][] weights = {{19, 1, 10, 1, 1, 14, 152, 11, 1, 1, 1, 1},
|
||||
{0, 4, 53, 0, 0, 80, 0, 4, 5, 0, 0, 0}, {4, 660, 3, 0, 30, 0, 3, 0, 4, 90, 0, 0},
|
||||
{7, 0, 18, 6, 770, 330, 7, 0, 0, 6, 0, 0}, {0, 20, 0, 4, 52, 3, 0, 0, 0, 5, 4, 0},
|
||||
{0, 0, 40, 70, 4, 63, 0, 0, 60, 0, 4, 0}, {0, 32, 0, 0, 0, 5, 0, 3, 0, 660, 0, 9}};
|
||||
|
||||
int maxCapacity = -1;
|
||||
for (int c : capacity) {
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.*;
|
||||
public class MagicSquare {
|
||||
/** Solves the Magic Square problem. See http://www.hakank.org/google_or_tools/magic_square.py */
|
||||
private static void solve(int n, int num) {
|
||||
|
||||
Solver solver = new Solver("MagicSquare");
|
||||
|
||||
System.out.println("n: " + n);
|
||||
|
||||
@@ -26,7 +26,6 @@ public class Map2 {
|
||||
* Solves a simple map coloring problem, take II. See http://www.hakank.org/google_or_tools/map.py
|
||||
*/
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("Map2");
|
||||
|
||||
//
|
||||
@@ -42,17 +41,9 @@ public class Map2 {
|
||||
int n = 6;
|
||||
int max_num_colors = 4;
|
||||
|
||||
int[][] neighbours = {
|
||||
{France, Belgium},
|
||||
{France, Luxembourg},
|
||||
{France, Germany},
|
||||
{Luxembourg, Germany},
|
||||
{Luxembourg, Belgium},
|
||||
{Belgium, Netherlands},
|
||||
{Belgium, Germany},
|
||||
{Germany, Netherlands},
|
||||
{Germany, Denmark}
|
||||
};
|
||||
int[][] neighbours = {{France, Belgium}, {France, Luxembourg}, {France, Germany},
|
||||
{Luxembourg, Germany}, {Luxembourg, Belgium}, {Belgium, Netherlands}, {Belgium, Germany},
|
||||
{Germany, Netherlands}, {Germany, Denmark}};
|
||||
|
||||
//
|
||||
// Variables
|
||||
|
||||
@@ -29,16 +29,9 @@ public class Minesweeper {
|
||||
//
|
||||
static int default_r = 8;
|
||||
static int default_c = 8;
|
||||
static int[][] default_game = {
|
||||
{2, 3, X, 2, 2, X, 2, 1},
|
||||
{X, X, 4, X, X, 4, X, 2},
|
||||
{X, X, X, X, X, X, 4, X},
|
||||
{X, 5, X, 6, X, X, X, 2},
|
||||
{2, X, X, X, 5, 5, X, 2},
|
||||
{1, 3, 4, X, X, X, 4, X},
|
||||
{0, 1, X, 4, X, X, X, 3},
|
||||
{0, 1, 2, X, 2, 3, X, 2}
|
||||
};
|
||||
static int[][] default_game = {{2, 3, X, 2, 2, X, 2, 1}, {X, X, 4, X, X, 4, X, 2},
|
||||
{X, X, X, X, X, X, 4, X}, {X, 5, X, 6, X, X, X, 2}, {2, X, X, X, 5, 5, X, 2},
|
||||
{1, 3, 4, X, X, X, 4, X}, {0, 1, X, 4, X, X, X, 3}, {0, 1, 2, X, 2, 3, X, 2}};
|
||||
|
||||
// for the actual problem
|
||||
static int r;
|
||||
@@ -47,7 +40,6 @@ public class Minesweeper {
|
||||
|
||||
/** Solves the Minesweeper problems. See http://www.hakank.org/google_or_tools/minesweeper.py */
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("Minesweeper");
|
||||
|
||||
int[] S = {-1, 0, 1};
|
||||
@@ -148,16 +140,13 @@ public class Minesweeper {
|
||||
* ..2.3. 2..... ..24.3 1.34.. .....3 .3.3..
|
||||
*/
|
||||
private static void readFile(String file) {
|
||||
|
||||
System.out.println("readFile(" + file + ")");
|
||||
int lineCount = 0;
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader inr = new BufferedReader(new FileReader(file));
|
||||
String str;
|
||||
while ((str = inr.readLine()) != null && str.length() > 0) {
|
||||
|
||||
str = str.trim();
|
||||
|
||||
// ignore comments
|
||||
|
||||
@@ -27,9 +27,7 @@ public class MultiThreadTest {
|
||||
|
||||
public static void launchProtocol(int wholeLoopAttmpts, int threadPoolSize, boolean runInParallel)
|
||||
throws Exception {
|
||||
|
||||
for (int noAttmpt = 0; noAttmpt < wholeLoopAttmpts; noAttmpt++) {
|
||||
|
||||
System.out.println(String.format("Attempt %d", noAttmpt));
|
||||
|
||||
int maxThreads = threadPoolSize;
|
||||
@@ -50,7 +48,6 @@ public class MultiThreadTest {
|
||||
System.out.println(thread.getStatusSolver().toString());
|
||||
}
|
||||
} else {
|
||||
|
||||
for (SolverThread thread : threadList) {
|
||||
System.out.println("Launching single thread");
|
||||
executor.invokeAll(Arrays.asList(thread));
|
||||
@@ -66,10 +63,8 @@ public class MultiThreadTest {
|
||||
}
|
||||
|
||||
private static MPSolver makeProblem() {
|
||||
|
||||
MPSolver solver =
|
||||
new MPSolver(
|
||||
UUID.randomUUID().toString(), OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
|
||||
MPSolver solver = new MPSolver(
|
||||
UUID.randomUUID().toString(), OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
|
||||
|
||||
double infinity = MPSolver.infinity();
|
||||
|
||||
@@ -95,7 +90,6 @@ public class MultiThreadTest {
|
||||
}
|
||||
|
||||
private static final class SolverThread implements Callable<MPSolver.ResultStatus> {
|
||||
|
||||
private MPSolver.ResultStatus statusSolver;
|
||||
|
||||
public SolverThread() {}
|
||||
|
||||
@@ -39,12 +39,7 @@ public class QuasigroupCompletion {
|
||||
*/
|
||||
static int default_n = 5;
|
||||
static int[][] default_problem = {
|
||||
{1, X, X, X, 4},
|
||||
{X, 5, X, X, X},
|
||||
{4, X, X, 2, X},
|
||||
{X, 4, X, X, X},
|
||||
{X, X, 5, X, 1}
|
||||
};
|
||||
{1, X, X, X, 4}, {X, 5, X, X, X}, {4, X, X, 2, X}, {X, 4, X, X, X}, {X, X, 5, X, 1}};
|
||||
|
||||
// for the actual problem
|
||||
static int n;
|
||||
@@ -55,7 +50,6 @@ public class QuasigroupCompletion {
|
||||
* http://www.hakank.org/google_or_tools/quasigroup_completion.py
|
||||
*/
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("QuasigroupCompletion");
|
||||
|
||||
//
|
||||
@@ -152,16 +146,13 @@ public class QuasigroupCompletion {
|
||||
* <p>Example 5 1 . . . 4 . 5 . . . 4 . . 2 . . 4 . . . . . 5 . 1
|
||||
*/
|
||||
private static void readFile(String file) {
|
||||
|
||||
System.out.println("readFile(" + file + ")");
|
||||
int lineCount = 0;
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader inr = new BufferedReader(new FileReader(file));
|
||||
String str;
|
||||
while ((str = inr.readLine()) != null && str.length() > 0) {
|
||||
|
||||
str = str.trim();
|
||||
|
||||
// ignore comments
|
||||
|
||||
@@ -39,26 +39,18 @@ public class SendMoreMoney {
|
||||
|
||||
IntVar[] eq = {s, e, n, d, m, o, r, e, m, o, n, e, y};
|
||||
int[] coeffs = {
|
||||
1000,
|
||||
100,
|
||||
10,
|
||||
1, // S E N D +
|
||||
1000,
|
||||
100,
|
||||
10,
|
||||
1, // M O R E
|
||||
-10000,
|
||||
-1000,
|
||||
-100,
|
||||
-10,
|
||||
-1 // == M O N E Y
|
||||
1000, 100, 10,
|
||||
1, // S E N D +
|
||||
1000, 100, 10,
|
||||
1, // M O R E
|
||||
-10000, -1000, -100, -10,
|
||||
-1 // == M O N E Y
|
||||
};
|
||||
solver.addConstraint(solver.makeScalProdEquality(eq, coeffs, 0));
|
||||
|
||||
// alternative:
|
||||
solver.addConstraint(
|
||||
solver.makeScalProdEquality(
|
||||
new IntVar[] {s, e, n, d, m, o, r, e, m, o, n, e, y}, coeffs, 0));
|
||||
solver.addConstraint(solver.makeScalProdEquality(
|
||||
new IntVar[] {s, e, n, d, m, o, r, e, m, o, n, e, y}, coeffs, 0));
|
||||
|
||||
// s > 0
|
||||
solver.addConstraint(solver.makeGreater(s, 0));
|
||||
|
||||
@@ -43,7 +43,6 @@ public class SendMoreMoney2 {
|
||||
|
||||
/** Solves the SEND+MORE=MONEY problem with different approaches. */
|
||||
private static void solve(int alt) {
|
||||
|
||||
sol = new Solver("SendMoreMoney");
|
||||
|
||||
int base = 10;
|
||||
@@ -81,71 +80,52 @@ public class SendMoreMoney2 {
|
||||
//
|
||||
// First, a version approach which is just too noisy.
|
||||
//
|
||||
sol.addConstraint(
|
||||
sol.makeEquality(
|
||||
sol.makeSum(
|
||||
sol.makeSum(
|
||||
sol.makeProd(s, 1000),
|
||||
sol.makeSum(
|
||||
sol.makeProd(e, 100),
|
||||
sol.addConstraint(sol.makeEquality(
|
||||
sol.makeSum(sol.makeSum(sol.makeProd(s, 1000),
|
||||
sol.makeSum(sol.makeProd(e, 100),
|
||||
sol.makeSum(sol.makeProd(n, 10), sol.makeProd(d, 1)))),
|
||||
sol.makeSum(
|
||||
sol.makeProd(m, 1000),
|
||||
sol.makeSum(
|
||||
sol.makeProd(o, 100),
|
||||
sol.makeSum(sol.makeProd(r, 10), sol.makeProd(e, 1)))))
|
||||
.var(),
|
||||
sol.makeSum(
|
||||
sol.makeProd(m, 10000),
|
||||
sol.makeSum(
|
||||
sol.makeProd(o, 1000),
|
||||
sol.makeSum(
|
||||
sol.makeProd(n, 100),
|
||||
sol.makeSum(sol.makeProd(e, 10), sol.makeProd(y, 1)))))
|
||||
.var()));
|
||||
sol.makeSum(sol.makeProd(m, 1000),
|
||||
sol.makeSum(sol.makeProd(o, 100),
|
||||
sol.makeSum(sol.makeProd(r, 10), sol.makeProd(e, 1)))))
|
||||
.var(),
|
||||
sol.makeSum(sol.makeProd(m, 10000),
|
||||
sol.makeSum(sol.makeProd(o, 1000),
|
||||
sol.makeSum(sol.makeProd(n, 100),
|
||||
sol.makeSum(sol.makeProd(e, 10), sol.makeProd(y, 1)))))
|
||||
.var()));
|
||||
|
||||
} else if (alt == 1) {
|
||||
|
||||
//
|
||||
// Alternative 1, using the helper methods
|
||||
//
|
||||
// p(IntExpr, int, IntExpr) and
|
||||
// p(IntVar, int)
|
||||
//
|
||||
sol.addConstraint(
|
||||
sol.makeEquality(
|
||||
sol.makeSum(
|
||||
p(s, 1000, p(e, 100, p(n, 10, p(d, 1)))),
|
||||
p(m, 1000, p(o, 100, p(r, 10, p(e, 1)))))
|
||||
.var(),
|
||||
p(m, 10000, p(o, 1000, p(n, 100, p(e, 10, p(y, 1))))).var()));
|
||||
sol.addConstraint(sol.makeEquality(sol.makeSum(p(s, 1000, p(e, 100, p(n, 10, p(d, 1)))),
|
||||
p(m, 1000, p(o, 100, p(r, 10, p(e, 1)))))
|
||||
.var(),
|
||||
p(m, 10000, p(o, 1000, p(n, 100, p(e, 10, p(y, 1))))).var()));
|
||||
|
||||
} else if (alt == 2) {
|
||||
|
||||
//
|
||||
// Alternative 2
|
||||
//
|
||||
sol.addConstraint(
|
||||
sol.makeEquality(
|
||||
sol.makeSum(
|
||||
sol.makeScalProd(new IntVar[] {s, e, n, d}, new int[] {1000, 100, 10, 1}),
|
||||
sol.makeScalProd(new IntVar[] {m, o, r, e}, new int[] {1000, 100, 10, 1}))
|
||||
.var(),
|
||||
sol.makeScalProd(new IntVar[] {m, o, n, e, y}, new int[] {10000, 1000, 100, 10, 1})
|
||||
.var()));
|
||||
sol.addConstraint(sol.makeEquality(
|
||||
sol.makeSum(sol.makeScalProd(new IntVar[] {s, e, n, d}, new int[] {1000, 100, 10, 1}),
|
||||
sol.makeScalProd(new IntVar[] {m, o, r, e}, new int[] {1000, 100, 10, 1}))
|
||||
.var(),
|
||||
sol.makeScalProd(new IntVar[] {m, o, n, e, y}, new int[] {10000, 1000, 100, 10, 1})
|
||||
.var()));
|
||||
|
||||
} else if (alt == 3) {
|
||||
|
||||
//
|
||||
// alternative 3: same approach as 2, with some helper methods
|
||||
//
|
||||
sol.addConstraint(
|
||||
sol.makeEquality(
|
||||
sol.makeSum(sp(new IntVar[] {s, e, n, d}), sp(new IntVar[] {m, o, r, e})).var(),
|
||||
sp(new IntVar[] {m, o, n, e, y}).var()));
|
||||
sol.addConstraint(sol.makeEquality(
|
||||
sol.makeSum(sp(new IntVar[] {s, e, n, d}), sp(new IntVar[] {m, o, r, e})).var(),
|
||||
sp(new IntVar[] {m, o, n, e, y}).var()));
|
||||
|
||||
} else if (alt == 4) {
|
||||
|
||||
//
|
||||
// Alternative 4, using explicit variables
|
||||
//
|
||||
|
||||
@@ -50,25 +50,17 @@ public class SendMostMoney {
|
||||
|
||||
IntVar[] eq = {s, e, n, d, m, o, s, t, m, o, n, e, y};
|
||||
int[] coeffs = {
|
||||
1000,
|
||||
100,
|
||||
10,
|
||||
1, // S E N D +
|
||||
1000,
|
||||
100,
|
||||
10,
|
||||
1, // M O S T
|
||||
-10000,
|
||||
-1000,
|
||||
-100,
|
||||
-10,
|
||||
-1 // == M O N E Y
|
||||
1000, 100, 10,
|
||||
1, // S E N D +
|
||||
1000, 100, 10,
|
||||
1, // M O S T
|
||||
-10000, -1000, -100, -10,
|
||||
-1 // == M O N E Y
|
||||
};
|
||||
solver.addConstraint(solver.makeScalProdEquality(eq, coeffs, 0));
|
||||
|
||||
IntVar money =
|
||||
solver
|
||||
.makeScalProd(new IntVar[] {m, o, n, e, y}, new int[] {10000, 1000, 100, 10, 1})
|
||||
solver.makeScalProd(new IntVar[] {m, o, n, e, y}, new int[] {10000, 1000, 100, 10, 1})
|
||||
.var();
|
||||
|
||||
//
|
||||
|
||||
@@ -34,14 +34,8 @@ public class SetCovering {
|
||||
int min_distance = 15;
|
||||
int num_cities = 6;
|
||||
|
||||
int[][] distance = {
|
||||
{0, 10, 20, 30, 30, 20},
|
||||
{10, 0, 25, 35, 20, 10},
|
||||
{20, 25, 0, 15, 30, 20},
|
||||
{30, 35, 15, 0, 15, 25},
|
||||
{30, 20, 30, 15, 0, 14},
|
||||
{20, 10, 20, 25, 14, 0}
|
||||
};
|
||||
int[][] distance = {{0, 10, 20, 30, 30, 20}, {10, 0, 25, 35, 20, 10}, {20, 25, 0, 15, 30, 20},
|
||||
{30, 35, 15, 0, 15, 25}, {30, 20, 30, 15, 0, 14}, {20, 10, 20, 25, 14, 0}};
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -42,18 +42,7 @@ public class SetCovering2 {
|
||||
// corners of each street
|
||||
// Note: 1-based (handled below)
|
||||
int[][] corner = {
|
||||
{1, 2},
|
||||
{2, 3},
|
||||
{4, 5},
|
||||
{7, 8},
|
||||
{6, 7},
|
||||
{2, 6},
|
||||
{1, 6},
|
||||
{4, 7},
|
||||
{2, 4},
|
||||
{5, 8},
|
||||
{3, 5}
|
||||
};
|
||||
{1, 2}, {2, 3}, {4, 5}, {7, 8}, {6, 7}, {2, 6}, {1, 6}, {4, 7}, {2, 4}, {5, 8}, {3, 5}};
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -38,14 +38,12 @@ public class SetCovering3 {
|
||||
int num_senators = 10;
|
||||
|
||||
// which group does a senator belong to?
|
||||
int[][] belongs = {
|
||||
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 1 southern
|
||||
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 2 northern
|
||||
{0, 1, 1, 0, 0, 0, 0, 1, 1, 1}, // 3 liberals
|
||||
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0}, // 4 conservative
|
||||
{0, 0, 1, 1, 1, 1, 1, 0, 1, 0}, // 5 democrats
|
||||
{1, 1, 0, 0, 0, 0, 0, 1, 0, 1}
|
||||
}; // 6 republicans
|
||||
int[][] belongs = {{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 1 southern
|
||||
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 2 northern
|
||||
{0, 1, 1, 0, 0, 0, 0, 1, 1, 1}, // 3 liberals
|
||||
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0}, // 4 conservative
|
||||
{0, 0, 1, 1, 1, 1, 1, 0, 1, 0}, // 5 democrats
|
||||
{1, 1, 0, 0, 0, 0, 0, 1, 0, 1}}; // 6 republicans
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -42,19 +42,17 @@ public class SetCovering4 {
|
||||
int[] costs = {19, 16, 18, 13, 15, 19, 15, 17, 16, 15};
|
||||
|
||||
// the alternatives, and their objects
|
||||
int[][] a = {
|
||||
// 1 2 3 4 5 6 7 8 the objects
|
||||
{1, 0, 0, 0, 0, 1, 0, 0}, // alternative 1
|
||||
{0, 1, 0, 0, 0, 1, 0, 1}, // alternative 2
|
||||
{1, 0, 0, 1, 0, 0, 1, 0}, // alternative 3
|
||||
{0, 1, 1, 0, 1, 0, 0, 0}, // alternative 4
|
||||
{0, 1, 0, 0, 1, 0, 0, 0}, // alternative 5
|
||||
{0, 1, 1, 0, 0, 0, 0, 0}, // alternative 6
|
||||
{0, 1, 1, 1, 0, 0, 0, 0}, // alternative 7
|
||||
{0, 0, 0, 1, 1, 0, 0, 1}, // alternative 8
|
||||
{0, 0, 1, 0, 0, 1, 0, 1}, // alternative 9
|
||||
{1, 0, 0, 0, 0, 1, 1, 0}
|
||||
}; // alternative 10
|
||||
int[][] a = {// 1 2 3 4 5 6 7 8 the objects
|
||||
{1, 0, 0, 0, 0, 1, 0, 0}, // alternative 1
|
||||
{0, 1, 0, 0, 0, 1, 0, 1}, // alternative 2
|
||||
{1, 0, 0, 1, 0, 0, 1, 0}, // alternative 3
|
||||
{0, 1, 1, 0, 1, 0, 0, 0}, // alternative 4
|
||||
{0, 1, 0, 0, 1, 0, 0, 0}, // alternative 5
|
||||
{0, 1, 1, 0, 0, 0, 0, 0}, // alternative 6
|
||||
{0, 1, 1, 1, 0, 0, 0, 0}, // alternative 7
|
||||
{0, 0, 0, 1, 1, 0, 0, 1}, // alternative 8
|
||||
{0, 0, 1, 0, 0, 1, 0, 1}, // alternative 9
|
||||
{1, 0, 0, 0, 0, 1, 1, 0}}; // alternative 10
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -35,22 +35,14 @@ public class SetCoveringDeployment {
|
||||
|
||||
// From http://mathworld.wolfram.com/SetCoveringDeployment.html
|
||||
String[] countries = {
|
||||
"Alexandria", "Asia Minor", "Britain", "Byzantium", "Gaul", "Iberia", "Rome", "Tunis"
|
||||
};
|
||||
"Alexandria", "Asia Minor", "Britain", "Byzantium", "Gaul", "Iberia", "Rome", "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},
|
||||
{0, 0, 0, 0, 1, 1, 0, 0},
|
||||
{1, 1, 0, 0, 0, 0, 1, 0},
|
||||
{0, 0, 1, 0, 0, 1, 1, 0},
|
||||
{0, 0, 1, 0, 1, 0, 1, 1},
|
||||
{1, 0, 0, 1, 1, 1, 0, 1},
|
||||
{1, 0, 0, 0, 0, 1, 1, 0}
|
||||
};
|
||||
int[][] mat = {{0, 1, 0, 1, 0, 0, 1, 1}, {1, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0},
|
||||
{1, 1, 0, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 1, 0, 1, 0, 1, 1},
|
||||
{1, 0, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 0}};
|
||||
|
||||
//
|
||||
// variables
|
||||
@@ -90,10 +82,8 @@ public class SetCoveringDeployment {
|
||||
count_neighbours.add(y[j]);
|
||||
}
|
||||
}
|
||||
solver.addConstraint(
|
||||
solver.makeGreaterOrEqual(
|
||||
solver.makeSum(x[i], solver.makeSum(count_neighbours.toArray(new IntVar[1])).var()),
|
||||
1));
|
||||
solver.addConstraint(solver.makeGreaterOrEqual(
|
||||
solver.makeSum(x[i], solver.makeSum(count_neighbours.toArray(new IntVar[1])).var()), 1));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -79,9 +79,8 @@ public class SimpleRoutingTest {
|
||||
Assignment solution = routing.solve();
|
||||
if (solution != null) {
|
||||
int route_number = 0;
|
||||
for (long node = routing.start(route_number);
|
||||
!routing.isEnd(node);
|
||||
node = solution.value(routing.nextVar(node))) {
|
||||
for (long node = routing.start(route_number); !routing.isEnd(node);
|
||||
node = solution.value(routing.nextVar(node))) {
|
||||
globalRes.add((int) node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,13 +65,11 @@ public class StableMarriage {
|
||||
// rankWomen[o,husband[o]] < rankWomen[o,m]);
|
||||
for (int m = 0; m < n; m++) {
|
||||
for (int o = 0; o < n; o++) {
|
||||
IntVar b1 =
|
||||
solver.makeIsGreaterCstVar(
|
||||
solver.makeElement(rankMen[m], wife[m]).var(), rankMen[m][o]);
|
||||
IntVar b1 = solver.makeIsGreaterCstVar(
|
||||
solver.makeElement(rankMen[m], wife[m]).var(), rankMen[m][o]);
|
||||
|
||||
IntVar b2 =
|
||||
solver.makeIsLessCstVar(
|
||||
solver.makeElement(rankWomen[o], husband[o]).var(), rankWomen[o][m]);
|
||||
IntVar b2 = solver.makeIsLessCstVar(
|
||||
solver.makeElement(rankWomen[o], husband[o]).var(), rankWomen[o][m]);
|
||||
solver.addConstraint(solver.makeLessOrEqual(solver.makeDifference(b1, b2), 0));
|
||||
}
|
||||
}
|
||||
@@ -81,9 +79,8 @@ public class StableMarriage {
|
||||
// rankMen[o,wife[o]] < rankMen[o,w]);
|
||||
for (int w = 0; w < n; w++) {
|
||||
for (int o = 0; o < n; o++) {
|
||||
IntVar b1 =
|
||||
solver.makeIsGreaterCstVar(
|
||||
solver.makeElement(rankWomen[w], husband[w]).var(), rankWomen[w][o]);
|
||||
IntVar b1 = solver.makeIsGreaterCstVar(
|
||||
solver.makeElement(rankWomen[w], husband[w]).var(), rankWomen[w][o]);
|
||||
IntVar b2 =
|
||||
solver.makeIsLessCstVar(solver.makeElement(rankMen[o], wife[o]).var(), rankMen[o][w]);
|
||||
solver.addConstraint(solver.makeLessOrEqual(solver.makeDifference(b1, b2), 0));
|
||||
@@ -128,106 +125,48 @@ public class StableMarriage {
|
||||
//
|
||||
// From Pascal Van Hentenryck's OPL book
|
||||
//
|
||||
long[][][] van_hentenryck = {
|
||||
// rankWomen
|
||||
{
|
||||
{1, 2, 4, 3, 5},
|
||||
{3, 5, 1, 2, 4},
|
||||
{5, 4, 2, 1, 3},
|
||||
{1, 3, 5, 4, 2},
|
||||
{4, 2, 3, 5, 1}
|
||||
},
|
||||
long[][][] van_hentenryck = {// rankWomen
|
||||
{{1, 2, 4, 3, 5}, {3, 5, 1, 2, 4}, {5, 4, 2, 1, 3}, {1, 3, 5, 4, 2}, {4, 2, 3, 5, 1}},
|
||||
|
||||
// rankMen
|
||||
{
|
||||
{5, 1, 2, 4, 3},
|
||||
{4, 1, 3, 2, 5},
|
||||
{5, 3, 2, 4, 1},
|
||||
{1, 5, 4, 3, 2},
|
||||
{4, 3, 2, 1, 5}
|
||||
}
|
||||
};
|
||||
// rankMen
|
||||
{{5, 1, 2, 4, 3}, {4, 1, 3, 2, 5}, {5, 3, 2, 4, 1}, {1, 5, 4, 3, 2}, {4, 3, 2, 1, 5}}};
|
||||
|
||||
//
|
||||
// Data from MathWorld
|
||||
// http://mathworld.wolfram.com/StableMarriageProblem.html
|
||||
//
|
||||
long[][][] mathworld = {
|
||||
// rankWomen
|
||||
{
|
||||
{3, 1, 5, 2, 8, 7, 6, 9, 4},
|
||||
{9, 4, 8, 1, 7, 6, 3, 2, 5},
|
||||
{3, 1, 8, 9, 5, 4, 2, 6, 7},
|
||||
{8, 7, 5, 3, 2, 6, 4, 9, 1},
|
||||
{6, 9, 2, 5, 1, 4, 7, 3, 8},
|
||||
{2, 4, 5, 1, 6, 8, 3, 9, 7},
|
||||
{9, 3, 8, 2, 7, 5, 4, 6, 1},
|
||||
{6, 3, 2, 1, 8, 4, 5, 9, 7},
|
||||
{8, 2, 6, 4, 9, 1, 3, 7, 5}
|
||||
},
|
||||
long[][][] mathworld = {// rankWomen
|
||||
{{3, 1, 5, 2, 8, 7, 6, 9, 4}, {9, 4, 8, 1, 7, 6, 3, 2, 5}, {3, 1, 8, 9, 5, 4, 2, 6, 7},
|
||||
{8, 7, 5, 3, 2, 6, 4, 9, 1}, {6, 9, 2, 5, 1, 4, 7, 3, 8}, {2, 4, 5, 1, 6, 8, 3, 9, 7},
|
||||
{9, 3, 8, 2, 7, 5, 4, 6, 1}, {6, 3, 2, 1, 8, 4, 5, 9, 7}, {8, 2, 6, 4, 9, 1, 3, 7, 5}},
|
||||
|
||||
// rankMen
|
||||
{
|
||||
{7, 3, 8, 9, 6, 4, 2, 1, 5},
|
||||
{5, 4, 8, 3, 1, 2, 6, 7, 9},
|
||||
{4, 8, 3, 9, 7, 5, 6, 1, 2},
|
||||
{9, 7, 4, 2, 5, 8, 3, 1, 6},
|
||||
{2, 6, 4, 9, 8, 7, 5, 1, 3},
|
||||
{2, 7, 8, 6, 5, 3, 4, 1, 9},
|
||||
{1, 6, 2, 3, 8, 5, 4, 9, 7},
|
||||
{5, 6, 9, 1, 2, 8, 4, 3, 7},
|
||||
{6, 1, 4, 7, 5, 8, 3, 9, 2}
|
||||
}
|
||||
};
|
||||
// rankMen
|
||||
{{7, 3, 8, 9, 6, 4, 2, 1, 5}, {5, 4, 8, 3, 1, 2, 6, 7, 9}, {4, 8, 3, 9, 7, 5, 6, 1, 2},
|
||||
{9, 7, 4, 2, 5, 8, 3, 1, 6}, {2, 6, 4, 9, 8, 7, 5, 1, 3}, {2, 7, 8, 6, 5, 3, 4, 1, 9},
|
||||
{1, 6, 2, 3, 8, 5, 4, 9, 7}, {5, 6, 9, 1, 2, 8, 4, 3, 7}, {6, 1, 4, 7, 5, 8, 3, 9, 2}}};
|
||||
|
||||
//
|
||||
// Data from
|
||||
// http://www.csee.wvu.edu/~ksmani/courses/fa01/random/lecnotes/lecture5.pdf
|
||||
//
|
||||
long[][][] problem3 = {
|
||||
// rankWomen
|
||||
{
|
||||
{1, 2, 3, 4},
|
||||
{4, 3, 2, 1},
|
||||
{1, 2, 3, 4},
|
||||
{3, 4, 1, 2}
|
||||
},
|
||||
long[][][] problem3 = {// rankWomen
|
||||
{{1, 2, 3, 4}, {4, 3, 2, 1}, {1, 2, 3, 4}, {3, 4, 1, 2}},
|
||||
|
||||
// rankMen"
|
||||
{
|
||||
{1, 2, 3, 4},
|
||||
{2, 1, 3, 4},
|
||||
{1, 4, 3, 2},
|
||||
{4, 3, 1, 2}
|
||||
}
|
||||
};
|
||||
// rankMen"
|
||||
{{1, 2, 3, 4}, {2, 1, 3, 4}, {1, 4, 3, 2}, {4, 3, 1, 2}}};
|
||||
|
||||
//
|
||||
// Data from
|
||||
// http://www.comp.rgu.ac.uk/staff/ha/ZCSP/additional_problems/stable_marriage/stable_marriage.pdf
|
||||
// page 4
|
||||
//
|
||||
long[][][] problem4 = {
|
||||
// rankWomen
|
||||
{
|
||||
{1, 5, 4, 6, 2, 3},
|
||||
{4, 1, 5, 2, 6, 3},
|
||||
{6, 4, 2, 1, 5, 3},
|
||||
{1, 5, 2, 4, 3, 6},
|
||||
{4, 2, 1, 5, 6, 3},
|
||||
{2, 6, 3, 5, 1, 4}
|
||||
},
|
||||
long[][][] problem4 = {// rankWomen
|
||||
{{1, 5, 4, 6, 2, 3}, {4, 1, 5, 2, 6, 3}, {6, 4, 2, 1, 5, 3}, {1, 5, 2, 4, 3, 6},
|
||||
{4, 2, 1, 5, 6, 3}, {2, 6, 3, 5, 1, 4}},
|
||||
|
||||
// rankMen
|
||||
{
|
||||
{1, 4, 2, 5, 6, 3},
|
||||
{3, 4, 6, 1, 5, 2},
|
||||
{1, 6, 4, 2, 3, 5},
|
||||
{6, 5, 3, 4, 2, 1},
|
||||
{3, 1, 2, 4, 5, 6},
|
||||
{2, 3, 1, 6, 5, 4}
|
||||
}
|
||||
};
|
||||
// rankMen
|
||||
{{1, 4, 2, 5, 6, 3}, {3, 4, 6, 1, 5, 2}, {1, 6, 4, 2, 3, 5}, {6, 5, 3, 4, 2, 1},
|
||||
{3, 1, 2, 4, 5, 6}, {2, 3, 1, 6, 5, 4}}};
|
||||
|
||||
StableMarriage.solve(van_hentenryck, "Van Hentenryck");
|
||||
StableMarriage.solve(mathworld, "MathWorld");
|
||||
|
||||
287
examples/contrib/StiglerMIP.java
Executable file → Normal file
287
examples/contrib/StiglerMIP.java
Executable file → Normal file
@@ -33,7 +33,8 @@ public class StiglerMIP {
|
||||
System.out.println("---- StiglerMIP with " + solverType);
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(solverType);
|
||||
if (solver == null) return;
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
double infinity = MPSolver.infinity();
|
||||
|
||||
@@ -43,177 +44,119 @@ public class StiglerMIP {
|
||||
int commoditiesCount = 77;
|
||||
|
||||
String[] nutrients = {
|
||||
"calories", // Calories, unit = 1000
|
||||
"protein", // Protein, unit = grams
|
||||
"calcium", // Calcium, unit = grams
|
||||
"iron", // Iron, unit = milligrams
|
||||
"vitaminA", // Vitamin A, unit = 1000 International Units
|
||||
"thiamine", // Thiamine, Vit. B1, unit = milligrams
|
||||
"riboflavin", // Riboflavin, Vit. B2, unit = milligrams
|
||||
"niacin", // Niacin (Nicotinic Acid), unit = milligrams
|
||||
"ascorbicAcid" // Ascorbic Acid, Vit. C, unit = milligrams
|
||||
"calories", // Calories, unit = 1000
|
||||
"protein", // Protein, unit = grams
|
||||
"calcium", // Calcium, unit = grams
|
||||
"iron", // Iron, unit = milligrams
|
||||
"vitaminA", // Vitamin A, unit = 1000 International Units
|
||||
"thiamine", // Thiamine, Vit. B1, unit = milligrams
|
||||
"riboflavin", // Riboflavin, Vit. B2, unit = milligrams
|
||||
"niacin", // Niacin (Nicotinic Acid), unit = milligrams
|
||||
"ascorbicAcid" // Ascorbic Acid, Vit. C, unit = milligrams
|
||||
};
|
||||
|
||||
String[] commodities = {
|
||||
"Wheat Flour (Enriched), 10 lb.",
|
||||
"Macaroni, 1 lb.",
|
||||
"Wheat Cereal (Enriched), 28 oz.",
|
||||
"Corn Flakes, 8 oz.",
|
||||
"Corn Meal, 1 lb.",
|
||||
"Hominy Grits, 24 oz.",
|
||||
"Rice, 1 lb.",
|
||||
"Rolled Oats, 1 lb.",
|
||||
"White Bread (Enriched), 1 lb.",
|
||||
"Whole Wheat Bread, 1 lb.",
|
||||
"Rye Bread, 1 lb.",
|
||||
"Pound Cake, 1 lb.",
|
||||
"Soda Crackers, 1 lb.",
|
||||
"Milk, 1 qt.",
|
||||
"Evaporated Milk (can), 14.5 oz.",
|
||||
"Butter, 1 lb.",
|
||||
"Oleomargarine, 1 lb.",
|
||||
"Eggs, 1 doz.",
|
||||
"Cheese (Cheddar), 1 lb.",
|
||||
"Cream, 1/2 pt.",
|
||||
"Peanut Butter, 1 lb.",
|
||||
"Mayonnaise, 1/2 pt.",
|
||||
"Crisco, 1 lb.",
|
||||
"Lard, 1 lb.",
|
||||
"Sirloin Steak, 1 lb.",
|
||||
"Round Steak, 1 lb.",
|
||||
"Rib Roast, 1 lb.",
|
||||
"Chuck Roast, 1 lb.",
|
||||
"Plate, 1 lb.",
|
||||
"Liver (Beef), 1 lb.",
|
||||
"Leg of Lamb, 1 lb.",
|
||||
"Lamb Chops (Rib), 1 lb.",
|
||||
"Pork Chops, 1 lb.",
|
||||
"Pork Loin Roast, 1 lb.",
|
||||
"Bacon, 1 lb.",
|
||||
"Ham - smoked, 1 lb.",
|
||||
"Salt Pork, 1 lb.",
|
||||
"Roasting Chicken, 1 lb.",
|
||||
"Veal Cutlets, 1 lb.",
|
||||
"Salmon, Pink (can), 16 oz.",
|
||||
"Apples, 1 lb.",
|
||||
"Bananas, 1 lb.",
|
||||
"Lemons, 1 doz.",
|
||||
"Oranges, 1 doz.",
|
||||
"Green Beans, 1 lb.",
|
||||
"Cabbage, 1 lb.",
|
||||
"Carrots, 1 bunch",
|
||||
"Celery, 1 stalk",
|
||||
"Lettuce, 1 head",
|
||||
"Onions, 1 lb.",
|
||||
"Potatoes, 15 lb.",
|
||||
"Spinach, 1 lb.",
|
||||
"Sweet Potatoes, 1 lb.",
|
||||
"Peaches (can), No. 2 1/2",
|
||||
"Pears (can), No. 2 1/2,",
|
||||
"Pineapple (can), No. 2 1/2",
|
||||
"Asparagus (can), No. 2",
|
||||
"Grean Beans (can), No. 2",
|
||||
"Pork and Beans (can), 16 oz.",
|
||||
"Corn (can), No. 2",
|
||||
"Peas (can), No. 2",
|
||||
"Tomatoes (can), No. 2",
|
||||
"Tomato Soup (can), 10 1/2 oz.",
|
||||
"Peaches, Dried, 1 lb.",
|
||||
"Prunes, Dried, 1 lb.",
|
||||
"Raisins, Dried, 15 oz.",
|
||||
"Peas, Dried, 1 lb.",
|
||||
"Lima Beans, Dried, 1 lb.",
|
||||
"Navy Beans, Dried, 1 lb.",
|
||||
"Coffee, 1 lb.",
|
||||
"Tea, 1/4 lb.",
|
||||
"Cocoa, 8 oz.",
|
||||
"Chocolate, 8 oz.",
|
||||
"Sugar, 10 lb.",
|
||||
"Corn Sirup, 24 oz.",
|
||||
"Molasses, 18 oz.",
|
||||
"Strawberry Preserve, 1 lb."
|
||||
};
|
||||
String[] commodities = {"Wheat Flour (Enriched), 10 lb.", "Macaroni, 1 lb.",
|
||||
"Wheat Cereal (Enriched), 28 oz.", "Corn Flakes, 8 oz.", "Corn Meal, 1 lb.",
|
||||
"Hominy Grits, 24 oz.", "Rice, 1 lb.", "Rolled Oats, 1 lb.",
|
||||
"White Bread (Enriched), 1 lb.", "Whole Wheat Bread, 1 lb.", "Rye Bread, 1 lb.",
|
||||
"Pound Cake, 1 lb.", "Soda Crackers, 1 lb.", "Milk, 1 qt.",
|
||||
"Evaporated Milk (can), 14.5 oz.", "Butter, 1 lb.", "Oleomargarine, 1 lb.", "Eggs, 1 doz.",
|
||||
"Cheese (Cheddar), 1 lb.", "Cream, 1/2 pt.", "Peanut Butter, 1 lb.", "Mayonnaise, 1/2 pt.",
|
||||
"Crisco, 1 lb.", "Lard, 1 lb.", "Sirloin Steak, 1 lb.", "Round Steak, 1 lb.",
|
||||
"Rib Roast, 1 lb.", "Chuck Roast, 1 lb.", "Plate, 1 lb.", "Liver (Beef), 1 lb.",
|
||||
"Leg of Lamb, 1 lb.", "Lamb Chops (Rib), 1 lb.", "Pork Chops, 1 lb.",
|
||||
"Pork Loin Roast, 1 lb.", "Bacon, 1 lb.", "Ham - smoked, 1 lb.", "Salt Pork, 1 lb.",
|
||||
"Roasting Chicken, 1 lb.", "Veal Cutlets, 1 lb.", "Salmon, Pink (can), 16 oz.",
|
||||
"Apples, 1 lb.", "Bananas, 1 lb.", "Lemons, 1 doz.", "Oranges, 1 doz.",
|
||||
"Green Beans, 1 lb.", "Cabbage, 1 lb.", "Carrots, 1 bunch", "Celery, 1 stalk",
|
||||
"Lettuce, 1 head", "Onions, 1 lb.", "Potatoes, 15 lb.", "Spinach, 1 lb.",
|
||||
"Sweet Potatoes, 1 lb.", "Peaches (can), No. 2 1/2", "Pears (can), No. 2 1/2,",
|
||||
"Pineapple (can), No. 2 1/2", "Asparagus (can), No. 2", "Grean Beans (can), No. 2",
|
||||
"Pork and Beans (can), 16 oz.", "Corn (can), No. 2", "Peas (can), No. 2",
|
||||
"Tomatoes (can), No. 2", "Tomato Soup (can), 10 1/2 oz.", "Peaches, Dried, 1 lb.",
|
||||
"Prunes, Dried, 1 lb.", "Raisins, Dried, 15 oz.", "Peas, Dried, 1 lb.",
|
||||
"Lima Beans, Dried, 1 lb.", "Navy Beans, Dried, 1 lb.", "Coffee, 1 lb.", "Tea, 1/4 lb.",
|
||||
"Cocoa, 8 oz.", "Chocolate, 8 oz.", "Sugar, 10 lb.", "Corn Sirup, 24 oz.",
|
||||
"Molasses, 18 oz.", "Strawberry Preserve, 1 lb."};
|
||||
|
||||
// price and weight per unit correspond to the two first columns
|
||||
double[][] data = {
|
||||
{36.0, 12600.0, 44.7, 1411.0, 2.0, 365.0, 0.0, 55.4, 33.3, 441.0, 0.0},
|
||||
{14.1, 3217.0, 11.6, 418.0, 0.7, 54.0, 0.0, 3.2, 1.9, 68.0, 0.0},
|
||||
{24.2, 3280.0, 11.8, 377.0, 14.4, 175.0, 0.0, 14.4, 8.8, 114.0, 0.0},
|
||||
{7.1, 3194.0, 11.4, 252.0, 0.1, 56.0, 0.0, 13.5, 2.3, 68.0, 0.0},
|
||||
{4.6, 9861.0, 36.0, 897.0, 1.7, 99.0, 30.9, 17.4, 7.9, 106.0, 0.0},
|
||||
{8.5, 8005.0, 28.6, 680.0, 0.8, 80.0, 0.0, 10.6, 1.6, 110.0, 0.0},
|
||||
{7.5, 6048.0, 21.2, 460.0, 0.6, 41.0, 0.0, 2.0, 4.8, 60.0, 0.0},
|
||||
{7.1, 6389.0, 25.3, 907.0, 5.1, 341.0, 0.0, 37.1, 8.9, 64.0, 0.0},
|
||||
{7.9, 5742.0, 15.6, 488.0, 2.5, 115.0, 0.0, 13.8, 8.5, 126.0, 0.0},
|
||||
{9.1, 4985.0, 12.2, 484.0, 2.7, 125.0, 0.0, 13.9, 6.4, 160.0, 0.0},
|
||||
{9.2, 4930.0, 12.4, 439.0, 1.1, 82.0, 0.0, 9.9, 3.0, 66.0, 0.0},
|
||||
{24.8, 1829.0, 8.0, 130.0, 0.4, 31.0, 18.9, 2.8, 3.0, 17.0, 0.0},
|
||||
{15.1, 3004.0, 12.5, 288.0, 0.5, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{11.0, 8867.0, 6.1, 310.0, 10.5, 18.0, 16.8, 4.0, 16.0, 7.0, 177.0},
|
||||
{6.7, 6035.0, 8.4, 422.0, 15.1, 9.0, 26.0, 3.0, 23.5, 11.0, 60.0},
|
||||
{20.8, 1473.0, 10.8, 9.0, 0.2, 3.0, 44.2, 0.0, 0.2, 2.0, 0.0},
|
||||
{16.1, 2817.0, 20.6, 17.0, 0.6, 6.0, 55.8, 0.2, 0.0, 0.0, 0.0},
|
||||
{32.6, 1857.0, 2.9, 238.0, 1.0, 52.0, 18.6, 2.8, 6.5, 1.0, 0.0},
|
||||
{24.2, 1874.0, 7.4, 448.0, 16.4, 19.0, 28.1, 0.8, 10.3, 4.0, 0.0},
|
||||
{14.1, 1689.0, 3.5, 49.0, 1.7, 3.0, 16.9, 0.6, 2.5, 0.0, 17.0},
|
||||
{17.9, 2534.0, 15.7, 661.0, 1.0, 48.0, 0.0, 9.6, 8.1, 471.0, 0.0},
|
||||
{16.7, 1198.0, 8.6, 18.0, 0.2, 8.0, 2.7, 0.4, 0.5, 0.0, 0.0},
|
||||
{20.3, 2234.0, 20.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{9.8, 4628.0, 41.7, 0.0, 0.0, 0.0, 0.2, 0.0, 0.5, 5.0, 0.0},
|
||||
{39.6, 1145.0, 2.9, 166.0, 0.1, 34.0, 0.2, 2.1, 2.9, 69.0, 0.0},
|
||||
{36.4, 1246.0, 2.2, 214.0, 0.1, 32.0, 0.4, 2.5, 2.4, 87.0, 0.0},
|
||||
{29.2, 1553.0, 3.4, 213.0, 0.1, 33.0, 0.0, 0.0, 2.0, 0.0, 0.0},
|
||||
{22.6, 2007.0, 3.6, 309.0, 0.2, 46.0, 0.4, 1.0, 4.0, 120.0, 0.0},
|
||||
{14.6, 3107.0, 8.5, 404.0, 0.2, 62.0, 0.0, 0.9, 0.0, 0.0, 0.0},
|
||||
{26.8, 1692.0, 2.2, 333.0, 0.2, 139.0, 169.2, 6.4, 50.8, 316.0, 525.0},
|
||||
{27.6, 1643.0, 3.1, 245.0, 0.1, 20.0, 0.0, 2.8, 3.0, 86.0, 0.0},
|
||||
{36.6, 1239.0, 3.3, 140.0, 0.1, 15.0, 0.0, 1.7, 2.7, 54.0, 0.0},
|
||||
{30.7, 1477.0, 3.5, 196.0, 0.2, 80.0, 0.0, 17.4, 2.7, 60.0, 0.0},
|
||||
{24.2, 1874.0, 4.4, 249.0, 0.3, 37.0, 0.0, 18.2, 3.6, 79.0, 0.0},
|
||||
{25.6, 1772.0, 10.4, 152.0, 0.2, 23.0, 0.0, 1.8, 1.8, 71.0, 0.0},
|
||||
{27.4, 1655.0, 6.7, 212.0, 0.2, 31.0, 0.0, 9.9, 3.3, 50.0, 0.0},
|
||||
{16.0, 2835.0, 18.8, 164.0, 0.1, 26.0, 0.0, 1.4, 1.8, 0.0, 0.0},
|
||||
{30.3, 1497.0, 1.8, 184.0, 0.1, 30.0, 0.1, 0.9, 1.8, 68.0, 46.0},
|
||||
{42.3, 1072.0, 1.7, 156.0, 0.1, 24.0, 0.0, 1.4, 2.4, 57.0, 0.0},
|
||||
{13.0, 3489.0, 5.8, 705.0, 6.8, 45.0, 3.5, 1.0, 4.9, 209.0, 0.0},
|
||||
{4.4, 9072.0, 5.8, 27.0, 0.5, 36.0, 7.3, 3.6, 2.7, 5.0, 544.0},
|
||||
{6.1, 4982.0, 4.9, 60.0, 0.4, 30.0, 17.4, 2.5, 3.5, 28.0, 498.0},
|
||||
{26.0, 2380.0, 1.0, 21.0, 0.5, 14.0, 0.0, 0.5, 0.0, 4.0, 952.0},
|
||||
{30.9, 4439.0, 2.2, 40.0, 1.1, 18.0, 11.1, 3.6, 1.3, 10.0, 1993.0},
|
||||
{7.1, 5750.0, 2.4, 138.0, 3.7, 80.0, 69.0, 4.3, 5.8, 37.0, 862.0},
|
||||
{3.7, 8949.0, 2.6, 125.0, 4.0, 36.0, 7.2, 9.0, 4.5, 26.0, 5369.0},
|
||||
{4.7, 6080.0, 2.7, 73.0, 2.8, 43.0, 188.5, 6.1, 4.3, 89.0, 608.0},
|
||||
{7.3, 3915.0, 0.9, 51.0, 3.0, 23.0, 0.9, 1.4, 1.4, 9.0, 313.0},
|
||||
{8.2, 2247.0, 0.4, 27.0, 1.1, 22.0, 112.4, 1.8, 3.4, 11.0, 449.0},
|
||||
{3.6, 11844.0, 5.8, 166.0, 3.8, 59.0, 16.6, 4.7, 5.9, 21.0, 1184.0},
|
||||
{34.0, 16810.0, 14.3, 336.0, 1.8, 118.0, 6.7, 29.4, 7.1, 198.0, 2522.0},
|
||||
{8.1, 4592.0, 1.1, 106.0, 0.0, 138.0, 918.4, 5.7, 13.8, 33.0, 2755.0},
|
||||
{5.1, 7649.0, 9.6, 138.0, 2.7, 54.0, 290.7, 8.4, 5.4, 83.0, 1912.0},
|
||||
{16.8, 4894.0, 3.7, 20.0, 0.4, 10.0, 21.5, 0.5, 1.0, 31.0, 196.0},
|
||||
{20.4, 4030.0, 3.0, 8.0, 0.3, 8.0, 0.8, 0.8, 0.8, 5.0, 81.0},
|
||||
{21.3, 3993.0, 2.4, 16.0, 0.4, 8.0, 2.0, 2.8, 0.8, 7.0, 399.0},
|
||||
{27.7, 1945.0, 0.4, 33.0, 0.3, 12.0, 16.3, 1.4, 2.1, 17.0, 272.0},
|
||||
{10.0, 5386.0, 1.0, 54.0, 2.0, 65.0, 53.9, 1.6, 4.3, 32.0, 431.0},
|
||||
{7.1, 6389.0, 7.5, 364.0, 4.0, 134.0, 3.5, 8.3, 7.7, 56.0, 0.0},
|
||||
{10.4, 5452.0, 5.2, 136.0, 0.2, 16.0, 12.0, 1.6, 2.7, 42.0, 218.0},
|
||||
{13.8, 4109.0, 2.3, 136.0, 0.6, 45.0, 34.9, 4.9, 2.5, 37.0, 370.0},
|
||||
{8.6, 6263.0, 1.3, 63.0, 0.7, 38.0, 53.2, 3.4, 2.5, 36.0, 1253.0},
|
||||
{7.6, 3917.0, 1.6, 71.0, 0.6, 43.0, 57.9, 3.5, 2.4, 67.0, 862.0},
|
||||
{15.7, 2889.0, 8.5, 87.0, 1.7, 173.0, 86.8, 1.2, 4.3, 55.0, 57.0},
|
||||
{9.0, 4284.0, 12.8, 99.0, 2.5, 154.0, 85.7, 3.9, 4.3, 65.0, 257.0},
|
||||
{9.4, 4524.0, 13.5, 104.0, 2.5, 136.0, 4.5, 6.3, 1.4, 24.0, 136.0},
|
||||
{7.9, 5742.0, 20.0, 1367.0, 4.2, 345.0, 2.9, 28.7, 18.4, 162.0, 0.0},
|
||||
{8.9, 5097.0, 17.4, 1055.0, 3.7, 459.0, 5.1, 26.9, 38.2, 93.0, 0.0},
|
||||
{5.9, 7688.0, 26.9, 1691.0, 11.4, 792.0, 0.0, 38.4, 24.6, 217.0, 0.0},
|
||||
{22.4, 2025.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 5.1, 50.0, 0.0},
|
||||
{17.4, 652.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.3, 42.0, 0.0},
|
||||
{8.6, 2637.0, 8.7, 237.0, 3.0, 72.0, 0.0, 2.0, 11.9, 40.0, 0.0},
|
||||
{16.2, 1400.0, 8.0, 77.0, 1.3, 39.0, 0.0, 0.9, 3.4, 14.0, 0.0},
|
||||
{51.7, 8773.0, 34.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{13.7, 4996.0, 14.7, 0.0, 0.5, 74.0, 0.0, 0.0, 0.0, 5.0, 0.0},
|
||||
{13.6, 3752.0, 9.0, 0.0, 10.3, 244.0, 0.0, 1.9, 7.5, 146.0, 0.0},
|
||||
{20.5, 2213.0, 6.4, 11.0, 0.4, 7.0, 0.2, 0.2, 0.4, 3.0, 0.0}
|
||||
};
|
||||
double[][] data = {{36.0, 12600.0, 44.7, 1411.0, 2.0, 365.0, 0.0, 55.4, 33.3, 441.0, 0.0},
|
||||
{14.1, 3217.0, 11.6, 418.0, 0.7, 54.0, 0.0, 3.2, 1.9, 68.0, 0.0},
|
||||
{24.2, 3280.0, 11.8, 377.0, 14.4, 175.0, 0.0, 14.4, 8.8, 114.0, 0.0},
|
||||
{7.1, 3194.0, 11.4, 252.0, 0.1, 56.0, 0.0, 13.5, 2.3, 68.0, 0.0},
|
||||
{4.6, 9861.0, 36.0, 897.0, 1.7, 99.0, 30.9, 17.4, 7.9, 106.0, 0.0},
|
||||
{8.5, 8005.0, 28.6, 680.0, 0.8, 80.0, 0.0, 10.6, 1.6, 110.0, 0.0},
|
||||
{7.5, 6048.0, 21.2, 460.0, 0.6, 41.0, 0.0, 2.0, 4.8, 60.0, 0.0},
|
||||
{7.1, 6389.0, 25.3, 907.0, 5.1, 341.0, 0.0, 37.1, 8.9, 64.0, 0.0},
|
||||
{7.9, 5742.0, 15.6, 488.0, 2.5, 115.0, 0.0, 13.8, 8.5, 126.0, 0.0},
|
||||
{9.1, 4985.0, 12.2, 484.0, 2.7, 125.0, 0.0, 13.9, 6.4, 160.0, 0.0},
|
||||
{9.2, 4930.0, 12.4, 439.0, 1.1, 82.0, 0.0, 9.9, 3.0, 66.0, 0.0},
|
||||
{24.8, 1829.0, 8.0, 130.0, 0.4, 31.0, 18.9, 2.8, 3.0, 17.0, 0.0},
|
||||
{15.1, 3004.0, 12.5, 288.0, 0.5, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{11.0, 8867.0, 6.1, 310.0, 10.5, 18.0, 16.8, 4.0, 16.0, 7.0, 177.0},
|
||||
{6.7, 6035.0, 8.4, 422.0, 15.1, 9.0, 26.0, 3.0, 23.5, 11.0, 60.0},
|
||||
{20.8, 1473.0, 10.8, 9.0, 0.2, 3.0, 44.2, 0.0, 0.2, 2.0, 0.0},
|
||||
{16.1, 2817.0, 20.6, 17.0, 0.6, 6.0, 55.8, 0.2, 0.0, 0.0, 0.0},
|
||||
{32.6, 1857.0, 2.9, 238.0, 1.0, 52.0, 18.6, 2.8, 6.5, 1.0, 0.0},
|
||||
{24.2, 1874.0, 7.4, 448.0, 16.4, 19.0, 28.1, 0.8, 10.3, 4.0, 0.0},
|
||||
{14.1, 1689.0, 3.5, 49.0, 1.7, 3.0, 16.9, 0.6, 2.5, 0.0, 17.0},
|
||||
{17.9, 2534.0, 15.7, 661.0, 1.0, 48.0, 0.0, 9.6, 8.1, 471.0, 0.0},
|
||||
{16.7, 1198.0, 8.6, 18.0, 0.2, 8.0, 2.7, 0.4, 0.5, 0.0, 0.0},
|
||||
{20.3, 2234.0, 20.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{9.8, 4628.0, 41.7, 0.0, 0.0, 0.0, 0.2, 0.0, 0.5, 5.0, 0.0},
|
||||
{39.6, 1145.0, 2.9, 166.0, 0.1, 34.0, 0.2, 2.1, 2.9, 69.0, 0.0},
|
||||
{36.4, 1246.0, 2.2, 214.0, 0.1, 32.0, 0.4, 2.5, 2.4, 87.0, 0.0},
|
||||
{29.2, 1553.0, 3.4, 213.0, 0.1, 33.0, 0.0, 0.0, 2.0, 0.0, 0.0},
|
||||
{22.6, 2007.0, 3.6, 309.0, 0.2, 46.0, 0.4, 1.0, 4.0, 120.0, 0.0},
|
||||
{14.6, 3107.0, 8.5, 404.0, 0.2, 62.0, 0.0, 0.9, 0.0, 0.0, 0.0},
|
||||
{26.8, 1692.0, 2.2, 333.0, 0.2, 139.0, 169.2, 6.4, 50.8, 316.0, 525.0},
|
||||
{27.6, 1643.0, 3.1, 245.0, 0.1, 20.0, 0.0, 2.8, 3.0, 86.0, 0.0},
|
||||
{36.6, 1239.0, 3.3, 140.0, 0.1, 15.0, 0.0, 1.7, 2.7, 54.0, 0.0},
|
||||
{30.7, 1477.0, 3.5, 196.0, 0.2, 80.0, 0.0, 17.4, 2.7, 60.0, 0.0},
|
||||
{24.2, 1874.0, 4.4, 249.0, 0.3, 37.0, 0.0, 18.2, 3.6, 79.0, 0.0},
|
||||
{25.6, 1772.0, 10.4, 152.0, 0.2, 23.0, 0.0, 1.8, 1.8, 71.0, 0.0},
|
||||
{27.4, 1655.0, 6.7, 212.0, 0.2, 31.0, 0.0, 9.9, 3.3, 50.0, 0.0},
|
||||
{16.0, 2835.0, 18.8, 164.0, 0.1, 26.0, 0.0, 1.4, 1.8, 0.0, 0.0},
|
||||
{30.3, 1497.0, 1.8, 184.0, 0.1, 30.0, 0.1, 0.9, 1.8, 68.0, 46.0},
|
||||
{42.3, 1072.0, 1.7, 156.0, 0.1, 24.0, 0.0, 1.4, 2.4, 57.0, 0.0},
|
||||
{13.0, 3489.0, 5.8, 705.0, 6.8, 45.0, 3.5, 1.0, 4.9, 209.0, 0.0},
|
||||
{4.4, 9072.0, 5.8, 27.0, 0.5, 36.0, 7.3, 3.6, 2.7, 5.0, 544.0},
|
||||
{6.1, 4982.0, 4.9, 60.0, 0.4, 30.0, 17.4, 2.5, 3.5, 28.0, 498.0},
|
||||
{26.0, 2380.0, 1.0, 21.0, 0.5, 14.0, 0.0, 0.5, 0.0, 4.0, 952.0},
|
||||
{30.9, 4439.0, 2.2, 40.0, 1.1, 18.0, 11.1, 3.6, 1.3, 10.0, 1993.0},
|
||||
{7.1, 5750.0, 2.4, 138.0, 3.7, 80.0, 69.0, 4.3, 5.8, 37.0, 862.0},
|
||||
{3.7, 8949.0, 2.6, 125.0, 4.0, 36.0, 7.2, 9.0, 4.5, 26.0, 5369.0},
|
||||
{4.7, 6080.0, 2.7, 73.0, 2.8, 43.0, 188.5, 6.1, 4.3, 89.0, 608.0},
|
||||
{7.3, 3915.0, 0.9, 51.0, 3.0, 23.0, 0.9, 1.4, 1.4, 9.0, 313.0},
|
||||
{8.2, 2247.0, 0.4, 27.0, 1.1, 22.0, 112.4, 1.8, 3.4, 11.0, 449.0},
|
||||
{3.6, 11844.0, 5.8, 166.0, 3.8, 59.0, 16.6, 4.7, 5.9, 21.0, 1184.0},
|
||||
{34.0, 16810.0, 14.3, 336.0, 1.8, 118.0, 6.7, 29.4, 7.1, 198.0, 2522.0},
|
||||
{8.1, 4592.0, 1.1, 106.0, 0.0, 138.0, 918.4, 5.7, 13.8, 33.0, 2755.0},
|
||||
{5.1, 7649.0, 9.6, 138.0, 2.7, 54.0, 290.7, 8.4, 5.4, 83.0, 1912.0},
|
||||
{16.8, 4894.0, 3.7, 20.0, 0.4, 10.0, 21.5, 0.5, 1.0, 31.0, 196.0},
|
||||
{20.4, 4030.0, 3.0, 8.0, 0.3, 8.0, 0.8, 0.8, 0.8, 5.0, 81.0},
|
||||
{21.3, 3993.0, 2.4, 16.0, 0.4, 8.0, 2.0, 2.8, 0.8, 7.0, 399.0},
|
||||
{27.7, 1945.0, 0.4, 33.0, 0.3, 12.0, 16.3, 1.4, 2.1, 17.0, 272.0},
|
||||
{10.0, 5386.0, 1.0, 54.0, 2.0, 65.0, 53.9, 1.6, 4.3, 32.0, 431.0},
|
||||
{7.1, 6389.0, 7.5, 364.0, 4.0, 134.0, 3.5, 8.3, 7.7, 56.0, 0.0},
|
||||
{10.4, 5452.0, 5.2, 136.0, 0.2, 16.0, 12.0, 1.6, 2.7, 42.0, 218.0},
|
||||
{13.8, 4109.0, 2.3, 136.0, 0.6, 45.0, 34.9, 4.9, 2.5, 37.0, 370.0},
|
||||
{8.6, 6263.0, 1.3, 63.0, 0.7, 38.0, 53.2, 3.4, 2.5, 36.0, 1253.0},
|
||||
{7.6, 3917.0, 1.6, 71.0, 0.6, 43.0, 57.9, 3.5, 2.4, 67.0, 862.0},
|
||||
{15.7, 2889.0, 8.5, 87.0, 1.7, 173.0, 86.8, 1.2, 4.3, 55.0, 57.0},
|
||||
{9.0, 4284.0, 12.8, 99.0, 2.5, 154.0, 85.7, 3.9, 4.3, 65.0, 257.0},
|
||||
{9.4, 4524.0, 13.5, 104.0, 2.5, 136.0, 4.5, 6.3, 1.4, 24.0, 136.0},
|
||||
{7.9, 5742.0, 20.0, 1367.0, 4.2, 345.0, 2.9, 28.7, 18.4, 162.0, 0.0},
|
||||
{8.9, 5097.0, 17.4, 1055.0, 3.7, 459.0, 5.1, 26.9, 38.2, 93.0, 0.0},
|
||||
{5.9, 7688.0, 26.9, 1691.0, 11.4, 792.0, 0.0, 38.4, 24.6, 217.0, 0.0},
|
||||
{22.4, 2025.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 5.1, 50.0, 0.0},
|
||||
{17.4, 652.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.3, 42.0, 0.0},
|
||||
{8.6, 2637.0, 8.7, 237.0, 3.0, 72.0, 0.0, 2.0, 11.9, 40.0, 0.0},
|
||||
{16.2, 1400.0, 8.0, 77.0, 1.3, 39.0, 0.0, 0.9, 3.4, 14.0, 0.0},
|
||||
{51.7, 8773.0, 34.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
|
||||
{13.7, 4996.0, 14.7, 0.0, 0.5, 74.0, 0.0, 0.0, 0.0, 5.0, 0.0},
|
||||
{13.6, 3752.0, 9.0, 0.0, 10.3, 244.0, 0.0, 1.9, 7.5, 146.0, 0.0},
|
||||
{20.5, 2213.0, 6.4, 11.0, 0.4, 7.0, 0.2, 0.2, 0.4, 3.0, 0.0}};
|
||||
|
||||
// recommended daily nutritional allowance
|
||||
double[] allowance = {3.0, 70.0, 0.8, 12.0, 5.0, 1.8, 2.7, 18.0, 75.0};
|
||||
@@ -268,12 +211,8 @@ public class StiglerMIP {
|
||||
|
||||
for (int i = 0; i < commoditiesCount; i++) {
|
||||
if (x[i].solutionValue() > 0) {
|
||||
System.out.println(
|
||||
commodities[i]
|
||||
+ ": "
|
||||
+ df.format(xCost[i].solutionValue())
|
||||
+ " "
|
||||
+ df.format(quant[i].solutionValue()));
|
||||
System.out.println(commodities[i] + ": " + df.format(xCost[i].solutionValue()) + " "
|
||||
+ df.format(quant[i].solutionValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,29 +28,12 @@ public class Strimko2 {
|
||||
//
|
||||
// data
|
||||
//
|
||||
int[][] streams = {
|
||||
{1, 1, 2, 2, 2, 2, 2},
|
||||
{1, 1, 2, 3, 3, 3, 2},
|
||||
{1, 4, 1, 3, 3, 5, 5},
|
||||
{4, 4, 3, 1, 3, 5, 5},
|
||||
{4, 6, 6, 6, 7, 7, 5},
|
||||
{6, 4, 6, 4, 5, 5, 7},
|
||||
{6, 6, 4, 7, 7, 7, 7}
|
||||
};
|
||||
int[][] streams = {{1, 1, 2, 2, 2, 2, 2}, {1, 1, 2, 3, 3, 3, 2}, {1, 4, 1, 3, 3, 5, 5},
|
||||
{4, 4, 3, 1, 3, 5, 5}, {4, 6, 6, 6, 7, 7, 5}, {6, 4, 6, 4, 5, 5, 7}, {6, 6, 4, 7, 7, 7, 7}};
|
||||
|
||||
// Note: This is 1-based
|
||||
int[][] placed = {
|
||||
{2, 1, 1},
|
||||
{2, 3, 7},
|
||||
{2, 5, 6},
|
||||
{2, 7, 4},
|
||||
{3, 2, 7},
|
||||
{3, 6, 1},
|
||||
{4, 1, 4},
|
||||
{4, 7, 5},
|
||||
{5, 2, 2},
|
||||
{5, 6, 6}
|
||||
};
|
||||
int[][] placed = {{2, 1, 1}, {2, 3, 7}, {2, 5, 6}, {2, 7, 4}, {3, 2, 7}, {3, 6, 1}, {4, 1, 4},
|
||||
{4, 7, 5}, {5, 2, 2}, {5, 6, 6}};
|
||||
|
||||
int n = streams.length;
|
||||
int num_placed = placed.length;
|
||||
@@ -74,7 +57,6 @@ public class Strimko2 {
|
||||
|
||||
// all rows and columns must be unique, i.e. a Latin Square
|
||||
for (int i = 0; i < n; i++) {
|
||||
|
||||
IntVar[] row = new IntVar[n];
|
||||
IntVar[] col = new IntVar[n];
|
||||
for (int j = 0; j < n; j++) {
|
||||
|
||||
@@ -29,18 +29,10 @@ public class Sudoku {
|
||||
int n = cell_size * cell_size;
|
||||
|
||||
// 0 marks an unknown value
|
||||
int[][] initial_grid =
|
||||
new int[][] {
|
||||
{0, 6, 0, 0, 5, 0, 0, 2, 0},
|
||||
{0, 0, 0, 3, 0, 0, 0, 9, 0},
|
||||
{7, 0, 0, 6, 0, 0, 0, 1, 0},
|
||||
{0, 0, 6, 0, 3, 0, 4, 0, 0},
|
||||
{0, 0, 4, 0, 7, 0, 1, 0, 0},
|
||||
{0, 0, 5, 0, 9, 0, 8, 0, 0},
|
||||
{0, 4, 0, 0, 0, 1, 0, 0, 6},
|
||||
{0, 3, 0, 0, 0, 8, 0, 0, 0},
|
||||
{0, 2, 0, 0, 4, 0, 0, 5, 0}
|
||||
};
|
||||
int[][] initial_grid = new int[][] {{0, 6, 0, 0, 5, 0, 0, 2, 0}, {0, 0, 0, 3, 0, 0, 0, 9, 0},
|
||||
{7, 0, 0, 6, 0, 0, 0, 1, 0}, {0, 0, 6, 0, 3, 0, 4, 0, 0}, {0, 0, 4, 0, 7, 0, 1, 0, 0},
|
||||
{0, 0, 5, 0, 9, 0, 8, 0, 0}, {0, 4, 0, 0, 0, 1, 0, 0, 6}, {0, 3, 0, 0, 0, 8, 0, 0, 0},
|
||||
{0, 2, 0, 0, 4, 0, 0, 5, 0}};
|
||||
|
||||
//
|
||||
// variables
|
||||
|
||||
@@ -27,11 +27,7 @@ public class SurvoPuzzle {
|
||||
static int default_c = 4;
|
||||
static int[] default_rowsums = {30, 18, 30};
|
||||
static int[] default_colsums = {27, 16, 10, 25};
|
||||
static int[][] default_game = {
|
||||
{0, 6, 0, 0},
|
||||
{8, 0, 0, 0},
|
||||
{0, 0, 3, 0}
|
||||
};
|
||||
static int[][] default_game = {{0, 6, 0, 0}, {8, 0, 0, 0}, {0, 0, 3, 0}};
|
||||
|
||||
// for the actual problem
|
||||
static int r;
|
||||
@@ -42,7 +38,6 @@ public class SurvoPuzzle {
|
||||
|
||||
/** Solves the Survo puzzle problem. See http://www.hakank.org/google_or_tools/survo_puzzle.py */
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("Survopuzzle");
|
||||
|
||||
//
|
||||
@@ -138,11 +133,9 @@ public class SurvoPuzzle {
|
||||
* <p>Example: 3 4 30,18,30 27,16,10,25 0,6,0,0 8,0,0,0 0,0,3,0
|
||||
*/
|
||||
private static void readFile(String file) {
|
||||
|
||||
System.out.println("readFile(" + file + ")");
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader inr = new BufferedReader(new FileReader(file));
|
||||
|
||||
r = Integer.parseInt(inr.readLine());
|
||||
|
||||
@@ -40,7 +40,6 @@ public class ToNum {
|
||||
* http://www.hakank.org/google_or_tools/toNum.py
|
||||
*/
|
||||
private static void solve() {
|
||||
|
||||
Solver solver = new Solver("ToNum");
|
||||
|
||||
int n = 5;
|
||||
|
||||
@@ -76,24 +76,20 @@ public class WhoKilledAgatha {
|
||||
// A killer always hates, and is no richer than his victim.
|
||||
// hates[the_killer, the_victim] == 1
|
||||
// hates_flat[the_killer * n + the_victim] == 1
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(
|
||||
solver
|
||||
.makeElement(
|
||||
hates_flat,
|
||||
solver.makeSum(solver.makeProd(the_killer, n).var(), the_victim).var())
|
||||
.var(),
|
||||
1));
|
||||
solver.addConstraint(solver.makeEquality(
|
||||
solver
|
||||
.makeElement(
|
||||
hates_flat, solver.makeSum(solver.makeProd(the_killer, n).var(), the_victim).var())
|
||||
.var(),
|
||||
1));
|
||||
|
||||
// richer[the_killer, the_victim] == 0
|
||||
solver.addConstraint(
|
||||
solver.makeEquality(
|
||||
solver
|
||||
.makeElement(
|
||||
richer_flat,
|
||||
solver.makeSum(solver.makeProd(the_killer, n).var(), the_victim).var())
|
||||
.var(),
|
||||
0));
|
||||
solver.addConstraint(solver.makeEquality(
|
||||
solver
|
||||
.makeElement(
|
||||
richer_flat, solver.makeSum(solver.makeProd(the_killer, n).var(), the_victim).var())
|
||||
.var(),
|
||||
0));
|
||||
|
||||
// define the concept of richer:
|
||||
// no one is richer than him-/herself...
|
||||
|
||||
@@ -85,7 +85,8 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
* @param manager Node Index Manager.
|
||||
* @param costCoefficient The coefficient to apply to the evaluator.
|
||||
*/
|
||||
private LongBinaryOperator buildManhattanCallback(RoutingIndexManager manager, int costCoefficient) {
|
||||
private LongBinaryOperator buildManhattanCallback(
|
||||
RoutingIndexManager manager, int costCoefficient) {
|
||||
return new LongBinaryOperator() {
|
||||
public long applyAsLong(long firstIndex, long secondIndex) {
|
||||
try {
|
||||
@@ -117,15 +118,8 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
* @param penaltyMin minimum pernalty cost if order is dropped.
|
||||
* @param penaltyMax maximum pernalty cost if order is dropped.
|
||||
*/
|
||||
private void buildOrders(
|
||||
int numberOfOrders,
|
||||
int xMax,
|
||||
int yMax,
|
||||
int demandMax,
|
||||
int timeWindowMax,
|
||||
int timeWindowWidth,
|
||||
int penaltyMin,
|
||||
int penaltyMax) {
|
||||
private void buildOrders(int numberOfOrders, int xMax, int yMax, int demandMax, int timeWindowMax,
|
||||
int timeWindowWidth, int penaltyMin, int penaltyMax) {
|
||||
logger.info("Building orders.");
|
||||
for (int order = 0; order < numberOfOrders; ++order) {
|
||||
locations.add(Pair.of(randomGenerator.nextInt(xMax + 1), randomGenerator.nextInt(yMax + 1)));
|
||||
@@ -182,21 +176,20 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
model.registerTransitCallback(callback), bigNumber, bigNumber, false, timeStr);
|
||||
RoutingDimension timeDimension = model.getMutableDimension(timeStr);
|
||||
|
||||
LongUnaryOperator demandCallback =
|
||||
new LongUnaryOperator() {
|
||||
public long applyAsLong(long index) {
|
||||
try {
|
||||
int node = manager.indexToNode(index);
|
||||
if (node < numberOfOrders) {
|
||||
return orderDemands.get(node);
|
||||
}
|
||||
return 0;
|
||||
} catch (Throwable throwed) {
|
||||
logger.warning(throwed.getMessage());
|
||||
return 0;
|
||||
}
|
||||
LongUnaryOperator demandCallback = new LongUnaryOperator() {
|
||||
public long applyAsLong(long index) {
|
||||
try {
|
||||
int node = manager.indexToNode(index);
|
||||
if (node < numberOfOrders) {
|
||||
return orderDemands.get(node);
|
||||
}
|
||||
};
|
||||
return 0;
|
||||
} catch (Throwable throwed) {
|
||||
logger.warning(throwed.getMessage());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
final String capacityStr = "capacity";
|
||||
model.addDimension(
|
||||
model.registerUnaryTransitCallback(demandCallback), 0, vehicleCapacity, true, capacityStr);
|
||||
@@ -214,9 +207,8 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
|
||||
// Setting up orders
|
||||
for (int order = 0; order < numberOfOrders; ++order) {
|
||||
timeDimension
|
||||
.cumulVar(order)
|
||||
.setRange(orderTimeWindows.get(order).first, orderTimeWindows.get(order).second);
|
||||
timeDimension.cumulVar(order).setRange(
|
||||
orderTimeWindows.get(order).first, orderTimeWindows.get(order).second);
|
||||
long[] orderIndices = {manager.nodeToIndex(order)};
|
||||
model.addDisjunction(orderIndices, orderPenalties.get(order));
|
||||
}
|
||||
@@ -254,29 +246,13 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
|
||||
for (; !model.isEnd(order); order = solution.value(model.nextVar(order))) {
|
||||
IntVar load = capacityDimension.cumulVar(order);
|
||||
IntVar time = timeDimension.cumulVar(order);
|
||||
route +=
|
||||
order
|
||||
+ " Load("
|
||||
+ solution.value(load)
|
||||
+ ") "
|
||||
+ "Time("
|
||||
+ solution.min(time)
|
||||
+ ", "
|
||||
+ solution.max(time)
|
||||
+ ") -> ";
|
||||
route += order + " Load(" + solution.value(load) + ") "
|
||||
+ "Time(" + solution.min(time) + ", " + solution.max(time) + ") -> ";
|
||||
}
|
||||
IntVar load = capacityDimension.cumulVar(order);
|
||||
IntVar time = timeDimension.cumulVar(order);
|
||||
route +=
|
||||
order
|
||||
+ " Load("
|
||||
+ solution.value(load)
|
||||
+ ") "
|
||||
+ "Time("
|
||||
+ solution.min(time)
|
||||
+ ", "
|
||||
+ solution.max(time)
|
||||
+ ")";
|
||||
route += order + " Load(" + solution.value(load) + ") "
|
||||
+ "Time(" + solution.min(time) + ", " + solution.max(time) + ")";
|
||||
}
|
||||
output += route + "\n";
|
||||
}
|
||||
|
||||
@@ -20,17 +20,16 @@ import com.google.ortools.constraintsolver.RoutingIndexManager;
|
||||
import com.google.ortools.constraintsolver.RoutingModel;
|
||||
import com.google.ortools.constraintsolver.RoutingSearchParameters;
|
||||
import com.google.ortools.constraintsolver.main;
|
||||
//import java.io.*;
|
||||
//import java.text.*;
|
||||
//import java.util.*;
|
||||
// import java.io.*;
|
||||
// import java.text.*;
|
||||
// import java.util.*;
|
||||
import java.util.Random;
|
||||
import java.util.function.LongBinaryOperator;
|
||||
import java.util.function.LongUnaryOperator;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RandomTsp {
|
||||
private static Logger logger =
|
||||
Logger.getLogger(RandomTsp.class.getName());
|
||||
private static Logger logger = Logger.getLogger(RandomTsp.class.getName());
|
||||
|
||||
static class RandomManhattan implements LongBinaryOperator {
|
||||
public RandomManhattan(RoutingIndexManager manager, int size, int seed) {
|
||||
@@ -86,12 +85,8 @@ public class RandomTsp {
|
||||
}
|
||||
|
||||
// Add dummy dimension to test API.
|
||||
routing.addDimension(
|
||||
routing.registerUnaryTransitCallback(new ConstantCallback()),
|
||||
size + 1,
|
||||
size + 1,
|
||||
true,
|
||||
"dummy");
|
||||
routing.addDimension(routing.registerUnaryTransitCallback(new ConstantCallback()), size + 1,
|
||||
size + 1, true, "dummy");
|
||||
|
||||
// Solve, returns a solution if any (owned by RoutingModel).
|
||||
RoutingSearchParameters search_parameters =
|
||||
@@ -108,10 +103,9 @@ public class RandomTsp {
|
||||
// Only one route here; otherwise iterate from 0 to routing.vehicles() - 1
|
||||
int route_number = 0;
|
||||
String route = "";
|
||||
for (long node = routing.start(route_number);
|
||||
!routing.isEnd(node);
|
||||
node = solution.value(routing.nextVar(node))) {
|
||||
route += "" + node + " -> ";
|
||||
for (long node = routing.start(route_number); !routing.isEnd(node);
|
||||
node = solution.value(routing.nextVar(node))) {
|
||||
route += "" + node + " -> ";
|
||||
}
|
||||
logger.info(route + "0");
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ import com.google.ortools.constraintsolver.DecisionBuilder;
|
||||
import com.google.ortools.constraintsolver.IntVar;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchFilter;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchOperator;
|
||||
import com.google.ortools.constraintsolver.LocalSearchPhaseParameters;
|
||||
import com.google.ortools.constraintsolver.LocalSearchFilterManager;
|
||||
import com.google.ortools.constraintsolver.LocalSearchPhaseParameters;
|
||||
import com.google.ortools.constraintsolver.OptimizeVar;
|
||||
import com.google.ortools.constraintsolver.SearchLog;
|
||||
import com.google.ortools.constraintsolver.SearchMonitor;
|
||||
@@ -44,7 +44,7 @@ public class ConstraintSolverTest {
|
||||
Object obj = new Object();
|
||||
WeakReference ref = new WeakReference<Object>(obj);
|
||||
obj = null;
|
||||
while(ref.get() != null) {
|
||||
while (ref.get() != null) {
|
||||
System.gc();
|
||||
}
|
||||
}
|
||||
@@ -56,8 +56,12 @@ public class ConstraintSolverTest {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testSolverCtor...");
|
||||
Solver solver = new Solver("TestSolver");
|
||||
if (!solver.model_name().equals("TestSolver")) {throw new AssertionError("Solver ill formed");}
|
||||
if (solver.toString().length() < 0) {throw new AssertionError("Solver ill formed");}
|
||||
if (!solver.model_name().equals("TestSolver")) {
|
||||
throw new AssertionError("Solver ill formed");
|
||||
}
|
||||
if (solver.toString().length() < 0) {
|
||||
throw new AssertionError("Solver ill formed");
|
||||
}
|
||||
logger.info("testSolverCtor...DONE");
|
||||
}
|
||||
|
||||
@@ -67,8 +71,12 @@ public class ConstraintSolverTest {
|
||||
logger.info("testIntVar...");
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar var = solver.makeIntVar(3, 11, "IntVar");
|
||||
if (var.min() != 3) {throw new AssertionError("IntVar Min wrong");}
|
||||
if (var.max() != 11) {throw new AssertionError("IntVar Max wrong");}
|
||||
if (var.min() != 3) {
|
||||
throw new AssertionError("IntVar Min wrong");
|
||||
}
|
||||
if (var.max() != 11) {
|
||||
throw new AssertionError("IntVar Max wrong");
|
||||
}
|
||||
logger.info("testIntVar...DONE");
|
||||
}
|
||||
|
||||
@@ -78,10 +86,16 @@ public class ConstraintSolverTest {
|
||||
logger.info("testIntVarArray...");
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar[] vars = solver.makeIntVarArray(7, 3, 5, "vars");
|
||||
if (vars.length != 7) {throw new AssertionError("Vars length wrong");}
|
||||
for(IntVar var: vars) {
|
||||
if (var.min() != 3) {throw new AssertionError("IntVar Min wrong");}
|
||||
if (var.max() != 5) {throw new AssertionError("IntVar Max wrong");}
|
||||
if (vars.length != 7) {
|
||||
throw new AssertionError("Vars length wrong");
|
||||
}
|
||||
for (IntVar var : vars) {
|
||||
if (var.min() != 3) {
|
||||
throw new AssertionError("IntVar Min wrong");
|
||||
}
|
||||
if (var.max() != 5) {
|
||||
throw new AssertionError("IntVar Max wrong");
|
||||
}
|
||||
}
|
||||
logger.info("testIntVarArray...DONE");
|
||||
}
|
||||
@@ -125,7 +139,8 @@ public class ConstraintSolverTest {
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(sumVar, moveOneVar, db);
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(sumVar, moveOneVar, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
@@ -150,7 +165,7 @@ public class ConstraintSolverTest {
|
||||
|
||||
@Override
|
||||
public boolean accept(Assignment delta, Assignment unusedDeltadelta, long unusedObjectiveMin,
|
||||
long unusedObjectiveMax) {
|
||||
long unusedObjectiveMax) {
|
||||
AssignmentIntContainer solutionDelta = delta.intVarContainer();
|
||||
int solutionDeltaSize = solutionDelta.size();
|
||||
|
||||
@@ -232,7 +247,7 @@ public class ConstraintSolverTest {
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
OneVarLns oneVarLns = new OneVarLns(vars);
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(sumVar, oneVarLns, db);
|
||||
solver.makeLocalSearchPhaseParameters(sumVar, oneVarLns, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
@@ -278,7 +293,7 @@ public class ConstraintSolverTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testSearchLogWithCallback(boolean enableGC) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testSearchLogWithCallback (enable gc:" + enableGC + ")...");
|
||||
@@ -286,8 +301,7 @@ public class ConstraintSolverTest {
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
OptimizeVar objective = solver.makeMinimize(var, 1);
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
SearchMonitor searchlog = solver.makeSearchLog(
|
||||
0, // branch period
|
||||
SearchMonitor searchlog = solver.makeSearchLog(0, // branch period
|
||||
new SearchCount(count));
|
||||
if (enableGC) {
|
||||
gc();
|
||||
@@ -295,12 +309,14 @@ public class ConstraintSolverTest {
|
||||
runSearchLog(searchlog);
|
||||
logger.info("count:" + count.intValue());
|
||||
|
||||
if (count.intValue() != 1) throw new AssertionError("count != 1"); ;
|
||||
if (count.intValue() != 1)
|
||||
throw new AssertionError("count != 1");
|
||||
;
|
||||
logger.info("testSearchLogWithCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testSearchLogWithIntVarCallback(boolean enableGC) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testSearchLogWithIntVarCallback (enable gc:" + enableGC + ")...");
|
||||
@@ -308,20 +324,21 @@ public class ConstraintSolverTest {
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
OptimizeVar objective = solver.makeMinimize(var, 1);
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
SearchMonitor searchlog = solver.makeSearchLog(
|
||||
0, // branch period
|
||||
SearchMonitor searchlog = solver.makeSearchLog(0, // branch period
|
||||
var, // IntVar to monitor
|
||||
new SearchCount(count));
|
||||
if (enableGC) {
|
||||
gc();
|
||||
}
|
||||
runSearchLog(searchlog);
|
||||
if (count.intValue() != 1) throw new AssertionError("count != 1"); ;
|
||||
if (count.intValue() != 1)
|
||||
throw new AssertionError("count != 1");
|
||||
;
|
||||
logger.info("testSearchLogWithIntVarCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testSearchLogWithObjectiveCallback(boolean enableGC) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testSearchLogWithObjectiveCallback (enable gc:" + enableGC + ")...");
|
||||
@@ -329,15 +346,16 @@ public class ConstraintSolverTest {
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
OptimizeVar objective = solver.makeMinimize(var, 1);
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
SearchMonitor searchlog = solver.makeSearchLog(
|
||||
0, // branch period
|
||||
SearchMonitor searchlog = solver.makeSearchLog(0, // branch period
|
||||
objective, // objective var to monitor
|
||||
new SearchCount(count));
|
||||
if (enableGC) {
|
||||
gc();
|
||||
}
|
||||
runSearchLog(searchlog);
|
||||
if (count.intValue() != 1) throw new AssertionError("count != 1"); ;
|
||||
if (count.intValue() != 1)
|
||||
throw new AssertionError("count != 1");
|
||||
;
|
||||
logger.info("testSearchLogWithObjectiveCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
@@ -346,7 +364,8 @@ public class ConstraintSolverTest {
|
||||
value_ = value;
|
||||
}
|
||||
public void setValue(String value) {
|
||||
value_ = value;;
|
||||
value_ = value;
|
||||
;
|
||||
}
|
||||
public String toString() {
|
||||
return value_;
|
||||
@@ -355,40 +374,48 @@ public class ConstraintSolverTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testClosureDecision(boolean enableGC) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testClosureDecision (enable gc:" + enableGC + ")...");
|
||||
final StringProperty call = new StringProperty("");
|
||||
Solver solver = new Solver("ClosureDecisionTest");
|
||||
Decision decision = solver.makeDecision(
|
||||
(Solver s) -> { call.setValue("Apply"); },
|
||||
(Solver s) -> { call.setValue("Refute"); });
|
||||
(Solver s) -> { call.setValue("Apply"); }, (Solver s) -> { call.setValue("Refute"); });
|
||||
if (enableGC) {
|
||||
gc();
|
||||
}
|
||||
|
||||
decision.apply(solver);
|
||||
if (!call.toString().equals("Apply")) {throw new AssertionError("Apply action not called");}
|
||||
if (!call.toString().equals("Apply")) {
|
||||
throw new AssertionError("Apply action not called");
|
||||
}
|
||||
|
||||
decision.refute(solver);
|
||||
if (!call.toString().equals("Refute")) {throw new AssertionError("Refute action not called");}
|
||||
if (!call.toString().equals("Refute")) {
|
||||
throw new AssertionError("Refute action not called");
|
||||
}
|
||||
logger.info("testClosureDecision (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testSolverInClosureDecision(boolean enableGC) throws Exception {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testSolverInClosureDecision (enable gc:" + enableGC + ")...");
|
||||
Solver solver = new Solver("SolverTestName");
|
||||
String model_name = solver.model_name();
|
||||
Decision decision = solver.makeDecision(
|
||||
(Solver s) -> {
|
||||
if (!s.model_name().equals(model_name)) {throw new AssertionError("Solver ill formed");}
|
||||
(Solver s)
|
||||
-> {
|
||||
if (!s.model_name().equals(model_name)) {
|
||||
throw new AssertionError("Solver ill formed");
|
||||
}
|
||||
},
|
||||
(Solver s) -> {
|
||||
if (!s.model_name().equals(model_name)) {throw new AssertionError("Solver ill formed");}
|
||||
if (!s.model_name().equals(model_name)) {
|
||||
throw new AssertionError("Solver ill formed");
|
||||
}
|
||||
});
|
||||
if (enableGC) {
|
||||
gc(); // verify SearchCount is kept alive
|
||||
|
||||
@@ -17,8 +17,8 @@ import com.google.ortools.linearsolver.MPConstraint;
|
||||
import com.google.ortools.linearsolver.MPObjective;
|
||||
import com.google.ortools.linearsolver.MPSolver;
|
||||
import com.google.ortools.linearsolver.MPVariable;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -26,15 +26,16 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
public class LinearSolverTest {
|
||||
static {
|
||||
System.setProperty("java.util.logging.SimpleFormatter.format",
|
||||
"[%1$tF %1$tT] [%4$-7s] %5$s %n");
|
||||
System.setProperty(
|
||||
"java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] %5$s %n");
|
||||
}
|
||||
|
||||
private static final Logger logger = Logger.getLogger(LinearSolverTest.class.getName());
|
||||
|
||||
private static void solveAndPrint(MPSolver solver, MPVariable[] variables, MPConstraint[] constraints) {
|
||||
private static void solveAndPrint(
|
||||
MPSolver solver, MPVariable[] variables, MPConstraint[] constraints) {
|
||||
logger.info("Number of variables = " + solver.numVariables());
|
||||
logger.info("Number of constraints = "+ solver.numConstraints());
|
||||
logger.info("Number of constraints = " + solver.numConstraints());
|
||||
|
||||
final MPSolver.ResultStatus status = solver.solve();
|
||||
// Check that the problem has an optimal solution.
|
||||
@@ -44,35 +45,32 @@ public class LinearSolverTest {
|
||||
|
||||
logger.info("Solution:");
|
||||
ArrayList<MPVariable> vars = new ArrayList<>(Arrays.asList(variables));
|
||||
vars.forEach(
|
||||
var -> logger.info(var.name() + " = " + var.solutionValue())
|
||||
);
|
||||
vars.forEach(var -> logger.info(var.name() + " = " + var.solutionValue()));
|
||||
logger.info("Optimal objective value = " + solver.objective().value());
|
||||
logger.info("");
|
||||
logger.info("Advanced usage:");
|
||||
logger.info("Problem solved in " + solver.wallTime() + " milliseconds");
|
||||
logger.info("Problem solved in " + solver.iterations() + " iterations");
|
||||
if (solver.isMip()) return;
|
||||
if (solver.isMip())
|
||||
return;
|
||||
|
||||
vars.forEach(
|
||||
var-> logger.info(var.name() + ": reduced cost " + var.reducedCost())
|
||||
);
|
||||
vars.forEach(var -> logger.info(var.name() + ": reduced cost " + var.reducedCost()));
|
||||
|
||||
final double[] activities = solver.computeConstraintActivities();
|
||||
ArrayList<MPConstraint> cts = new ArrayList<>(Arrays.asList(constraints));
|
||||
cts.forEach(
|
||||
ct -> logger.info(ct.name() + ": dual value = " + ct.dualValue()
|
||||
+ " activity = " + activities[ct.index()])
|
||||
);
|
||||
cts.forEach(ct
|
||||
-> logger.info(ct.name() + ": dual value = " + ct.dualValue()
|
||||
+ " activity = " + activities[ct.index()]));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "GLOP", "GLPK_LP", "CLP", "GUROBI_LP" })
|
||||
@ValueSource(strings = {"GLOP", "GLPK_LP", "CLP", "GUROBI_LP"})
|
||||
private static void testLinearProgramming(String problem_type) {
|
||||
logger.info("------ Linear programming example with " + problem_type + " ------");
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(problem_type);
|
||||
if (solver == null) return;
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
// x and y are continuous non-negative variables.
|
||||
MPVariable x = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "x");
|
||||
@@ -103,12 +101,13 @@ public class LinearSolverTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "GLPK", "CBC", "SCIP", "SAT" })
|
||||
@ValueSource(strings = {"GLPK", "CBC", "SCIP", "SAT"})
|
||||
private static void testMixedIntegerProgramming(String problem_type) {
|
||||
logger.info("------ Mixed integer programming example with " + problem_type + " ------");
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(problem_type);
|
||||
if (solver == null) return;
|
||||
MPSolver solver = MPSolver.createSolver(problem_type);
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
// x and y are continuous non-negative variables.
|
||||
MPVariable x = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "x");
|
||||
@@ -134,12 +133,13 @@ public class LinearSolverTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "SAT", "BOP" })
|
||||
@ValueSource(strings = {"SAT", "BOP"})
|
||||
private static void testBooleanProgramming(String problem_type) {
|
||||
logger.info("------ Boolean programming example with " + problem_type + " ------");
|
||||
|
||||
MPSolver solver = MPSolver.createSolver(problem_type);
|
||||
if (solver == null) return;
|
||||
MPSolver solver = MPSolver.createSolver(problem_type);
|
||||
if (solver == null)
|
||||
return;
|
||||
|
||||
// x and y are continuous non-negative variables.
|
||||
MPVariable x = solver.makeBoolVar("x");
|
||||
@@ -167,7 +167,7 @@ public class LinearSolverTest {
|
||||
solver.makeConstraint("my_const_name");
|
||||
try {
|
||||
solver.makeConstraint("my_const_name");
|
||||
} catch(Throwable e) {
|
||||
} catch (Throwable e) {
|
||||
System.out.println(e);
|
||||
success = false;
|
||||
}
|
||||
@@ -177,7 +177,7 @@ public class LinearSolverTest {
|
||||
@Test
|
||||
public void testSetHintAndSolverGetters() {
|
||||
Loader.loadNativeLibraries();
|
||||
MPSolver solver = MPSolver.createSolver("GLOP");
|
||||
MPSolver solver = MPSolver.createSolver("GLOP");
|
||||
|
||||
// x and y are continuous non-negative variables.
|
||||
MPVariable x = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "x");
|
||||
|
||||
@@ -12,15 +12,16 @@
|
||||
// limitations under the License.
|
||||
package com.google.ortools;
|
||||
|
||||
import com.google.ortools.Loader;
|
||||
import static java.lang.Math.abs;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.google.ortools.Loader;
|
||||
import com.google.ortools.constraintsolver.Assignment;
|
||||
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
|
||||
import com.google.ortools.constraintsolver.RoutingIndexManager;
|
||||
import com.google.ortools.constraintsolver.RoutingModel;
|
||||
import com.google.ortools.constraintsolver.RoutingSearchParameters;
|
||||
import com.google.ortools.constraintsolver.main;
|
||||
import java.util.logging.Logger;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
@@ -29,25 +30,24 @@ public class RoutingSolverTest {
|
||||
private static final Logger logger = Logger.getLogger(RoutingSolverTest.class.getName());
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testRoutingTransitCallback(boolean enableGC) {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testRoutingTransitCallback (enable gc:" + enableGC + ")...");
|
||||
// Create Routing Index Manager
|
||||
RoutingIndexManager manager =
|
||||
new RoutingIndexManager(5/*location*/, 1/*vehicle*/, 0/*depot*/);
|
||||
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
|
||||
// Create Routing Model.
|
||||
RoutingModel routing = new RoutingModel(manager);
|
||||
// Define cost of each arc.
|
||||
int transitCallbackIndex;
|
||||
if (true) {
|
||||
transitCallbackIndex = routing.registerTransitCallback(
|
||||
(long fromIndex, long toIndex) -> {
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
int fromNode = manager.indexToNode(fromIndex);
|
||||
int toNode = manager.indexToNode(toIndex);
|
||||
return abs(toNode - fromNode);
|
||||
});
|
||||
transitCallbackIndex = routing.registerTransitCallback((long fromIndex, long toIndex) -> {
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
int fromNode = manager.indexToNode(fromIndex);
|
||||
int toNode = manager.indexToNode(toIndex);
|
||||
return abs(toNode - fromNode);
|
||||
});
|
||||
}
|
||||
if (enableGC) {
|
||||
System.gc();
|
||||
@@ -61,30 +61,31 @@ public class RoutingSolverTest {
|
||||
.build();
|
||||
// Solve the problem.
|
||||
Assignment solution = routing.solveWithParameters(searchParameters);
|
||||
if (null == solution) throw new AssertionError("null == solution");
|
||||
if (8 != solution.objectiveValue()) throw new AssertionError("5 != objective");
|
||||
if (null == solution)
|
||||
throw new AssertionError("null == solution");
|
||||
if (8 != solution.objectiveValue())
|
||||
throw new AssertionError("5 != objective");
|
||||
logger.info("testRoutingTransitCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = { false, true })
|
||||
@ValueSource(booleans = {false, true})
|
||||
public void testRoutingUnaryTransitCallback(boolean enableGC) {
|
||||
Loader.loadNativeLibraries();
|
||||
logger.info("testRoutingUnaryTransitCallback (enable gc:" + enableGC + ")...");
|
||||
// Create Routing Index Manager
|
||||
RoutingIndexManager manager =
|
||||
new RoutingIndexManager(5/*location*/, 1/*vehicle*/, 0/*depot*/);
|
||||
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
|
||||
// Create Routing Model.
|
||||
RoutingModel routing = new RoutingModel(manager);
|
||||
// Define cost of each arc.
|
||||
int transitCallbackIndex;
|
||||
if (true) {
|
||||
transitCallbackIndex = routing.registerUnaryTransitCallback(
|
||||
(long fromIndex) -> {
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
int fromNode = manager.indexToNode(fromIndex);
|
||||
return abs(fromNode);
|
||||
});
|
||||
transitCallbackIndex = routing.registerUnaryTransitCallback((long fromIndex) -> {
|
||||
// Convert from routing variable Index to user NodeIndex.
|
||||
int fromNode = manager.indexToNode(fromIndex);
|
||||
return abs(fromNode);
|
||||
});
|
||||
}
|
||||
if (enableGC) {
|
||||
System.gc();
|
||||
@@ -98,8 +99,10 @@ public class RoutingSolverTest {
|
||||
.build();
|
||||
// Solve the problem.
|
||||
Assignment solution = routing.solveWithParameters(searchParameters);
|
||||
if (null == solution) throw new AssertionError("null == solution");
|
||||
if (10 != solution.objectiveValue()) throw new AssertionError("5 != objective");
|
||||
if (null == solution)
|
||||
throw new AssertionError("null == solution");
|
||||
if (10 != solution.objectiveValue())
|
||||
throw new AssertionError("5 != objective");
|
||||
logger.info("testRoutingUnaryTransitCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ import com.google.ortools.sat.CpSolverStatus;
|
||||
import com.google.ortools.sat.IntVar;
|
||||
import com.google.ortools.sat.LinearExpr;
|
||||
import com.google.ortools.util.Domain;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Tests the CP-SAT java interface. */
|
||||
@@ -55,7 +55,7 @@ public class SatSolverTest {
|
||||
|
||||
// Create a linear constraint which enforces that only x or y can be greater
|
||||
// than 0.
|
||||
model.addLinearConstraint(LinearExpr.sum(new IntVar[] { x, y }), 0, 1);
|
||||
model.addLinearConstraint(LinearExpr.sum(new IntVar[] {x, y}), 0, 1);
|
||||
|
||||
// Create the objective variable
|
||||
IntVar obj = model.newIntVar(0, 3, "obj");
|
||||
@@ -64,7 +64,7 @@ public class SatSolverTest {
|
||||
model.addGreaterOrEqual(obj, 2);
|
||||
|
||||
// Set a constraint that makes the problem infeasible
|
||||
model.addMaxEquality(obj, new IntVar[] { x, y });
|
||||
model.addMaxEquality(obj, new IntVar[] {x, y});
|
||||
|
||||
// Optimize objective
|
||||
model.minimize(obj);
|
||||
@@ -120,27 +120,27 @@ public class SatSolverTest {
|
||||
entities[i] = model.newIntVar(1, 5, "E" + i);
|
||||
}
|
||||
|
||||
final Integer[] equalities = new Integer[] { 18, 4, 19, 3, 12 };
|
||||
final Integer[] equalities = new Integer[] {18, 4, 19, 3, 12};
|
||||
addEqualities(model, entities, equalities);
|
||||
|
||||
final Integer[] allowedAssignments = new Integer[] { 12, 8, 15 };
|
||||
final Integer[] allowedAssignmentValues = new Integer[] { 1, 3 };
|
||||
final Integer[] allowedAssignments = new Integer[] {12, 8, 15};
|
||||
final Integer[] allowedAssignmentValues = new Integer[] {1, 3};
|
||||
addAllowedAssignMents(model, entities, allowedAssignments, allowedAssignmentValues);
|
||||
|
||||
final Integer[] forbiddenAssignments1 = new Integer[] { 6, 15, 19 };
|
||||
final Integer[] forbiddenAssignments1Values = new Integer[] { 3 };
|
||||
final Integer[] forbiddenAssignments2 = new Integer[] { 10, 19 };
|
||||
final Integer[] forbiddenAssignments2Values = new Integer[] { 4 };
|
||||
final Integer[] forbiddenAssignments3 = new Integer[] { 18, 0, 9, 7 };
|
||||
final Integer[] forbiddenAssignments3Values = new Integer[] { 4 };
|
||||
final Integer[] forbiddenAssignments4 = new Integer[] { 14, 11 };
|
||||
final Integer[] forbiddenAssignments4Values = new Integer[] { 1, 2, 3, 4, 5 };
|
||||
final Integer[] forbiddenAssignments5 = new Integer[] { 5, 16, 1, 3 };
|
||||
final Integer[] forbiddenAssignments5Values = new Integer[] { 1, 2, 3, 4, 5 };
|
||||
final Integer[] forbiddenAssignments6 = new Integer[] { 2, 6, 11, 4 };
|
||||
final Integer[] forbiddenAssignments6Values = new Integer[] { 1, 2, 3, 4, 5 };
|
||||
final Integer[] forbiddenAssignments7 = new Integer[] { 6, 18, 12, 2, 9, 14 };
|
||||
final Integer[] forbiddenAssignments7Values = new Integer[] { 1, 2, 3, 4, 5 };
|
||||
final Integer[] forbiddenAssignments1 = new Integer[] {6, 15, 19};
|
||||
final Integer[] forbiddenAssignments1Values = new Integer[] {3};
|
||||
final Integer[] forbiddenAssignments2 = new Integer[] {10, 19};
|
||||
final Integer[] forbiddenAssignments2Values = new Integer[] {4};
|
||||
final Integer[] forbiddenAssignments3 = new Integer[] {18, 0, 9, 7};
|
||||
final Integer[] forbiddenAssignments3Values = new Integer[] {4};
|
||||
final Integer[] forbiddenAssignments4 = new Integer[] {14, 11};
|
||||
final Integer[] forbiddenAssignments4Values = new Integer[] {1, 2, 3, 4, 5};
|
||||
final Integer[] forbiddenAssignments5 = new Integer[] {5, 16, 1, 3};
|
||||
final Integer[] forbiddenAssignments5Values = new Integer[] {1, 2, 3, 4, 5};
|
||||
final Integer[] forbiddenAssignments6 = new Integer[] {2, 6, 11, 4};
|
||||
final Integer[] forbiddenAssignments6Values = new Integer[] {1, 2, 3, 4, 5};
|
||||
final Integer[] forbiddenAssignments7 = new Integer[] {6, 18, 12, 2, 9, 14};
|
||||
final Integer[] forbiddenAssignments7Values = new Integer[] {1, 2, 3, 4, 5};
|
||||
|
||||
addForbiddenAssignments(forbiddenAssignments1Values, forbiddenAssignments1, entities, model);
|
||||
addForbiddenAssignments(forbiddenAssignments2Values, forbiddenAssignments2, entities, model);
|
||||
@@ -150,7 +150,8 @@ public class SatSolverTest {
|
||||
addForbiddenAssignments(forbiddenAssignments6Values, forbiddenAssignments6, entities, model);
|
||||
addForbiddenAssignments(forbiddenAssignments7Values, forbiddenAssignments7, entities, model);
|
||||
|
||||
final Integer[] configuration = new Integer[] { 5, 4, 2, 3, 3, 3, 4, 3, 3, 1, 4, 4, 3, 1, 4, 1, 4, 4, 3, 3 };
|
||||
final Integer[] configuration =
|
||||
new Integer[] {5, 4, 2, 3, 3, 3, 4, 3, 3, 1, 4, 4, 3, 1, 4, 1, 4, 4, 3, 3};
|
||||
for (int i = 0; i < configuration.length; i++) {
|
||||
model.addEquality(entities[i], configuration[i]);
|
||||
}
|
||||
@@ -159,15 +160,17 @@ public class SatSolverTest {
|
||||
solver.solve(model);
|
||||
}
|
||||
|
||||
private void addEqualities(final CpModel model, final IntVar[] entities, final Integer[] equalities) {
|
||||
private void addEqualities(
|
||||
final CpModel model, final IntVar[] entities, final Integer[] equalities) {
|
||||
for (int i = 0; i < (equalities.length - 1); i++) {
|
||||
model.addEquality(entities[equalities[i]], entities[equalities[i + 1]]);
|
||||
}
|
||||
}
|
||||
|
||||
private void addAllowedAssignMents(final CpModel model, final IntVar[] entities, final Integer[] allowedAssignments,
|
||||
final Integer[] allowedAssignmentValues) {
|
||||
final int[][] allAllowedValues = new int[allowedAssignmentValues.length][allowedAssignments.length];
|
||||
private void addAllowedAssignMents(final CpModel model, final IntVar[] entities,
|
||||
final Integer[] allowedAssignments, final Integer[] allowedAssignmentValues) {
|
||||
final int[][] allAllowedValues =
|
||||
new int[allowedAssignmentValues.length][allowedAssignments.length];
|
||||
for (int i = 0; i < allowedAssignmentValues.length; i++) {
|
||||
final Integer value = allowedAssignmentValues[i];
|
||||
for (int j = 0; j < allowedAssignments.length; j++) {
|
||||
@@ -185,14 +188,15 @@ public class SatSolverTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void addForbiddenAssignments(final Integer[] forbiddenAssignmentsValues, final Integer[] forbiddenAssignments,
|
||||
final IntVar[] entities, final CpModel model) {
|
||||
private void addForbiddenAssignments(final Integer[] forbiddenAssignmentsValues,
|
||||
final Integer[] forbiddenAssignments, final IntVar[] entities, final CpModel model) {
|
||||
final IntVar[] specificEntities = new IntVar[forbiddenAssignments.length];
|
||||
for (int i = 0; i < forbiddenAssignments.length; i++) {
|
||||
specificEntities[i] = entities[forbiddenAssignments[i]];
|
||||
}
|
||||
|
||||
final int[][] notAllowedValues = new int[forbiddenAssignmentsValues.length][forbiddenAssignments.length];
|
||||
final int[][] notAllowedValues =
|
||||
new int[forbiddenAssignmentsValues.length][forbiddenAssignments.length];
|
||||
for (int i = 0; i < forbiddenAssignmentsValues.length; i++) {
|
||||
final Integer value = forbiddenAssignmentsValues[i];
|
||||
for (int j = 0; j < forbiddenAssignments.length; j++) {
|
||||
@@ -204,6 +208,5 @@ public class SatSolverTest {
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ public class VrpWithTimeLimit {
|
||||
|
||||
// [START solution_printer]
|
||||
/// @brief Print the solution.
|
||||
static void printSolution(RoutingIndexManager manager, RoutingModel routing, Assignment solution) {
|
||||
static void printSolution(
|
||||
RoutingIndexManager manager, RoutingModel routing, Assignment solution) {
|
||||
// Inspect solution.
|
||||
long maxRouteDistance = 0;
|
||||
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
|
||||
@@ -66,8 +67,7 @@ public class VrpWithTimeLimit {
|
||||
|
||||
// Create Routing Index Manager
|
||||
// [START index_manager]
|
||||
RoutingIndexManager manager =
|
||||
new RoutingIndexManager(locationNumber, vehicleNumber, depot);
|
||||
RoutingIndexManager manager = new RoutingIndexManager(locationNumber, vehicleNumber, depot);
|
||||
// [END index_manager]
|
||||
|
||||
// Create Routing Model.
|
||||
@@ -93,12 +93,10 @@ public class VrpWithTimeLimit {
|
||||
|
||||
// Add Distance constraint.
|
||||
// [START distance_constraint]
|
||||
routing.addDimension(
|
||||
transitCallbackIndex,
|
||||
routing.addDimension(transitCallbackIndex,
|
||||
/*slack=*/0,
|
||||
/*horizon=*/3000,
|
||||
/*start_cumul_to_zero=*/true,
|
||||
"Distance");
|
||||
/*start_cumul_to_zero=*/true, "Distance");
|
||||
RoutingDimension distanceDimension = routing.getMutableDimension("Distance");
|
||||
distanceDimension.setGlobalSpanCostCoefficient(100);
|
||||
// [END distance_constraint]
|
||||
|
||||
@@ -13,7 +13,7 @@ public class CMakeTest {
|
||||
public void testLP() {
|
||||
Loader.loadNativeLibraries();
|
||||
MPSolver solver =
|
||||
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
|
||||
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
|
||||
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
|
||||
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
|
||||
System.out.println("Number of variables = " + solver.numVariables());
|
||||
|
||||
@@ -23,9 +23,7 @@ public class Loader {
|
||||
String resource = Platform.RESOURCE_PREFIX + "/";
|
||||
URL resourceURL = loader.getResource(resource);
|
||||
Objects.requireNonNull(resourceURL,
|
||||
String.format("Resource %s was not found in ClassLoader %s",
|
||||
resource,
|
||||
loader));
|
||||
String.format("Resource %s was not found in ClassLoader %s", resource, loader));
|
||||
|
||||
URI resourceURI;
|
||||
try {
|
||||
@@ -41,7 +39,8 @@ public class Loader {
|
||||
void accept(Path path) throws T;
|
||||
}
|
||||
|
||||
/** Extract native resources in a temp directory.
|
||||
/**
|
||||
* Extract native resources in a temp directory.
|
||||
* @param resourceURI Native resource location.
|
||||
* @return The directory path containing all extracted libraries.
|
||||
*/
|
||||
@@ -61,7 +60,8 @@ public class Loader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
|
||||
throws IOException {
|
||||
Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
|
||||
Files.copy(dir, newPath);
|
||||
newPath.toFile().deleteOnExit();
|
||||
@@ -78,15 +78,14 @@ public class Loader {
|
||||
/** Unpack and Load the native libraries needed for using ortools-java.*/
|
||||
private static boolean loaded = false;
|
||||
public static void loadNativeLibraries() {
|
||||
if(!loaded) {
|
||||
if (!loaded) {
|
||||
try {
|
||||
URI resourceURI = getNativeResourceURI();
|
||||
Path tempPath = unpackNativeResources(resourceURI);
|
||||
// Load the native library
|
||||
System.load(
|
||||
tempPath.resolve(Platform.RESOURCE_PREFIX)
|
||||
.resolve(System.mapLibraryName("jniortools"))
|
||||
.toString());
|
||||
System.load(tempPath.resolve(Platform.RESOURCE_PREFIX)
|
||||
.resolve(System.mapLibraryName("jniortools"))
|
||||
.toString());
|
||||
loaded = true;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -94,4 +93,3 @@ public class Loader {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user