source: trunk/Test/Cbc_ampl.cpp @ 241

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

out sleep

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