1 /*
2  * Name:    init.c
3  * Author:  Pietro Belotti
4  * Purpose: initialize data structures of the sparse LP
5  *
6  * This code is published under the Eclipse Public License (EPL).
7  * See http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10 
11 #include <stdio.h>
12 #include <math.h>
13 #include <stdlib.h>
14 
15 
16 #ifdef RTR_MPI
17 #include <mpi.h>
18 #endif
19 
20 #include "sparse.h"
21 #include "init.h"
22 
23 /*
24  * Initialize variables
25  */
26 
init_x(sparseLP * lp,double * x)27 void init_x (sparseLP *lp, double *x) {
28 
29   double *lb = lp -> lb,
30          *ub = lp -> ub;
31 
32   if (lp -> my_id == 0) {
33 
34     register int i;
35 
36     for (i = lp->c0; i > 0; --i, ++ub, ++lb)
37 
38       if (*ub - *lb > 1e6)
39 	if (fabs (*lb) < 1e5) *x++ = *lb + drand48 ();
40 	else                  *x++ = 0;/* *ub - drand48 (); */
41       else                    *x++ = *lb + drand48 () * (*ub - *lb);
42 
43     x  -= (lp -> c0);
44     lb -= (lp -> c0);
45     ub -= (lp -> c0);
46   }
47 
48 #ifdef RTR_MPI
49   MPI_Bcast (x, lp->c0, MPI_DOUBLE, 0, MPI_COMM_WORLD);
50 #endif
51 }
52 
53 
54 /*
55  * Initialize auxiliary vectors based on x
56  */
57 
init_sat(sparseLP * lp,char * sat,double * b_Ax,double * x,double * sumViol)58 int init_sat (sparseLP *lp,   /* LP data                                                     */
59 	      char *sat,      /* sat [j] is 1 if j-th constraint fulfilled, 0 otherwise      */
60 	      double *b_Ax,   /* b_Ax [j] is the (positive) violation of the j-th constraint */
61 	      double *x,      /* initial solution                                            */
62 	      double *sumViol /* sum of all violations                                       */
63 	      ) {
64 
65   register int i;
66 
67   int      ns = 0;
68 
69   double **ic  = lp -> ic, *coe;
70   int    **ip  = lp -> ip, *pos;
71 
72   double  *rhs = lp -> rhs;
73   int     *il  = lp -> il;
74 
75   double z;
76 
77   *sumViol = 0;
78 
79   for (i=lp->rk; i--;) {
80 
81     coe =   *  ic ++;
82     pos =   *  ip ++;
83     z   = - * rhs ++;
84 
85 #ifdef RTR_USE_PRAGMAS
86     calc_lhs (&z, *il++, coe, x, pos);
87 #else
88     {
89       register int j;
90       for (j = *il++; j>0; j--)
91 	z += *coe++ * x [*pos++];
92     }
93 #endif
94 
95     *b_Ax++ = -z;
96 
97     if (z<0) {
98 
99       *sat++ = UNSATD;
100       *sumViol -= z;
101     }
102     else {
103       *sat++ = SATD;
104       ns ++;
105     }
106   }
107 
108   return ns;
109 }
110