Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev-mailbox-clear' into 'master'
[simgrid.git] / teshsuite / surf / lmm_usage / lmm_usage.cpp
1 /* A few tests for the maxmin library                                       */
2
3 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "xbt/log.h"
12 #include "xbt/module.h"
13 #include "xbt/sysdep.h"
14 #include <algorithm>
15 #include <array>
16 #include <cmath>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
19
20 namespace lmm = simgrid::kernel::lmm;
21
22 #define PRINT_VAR(var) XBT_DEBUG(_XBT_STRINGIFY(var) " = %g", (var)->get_value())
23 #define SHOW_EXPR(expr) XBT_DEBUG(_XBT_STRINGIFY(expr) " = %g", (expr))
24
25 /*        ______                 */
26 /*  ==l1==  L2  ==L3==           */
27 /*        ------                 */
28
29 static void test1()
30 {
31   double a = 1.0;
32   double b = 10.0;
33
34   lmm::MaxMin Sys(false);
35   lmm::Constraint* L1 = Sys.constraint_new(nullptr, a);
36   lmm::Constraint* L2 = Sys.constraint_new(nullptr, b);
37   lmm::Constraint* L3 = Sys.constraint_new(nullptr, a);
38
39   lmm::Variable* R_1_2_3 = Sys.variable_new(nullptr, 1.0, -1.0, 3);
40   lmm::Variable* R_1     = Sys.variable_new(nullptr, 1.0, -1.0, 1);
41   lmm::Variable* R_2     = Sys.variable_new(nullptr, 1.0, -1.0, 1);
42   lmm::Variable* R_3     = Sys.variable_new(nullptr, 1.0, -1.0, 1);
43
44   Sys.update_variable_penalty(R_1_2_3, 1.0);
45   Sys.update_variable_penalty(R_1, 1.0);
46   Sys.update_variable_penalty(R_2, 1.0);
47   Sys.update_variable_penalty(R_3, 1.0);
48
49   Sys.expand(L1, R_1_2_3, 1.0);
50   Sys.expand(L2, R_1_2_3, 1.0);
51   Sys.expand(L3, R_1_2_3, 1.0);
52
53   Sys.expand(L1, R_1, 1.0);
54   Sys.expand(L2, R_2, 1.0);
55   Sys.expand(L3, R_3, 1.0);
56
57   Sys.solve();
58
59   PRINT_VAR(R_1_2_3);
60   PRINT_VAR(R_1);
61   PRINT_VAR(R_2);
62   PRINT_VAR(R_3);
63
64   Sys.variable_free(R_1_2_3);
65   Sys.variable_free(R_1);
66   Sys.variable_free(R_2);
67   Sys.variable_free(R_3);
68 }
69
70 static void test2()
71 {
72   lmm::MaxMin Sys(false);
73
74   lmm::Constraint* CPU1 = Sys.constraint_new(nullptr, 200.0);
75   lmm::Constraint* CPU2 = Sys.constraint_new(nullptr, 100.0);
76
77   lmm::Variable* T1 = Sys.variable_new(nullptr, 1.0, -1.0, 1);
78   lmm::Variable* T2 = Sys.variable_new(nullptr, 1.0, -1.0, 1);
79
80   Sys.update_variable_penalty(T1, 1.0);
81   Sys.update_variable_penalty(T2, 1.0);
82
83   Sys.expand(CPU1, T1, 1.0);
84   Sys.expand(CPU2, T2, 1.0);
85
86   Sys.solve();
87
88   PRINT_VAR(T1);
89   PRINT_VAR(T2);
90
91   Sys.variable_free(T1);
92   Sys.variable_free(T2);
93 }
94
95 static void test3()
96 {
97   constexpr int flows = 11;
98   constexpr int links = 10;
99
100   std::array<std::array<double, flows + 5>, links + 5> A;
101   /* array to add the constraints of fictitious variables */
102   std::array<double, 15> B{{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1}};
103
104   for (int i = 0; i < links + 5; i++) {
105     for (int j = 0; j < flows + 5; j++) {
106       A[i][j] = 0.0;
107       if (i >= links || j >= flows) {
108         A[i][j] = 0.0;
109       }
110     }
111   }
112
113   /*matrix that store the constraints/topology */
114   A[0][1] = A[0][7] =                                1.0;
115   A[1][1] = A[1][7] = A[1][8] =                      1.0;
116   A[2][1] = A[2][8] =                                1.0;
117   A[3][8] =                                          1.0;
118   A[4][0] = A[4][3] = A[4][9] =                      1.0;
119   A[5][0] = A[5][3] = A[5][4] = A[5][9] =            1.0;
120   A[6][0] = A[6][4] = A[6][9] = A[6][10] =           1.0;
121   A[7][2] = A[7][4] = A[7][6] = A[7][9] = A[7][10] = 1.0;
122   A[8][2] = A[8][10] =                               1.0;
123   A[9][5] = A[9][6] = A[9][9] =                      1.0;
124   A[10][11] =                                        1.0;
125   A[11][12] =                                        1.0;
126   A[12][13] =                                        1.0;
127   A[13][14] =                                        1.0;
128   A[14][15] =                                        1.0;
129
130   lmm::MaxMin Sys(false);
131
132   /* Creates the constraints */
133   std::array<lmm::Constraint*, 15> tmp_cnst;
134   for (int i = 0; i < 15; i++)
135     tmp_cnst[i] = Sys.constraint_new(nullptr, B[i]);
136
137   /* Creates the variables */
138   std::array<lmm::Variable*, 16> tmp_var;
139   for (int j = 0; j < 16; j++) {
140     tmp_var[j] = Sys.variable_new(nullptr, 1.0, -1.0, 15);
141     Sys.update_variable_penalty(tmp_var[j], 1.0);
142   }
143
144   /* Link constraints and variables */
145   for (int i = 0; i < 15; i++)
146     for (int j = 0; j < 16; j++)
147       if (A[i][j] != 0.0)
148         Sys.expand(tmp_cnst[i], tmp_var[j], 1.0);
149
150   Sys.solve();
151
152   for (int j = 0; j < 16; j++)
153     PRINT_VAR(tmp_var[j]);
154
155   for (int j = 0; j < 16; j++)
156     Sys.variable_free(tmp_var[j]);
157 }
158
159 int main(int argc, char** argv)
160 {
161   simgrid::s4u::Engine e(&argc, argv);
162   XBT_INFO("***** Test 1");
163   test1();
164
165   XBT_INFO("***** Test 2");
166   test2();
167
168   XBT_INFO("***** Test 3");
169   test3();
170
171   return 0;
172 }