aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/test/java/tools/refinery/store/reasoning/scope/MPSolverTest.java
blob: 95c4ac68eaadfac4084a145b1a59890476a3fa5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.scope;

import com.google.ortools.Loader;
import com.google.ortools.linearsolver.MPSolver;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.is;

class MPSolverTest {
	@BeforeAll
	static void beforeAll() {
		Loader.loadNativeLibraries();
	}

	@Test
	void updateProblemTest() {
		var solver = MPSolver.createSolver("GLOP");
		var x = solver.makeNumVar(0, Double.POSITIVE_INFINITY, "x");
		var y = solver.makeNumVar(0, 1, "y");
		var constraint = solver.makeConstraint(5, 5);
		constraint.setCoefficient(x, 1);
		constraint.setCoefficient(y, 1);
		var objective = solver.objective();

		objective.setCoefficient(x, 1);
		objective.setMinimization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(4, 0.01));

		objective.setMaximization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(5, 0.01));

		objective.setCoefficient(x, 0);
		objective.setCoefficient(y, 1);
		objective.setMinimization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(0, 0.01));

		objective.setMaximization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(1, 0.01));
	}

	@Test
	void unboundedIsInfeasibleTest() {
		var solver = MPSolver.createSolver("GLOP");
		var x = solver.makeNumVar(0, Double.POSITIVE_INFINITY, "x");
		var objective = solver.objective();
		objective.setCoefficient(x, 1);

		objective.setMinimization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(0, 0.01));

		objective.setMaximization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.INFEASIBLE));
	}

	@Test
	void constantTest() {
		var solver = MPSolver.createSolver("GLOP");
		var x = solver.makeNumVar(1, 1, "x");
		var objective = solver.objective();
		objective.setCoefficient(x, 1);

		objective.setMinimization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(1, 0.01));

		objective.setMaximization();
		assertThat(solver.solve(), is(MPSolver.ResultStatus.OPTIMAL));
		assertThat(objective.value(), closeTo(1, 0.01));
	}
}