source: trunk/Samples/ekk.cpp @ 625

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

correct ekk memory leak

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4/*  This can be used to compare OSL and Clp.  The interface functions
5    below (in ekk_interface.cpp) were written so that calls to osl
6    in the middle of complex algorithms could easily be swapped
7    for Clp code.  This was to stress test Clp (and Osl :-)).
8
9    With the addition of ekk_crash it may be used to see if we need
10    a crash in Clp.  With "both" set it can also be used to see which
11    gives better behavior after postSolve.
12
13    However they may be useful as sample code.  Virtuous people would
14    add return code checking.
15
16    This main could just as easily be C code.
17
18*/
19
20#include "ekk_c_api.h"
21// These interface functions are needed
22// Note - This example wastes memory as it has several copies of matrix
23/* As ekk_primalSimplex + postsolve instructions:
24   presolve - 0 , no presolve, 1 presolve but no primal after postsolve,
25                  2 do primal if any infeasibilities,
26                  3 always do primal.
27*/
28extern "C" int ekk_primalClp(EKKModel * model, int  startup, int presolve);
29
30/* As ekk_dualSimplex + postsolve instructions:
31   presolve - 0 , no presolve, 1 presolve but no primal after postsolve,
32                  2 do primal if any infeasibilities,
33                  3 always do primal.
34*/
35extern "C" int ekk_dualClp(EKKModel * model, int presolve);
36
37/* rather like ekk_preSolve (3) plus:
38   keepIntegers - false to treat as if continuous
39   pass   - do this many passes (0==default(5))
40
41   returns 1 if infeasible
42*/
43extern "C" int ekk_preSolveClp(EKKModel * model, bool keepIntegers,
44                               int pass);
45
46#include "ClpSimplex.hpp"
47#include <stdio.h>
48#include <stdarg.h>
49
50int main (int argc, const char *argv[])
51{
52  const char * name;
53  // problem is actually scaled for osl, dynamically for clp (slows clp)
54  // default is primal, no presolve, minimise and use clp
55  bool    primal=true,presolve=false;
56  int useosl=0;
57  bool freeFormat=false;
58  bool exportIt=false;
59
60  EKKModel * model;
61  EKKContext * context;
62
63  if ( argc>1 ) {
64    name=argv[1];
65  } else {
66    name="../../Mps/Sample/p0033.mps";
67  }
68
69  /* initialize OSL environment */
70  context=ekk_initializeContext();
71  model=ekk_newModel(context,"");
72
73  int i;
74  printf("*** Options ");
75  for (i=2;i<argc;i++) {
76    printf("%s ",argv[i]);
77  }
78  printf("\n");
79
80  // see if free format needed
81
82  for (i=2;i<argc;i++) {
83    if (!strncmp(argv[i],"free",4)) {
84      freeFormat=true;
85    }
86  }
87
88  // create model from MPS file
89
90  if (!freeFormat) {
91    ekk_importModel(model,name);
92  } else {
93    ekk_importModelFree(model,name);
94  }
95
96  // other options
97  for (i=2;i<argc;i++) {
98    if (!strncmp(argv[i],"max",3)) {
99      if (!strncmp(argv[i],"max2",4)) {
100        // This is for testing - it just reverses signs and maximizes
101        int i,n=ekk_getInumcols(model);
102        double * objective=ekk_getObjective(model);
103        for (i=0;i<n;i++) {
104          objective[i]=-objective[i];
105        }
106        ekk_setObjective(model,objective);
107        ekk_setMaximize(model);
108      } else {
109        // maximize
110        ekk_setMaximize(model);
111      }
112    }
113    if (!strncmp(argv[i],"dual",4)) {
114      primal=false;
115    }
116    if (!strncmp(argv[i],"presol",6)) {
117      presolve=true;
118    }
119    if (!strncmp(argv[i],"osl",3)) {
120      useosl=1;
121    }
122    if (!strncmp(argv[i],"both",4)) {
123      useosl=2;
124    }
125    if (!strncmp(argv[i],"export",6)) {
126      exportIt=true;
127    }
128  }
129  if (useosl) {
130    // OSL
131    if (presolve)
132      ekk_preSolve(model,3,NULL);
133    ekk_scale(model);
134   
135    if (primal)
136      ekk_primalSimplex(model,1);
137    else
138      ekk_dualSimplex(model);
139    if (presolve) {
140      ekk_postSolve(model,NULL);
141      ekk_primalSimplex(model,3);
142    }
143    if (useosl==2)
144      ekk_allSlackBasis(model); // otherwise it would be easy
145  }
146  if ((useosl&2)==0) {
147    // CLP
148    if (presolve)
149      ekk_preSolveClp(model,true,5);
150    /* 3 is because it is ignored if no presolve, and we
151       are forcing Clp to re-optimize */
152    if (primal)
153      ekk_primalClp(model,1,3);
154    else
155      ekk_dualClp(model,3);
156  }
157  if (exportIt) {
158    ClpSimplex * clp = new ClpSimplex();;
159    int numberRows=ekk_getInumrows(model);
160    int numberColumns= ekk_getInumcols(model);
161    clp->loadProblem(numberColumns,numberRows,ekk_blockColumn(model,0),
162                     ekk_blockRow(model,0),ekk_blockElement(model,0),
163                     ekk_collower(model),ekk_colupper(model),
164                     ekk_objective(model),
165                     ekk_rowlower(model),ekk_rowupper(model));
166    // Do integer stuff
167    int * which =  ekk_listOfIntegers(model);
168    int numberIntegers =  ekk_getInumints(model);
169    for (int i=0;i<numberIntegers;i++) 
170      clp->setInteger(which[i]);
171    ekk_free(which);
172    clp->writeMps("try1.mps");
173    delete clp;
174  }
175  ekk_deleteModel(model);
176  ekk_endContext(context);
177  return 0;
178}   
Note: See TracBrowser for help on using the repository browser.