|
| 1 | +# Tests |
| 2 | + |
| 3 | +## Some notes about specific test cases |
| 4 | + |
| 5 | +### GLPK Tests |
| 6 | + |
| 7 | +#### All GLPK tests |
| 8 | +For GLPK all SolverConfigs set presolve to true. |
| 9 | +If set to false, an error occurs because glpk expects the problem object to contain an optimal solution to the LP relaxation. |
| 10 | + |
| 11 | +See GLPK documentation of glp_intopt: |
| 12 | + |
| 13 | +> glp intopt — solve MIP problem with the branch-and-cut method |
| 14 | +> |
| 15 | +> Synopsis |
| 16 | +> int glp_intopt(glp_prob *P, const glp_iocp *parm); |
| 17 | +> |
| 18 | +> Description |
| 19 | +> [...] |
| 20 | +> If the presolver is disabled (see paragraph “Control parameters” below), on entry to the routine |
| 21 | +> glp_intopt the problem object, which the parameter mip points to, should contain optimal solution |
| 22 | +> to LP relaxation (it can be obtained, for example, with the routine glp_simplex). Otherwise, if |
| 23 | +> the presolver is enabled, it is not necessary. |
| 24 | +
|
| 25 | +#### testNotEqualLinearConstraint |
| 26 | + |
| 27 | +Espilon and the tolerance had to be changed for this test case. |
| 28 | + |
| 29 | +The values for psi and psi_prime in the substitution of != otherwise were so close to zero, that the constraints didn't work as intended. |
| 30 | +The optimized value for i1 was 5 (c1: i1 != 5) and for r2 was 100 (c2: r2 != 100). |
| 31 | + |
| 32 | +Experimental values that worked: |
| 33 | +1. tolerance = 1.0E-8 |
| 34 | +2. tolerance = 1.0E-6 and epsilon = 9.9999E-2 |
| 35 | + |
| 36 | +### CPLEX Tests |
| 37 | + |
| 38 | +#### testLessQuadraticConstraint |
| 39 | +For this testcase the tolerance of the solver had to be changed. |
| 40 | +tolerance = 1.0E-6 |
| 41 | + |
| 42 | +#### testGreaterQuadraticConstraint |
| 43 | +For this testcase the tolerance of the solver had to be changed. |
| 44 | +tolerance = 1.0E-6 |
| 45 | + |
| 46 | +## How to run tests |
| 47 | +Remember: Depending on the solver a license is necessary (e.g. for Gurobi). |
| 48 | + |
| 49 | +- Run all tests: |
| 50 | + `$ mvn clean verify` |
| 51 | +- Run a specific test class (e.g. GlpkTest.java): |
| 52 | + `$ mvn -Dtest=GlpkTest -DfailIfNoTests=false verify` |
| 53 | + |
| 54 | +Before running tests with the Cplex solver, it might be necessary to add the following Run Configuration to the VM Arguments (Eclipse: right click on the project -> `Run as` -> `Run Configurations` -> `Arguments` tab), replace with the appropriate path: |
| 55 | +`-Djava.library.path=/opt/ibm/ILOG/CPLEX_Studioxxx/cplex/bin/x86-64_xxx` |
| 56 | + |
| 57 | +## Example Problem |
| 58 | +### Knapsack Problem |
| 59 | +This example can be found in the Test Class SolverTest.java. |
| 60 | + |
| 61 | +There is a set of items, which all have a weight and a value. |
| 62 | +The goal is to determine a collection of items, for which the profit is maximized but the capacity constraint is satisfied. |
| 63 | + |
| 64 | +``` |
| 65 | +// Amount of items |
| 66 | +int I = 6; |
| 67 | +// Profit |
| 68 | +int[] p = { 10, 13, 18, 32, 7, 15 }; |
| 69 | +// Weight |
| 70 | +int[] w = { 11, 15, 20, 35, 10, 33 }; |
| 71 | +// Capacity |
| 72 | +int c = 47; |
| 73 | +
|
| 74 | +// Create variables: |
| 75 | +// 0 -> item i not put in knapsack |
| 76 | +// 1 -> item i put in knapsack |
| 77 | +List<BinaryVariable> x_i = new ArrayList<>(); |
| 78 | +for (int i = 0; i < I; i++) { |
| 79 | + x_i.add(new BinaryVariable("x_" + i)); |
| 80 | +} |
| 81 | +
|
| 82 | +// Objective: maximize the total price of selected items |
| 83 | +// maximize SUM(p_i * x_i) |
| 84 | +Problem problem = new Problem(); |
| 85 | +problem.setType(ObjectiveType.MAX); |
| 86 | +
|
| 87 | +LinearFunction lin = new LinearFunction(); |
| 88 | +for (int i = 0; i < I; i++) { |
| 89 | + lin.addTerm(x_i.get(i), p[i]); |
| 90 | +} |
| 91 | +
|
| 92 | +// Constraint: Total weight must be equal or less than the capacity |
| 93 | +// SUM(w_i * x_i) <= c |
| 94 | +LinearConstraint c1 = new LinearConstraint(Operator.LESS_OR_EQUAL, c); |
| 95 | +for (int i = 0; i < I; i++) { |
| 96 | + c1.addTerm(x_i.get(i), w[i]); |
| 97 | +} |
| 98 | +
|
| 99 | +// Model |
| 100 | +problem.setObjective(lin); |
| 101 | +problem.add(c1); |
| 102 | +
|
| 103 | +// Optimize |
| 104 | +SolverConfig config = new SolverConfig(SolverType.GLPK, false, 0.0, true, 42, false, 0.0, false, 0, 0, true, false, false, null); |
| 105 | +Solver solver = (new SolverHelper(config)).getSolver(); |
| 106 | +solver.buildILPProblem(problem); |
| 107 | +SolverOutput out = solver.solve(); |
| 108 | +// Prints the result of the objective |
| 109 | +System.out.println(out.toString()); |
| 110 | +// Sets the values for the Variables |
| 111 | +solver.updateValuesFromSolution(); |
| 112 | +
|
| 113 | +// Do something, e.g. print |
| 114 | +
|
| 115 | +solver.terminate(); |
| 116 | +``` |
0 commit comments