source: trunk/Test/Cbc_ampl.cpp @ 242

Last change on this file since 242 was 242, checked in by forrest, 14 years ago

infeasible on preprocessing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1/****************************************************************
2Copyright (C) 1997-2000 Lucent Technologies
3Modifications for Coin -  Copyright (C) 2006, International Business Machines Corporation and others.
4All Rights Reserved
5
6Permission to use, copy, modify, and distribute this software and
7its documentation for any purpose and without fee is hereby
8granted, provided that the above copyright notice appear in all
9copies and that both that the copyright notice and this
10permission notice and warranty disclaimer appear in supporting
11documentation, and that the name of Lucent or any of its entities
12not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior
14permission.
15
16LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23THIS SOFTWARE.
24****************************************************************/
25#ifdef CBC_AMPL
26#include "getstub.h"
27#include "Cbc_ampl.h"
28#include "unistd.h"
29#include <string>
30static char*
31clpCheck(Option_Info *oi, keyword *kw, char *v)
32{
33  char *rv=NULL;
34  printf("string %s\n",v);
35  return rv;
36}
37/* so decodePhrase can access */
38static ampl_info * saveInfo=NULL;
39static fint
40decodePhrase(char * phrase,ftnlen length)
41{
42  char * blank = strchr(phrase,' ');
43  if (blank) {
44    /* split arguments */
45    *blank='\0';
46    saveInfo->arguments=(char **) realloc(saveInfo->arguments,(saveInfo->numberArguments+2)*sizeof(char *));
47    saveInfo->arguments[saveInfo->numberArguments++]=strdup(phrase);
48    *blank=' ';
49    phrase=blank+1; /* move on */
50    saveInfo->arguments[saveInfo->numberArguments++]=strdup(phrase);
51  } else {
52    saveInfo->arguments=(char **) realloc(saveInfo->arguments,(saveInfo->numberArguments+1)*sizeof(char *));
53    saveInfo->arguments[saveInfo->numberArguments++]=strdup(phrase);
54  }
55  return 0;
56}
57static char xxxxxx[20];
58#define VP (char*)
59 static keyword keywds[] = { /* must be sorted */
60        { "aaaa",       clpCheck,               (char *) xxxxxx },
61        { "wantsol",    WS_val,         NULL, "write .sol file (without -AMPL)" }
62        };
63static Option_Info Oinfo = {"cbc", "Cbc 1.01", "cbc_options", keywds, nkeywds, 0, "",
64                                0,decodePhrase,0,0,0, 20060130 };
65// strdup used to avoid g++ compiler warning
66 static SufDecl
67suftab[] = {
68#if 0
69        { "current", 0, ASL_Sufkind_con | ASL_Sufkind_outonly },
70        { "current", 0, ASL_Sufkind_var | ASL_Sufkind_outonly },
71        { "direction", 0, ASL_Sufkind_var },
72        { "down", 0, ASL_Sufkind_con | ASL_Sufkind_outonly },
73        { "down", 0, ASL_Sufkind_var | ASL_Sufkind_outonly },
74        { "priority", 0, ASL_Sufkind_var },
75        { "ref", 0, ASL_Sufkind_var | ASL_Sufkind_real },
76        { "sos", 0, ASL_Sufkind_var },
77        { "sos", 0, ASL_Sufkind_con },
78        { "sosno", 0, ASL_Sufkind_var | ASL_Sufkind_real },
79        { "sosref", 0, ASL_Sufkind_var | ASL_Sufkind_real },
80#endif
81        { strdup("sstatus"), 0, ASL_Sufkind_var, 0 },
82        { strdup("sstatus"), 0, ASL_Sufkind_con, 0 }
83#if 0
84        { "unbdd", 0, ASL_Sufkind_var | ASL_Sufkind_outonly},
85        { "up", 0, ASL_Sufkind_con | ASL_Sufkind_outonly },
86        { "up", 0, ASL_Sufkind_var | ASL_Sufkind_outonly }
87#endif
88        };
89#include "float.h"
90#include "limits.h"
91static ASL *asl=NULL;
92static FILE *nl=NULL;
93
94static void
95stat_map(int *stat, int n, int *map, int mx, const char *what)
96{
97  int bad, i, i1=0, j, j1=0;
98  static char badfmt[] = "Coin driver: %s[%d] = %d\n";
99 
100  for(i = bad = 0; i < n; i++) {
101    if ((j = stat[i]) >= 0 && j <= mx)
102      stat[i] = map[j];
103    else {
104      stat[i] = 0;
105      i1 = i;
106      j1 = j;
107      if (!bad++)
108        fprintf(Stderr, badfmt, what, i, j);
109    }
110  }
111  if (bad > 1) {
112    if (bad == 2)
113      fprintf(Stderr, badfmt, what, i1, j1);
114    else
115      fprintf(Stderr,
116              "Coin driver: %d messages about bad %s values suppressed.\n",
117              bad-1, what);
118  }
119}
120int
121readAmpl(ampl_info * info, int argc, char **argv)
122{
123  char *stub;
124  ograd *og;
125  int i;
126  SufDesc *csd;
127  SufDesc *rsd;
128  /*bool *basis, *lower;*/
129  /*double *LU, *c, lb, objadj, *rshift, *shift, t, ub, *x, *x0, *x1;*/
130  char * environment = getenv("cbc_options");
131  char tempBuffer[20];
132  double * obj;
133  double * columnLower;
134  double * columnUpper;
135  double * rowLower;
136  double * rowUpper;
137  char ** saveArgv=argv;
138  int saveArgc = argc;
139  memset(info,0,sizeof(ampl_info));
140  /* save so can be accessed by decodePhrase */
141  saveInfo = info;
142  info->numberArguments=0;
143  info->arguments=(char **) malloc(2*sizeof(char *));
144  info->arguments[info->numberArguments++]=strdup("ampl");
145  info->arguments[info->numberArguments++]=strdup("cbc");
146  asl = ASL_alloc(ASL_read_f);
147  stub = getstub(&argv, &Oinfo);
148  if (!stub)
149    usage_ASL(&Oinfo, 1);
150  nl = jac0dim(stub, 0);
151  /*void * specialOrderedInfo = sos_add(nl,0);*/
152  suf_declare(suftab, sizeof(suftab)/sizeof(SufDecl));
153 
154  /* set A_vals to get the constraints column-wise (malloc so can be freed) */
155  A_vals = (double *) malloc(nzc*sizeof(double));
156  if (!A_vals) {
157    printf("no memory\n");
158    return 1;
159  }
160  /* say we want primal solution */
161  want_xpi0=1;
162  /* for basis info */
163  info->columnStatus = (int *) malloc(n_var*sizeof(int));
164  info->rowStatus = (int *) malloc(n_con*sizeof(int));
165  csd = suf_iput("sstatus", ASL_Sufkind_var, info->columnStatus);
166  rsd = suf_iput("sstatus", ASL_Sufkind_con, info->rowStatus);
167  /* read linear model*/
168  f_read(nl,0);
169
170  /*sos_finish(&specialOrderedInfo, 0, &j, 0, 0, 0, 0, 0);*/
171  Oinfo.uinfo = tempBuffer;
172  if (getopts(argv, &Oinfo))
173    return 1;
174  /* objective*/
175  obj = (double *) malloc(n_var*sizeof(double));
176  for (i=0;i<n_var;i++)
177    obj[i]=0.0;;
178  if (n_obj) {
179    for (og = Ograd[0];og;og = og->next)
180      obj[og->varno] = og->coef;
181  }
182  if (objtype[0])
183    info->direction=-1.0;
184  else
185    info->direction=1.0;
186  info->offset=objconst(0);
187  /* Column bounds*/
188  columnLower = (double *) malloc(n_var*sizeof(double));
189  columnUpper = (double *) malloc(n_var*sizeof(double));
190#define COIN_DBL_MAX DBL_MAX
191  for (i=0;i<n_var;i++) {
192    columnLower[i]=LUv[2*i];
193    if (columnLower[i]<= negInfinity)
194      columnLower[i]=-COIN_DBL_MAX;
195    columnUpper[i]=LUv[2*i+1];
196    if (columnUpper[i]>= Infinity)
197      columnUpper[i]=COIN_DBL_MAX;
198  }
199  /* Row bounds*/
200  rowLower = (double *) malloc(n_con*sizeof(double));
201  rowUpper = (double *) malloc(n_con*sizeof(double));
202  for (i=0;i<n_con;i++) {
203    rowLower[i]=LUrhs[2*i];
204    if (rowLower[i]<= negInfinity)
205      rowLower[i]=-COIN_DBL_MAX;
206    rowUpper[i]=LUrhs[2*i+1];
207    if (rowUpper[i]>= Infinity)
208      rowUpper[i]=COIN_DBL_MAX;
209  }
210  info->numberRows=n_con;
211  info->numberColumns=n_var;
212  info->numberElements=nzc;;
213  info->numberBinary=nbv;
214  info->numberIntegers=niv;
215  info->objective=obj;
216  info->rowLower=rowLower;
217  info->rowUpper=rowUpper;
218  info->columnLower=columnLower;
219  info->columnUpper=columnUpper;
220  info->starts=A_colstarts;
221  /*A_colstarts=NULL;*/
222  info->rows=A_rownos;
223  /*A_rownos=NULL;*/
224  info->elements=A_vals;
225  /*A_vals=NULL;*/
226  info->primalSolution=NULL;
227  /* put in primalSolution if exists */
228  if (X0) {
229    info->primalSolution=(double *) malloc(n_var*sizeof(double));
230    memcpy(info->primalSolution,X0,n_var*sizeof(double));
231  }
232  info->dualSolution=NULL;
233  if ((!(niv+nbv)&&(csd->kind & ASL_Sufkind_input))
234      ||(rsd->kind & ASL_Sufkind_input)) {
235    /* convert status - need info on map */
236    static int map[] = {1, 3, 1, 1, 2, 1, 1};
237    stat_map(info->columnStatus, n_var, map, 6, "incoming columnStatus");
238    stat_map(info->rowStatus, n_con, map, 6, "incoming rowStatus");
239  } else {
240    /* all slack basis */
241    free(info->rowStatus);
242    info->rowStatus=NULL;
243    free(info->columnStatus);
244    info->columnStatus=NULL;
245  }
246  /* add -solve - unless something there already
247   - also check for sleep=yes */
248  {
249    int found=0;
250    int foundLog=0;
251    int foundSleep=0;
252    const char * something[]={"solve","branch","duals","primals"};
253    for (i=0;i<info->numberArguments;i++) {
254      unsigned int j;
255      const char * argument = info->arguments[i];
256      for (j=0;j<sizeof(something)/sizeof(char *);j++) {
257        const char * check = something[j];
258        if (!strncmp(argument,check,sizeof(check))) {
259          found=(int)(j+1);
260        } else if (!strncmp(argument,"log",3)) {
261          foundLog=1;
262        } else if (!strncmp(argument,"sleep",5)) {
263          foundSleep=1;
264        }
265      }
266    }
267    if (foundLog) {
268      /* print options etc */
269      for (i=0;i<saveArgc;i++)
270        printf("%s ",saveArgv[i]);
271      printf("\n");
272      if (environment)
273        printf("env %s\n",environment);
274      /*printf("%d rows %d columns %d elements\n",n_con,n_var,nzc);*/
275    }
276    if (!found) {
277      info->arguments=(char **) realloc(info->arguments,(info->numberArguments+1)*sizeof(char *));
278      info->arguments[info->numberArguments++]=strdup("-solve");
279    }
280    if (foundSleep) {
281      /* let user copy .nl file */
282      fprintf(stderr,"You can copy .nl file %s for debug purposes or attach debugger\n",saveArgv[1]);
283      fprintf(stderr,"Type q to quit, anything else to continue\n");
284      char getChar = getc(stdin);
285      if (getChar=='q'||getChar=='Q')
286        exit(1);
287    }
288  }
289  /* add -quit */
290  info->arguments=(char **) realloc(info->arguments,(info->numberArguments+1)*sizeof(char *));
291  info->arguments[info->numberArguments++]=strdup("-quit");
292  return 0;
293}
294void freeArrays1(ampl_info * info)
295{
296  free(info->objective);
297  info->objective=NULL;
298  free(info->rowLower);
299  info->rowLower=NULL;
300  free(info->rowUpper);
301  info->rowUpper=NULL;
302  free(info->columnLower);
303  info->columnLower=NULL;
304  free(info->columnUpper);
305  info->columnUpper=NULL;
306  /* this one not freed by ASL_free */
307  free(info->elements);
308  info->elements=NULL;
309  free(info->primalSolution);
310  info->primalSolution=NULL;
311  free(info->dualSolution);
312  info->dualSolution=NULL;
313  /*free(info->rowStatus);
314  info->rowStatus=NULL;
315  free(info->columnStatus);
316  info->columnStatus=NULL;*/
317}
318void freeArrays2(ampl_info * info)
319{
320  free(info->primalSolution);
321  info->primalSolution=NULL;
322  free(info->dualSolution);
323  info->dualSolution=NULL;
324  free(info->rowStatus);
325  info->rowStatus=NULL;
326  free(info->columnStatus);
327  info->columnStatus=NULL;
328  ASL_free(&asl);
329}
330void freeArgs(ampl_info * info)
331{
332  int i;
333  for ( i=0; i<info->numberArguments;i++)
334    free(info->arguments[i]);
335  free(info->arguments);
336}
337int ampl_obj_prec()
338{
339  return obj_prec();
340}
341void writeAmpl(ampl_info * info)
342{
343  char buf[1000];
344  typedef struct { const char *msg; int code; int wantObj; } Sol_info;
345  static Sol_info solinfo[] = {
346    { "optimal solution",                       000, 1 },
347    { "infeasible",                             200, 1 },
348    { "unbounded",                              300, 0 },
349    { "iteration limit etc",                    400, 1 },
350    { "solution limit",                         401, 1 },
351    { "ran out of space",                       500, 0 },
352    { "status unknown",                         501, 1 },
353    { "bug!",                                   502, 0 },
354    { "best MIP solution so far restored",      101, 1 },
355    { "failed to restore best MIP solution",    503, 1 },
356    { "optimal (?) solution",                   100, 1 }
357  };
358  /* convert status - need info on map */
359  static int map[] = {0, 3, 4, 1};
360  sprintf(buf,"%s %s",Oinfo.bsname,info->buffer);
361  solve_result_num = solinfo[info->problemStatus].code;
362  if (info->columnStatus) {
363    stat_map(info->columnStatus, n_var, map, 4, "outgoing columnStatus");
364    stat_map(info->rowStatus, n_con, map, 4, "outgoing rowStatus");
365    suf_iput("sstatus", ASL_Sufkind_var, info->columnStatus);
366    suf_iput("sstatus", ASL_Sufkind_con, info->rowStatus);
367  }
368  write_sol(buf,info->primalSolution,info->dualSolution,&Oinfo);
369}
370
371#endif
Note: See TracBrowser for help on using the repository browser.