Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
lmm testing: Clean code
[simgrid.git] / teshsuite / surf / lmm_usage / lmm_solve.cpp
1 /* Copyright (c) 2019. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/include/catch.hpp"
7 #include "src/kernel/lmm/maxmin.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "xbt/log.h"
10
11 namespace lmm = simgrid::kernel::lmm;
12
13 TEST_CASE("kernel::lmm Single constraint shared systems", "[kernel-lmm-shared-single-sys]")
14 {
15   lmm::System* Sys = lmm::make_new_maxmin_system(false);
16
17   SECTION("Variable weight")
18   {
19     /*
20      * System under consideration:
21      * 1\times\rho_1^{1} + 1\times\rho_2^{2} + 1\times\rho_3^{3} \le 10
22      */
23
24     lmm::Constraint* sys_cnst = Sys->constraint_new(nullptr, 10);
25     lmm::Variable* sys_var_1  = Sys->variable_new(nullptr, 1, 0.0, 1);
26     lmm::Variable* sys_var_2  = Sys->variable_new(nullptr, 2, 0.0, 1);
27     lmm::Variable* sys_var_3  = Sys->variable_new(nullptr, 3, 0.0, 1);
28
29     Sys->expand(sys_cnst, sys_var_1, 1);
30     Sys->expand(sys_cnst, sys_var_2, 1);
31     Sys->expand(sys_cnst, sys_var_3, 1);
32     Sys->solve();
33
34     REQUIRE(double_equals(sys_var_1->get_value(), 5.45455, sg_maxmin_precision));
35     REQUIRE(double_equals(sys_var_2->get_value(), 2.72727, sg_maxmin_precision));
36     REQUIRE(double_equals(sys_var_3->get_value(), 1.81818, sg_maxmin_precision));
37   }
38
39   SECTION("Consumption weight")
40   {
41     /*
42      * System under consideration:
43      * 1\times\rho_1^{1} + 2\times\rho_2^{1} + 3\times\rho_3^{1} \le 10
44      */
45
46     lmm::Constraint* sys_cnst = Sys->constraint_new(nullptr, 10);
47     lmm::Variable* sys_var_1  = Sys->variable_new(nullptr, 1, 0.0, 1);
48     lmm::Variable* sys_var_2  = Sys->variable_new(nullptr, 1, 0.0, 1);
49     lmm::Variable* sys_var_3  = Sys->variable_new(nullptr, 1, 0.0, 1);
50
51     Sys->expand(sys_cnst, sys_var_1, 1);
52     Sys->expand(sys_cnst, sys_var_2, 2);
53     Sys->expand(sys_cnst, sys_var_3, 3);
54     Sys->solve();
55
56     REQUIRE(double_equals(sys_var_1->get_value(), 1.666667, sg_maxmin_precision));
57     REQUIRE(double_equals(sys_var_2->get_value(), 1.666667, sg_maxmin_precision));
58     REQUIRE(double_equals(sys_var_3->get_value(), 1.666667, sg_maxmin_precision));
59   }
60
61   SECTION("Consumption weight + variable weight")
62   {
63     /*
64      * Strange system under consideration:
65      * 56\times\rho_1^{74} + 21\times\rho_2^{6} + 2\times\rho_3^{2} \le 123
66      */
67
68     lmm::Constraint* sys_cnst = Sys->constraint_new(nullptr, 123);
69     lmm::Variable* sys_var_1  = Sys->variable_new(nullptr, 56, 0.0, 1);
70     lmm::Variable* sys_var_2  = Sys->variable_new(nullptr, 21, 0.0, 1);
71     lmm::Variable* sys_var_3  = Sys->variable_new(nullptr, 3, 0.0, 1);
72
73     Sys->expand(sys_cnst, sys_var_1, 74);
74     Sys->expand(sys_cnst, sys_var_2, 6);
75     Sys->expand(sys_cnst, sys_var_3, 2);
76     Sys->solve();
77
78     REQUIRE(double_equals(sys_var_1->get_value(), 0.9659686, sg_maxmin_precision));
79     REQUIRE(double_equals(sys_var_2->get_value(), 2.575916, sg_maxmin_precision));
80     REQUIRE(double_equals(sys_var_3->get_value(), 18.03141, sg_maxmin_precision));
81   }
82 }
83
84 TEST_CASE("kernel::lmm Multiple constraint shared systems", "[kernel-lmm-shared-multiple-sys]")
85 {
86
87   lmm::System* Sys = lmm::make_new_maxmin_system(false);
88
89   SECTION("3 Constraints system")
90   {
91
92     /*
93      * System under consideration:
94      * 4\times\rho_1^{5.1} + 2.6\times\rho_2^{7} + 1.2\times\rho_3^{8.5} \le 14.6 \\
95      * 5\times\rho_4^{6.2} + 2\times\rho_2^{7}   + 4.1\times\rho_3^{8.5} \le 40.7 \\
96      * 6\times\rho_5^1                                                   \le 7
97      */
98
99     lmm::Constraint* sys_cnst_1 = Sys->constraint_new(nullptr, 14.6);
100     lmm::Constraint* sys_cnst_2 = Sys->constraint_new(nullptr, 10.7);
101     lmm::Constraint* sys_cnst_3 = Sys->constraint_new(nullptr, 7);
102
103     lmm::Variable* sys_var_1 = Sys->variable_new(nullptr, 5.1, 0.0, 1);
104     lmm::Variable* sys_var_2 = Sys->variable_new(nullptr, 7, 0.0, 2);
105     lmm::Variable* sys_var_3 = Sys->variable_new(nullptr, 8.5, 0.0, 2);
106     lmm::Variable* sys_var_4 = Sys->variable_new(nullptr, 6.2, 0.0, 1);
107     lmm::Variable* sys_var_5 = Sys->variable_new(nullptr, 1, 0.0, 1);
108
109     // Constraint 1
110     Sys->expand(sys_cnst_1, sys_var_1, 4);
111     Sys->expand(sys_cnst_1, sys_var_2, 2.6);
112     Sys->expand(sys_cnst_1, sys_var_3, 1.2);
113     // Constraint 2
114     Sys->expand(sys_cnst_2, sys_var_4, 5);
115     Sys->expand(sys_cnst_2, sys_var_2, 2);
116     Sys->expand(sys_cnst_2, sys_var_3, 4.1);
117     // Constraint 3
118     Sys->expand(sys_cnst_3, sys_var_5, 6);
119     Sys->solve();
120
121     REQUIRE(double_equals(sys_var_1->get_value(), 2.779119, sg_maxmin_precision));
122     REQUIRE(double_equals(sys_var_2->get_value(), 0.9708181, sg_maxmin_precision));
123     REQUIRE(double_equals(sys_var_3->get_value(), 0.7994973, sg_maxmin_precision));
124     REQUIRE(double_equals(sys_var_4->get_value(), 1.096085, sg_maxmin_precision));
125     REQUIRE(double_equals(sys_var_5->get_value(), 1.166667, sg_maxmin_precision));
126   }
127 }