Logo AND Algorithmique Numérique Distribuée

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