RLP  1.5
main.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <getopt.h>
12 #include "main.h"
13 
18 #define POS(R,C,NC) ((R)*(NC)+(C))
19 
21 static char* errors[]={"ERROR! Function \'loadMatrix\' -> \'calloc\'.\n"
22  ,"ERROR! Function \'printRes\' -> \'newArray\'.\n"
23  ,"ERROR! Function \'printRes\' -> \'malloc\'.\n"
24  ,"ERROR! Function \'printRes\' -> \'arrayInsert\'.\n"
25  ,"ERROR! Invalid parameters.\n"
26  };
27 
28 //##############################################################################
29 
30 double* loadMatrix(Prob prob,int varc,int condc,int eqc,int type)
31 {
32  double* res,*tmp;
33  int row,col,k;
34  Exp exp;
35 
36  k=varc+condc+eqc+2;
37  res=calloc((condc+1+eqc)*(varc+condc+eqc+2),sizeof(double));
38 
39  if(!res){
40  fputs(errors[0],stderr);
41  exit(1);
42  }
43 
44  listRemoveFst(prob->exps,(void**)&exp);
45 
46  res[POS(0,k-2,k)]=0;
47  res[POS(0,k-1,k)]=0;
48 
49  for(col=0;col<k-2;col++)
50  {
51  arrayAt(exp->coefs,col,(void**)&tmp);
52  res[POS(0,col,k)]=tmp?type*(*tmp):0;
53  }
54 
55  arrayMap(exp->coefs,free);
56  arrayDelete(exp->coefs);
57  free(exp);
58 
59  for(row=1;!listRemoveFst(prob->exps,(void**)&exp);)
60  {
61  if(exp->op>-1)
62  {
63  res[POS(row,k-2,k)]=exp->rhs;
64  res[POS(row,k-1,k)]=row+varc-1;
65 
66  for(col=0;col<varc;col++)
67  {
68  arrayAt(exp->coefs,col,(void**)&tmp);
69  res[POS(row,col,k)]=tmp?*tmp:0;
70  }
71 
72  row++;
73 
74  arrayMap(exp->coefs,free);
75  arrayDelete(exp->coefs);
76  free(exp);
77  }
78 
79  if(exp->op<1)
80  {
81  res[POS(row,k-2,k)]=-1*(exp->rhs);
82  res[POS(row,k-1,k)]=row+varc-1;
83 
84  for(col=0;col<varc;col++)
85  {
86  arrayAt(exp->coefs,col,(void**)&tmp);
87  res[POS(row,col,k)]=tmp?-(*tmp):0;
88  }
89 
90  row++;
91  }
92  }
93 
94  for(row=0;row<condc;row++)
95  for(col=0;col<condc;col++)
96  res[POS(row+1,col+varc,k)]=row==col;
97 
98  return res;
99 }
100 
101 //##############################################################################
102 
103 void printRes(Array vars,double* tab,const char* varob,int varc,int condc,int type)
104 {
105  int i,k,erro;
106  double* tmp;
107  char* str;
108  Array res;
109 
110  k=varc+condc+2;
111  res=newArray(varc);
112 
113  if(!res){
114  fputs(errors[1],stderr);
115  exit(2);
116  }
117 
118  for(i=1;i<condc+1;i++)
119  if(tab[POS(i,k-1,k)]<varc)
120  {
121  tmp=malloc(sizeof(double));
122 
123  if(!tmp){
124  fputs(errors[2],stderr);
125  exit(3);
126  }
127 
128  *tmp=tab[POS(i,k-2,k)];
129  erro=arrayInsert(res,tab[POS(i,k-1,k)],tmp,0);
130 
131  if(erro){
132  fputs(errors[3],stderr);
133  exit(4);
134  }
135  }
136 
137  puts("====================");
138  printf("%s = %f\n",varob,-type*tab[POS(0,k-2,k)]);
139  puts("--------------------");
140  for(i=0;i<varc;i++)
141  {
142  arrayAt(vars,i,(void**)&str);
143  printf("%s =",str);
144  if(!arrayAt(res,i,(void**)&tmp)) printf(" %f\n",*tmp);
145  else printf(" 0.0\n");
146  }
147  puts("====================");
148 
149  arrayMap(res,free);
150  arrayDelete(res);
151 }
152 
153 //##############################################################################
154 
155 int opt(int argc,char** argv,char** in,char** out)
156 {
157  int c,res;
158 
159  static struct option options[]={{"output" ,required_argument,NULL,'o'}
160  ,{"input" ,required_argument,NULL,'i'}
161  ,{"tables" ,no_argument ,NULL,'t'}
162  ,{"help" ,no_argument ,NULL,'h'}
163  ,{"version",no_argument ,NULL,'v'}
164  ,{0,0,0,0}
165  };
166 
167  res=0;
168 
169  while((c=getopt_long(argc,argv,"o:i:thv",options,NULL))!=-1)
170  {
171  switch(c)
172  {
173  case 'o' : *out=optarg;
174  break;
175  case 'i' : *in=optarg;
176  break;
177  case 't' : res+=1;
178  break;
179  case 'h' : puts("rlp version 1.5, Copyright (C) 2006, 2009, 2015 Rui Carlos Goncalves\n"
180  "rlp comes with ABSOLUTELY NO WARRANTY. This is free software, and\n"
181  "you are welcome to redistribute it under certain conditions. See the GNU\n"
182  "General Public Licence for details.\n\n"
183  "Solve linear programming problems using simplex method.\n\n"
184  "Options:\n"
185  " --help Print this help\n"
186  " --version Print version info\n"
187  " --input=<file> Input file (default: stdin)\n"
188  " --tables Print all tables\n"
189  " --output=<file> Output file (default: stdout)"
190  );
191  res-=2;
192  break;
193  case 'v' : puts("rlp version 1.5, Copyright (C) 2006, 2009, 2015 Rui Carlos Goncalves\n"
194  "rlp comes with ABSOLUTELY NO WARRANTY. This is free software, and\n"
195  "you are welcome to redistribute it under certain conditions. See the GNU\n"
196  "General Public Licence for details."
197  );
198  res-=2;
199  break;
200  default : break;
201  }
202  }
203 
204  if(optind<argc){
205  fputs(errors[4],stderr);
206  exit(5);
207  }
208 
209  return res;
210 }
void arrayDelete(Array array)
Deletes an array.
Definition: array.c:35
Array newArray(int size)
Creates an empty array, with the specified initial capacity.
Definition: array.c:12
Array coefs
Coefficients of the variables.
Definition: read.h:23
double * loadMatrix(Prob prob, int varc, int condc, int eqc, int type)
From the info collected by the parser, creates a matrix representing the problem to solve...
Definition: main.c:30
int op
Operator of the expression.
Definition: read.h:25
int listRemoveFst(List list, void **value)
Removes the first element of a list.
Definition: list.c:158
Definition of functions used by main.
int arrayAt(Array array, int index, void **elem)
Provides the element at the specified position of an array.
Definition: array.c:95
#define POS(R, C, NC)
Given a row (R), a column (C), and the number of columns (NC) of a matrix, computes an equivalent 1D ...
Definition: main.c:18
static char * errors[]
Error messages.
Definition: main.c:21
List exps
List of expressions (objective function, and conditions).
Definition: read.h:45
int arrayMap(Array array, void(*fun)(void *))
Applies a function to the elements of an array.
Definition: array.c:149
Problem structure.
Definition: read.h:38
int opt(int argc, char **argv, char **in, char **out)
Checks the options specified by the user.
Definition: main.c:155
void printRes(Array vars, double *tab, const char *varob, int varc, int condc, int type)
Prints the result.
Definition: main.c:103
double rhs
Right-hand side of the expression.
Definition: read.h:27
Array structure.
Definition: array.h:23
int arrayInsert(Array array, int index, void *elem, int replace)
Inserts an new element at the specified position of an array.
Definition: array.c:43
Expression structure.
Definition: read.h:20

RLP © 2006, 2009, 2015   Rui Carlos Gonçalves