Debian Codegraph @

tests/SimplexTest.cpp

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8
9 #include "kiwi/strength.h"
10 #include "kiwi/variable.h"
11 #include <gtest/gtest.h>
12 #include <kiwi/kiwi.h>
13
14 using namespace kiwi;
15
16 // https://edshare.soton.ac.uk/2458/1/MA230exam98_1.pdf
17 TEST(SimplexTest, Maximization) {
18 const auto x1 = Variable("x1");
19 const auto x2 = Variable("x2");
20 const auto x3 = Variable("x3");
21 const auto z = Variable("z");
22
23 Solver solver;
24 // put base constraints
25 solver.addConstraint(x1 >= 0);
26 solver.addConstraint(x2 >= 0);
27 solver.addConstraint(x3 >= 0);
28 // put in constraints
29 solver.addConstraint(2 * x1 - 5 * x2 <= 11);
30 solver.addConstraint(-x1 + 3 * x2 + x3 == 7);
31 solver.addConstraint(x1 - 8 * x2 + 4 * x3 >= 33);
32 // tell solver to maximize
33 solver.addConstraint(z == -2 * x1 + 7 * x2 + 4 * x3);
34 solver.addEditVariable(z, strength::weak);
35 solver.suggestValue(z, 1e+6);
36 // Solve
37 solver.updateVariables();
38
39 EXPECT_NEAR(x1.value(), 13.0, 1e-4);
40 EXPECT_NEAR(x2.value(), 3.0, 1e-4);
41 EXPECT_NEAR(x3.value(), 11.0, 1e-4);
42 EXPECT_NEAR(z.value(), 39.0, 1e-4);
43 }