Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Moving sdp tests in maxmin_usage (to have the same set of tests).
[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   A = (double **)calloc(links+5, sizeof(double));
152  
153   for(i=0 ; i< links+5; i++){
154     A[i] = (double *)calloc(flows+5, sizeof(double));
155
156     for(j=0 ; j< flows+5; j++){
157       A[i][j] = 0.0;
158
159       if(i >= links || j >= flows){
160           A[i][j] = 0.0;
161       }
162     }
163   }
164
165   //matrix that store the constraints/topollogy
166   /*double A[15][16]=
167     {{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,    0, 0, 0, 0, 0},
168      {0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,    0, 0, 0, 0, 0},
169      {0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
170      {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,    0, 0, 0, 0, 0},
171      {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
172      {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0,    0, 0, 0, 0, 0},
173      {1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,    0, 0, 0, 0, 0},
174      {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1,    0, 0, 0, 0, 0},
175      {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,    0, 0, 0, 0, 0},
176      {0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,    0, 0, 0, 0, 0},
177      
178      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    1, 0, 0, 0, 0},
179      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 1, 0, 0, 0},
180      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0},
181      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 1, 0},
182      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 1}
183      }; */
184
185   A[0][1]  = 1.0;
186   A[0][7]  = 1.0;
187   
188   A[1][1]  = 1.0;
189   A[1][7]  = 1.0;
190   A[1][8]  = 1.0;
191
192   A[2][1]  = 1.0;
193   A[2][8]  = 1.0;
194
195   A[2][1]  = 1.0;
196   A[2][8]  = 1.0;
197
198   A[3][8]  = 1.0;
199
200   A[4][0]  = 1.0;
201   A[4][3]  = 1.0;
202   A[4][9]  = 1.0;
203
204   A[5][0]  = 1.0;
205   A[5][3]  = 1.0;
206   A[5][4]  = 1.0;
207   A[5][9]  = 1.0;
208
209   A[6][0]  = 1.0;
210   A[6][4]  = 1.0;
211   A[6][9]  = 1.0;
212   A[6][10] = 1.0;
213
214   A[7][2]  = 1.0;
215   A[7][4]  = 1.0;
216   A[7][6]  = 1.0;
217   A[7][9]  = 1.0;
218   A[7][10] = 1.0;
219
220   A[8][2]  = 1.0;
221   A[8][10] = 1.0;
222
223   A[9][5]  = 1.0;
224   A[9][6]  = 1.0;
225   A[9][9]  = 1.0;
226
227   
228   A[10][11] = 1.0;
229   A[11][12] = 1.0;
230   A[12][13] = 1.0;
231   A[13][14] = 1.0;
232   A[14][15] = 1.0;
233
234   //array to add the the constraints of fictiv variables
235   double B[15] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
236             1, 1, 1, 1, 1};
237
238
239   lmm_system_t Sys = NULL ;
240   lmm_constraint_t *tmp_cnst = NULL;
241   lmm_variable_t   *tmp_var  = NULL;
242   char tmp_name[13];
243
244
245   Sys = lmm_system_new();
246   
247   
248   /*
249    * Creates the constraints
250    */
251   tmp_cnst = calloc(15, sizeof(lmm_constraint_t));
252   for(i=0; i<15; i++){
253     sprintf(tmp_name, "C_%03d", i); 
254     tmp_cnst[i] = lmm_constraint_new(Sys, (void *) tmp_name, B[i]);
255   }
256
257
258   /*
259    * Creates the variables
260    */
261   tmp_var = calloc(16, sizeof(lmm_variable_t));
262   for(j=0; j<16; j++){
263     sprintf(tmp_name, "X_%03d", j); 
264     tmp_var[j] = lmm_variable_new(Sys, (void *) tmp_name, 1.0, -1.0 , 15);
265   }
266
267   /*
268    * Link constraints and variables
269    */
270   for(i=0;i<15;i++) { 
271     for(j=0; j<16; j++){
272       if(A[i][j]){
273         lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); 
274       }
275     }
276   }
277
278   for(j=0; j<16; j++){
279     PRINT_VAR(tmp_var[j]);
280   }
281
282   if(method==MAXMIN)
283     lmm_solve(Sys);
284 #ifdef HAVE_SDP
285   else if(method==SDP)
286     sdp_solve(Sys);    
287 #endif
288   else 
289     xbt_assert0(0,"Invalid method");
290
291   for(j=0; j<16; j++){
292     PRINT_VAR(tmp_var[j]);
293   }
294
295   free(tmp_var);
296   free(tmp_cnst);
297   lmm_system_free(Sys);
298 }
299
300 #ifdef __BORLANDC__
301 #pragma argsused
302 #endif
303
304 int main(int argc, char **argv)
305 {
306   xbt_init(&argc,argv);
307
308   DEBUG0("***** Test 1 (Max-Min) ***** \n");
309   test1(MAXMIN);
310 #ifdef HAVE_SDP
311   DEBUG0("***** Test 1 (SDP) ***** \n");
312   test1(SDP);
313 #endif
314   DEBUG0("***** Test 2 (Max-Min) ***** \n");
315   test2(MAXMIN);
316 #ifdef HAVE_SDP
317   DEBUG0("***** Test 2 (SDP) ***** \n");
318   test2(SDP);
319 #endif
320   DEBUG0("***** Test 3 (Max-Min) ***** \n");
321   test3(MAXMIN);
322 #ifdef HAVE_SDP
323   DEBUG0("***** Test 3 (SDP) ***** \n");
324   test3(SDP);
325 #endif
326
327   return 0;
328 }