File: lmm_usage.cpp

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (170 lines) | stat: -rw-r--r-- 4,508 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* A few tests for the maxmin library                                       */

/* Copyright (c) 2007-2025. The SimGrid Team. All rights reserved.          */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

#include "simgrid/s4u/Engine.hpp"
#include "src/kernel/lmm/maxmin.hpp"
#include "xbt/log.h"
#include "xbt/sysdep.h"
#include <algorithm>
#include <array>
#include <cmath>

XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Messages specific for this example");

namespace lmm = simgrid::kernel::lmm;

#define PRINT_VAR(var) XBT_DEBUG(_XBT_STRINGIFY(var) " = %g", (var)->get_value())
#define SHOW_EXPR(expr) XBT_DEBUG(_XBT_STRINGIFY(expr) " = %g", (expr))

/*        ______                 */
/*  ==l1==  L2  ==L3==           */
/*        ------                 */

static void test1()
{
  double a = 1.0;
  double b = 10.0;

  lmm::MaxMin Sys(false);
  lmm::Constraint* L1 = Sys.constraint_new(nullptr, a);
  lmm::Constraint* L2 = Sys.constraint_new(nullptr, b);
  lmm::Constraint* L3 = Sys.constraint_new(nullptr, a);

  lmm::Variable* R_1_2_3 = Sys.variable_new(nullptr, 1.0, -1.0, 3);
  lmm::Variable* R_1     = Sys.variable_new(nullptr, 1.0, -1.0, 1);
  lmm::Variable* R_2     = Sys.variable_new(nullptr, 1.0, -1.0, 1);
  lmm::Variable* R_3     = Sys.variable_new(nullptr, 1.0, -1.0, 1);

  Sys.update_variable_penalty(R_1_2_3, 1.0);
  Sys.update_variable_penalty(R_1, 1.0);
  Sys.update_variable_penalty(R_2, 1.0);
  Sys.update_variable_penalty(R_3, 1.0);

  Sys.expand(L1, R_1_2_3, 1.0);
  Sys.expand(L2, R_1_2_3, 1.0);
  Sys.expand(L3, R_1_2_3, 1.0);

  Sys.expand(L1, R_1, 1.0);
  Sys.expand(L2, R_2, 1.0);
  Sys.expand(L3, R_3, 1.0);

  Sys.solve();

  PRINT_VAR(R_1_2_3);
  PRINT_VAR(R_1);
  PRINT_VAR(R_2);
  PRINT_VAR(R_3);

  Sys.variable_free(R_1_2_3);
  Sys.variable_free(R_1);
  Sys.variable_free(R_2);
  Sys.variable_free(R_3);
}

static void test2()
{
  lmm::MaxMin Sys(false);

  lmm::Constraint* CPU1 = Sys.constraint_new(nullptr, 200.0);
  lmm::Constraint* CPU2 = Sys.constraint_new(nullptr, 100.0);

  lmm::Variable* T1 = Sys.variable_new(nullptr, 1.0, -1.0, 1);
  lmm::Variable* T2 = Sys.variable_new(nullptr, 1.0, -1.0, 1);

  Sys.update_variable_penalty(T1, 1.0);
  Sys.update_variable_penalty(T2, 1.0);

  Sys.expand(CPU1, T1, 1.0);
  Sys.expand(CPU2, T2, 1.0);

  Sys.solve();

  PRINT_VAR(T1);
  PRINT_VAR(T2);

  Sys.variable_free(T1);
  Sys.variable_free(T2);
}

static void test3()
{
  constexpr int flows = 11;
  constexpr int links = 10;

  std::array<std::array<double, flows + 5>, links + 5> A;
  /* array to add the constraints of fictitious variables */
  std::array<double, 15> B{{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 1, 1, 1}};

  for (int i = 0; i < links + 5; i++) {
    for (int j = 0; j < flows + 5; j++) {
      A[i][j] = 0.0;
      if (i >= links || j >= flows) {
        A[i][j] = 0.0;
      }
    }
  }

  /*matrix that store the constraints/topology */
  A[0][1] = A[0][7] = 1.0;
  A[1][1] = A[1][7] = A[1][8] = 1.0;
  A[2][1] = A[2][8] = 1.0;
  A[3][8]           = 1.0;
  A[4][0] = A[4][3] = A[4][9] = 1.0;
  A[5][0] = A[5][3] = A[5][4] = A[5][9] = 1.0;
  A[6][0] = A[6][4] = A[6][9] = A[6][10] = 1.0;
  A[7][2] = A[7][4] = A[7][6] = A[7][9] = A[7][10] = 1.0;
  A[8][2] = A[8][10] = 1.0;
  A[9][5] = A[9][6] = A[9][9] = 1.0;
  A[10][11]                   = 1.0;
  A[11][12]                   = 1.0;
  A[12][13]                   = 1.0;
  A[13][14]                   = 1.0;
  A[14][15]                   = 1.0;

  lmm::MaxMin Sys(false);

  /* Creates the constraints */
  std::array<lmm::Constraint*, 15> tmp_cnst;
  for (int i = 0; i < 15; i++)
    tmp_cnst[i] = Sys.constraint_new(nullptr, B[i]);

  /* Creates the variables */
  std::array<lmm::Variable*, 16> tmp_var;
  for (int j = 0; j < 16; j++) {
    tmp_var[j] = Sys.variable_new(nullptr, 1.0, -1.0, 15);
    Sys.update_variable_penalty(tmp_var[j], 1.0);
  }

  /* Link constraints and variables */
  for (int i = 0; i < 15; i++)
    for (int j = 0; j < 16; j++)
      if (A[i][j] != 0.0)
        Sys.expand(tmp_cnst[i], tmp_var[j], 1.0);

  Sys.solve();

  for (int j = 0; j < 16; j++)
    PRINT_VAR(tmp_var[j]);

  for (int j = 0; j < 16; j++)
    Sys.variable_free(tmp_var[j]);
}

int main(int argc, char** argv)
{
  simgrid::s4u::Engine e(&argc, argv);
  XBT_INFO("***** Test 1");
  test1();

  XBT_INFO("***** Test 2");
  test2();

  XBT_INFO("***** Test 3");
  test3();

  return 0;
}