Logo AND Algorithmique Numérique Distribuée

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