Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added the test of lagrange with optimal step, also called lagrange
[simgrid.git] / testsuite / surf / simeng_usage.c
1 /*      $Id$     */
2
3 /* A few tests for the maxmin library                                       */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifdef __BORLANDC__
11 #pragma hdrstop
12 #endif
13
14 #include "xbt/sysdep.h"
15 #include "surf/maxmin.h"
16
17 #include "xbt/log.h"
18 #include "xbt/module.h"
19
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,"Messages specific for surf example");
22
23 #define PRINT_VAR(var) DEBUG1(#var " = %g",lmm_variable_getvalue(var));
24
25 /*                               */
26 /*        ______                 */
27 /*  ==l1==  L2  ==L3==           */
28 /*        ------                 */
29 /*                               */
30
31 typedef enum {
32   MAXMIN,
33   SDP,
34   LAGRANGE,
35   LAGRANGEDICO
36 } method_t;
37
38 void test1(method_t method);
39 void test1(method_t method)
40 {
41   lmm_system_t Sys = NULL ;
42   lmm_constraint_t L1 = NULL;
43   lmm_constraint_t L2 = NULL;
44   lmm_constraint_t L3 = NULL;
45
46   lmm_variable_t R_1_2_3 = NULL;
47   lmm_variable_t R_1 = NULL;
48   lmm_variable_t R_2 = NULL;
49   lmm_variable_t R_3 = NULL;
50
51   Sys = lmm_system_new();
52   L1 = lmm_constraint_new(Sys, (void *) "L1", 1.0);
53   L2 = lmm_constraint_new(Sys, (void *) "L2", 10.0);
54   L3 = lmm_constraint_new(Sys, (void *) "L3", 1.0);
55
56   R_1_2_3 = lmm_variable_new(Sys, (void *) "R 1->2->3", 1.0 , -1.0 , 3);
57   R_1 = lmm_variable_new(Sys, (void *) "R 1", 1.0 , -1.0 , 1);
58   R_2 = lmm_variable_new(Sys, (void *) "R 2", 1.0 , -1.0 , 1);
59   R_3 = lmm_variable_new(Sys, (void *) "R 3", 1.0 , -1.0 , 1);
60
61   lmm_expand(Sys, L1, R_1_2_3, 1.0);
62   lmm_expand(Sys, L2, R_1_2_3, 1.0);
63   lmm_expand(Sys, L3, R_1_2_3, 1.0);
64
65   lmm_expand(Sys, L1, R_1, 1.0);
66
67   lmm_expand(Sys, L2, R_2, 1.0);
68
69   lmm_expand(Sys, L3, R_3, 1.0);
70
71   PRINT_VAR(R_1_2_3);
72   PRINT_VAR(R_1);
73   PRINT_VAR(R_2);
74   PRINT_VAR(R_3);
75
76   DEBUG0("\n");
77   if(method==MAXMIN)
78     lmm_solve(Sys);
79 #ifdef HAVE_SDP
80   else if(method==SDP)
81     sdp_solve(Sys);    
82 #endif
83   else if(method==LAGRANGE)
84     lagrange_solve(Sys);  
85   else if(method==LAGRANGEDICO)
86     lagrange_dicotomi_solve(Sys);  
87   else 
88     xbt_assert0(0,"Invalid method");
89
90   PRINT_VAR(R_1_2_3);
91   PRINT_VAR(R_1);
92   PRINT_VAR(R_2);
93   PRINT_VAR(R_3);
94 /*   DEBUG0("\n"); */
95
96 /*   lmm_update_variable_weight(Sys,R_1_2_3,.5); */
97 /*   lmm_solve(Sys); */
98
99 /*   PRINT_VAR(R_1_2_3); */
100 /*   PRINT_VAR(R_1); */
101 /*   PRINT_VAR(R_2); */
102 /*   PRINT_VAR(R_3); */
103
104   lmm_system_free(Sys);
105
106
107 void test2(method_t method);
108 void test2(method_t method)
109 {
110   lmm_system_t Sys = NULL ;
111   lmm_constraint_t CPU1 = NULL;
112   lmm_constraint_t CPU2 = NULL;
113
114   lmm_variable_t T1 = NULL;
115   lmm_variable_t T2 = NULL;
116
117   Sys = lmm_system_new();
118   CPU1 = lmm_constraint_new(Sys, (void *) "CPU1", 200.0);
119   CPU2 = lmm_constraint_new(Sys, (void *) "CPU2", 100.0);
120
121   T1 = lmm_variable_new(Sys, (void *) "T1", 1.0 , -1.0 , 1);
122   T2 = lmm_variable_new(Sys, (void *) "T2", 1.0 , -1.0 , 1);
123
124   lmm_expand(Sys, CPU1, T1, 1.0);
125   lmm_expand(Sys, CPU2, T2, 1.0);
126
127   PRINT_VAR(T1);
128   PRINT_VAR(T2);
129
130   DEBUG0("\n");
131   if(method==MAXMIN)
132     lmm_solve(Sys);
133 #ifdef HAVE_SDP
134   else if(method==SDP)
135     sdp_solve(Sys);    
136 #endif
137   else if(method==LAGRANGE)
138     lagrange_solve(Sys);  
139   else 
140     xbt_assert0(0,"Invalid method");
141
142   PRINT_VAR(T1);
143   PRINT_VAR(T2);
144
145   DEBUG0("\n");
146
147   lmm_system_free(Sys);
148 }
149
150 void test3(method_t method);
151 void test3(method_t method)
152 {
153   int flows=11;  //flows and conexions are synonnims ?
154   int links=10;  //topology info
155   
156   //just to be carefull
157   int i = 0;
158   int j = 0;
159
160   double **A;
161
162   lmm_system_t Sys = NULL ;
163   lmm_constraint_t *tmp_cnst = NULL;
164   lmm_variable_t   *tmp_var  = NULL;
165   char tmp_name[13];
166
167
168   /*array to add the the constraints of fictiv variables */
169   double B[15] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
170                 1, 1, 1, 1, 1};
171
172   A = (double **)calloc(links+5, sizeof(double));
173  
174   for(i=0 ; i< links+5; i++){
175     A[i] = (double *)calloc(flows+5, sizeof(double));
176
177     for(j=0 ; j< flows+5; j++){
178       A[i][j] = 0.0;
179
180       if(i >= links || j >= flows){
181           A[i][j] = 0.0;
182       }
183     }
184   }
185
186   /*matrix that store the constraints/topollogy*/
187   /*double A[15][16]=
188     {{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,    0, 0, 0, 0, 0},
189      {0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,    0, 0, 0, 0, 0},
190      {0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
191      {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
192      {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
193      {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
194      {1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,    0, 0, 0, 0, 0},
195      {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1,    0, 0, 0, 0, 0},
196      {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,    0, 0, 0, 0, 0},
197      {0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,    0, 0, 0, 0, 0},
198      
199      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    1, 0, 0, 0, 0},
200      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 1, 0, 0, 0},
201      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0},
202      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 1, 0},
203      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 1}
204      }; */
205
206   A[0][1]  = 1.0;
207   A[0][7]  = 1.0;
208   
209   A[1][1]  = 1.0;
210   A[1][7]  = 1.0;
211   A[1][8]  = 1.0;
212
213   A[2][1]  = 1.0;
214   A[2][8]  = 1.0;
215
216   A[2][1]  = 1.0;
217   A[2][8]  = 1.0;
218
219   A[3][8]  = 1.0;
220
221   A[4][0]  = 1.0;
222   A[4][3]  = 1.0;
223   A[4][9]  = 1.0;
224
225   A[5][0]  = 1.0;
226   A[5][3]  = 1.0;
227   A[5][4]  = 1.0;
228   A[5][9]  = 1.0;
229
230   A[6][0]  = 1.0;
231   A[6][4]  = 1.0;
232   A[6][9]  = 1.0;
233   A[6][10] = 1.0;
234
235   A[7][2]  = 1.0;
236   A[7][4]  = 1.0;
237   A[7][6]  = 1.0;
238   A[7][9]  = 1.0;
239   A[7][10] = 1.0;
240
241   A[8][2]  = 1.0;
242   A[8][10] = 1.0;
243
244   A[9][5]  = 1.0;
245   A[9][6]  = 1.0;
246   A[9][9]  = 1.0;
247
248   
249   A[10][11] = 1.0;
250   A[11][12] = 1.0;
251   A[12][13] = 1.0;
252   A[13][14] = 1.0;
253   A[14][15] = 1.0;
254
255
256   Sys = lmm_system_new();
257   
258   
259   /*
260    * Creates the constraints
261    */
262   tmp_cnst = calloc(15, sizeof(lmm_constraint_t));
263   for(i=0; i<15; i++){
264     sprintf(tmp_name, "C_%03d", i); 
265     tmp_cnst[i] = lmm_constraint_new(Sys, (void *) tmp_name, B[i]);
266   }
267
268
269   /*
270    * Creates the variables
271    */
272   tmp_var = calloc(16, sizeof(lmm_variable_t));
273   for(j=0; j<16; j++){
274     sprintf(tmp_name, "X_%03d", j); 
275     tmp_var[j] = lmm_variable_new(Sys, (void *) tmp_name, 1.0, -1.0 , 15);
276   }
277
278   /*
279    * Link constraints and variables
280    */
281   for(i=0;i<15;i++) { 
282     for(j=0; j<16; j++){
283       if(A[i][j]){
284         lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); 
285       }
286     }
287   }
288
289   for(j=0; j<16; j++){
290     PRINT_VAR(tmp_var[j]);
291   }
292
293   if(method==MAXMIN)
294     lmm_solve(Sys);
295 #ifdef HAVE_SDP
296   else if(method==SDP)
297     sdp_solve(Sys);    
298 #endif
299   else if(method==LAGRANGE)
300     lagrange_solve(Sys);  
301   else 
302     xbt_assert0(0,"Invalid method");
303
304   for(j=0; j<16; j++){
305     PRINT_VAR(tmp_var[j]);
306   }
307
308   free(tmp_var);
309   free(tmp_cnst);
310   lmm_system_free(Sys);
311 }
312
313 #ifdef __BORLANDC__
314 #pragma argsused
315 #endif
316
317 int main(int argc, char **argv)
318 {
319   xbt_init(&argc,argv);
320
321 /*   DEBUG0("***** Test 1 (Max-Min) ***** \n"); */
322 /*   test1(MAXMIN); */
323 #ifdef HAVE_SDP
324   DEBUG0("***** Test 1 (SDP) ***** \n");
325   test1(SDP);
326 #endif
327   DEBUG0("***** Test 1 (Lagrange - dicotomi) ***** \n");
328   test1(LAGRANGEDICO);
329
330
331 /*   DEBUG0("***** Test 2 (Max-Min) ***** \n"); */
332 /*   test2(MAXMIN); */
333 /* #ifdef HAVE_SDP */
334 /*   DEBUG0("***** Test 2 (SDP) ***** \n"); */
335 /*   test2(SDP); */
336 /* #endif */
337 /*   DEBUG0("***** Test 2 (Lagrange) ***** \n"); */
338 /*   test2(LAGRANGE); */
339
340
341
342 /*   DEBUG0("***** Test 3 (Max-Min) ***** \n"); */
343 /*   test3(MAXMIN); */
344 /* #ifdef HAVE_SDP */
345 /*   DEBUG0("***** Test 3 (SDP) ***** \n"); */
346 /*   test3(SDP); */
347 /* #endif */
348 /*   DEBUG0("***** Test 3 (Lagrange) ***** \n"); */
349 /*   test3(LAGRANGE); */
350
351
352   return 0;
353 }