Changeset 789


Ignore:
Timestamp:
Sep 21, 2007 4:43:51 PM (12 years ago)
Author:
forrest
Message:

trying to move ampl stuff away

Location:
trunk/Cbc
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/CbcLinked.cpp

    r774 r789  
    88#include "CoinModel.hpp"
    99#include "ClpSimplex.hpp"
    10 #ifdef COIN_HAS_ASL
    11 #include "ClpAmplObjective.hpp"
    12 #endif
    1310// returns jColumn (-2 if linear term, -1 if unknown) and coefficient
    1411static
     
    76867683#include "ClpConstraintLinear.hpp"
    76877684#include "ClpConstraintQuadratic.hpp"
     7685#ifdef COIN_HAS_ASL
     7686//#include "ClpAmplObjective.hpp"
     7687#endif
    76887688/* Return an approximate solution to a CoinModel.
    76897689    Lots of bounds may be odd to force a solution.
     
    76957695                    int mode)
    76967696{
    7697 #ifdef COIN_HAS_ASL
     7697#if 1
     7698  //#ifdef COIN_HAS_ASL
    76987699  // matrix etc will be changed
    76997700  CoinModel coinModel2 = coinModel;
  • trunk/Cbc/src/CbcModel.cpp

    r779 r789  
    14331433  than the current objective cutoff.
    14341434*/
     1435  if (solver_->getRowCutDebuggerAlways()) {
     1436    OsiRowCutDebugger * debuggerX = const_cast<OsiRowCutDebugger *> (solver_->getRowCutDebuggerAlways());
     1437    const OsiRowCutDebugger *debugger = solver_->getRowCutDebugger() ;
     1438    if (!debugger) {
     1439      // infeasible!!
     1440      printf("before search\n");
     1441      const double * lower = solver_->getColLower();
     1442      const double * upper = solver_->getColUpper();
     1443      const double * solution = debuggerX->optimalSolution();
     1444      int numberColumns = solver_->getNumCols();
     1445      for (int i=0;i<numberColumns;i++) {
     1446        if (solver_->isInteger(i)) {
     1447          if (solution[i]<lower[i]-1.0e-6||solution[i]>upper[i]+1.0e-6)
     1448            printf("**** ");
     1449          printf("%d %g <= %g <= %g\n",
     1450                 i,lower[i],solution[i],upper[i]);
     1451        }
     1452      }
     1453      //abort();
     1454    }
     1455  }
    14351456  while (true) {
    14361457#ifdef CBC_THREAD
     
    72497270*/
    72507271double
    7251 CbcModel::checkSolution (double cutoff, const double *solution,
     7272CbcModel::checkSolution (double cutoff, double *solution,
    72527273                         bool fixVariables, double objectiveValue)
    72537274
     
    73797400      cutoff = objectiveValue; // relax
    73807401    if ((solver_->isProvenOptimal()||(specialOptions_&4)!=0) && objectiveValue <= cutoff) {
    7381       double * solution = new double[numberColumns];
    73827402      memcpy(solution ,solver_->getColSolution(),numberColumns*sizeof(double)) ;
    73837403     
     
    74177437        delete [] rowActivity ;
    74187438      }
    7419       delete [] solution;
    74207439    } else {
    74217440      objectiveValue=1.0e50 ;
     
    75807599void
    75817600CbcModel::setBestSolution (CBC_Message how,
    7582                            double & objectiveValue, const double *solution,
     7601                           double & objectiveValue, const double *solutionIn,
    75837602                           bool fixVariables)
    75847603
    75857604{
     7605  double * solution=CoinCopyOfArray(solutionIn,solver_->getNumCols());
    75867606  if (!solverCharacteristics_->solutionAddsCuts()) {
    75877607    // Can trust solution
     
    78287848    delete [] candidate;
    78297849  }
     7850  delete [] solution;
    78307851  return ;
    78317852}
  • trunk/Cbc/src/CbcModel.hpp

    r779 r789  
    10051005      Previously computed objective value is now passed in (in case user does not do solve)
    10061006 */
    1007   double checkSolution(double cutoff, const double * solution,
     1007  double checkSolution(double cutoff, double * solution,
    10081008                       bool fixVariables, double originalObjValue);
    10091009  /** Test the current solution for feasiblility.
  • trunk/Cbc/src/CbcNode.cpp

    r778 r789  
    35173517            anyAction=-1;
    35183518            worstFeasible = CoinMax(worstFeasible,choice.upMovement);
     3519            model->messageHandler()->message(CBC_STRONG,*model->messagesPointer())
     3520              << iObject << iColumn
     3521              <<choice.downMovement<<choice.numIntInfeasDown
     3522              <<choice.upMovement<<choice.numIntInfeasUp
     3523              <<choice.possibleBranch->value()
     3524              <<CoinMessageEol;
    35193525            //printf("Down infeasible for choice %d sequence %d\n",i,
    35203526            // model->object(choice.objectNumber)->columnNumber());
     
    35603566            anyAction=-1;
    35613567            worstFeasible = CoinMax(worstFeasible,choice.downMovement);
     3568            model->messageHandler()->message(CBC_STRONG,*model->messagesPointer())
     3569              << iObject << iColumn
     3570              <<choice.downMovement<<choice.numIntInfeasDown
     3571              <<choice.upMovement<<choice.numIntInfeasUp
     3572              <<choice.possibleBranch->value()
     3573              <<CoinMessageEol;
    35623574            //printf("Up infeasible for choice %d sequence %d\n",i,
    35633575            // model->object(choice.objectNumber)->columnNumber());
  • trunk/Cbc/src/CbcSolver.cpp

    r781 r789  
    103103#include "OsiColCut.hpp"
    104104#ifndef COIN_HAS_LINK
    105 //#ifdef COIN_HAS_ASL
    106105#define COIN_HAS_LINK
    107 //#endif
    108106#endif
    109107#ifdef COIN_HAS_LINK
     
    141139
    142140#include "OsiClpSolverInterface.hpp"
    143 //#define NEW_STYLE_SOLVER
    144 #ifdef NEW_STYLE_SOLVER
    145141#include "CbcSolver.hpp"
    146142CbcSolver::CbcSolver()
    147143  : babModel_(NULL),
    148144    userFunction_(NULL),
     145    statusUserFunction_(NULL),
     146    originalSolver_(NULL),
     147    originalCoinModel_(NULL),
     148    cutGenerator_(NULL),
    149149    numberUserFunctions_(0),
     150    numberCutGenerators_(0),
    150151    startTime_(CoinCpuTime()),
    151152    parameters_(NULL),
    152153    numberParameters_(0),
    153154    doMiplib_(false),
    154     noPrinting_(false)
     155    noPrinting_(false),
     156    readMode_(1)
    155157{
    156158  callBack_ = new CbcStopNow();
     
    160162  : babModel_(NULL),
    161163    userFunction_(NULL),
     164    statusUserFunction_(NULL),
     165    originalSolver_(NULL),
     166    originalCoinModel_(NULL),
     167    cutGenerator_(NULL),
    162168    numberUserFunctions_(0),
     169    numberCutGenerators_(0),
    163170    startTime_(CoinCpuTime()),
    164171    parameters_(NULL),
    165172    numberParameters_(0),
    166173    doMiplib_(false),
    167     noPrinting_(false)
     174    noPrinting_(false),
     175    readMode_(1)
    168176{
    169177  callBack_ = new CbcStopNow();
     
    174182  : babModel_(NULL),
    175183    userFunction_(NULL),
     184    statusUserFunction_(NULL),
     185    originalSolver_(NULL),
     186    originalCoinModel_(NULL),
     187    cutGenerator_(NULL),
    176188    numberUserFunctions_(0),
     189    numberCutGenerators_(0),
    177190    startTime_(CoinCpuTime()),
    178191    parameters_(NULL),
    179192    numberParameters_(0),
    180193    doMiplib_(false),
    181     noPrinting_(false)
     194    noPrinting_(false),
     195    readMode_(1)
    182196{
    183197  callBack_ = new CbcStopNow();
     
    191205    delete userFunction_[i];
    192206  delete [] userFunction_;
     207  for (i=0;i<numberCutGenerators_;i++)
     208    delete cutGenerator_[i];
     209  delete [] cutGenerator_;
     210  delete [] statusUserFunction_;
     211  delete originalSolver_;
     212  delete originalCoinModel_;
    193213  delete babModel_;
    194214  delete [] parameters_;
     
    200220    babModel_(NULL),
    201221    userFunction_(NULL),
     222    statusUserFunction_(NULL),
    202223    numberUserFunctions_(rhs.numberUserFunctions_),
    203224    startTime_(CoinCpuTime()),
     
    205226    numberParameters_(rhs.numberParameters_),
    206227    doMiplib_(rhs.doMiplib_),
    207     noPrinting_(rhs.noPrinting_)
     228    noPrinting_(rhs.noPrinting_),
     229    readMode_(rhs.readMode_)
    208230{
    209231  fillParameters();
     
    216238  for (i=0;i<numberParameters_;i++)
    217239    parameters_[i]=rhs.parameters_[i];
     240  for (i=0;i<numberCutGenerators_;i++)
     241    cutGenerator_[i] = rhs.cutGenerator_[i]->clone();
    218242  callBack_ = rhs.callBack_->clone();
     243  originalSolver_ = NULL;
     244  if (rhs.originalSolver_) {
     245    OsiSolverInterface * temp = rhs.originalSolver_->clone();
     246    originalSolver_ = dynamic_cast<OsiClpSolverInterface *> (temp);
     247    assert (originalSolver_);
     248  }
     249  originalCoinModel_ = NULL;
     250  if (rhs.originalCoinModel_)
     251    originalCoinModel_ = new CoinModel(*rhs.originalCoinModel_);
    219252}
    220253// Assignment operator
     
    227260      delete userFunction_[i];
    228261    delete [] userFunction_;
     262    for (i=0;i<numberCutGenerators_;i++)
     263      delete cutGenerator_[i];
     264    delete [] cutGenerator_;
     265    delete [] statusUserFunction_;
     266    delete originalSolver_;
     267    delete originalCoinModel_;
     268    statusUserFunction_ = NULL;
    229269    delete babModel_;
    230270    delete [] parameters_;
     
    235275    for (i=0;i<numberParameters_;i++)
    236276      parameters_[i]=rhs.parameters_[i];
     277    for (i=0;i<numberCutGenerators_;i++)
     278      cutGenerator_[i] = rhs.cutGenerator_[i]->clone();
    237279    noPrinting_ = rhs.noPrinting_;
     280    readMode_ = rhs.readMode_;
    238281    doMiplib_ = rhs.doMiplib_;
    239282    model_ = rhs.model_;
     
    246289      userFunction_[i] = rhs.userFunction_[i]->clone();
    247290    callBack_ = rhs.callBack_->clone();
     291    originalSolver_ = NULL;
     292    if (rhs.originalSolver_) {
     293      OsiSolverInterface * temp = rhs.originalSolver_->clone();
     294      originalSolver_ = dynamic_cast<OsiClpSolverInterface *> (temp);
     295    assert (originalSolver_);
     296    }
     297    originalCoinModel_ = NULL;
     298    if (rhs.originalCoinModel_)
     299      originalCoinModel_ = new CoinModel(*rhs.originalCoinModel_);
    248300}
    249301  return *this;
     
    325377  int outputFormat=2;
    326378  int substitution=3;
    327   int dualize=0;
     379  int dualize=3;
    328380  int preSolve=5;
    329381  int doSprint=-1;
     
    456508  parameters_[whichParam(INCREMENT,numberParameters_,parameters_)].setDoubleValue(model_.getDblParam(CbcModel::CbcCutoffIncrement));
    457509}
     510// Add user function
     511void
     512CbcSolver::addUserFunction(CbcUser * function)
     513{
     514  CbcUser ** temp = new CbcUser * [numberUserFunctions_+1];
     515  int i;
     516  for (i=0;i<numberUserFunctions_;i++)
     517    temp[i]=userFunction_[i];
     518  delete [] userFunction_;
     519  userFunction_ = temp;
     520  userFunction_[numberUserFunctions_++] = function->clone();
     521  delete [] statusUserFunction_;
     522  statusUserFunction_ = NULL;
     523}
     524// Set user call back
     525void
     526CbcSolver::setUserCallBack(CbcStopNow * function)
     527{
     528  delete callBack_;
     529  callBack_ = function->clone();
     530}
     531// Copy of model on initial load (will contain output solutions)
     532void
     533CbcSolver::setOriginalSolver(OsiClpSolverInterface * originalSolver)
     534{
     535  delete originalSolver_;
     536  OsiSolverInterface * temp = originalSolver->clone();
     537  originalSolver_ = dynamic_cast<OsiClpSolverInterface *> (temp);
     538  assert (originalSolver_);
     539 
     540}
     541// Copy of model on initial load
     542void
     543CbcSolver::setOriginalCoinModel(CoinModel * originalCoinModel)
     544{
     545  delete originalCoinModel_;
     546  originalCoinModel_ = new CoinModel(*originalCoinModel);
     547}
     548// Add cut generator
     549void
     550CbcSolver::addCutGenerator(CglCutGenerator * generator)
     551{
     552  CglCutGenerator ** temp = new CglCutGenerator * [numberCutGenerators_+1];
     553  int i;
     554  for (i=0;i<numberCutGenerators_;i++)
     555    temp[i]=cutGenerator_[i];
     556  delete [] cutGenerator_;
     557  cutGenerator_ = temp;
     558  cutGenerator_[numberCutGenerators_++] = generator->clone();
     559}
    458560// User stuff (base class)
    459561CbcUser::CbcUser()
     
    492594   0 model and solver untouched - babModel updated
    493595   1 model updated - just with solution basis etc
    494    2 model updated i.e. as babModel (babModel NULL)
     596   2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing!)
    495597*/
    496598void
     
    573675    }
    574676    clpSolver->setWarmStart(NULL); // synchronize bases
     677    if (originalSolver_) {
     678      ClpSimplex * lpSolver2 = originalSolver_->getModelPtr();
     679      assert (model2!=lpSolver2);
     680      lpSolver2->moveInfo(*model2);
     681      originalSolver_->setWarmStart(NULL); // synchronize bases
     682    }
    575683  } else if (returnMode==1) {
    576684    OsiClpSolverInterface * clpSolver = dynamic_cast< OsiClpSolverInterface*> (model_.solver());
     
    583691      OsiClpSolverInterface * clpSolver1 = dynamic_cast< OsiClpSolverInterface*> (babModel_->solver());
    584692      ClpSimplex * lpSolver1 = clpSolver1->getModelPtr();
    585       if (lpSolver1!=lpSolver) {
     693      if (lpSolver1!=lpSolver&&model_.bestSolution()) {
    586694        lpSolver->moveInfo(*lpSolver1);
    587695      }
     
    593701    babModel_=NULL;
    594702  }
    595 }
    596 // Clone
    597 CbcUser *
    598 CbcUser::clone() const
    599 {
    600   return new CbcUser(*this);
    601703}
    602704// Stop now stuff (base class)
     
    625727  return new CbcStopNow(*this);
    626728}
    627 #endif
     729//#define NEW_STYLE_SOLVER
     730#undef COIN_HAS_ASL
    628731#ifdef COIN_HAS_ASL
    629732#include "Cbc_ampl.h"
    630 static bool usingAmpl=false;
    631733#endif
    632734static double totalTime=0.0;
     
    10521154                                       const double * lastSolution, double dextra[5])
    10531155{
     1156  if (doAction==11&&!lastSolution)
     1157    lastSolution = model.bestSolution();
    10541158  assert ((doAction==1&&!lastSolution)||(doAction==11&&lastSolution));
    10551159  double fractionIntFixed = dextra[3];
     
    10631167  // Tighten bounds
    10641168  lpSolver->tightenPrimalBounds(0.0,11,true);
    1065   char generalPrint[200];
     1169  //char generalPrint[200];
    10661170  const double *objective = lpSolver->getObjCoefficients() ;
    10671171  double *columnLower = lpSolver->columnLower() ;
     
    14971601    lpSolver->setInfeasibilityCost(1.0e11);
    14981602    lpSolver->defaultFactorizationFrequency();
    1499     lpSolver->initialSolve(solveOptions);
     1603    if (doAction!=11)
     1604      lpSolver->initialSolve(solveOptions);
    15001605    double * columnLower = lpSolver->columnLower();
    15011606    double * columnUpper = lpSolver->columnUpper();
    15021607    double * fullSolution = lpSolver->primalColumnSolution();
    15031608    const double * dj = lpSolver->dualColumnSolution();
    1504     // First squash small and fix big
    1505     //double small=1.0e-7;
    1506     //double djTol=20.0;
    15071609    int iPass=0;
    15081610#define MAXPROB 2
     
    15381640      fullSolution = lpSolver->primalColumnSolution();
    15391641      if (doAction==11) {
     1642        {
     1643          double * columnLower = lpSolver->columnLower();
     1644          double * columnUpper = lpSolver->columnUpper();
     1645          //      lpSolver->dual();
     1646          memcpy(columnLower,originalLpSolver->columnLower(),numberColumns*sizeof(double));
     1647          memcpy(columnUpper,originalLpSolver->columnUpper(),numberColumns*sizeof(double));
     1648          //      lpSolver->dual();
     1649          int iColumn;
     1650          for (iColumn=0;iColumn<numberColumns;iColumn++) {
     1651            if (columnUpper[iColumn] > columnLower[iColumn]+1.0e-8) {
     1652              if (solver->isInteger(iColumn)) {
     1653                double value = lastSolution[iColumn];
     1654                int iValue = (int) (value+0.5);
     1655                assert (fabs(value-((double) iValue))<1.0e-3);
     1656                assert (iValue>=columnLower[iColumn]&&
     1657                        iValue<=columnUpper[iColumn]);
     1658                columnLower[iColumn]=iValue;
     1659                columnUpper[iColumn]=iValue;
     1660              }
     1661            }
     1662          }
     1663          lpSolver->initialSolve(solveOptions);
     1664          memcpy(columnLower,originalLpSolver->columnLower(),numberColumns*sizeof(double));
     1665          memcpy(columnUpper,originalLpSolver->columnUpper(),numberColumns*sizeof(double));
     1666        }
    15401667        for (iColumn=0;iColumn<numberColumns;iColumn++) {
    15411668          if (columnUpper[iColumn] > columnLower[iColumn]+1.0e-8) {
     
    17571884    double * dsort = new double[numberColumns];
    17581885    int chunk=20;
     1886    int iRelax=0;
    17591887    //double fractionFixed=6.0/8.0;
    17601888    // relax while lots fixed
     
    17621890      if (skipZero2>10&&doAction<10)
    17631891        break;
     1892      iRelax++;
    17641893      int n=0;
    17651894      double sum0=0.0;
     
    19172046      }
    19182047      printf("%d relaxed\n",nRelax);
     2048      if (iRelax>20&&nRelax==chunk)
     2049        nRelax=0;
     2050      if (iRelax>50)
     2051        nRelax=0;
    19192052      assert (!nBad);
    19202053      delete [] lo;
     
    19552088}
    19562089#endif
    1957 #ifdef COIN_HAS_ASL
     2090#ifdef COIN_HAS_LINK
    19582091/*  Returns OsiSolverInterface (User should delete)
    19592092    On entry numberKnapsack is maximum number of Total entries
     
    28382971  int outputFormat=2;
    28392972  int substitution=3;
    2840   int dualize=0;
     2973  int dualize=3;
    28412974  int preSolve=5;
    28422975  int doSprint=-1;
     
    31203253void CbcClpUnitTest (const CbcModel & saveModel,
    31213254                     std::string& dirMiplib, bool unitTestOnly);
    3122 
     3255#ifdef NEW_STYLE_SOLVER
     3256/* This takes a list of commands, does "stuff" and returns
     3257   returnMode -
     3258   0 model and solver untouched - babModel updated
     3259   1 model updated - just with solution basis etc
     3260   2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
     3261*/
     3262int
     3263CbcSolver::solve(const char * input2, int returnMode)
     3264{
     3265  char * input = strdup(input2);
     3266  int length = strlen(input);
     3267  bool blank = input[0]=='0';
     3268  int n=blank ? 0 : 1;
     3269  for (int i=0;i<length;i++) {
     3270    if (blank) {
     3271      // look for next non blank
     3272      if (input[i]==' ') {
     3273        continue;
     3274      } else {
     3275        n++;
     3276        blank=false;
     3277      }
     3278    } else {
     3279      // look for next blank
     3280      if (input[i]!=' ') {
     3281        continue;
     3282      } else {
     3283        blank=true;
     3284      }
     3285    }
     3286  }
     3287  char ** argv = new char * [n+2];
     3288  argv[0]=strdup("cbc");
     3289  int i=0;
     3290  while(input[i]==' ')
     3291    i++;
     3292  for (int j=0;j<n;j++) {
     3293    int saveI=i;
     3294    for (;i<length;i++) {
     3295      // look for next blank
     3296      if (input[i]!=' ') {
     3297        continue;
     3298      } else {
     3299        break;
     3300      }
     3301    }
     3302    char save = input[i];
     3303    input[i]='\0';
     3304    argv[j+1]=strdup(input+saveI);
     3305    input[i]=save;
     3306    while(input[i]==' ')
     3307      i++;
     3308  }
     3309  argv[n+1]=strdup("-quit");
     3310  free(input);
     3311  int returnCode = solve(n+2,const_cast<const char **>(argv),returnMode);
     3312  for (int k=0;k<n+2;k++)
     3313    free(argv[k]);
     3314  delete [] argv;
     3315  return returnCode;
     3316}
     3317#endif
    31233318/* Meaning of whereFrom:
    31243319   1 after initial solve by dualsimplex etc
     
    31513346  CbcModel * babModel_ = NULL;
    31523347  int returnMode=1;
     3348  int statusUserFunction_[1];
     3349  int numberUserFunctions_=1; // to allow for ampl
    31533350#else
    31543351  delete babModel_;
    31553352  babModel_ = NULL;
     3353  CbcOrClpRead_mode=1;
     3354  delete [] statusUserFunction_;
     3355  statusUserFunction_ = new int [numberUserFunctions_];
     3356  int iUser;
    31563357#endif
     3358  memset(statusUserFunction_,0,numberUserFunctions_*sizeof(int));
    31573359  /* Note
    31583360     This is meant as a stand-alone executable to do as much of coin as possible.
     
    32293431    CglStored storedAmpl;
    32303432    CoinModel * coinModel = NULL;
    3231 #ifdef COIN_HAS_ASL
    3232     ampl_info info;
    32333433    CoinModel saveCoinModel;
    32343434    CoinModel saveTightenedModel;
     
    32373437    int * knapsackRow=NULL;
    32383438    int numberKnapsack=0;
     3439#ifdef NEW_STYLE_SOLVER
     3440    int numberInputs=0;
     3441    readMode_=CbcOrClpRead_mode;
     3442    for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     3443      int status = userFunction_[iUser]->importData(this,argc,const_cast<char **>(argv));
     3444      if (status>=0) {
     3445        if (!status) {
     3446          numberInputs++;
     3447          statusUserFunction_[iUser]=1;
     3448          goodModel=true;
     3449          solver = model_.solver();
     3450          clpSolver = dynamic_cast< OsiClpSolverInterface*> (solver);
     3451          lpSolver = clpSolver->getModelPtr();
     3452        } else {
     3453          printf("Bad input from user function %s\n",userFunction_[iUser]->name().c_str());
     3454          abort();
     3455        }
     3456      }
     3457    }
     3458    if (numberInputs>1) {
     3459      printf("Two or more user inputs!\n");
     3460      abort();
     3461    }
     3462    if (originalCoinModel_)
     3463      complicatedInteger=1;
     3464    testOsiParameters = intValue(TESTOSI);
     3465    if (noPrinting_) {
     3466      model_.messageHandler()->setLogLevel(0);
     3467      setCbcOrClpPrinting(false);
     3468    }
     3469    CbcOrClpRead_mode=readMode_;
     3470#endif
     3471#ifdef COIN_HAS_ASL
     3472    ampl_info info;
     3473    {
    32393474    memset(&info,0,sizeof(info));
    32403475    if (argc>2&&!strcmp(argv[2],"-AMPL")) {
    3241       usingAmpl=true;
     3476      statusUserFunction_[0]=1;
    32423477      // see if log in list
    32433478      noPrinting_=true;
     
    33323567        si->setBiLinearPriority(10000);
    33333568        CoinModel * model2 = (CoinModel *) coinModel;
    3334         int logLevel = whichParam(LOGLEVEL,numberParameters_,parameters_);
     3569        int logLevel = parameters_[whichParam(LOGLEVEL,numberParameters_,parameters_)].intValue();
    33353570        si->load(*model2,true,logLevel);
    33363571        // redo
     
    34113646      argv = const_cast<const char **>(info.arguments);
    34123647    }
     3648    }
    34133649#endif   
    34143650    // default action on import
     
    34233659    int presolveOptions=0;
    34243660    int substitution=3;
    3425     int dualize=0;
     3661    int dualize=3;
    34263662    int doCrash=0;
    34273663    int doVector=0;
     
    35733809      if (argc>1) {
    35743810        sprintf(generalPrint,"command line - ");
    3575         for (int i=0;i<argc;i++)
     3811        for (int i=0;i<argc;i++) {
     3812          if (!argv[i])
     3813            break;
    35763814          sprintf(generalPrint+strlen(generalPrint),"%s ",argv[i]);
     3815        }
    35773816        generalMessageHandler->message(CLP_GENERAL,generalMessages)
    35783817          << generalPrint
     
    36523891          // check if any integers
    36533892#ifdef COIN_HAS_ASL
    3654           if (info.numberSos&&doSOS&&usingAmpl) {
     3893          if (info.numberSos&&doSOS&&statusUserFunction_[0]) {
    36553894            // SOS
    36563895            numberSOS = info.numberSos;
     
    36703909          }
    36713910#ifdef COIN_HAS_ASL
    3672           if (verbose<4&&usingAmpl)
     3911          if (verbose<4&&statusUserFunction_[0])
    36733912            verbose +=4;
    36743913#endif
     
    39504189                lpSolver->setPrimalColumnPivotAlgorithm(dantzig);
    39514190              } else if (action==3) {
    3952                 ClpPrimalColumnSteepest steep(2);
     4191                ClpPrimalColumnSteepest steep(4);
    39534192                lpSolver->setPrimalColumnPivotAlgorithm(steep);
    39544193              } else if (action==4) {
     
    39564195                lpSolver->setPrimalColumnPivotAlgorithm(steep);
    39574196              } else if (action==5) {
    3958                 ClpPrimalColumnSteepest steep(4);
     4197                ClpPrimalColumnSteepest steep(2);
    39594198                lpSolver->setPrimalColumnPivotAlgorithm(steep);
    39604199              } else if (action==6) {
     
    41464385          if (type==EXIT) {
    41474386#ifdef COIN_HAS_ASL
    4148             if(usingAmpl) {
     4387            if(statusUserFunction_[0]) {
    41494388              if (info.numberIntegers||info.numberBinary) {
    41504389                // integer
     
    41924431              ClpSimplex * model2 = lpSolver;
    41934432              if (dualize) {
    4194                 //printf("dualize %d\n",dualize);
    4195                 model2 = ((ClpSimplexOther *) model2)->dualOfModel();
    4196                 sprintf(generalPrint,"Dual of model has %d rows and %d columns",
    4197                        model2->numberRows(),model2->numberColumns());
    4198                 generalMessageHandler->message(CLP_GENERAL,generalMessages)
    4199                   << generalPrint
    4200                   <<CoinMessageEol;
    4201                 model2->setOptimizationDirection(1.0);
     4433                bool tryIt=true;
     4434                double fractionColumn=1.0;
     4435                double fractionRow=1.0;
     4436                if (dualize==3) {
     4437                  dualize=1;
     4438                  int numberColumns=lpSolver->numberColumns();
     4439                  int numberRows=lpSolver->numberRows();
     4440                  if (numberRows<50000||5*numberColumns>numberRows) {
     4441                    tryIt=false;
     4442                  } else {
     4443                    fractionColumn=0.1;
     4444                    fractionRow=0.1;
     4445                  }
     4446                }
     4447                if (tryIt) {
     4448                  model2 = ((ClpSimplexOther *) model2)->dualOfModel(fractionRow,fractionColumn);
     4449                  if (model2) {
     4450                    sprintf(generalPrint,"Dual of model has %d rows and %d columns",
     4451                            model2->numberRows(),model2->numberColumns());
     4452                    generalMessageHandler->message(CLP_GENERAL,generalMessages)
     4453                      << generalPrint
     4454                      <<CoinMessageEol;
     4455                    model2->setOptimizationDirection(1.0);
     4456                  } else {
     4457                    model2 = lpSolver;
     4458                    dualize=0;
     4459                  }
     4460                } else {
     4461                  dualize=0;
     4462                }
    42024463              }
    42034464              if (noPrinting_)
     
    44374698              if (dualize) {
    44384699                int returnCode=((ClpSimplexOther *) lpSolver)->restoreFromDual(model2);
     4700                if (model2->status()==3)
     4701                  returnCode=0;
    44394702                delete model2;
    44404703                if (returnCode&&dualize!=2)
     
    44424705                model2=lpSolver;
    44434706              }
     4707#ifdef NEW_STYLE_SOLVER
     4708              updateModel(model2,returnMode);
     4709              for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     4710                if (statusUserFunction_[iUser])
     4711                  userFunction_[iUser]->exportSolution(this,1);
     4712              }
     4713#endif
    44444714#ifdef COIN_HAS_ASL
    4445               if (usingAmpl) {
     4715              if (statusUserFunction_[0]) {
    44464716                double value = model2->getObjValue()*model2->getObjSense();
    44474717                char buf[300];
     
    51535423                  //process.messageHandler()->setLogLevel(babModel_->logLevel());
    51545424#ifdef COIN_HAS_ASL
    5155                   if (info.numberSos&&doSOS&&usingAmpl) {
     5425                  if (info.numberSos&&doSOS&&statusUserFunction_[0]) {
    51565426                    // SOS
    51575427                    numberSOS = info.numberSos;
     
    52375507                    solver2->setHintParam(OsiDoInBranchAndCut,false,OsiHintDo) ;
    52385508                }
     5509#ifdef NEW_STYLE_SOLVER
     5510                if (!solver2) {
     5511                  for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     5512                    if (statusUserFunction_[iUser])
     5513                      userFunction_[iUser]->exportSolution(this,11,"infeasible/unbounded by pre-processing");
     5514                  }
     5515                }
     5516#endif
    52395517#ifdef COIN_HAS_ASL
    5240                 if (!solver2&&usingAmpl) {
     5518                if (!solver2&&statusUserFunction_[0]) {
    52415519                  // infeasible
    52425520                  info.problemStatus=1;
     
    53595637              }
    53605638              int testOsiOptions = parameters_[whichParam(TESTOSI,numberParameters_,parameters_)].intValue();
    5361 #ifdef COIN_HAS_ASL
     5639              //#ifdef COIN_HAS_ASL
     5640#if 1
    53625641              // If linked then see if expansion wanted
    53635642              {
     
    53955674                        // allow gomory
    53965675                        complicatedInteger=0;
     5676#ifdef COIN_HAS_ASL
    53975677                        // Priorities already done
    53985678                        free(info.priorities);
    53995679                        info.priorities=NULL;
     5680#endif
    54005681                      } else {
    54015682                        numberKnapsack=0;
     
    56505931              }
    56515932              if (type==BAB) {
     5933#ifdef NEW_STYLE_SOLVER
     5934                {
     5935                  CbcSolverUsefulData info;
     5936                  bool useInfo=false;
     5937                  for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     5938                    if (statusUserFunction_[iUser]) {
     5939                      userFunction_[iUser]->fillInformation(this,info);
     5940                      useInfo=true;
     5941                    }
     5942                  }
     5943                  if (useInfo) {
     5944                    priorities=info.priorities_;
     5945                    branchDirection=info.branchDirection_;
     5946                    pseudoDown=info.pseudoDown_;
     5947                    pseudoUp=info.pseudoUp_;
     5948                    solutionIn=info.primalSolution_;
     5949                    int numberColumns = originalCoinModel_ ? originalCoinModel_->numberColumns() :
     5950                      lpSolver->getNumCols();
     5951                    prioritiesIn = (int *) malloc(numberColumns*sizeof(int));
     5952                    memcpy(prioritiesIn,info.priorities_,numberColumns*sizeof(int));
     5953                    sosPriority = info.sosPriority_;
     5954                  }
     5955                }
     5956#endif
    56525957#ifdef COIN_HAS_ASL
    5653                 if (usingAmpl) {
     5958                if (statusUserFunction_[0]) {
    56545959                  priorities=info.priorities;
    56555960                  branchDirection=info.branchDirection;
     
    61216426                int statistics = (printOptions>0) ? printOptions: 0;
    61226427#ifdef COIN_HAS_ASL
    6123                 if (!usingAmpl) {
     6428                if (!statusUserFunction_[0]) {
    61246429#endif
    61256430                  free(priorities);
     
    64416746#ifdef COIN_HAS_ASL
    64426747                // add in lotsizing
    6443                 if (usingAmpl&&info.special) {
     6748                if (statusUserFunction_[0]&&info.special) {
    64446749                  int numberColumns = babModel_->getNumCols();
    64456750                  int i;
     
    67447049                clpSolver = dynamic_cast< OsiClpSolverInterface*> (strengthenedModel);
    67457050#ifdef COIN_HAS_ASL
    6746               else if (usingAmpl)
     7051              else if (statusUserFunction_[0])
    67477052                clpSolver = dynamic_cast< OsiClpSolverInterface*> (babModel_->solver());
    67487053#endif
     
    68017106                  return returnCode;
    68027107                }
     7108#ifdef NEW_STYLE_SOLVER
     7109                if (bestSolution&&numberKnapsack) {
     7110                  // expanded knapsack
     7111                  assert (originalCoinModel_);
     7112                  // Fills in original solution (coinModel length - rest junk)
     7113                  clpSolver = dynamic_cast< OsiClpSolverInterface*> (babModel_->solver());
     7114                  lpSolver = clpSolver->getModelPtr();
     7115                  int numberColumns = originalCoinModel_->numberColumns();
     7116                  int numberColumns2 = lpSolver->numberColumns();
     7117                  assert (numberColumns2>numberColumns);
     7118                  double * primalSolution = new double [numberColumns2];
     7119                  memset(primalSolution,0,numberColumns2*sizeof(double));
     7120                  afterKnapsack(saveTightenedModel,  whichColumn,  knapsackStart,
     7121                                knapsackRow,  numberKnapsack,
     7122                                lpSolver->primalColumnSolution(), primalSolution,1);
     7123                  memcpy(lpSolver->primalColumnSolution(),primalSolution,numberColumns2*sizeof(double));
     7124                  delete [] primalSolution;
     7125                }
     7126                updateModel(NULL,returnMode);
     7127                for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     7128                  if (statusUserFunction_[iUser])
     7129                    userFunction_[iUser]->exportSolution(this,2);
     7130                }
     7131#endif
    68037132#ifdef COIN_HAS_ASL
    6804                 if (usingAmpl) {
     7133                if (statusUserFunction_[0]) {
    68057134                  clpSolver = dynamic_cast< OsiClpSolverInterface*> (babModel_->solver());
    68067135                  lpSolver = clpSolver->getModelPtr();
     
    68797208              time1 = time2;
    68807209#ifdef COIN_HAS_ASL
    6881               if (usingAmpl) {
     7210              if (statusUserFunction_[0]) {
    68827211                // keep if going to be destroyed
    68837212                OsiSolverInterface * solver = babModel_->solver();
     
    68997228            {
    69007229#ifdef COIN_HAS_ASL
    6901               if (!usingAmpl) {
     7230              if (!statusUserFunction_[0]) {
    69027231#endif
    69037232                free(priorities);
     
    71927521                bool deleteModel2=false;
    71937522                ClpSimplex * model2 = lpSolver;
    7194                 if (dualize) {
     7523                if (dualize&&dualize<3) {
    71957524                  model2 = ((ClpSimplexOther *) model2)->dualOfModel();
    71967525                  sprintf(generalPrint,"Dual of model has %d rows and %d columns",
     
    72027531                }
    72037532#ifdef COIN_HAS_ASL
    7204                 if (info.numberSos&&doSOS&&usingAmpl) {
     7533                if (info.numberSos&&doSOS&&statusUserFunction_[0]) {
    72057534                  // SOS
    72067535                  numberSOS = info.numberSos;
     
    76738002                fread(debugValues,sizeof(double),numberDebugValues,fp);
    76748003                printf("%d doubles read into debugValues\n",numberDebugValues);
     8004                if (numberDebugValues<200) {
     8005                  for (int i=0;i<numberDebugValues;i++) {
     8006                    if (lpSolver->isInteger(i)&&debugValues[i])
     8007                      printf("%d %g\n",i,debugValues[i]);
     8008                  }
     8009                }
    76758010                fclose(fp);
    76768011              } else {
     
    80948429              lpSolver = clpSolver->getModelPtr();
    80958430#ifdef COIN_HAS_ASL
    8096               if (usingAmpl) {
     8431              if (statusUserFunction_[0]) {
    80978432                int n = clpSolver->getNumCols();
    80988433                double value = objectiveValue*lpSolver->getObjSense();
     
    85178852#ifdef NEW_STYLE_SOLVER
    85188853  updateModel(NULL,returnMode);
     8854  for (iUser=0;iUser<numberUserFunctions_;iUser++) {
     8855    if (statusUserFunction_[iUser])
     8856      userFunction_[iUser]->exportData(this);
     8857  }
    85198858  sprintf(generalPrint,"Total time %.2f",CoinCpuTime()-startTime_);
    85208859#else
  • trunk/Cbc/src/CbcSolver.hpp

    r779 r789  
    1212class CbcUser;
    1313class CbcStopNow;
     14class CglCutGenerator;
    1415
    1516//#############################################################################
     
    3334      0 model and solver untouched - babModel updated
    3435      1 model updated - just with solution basis etc
    35       2 model updated i.e. as babModel (babModel NULL)
     36      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
    3637  */
    3738  int solve(int argc, const char * argv[], int returnMode);
     39  /** This takes a list of commands, does "stuff" and returns
     40      returnMode -
     41      0 model and solver untouched - babModel updated
     42      1 model updated - just with solution basis etc
     43      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
     44  */
     45  int solve(const char * input, int returnMode);
    3846  //@}
    3947  ///@name Constructors and destructors etc
     
    6169  /// Set default values in solvers from parameters
    6270  void fillValuesInSolver();
     71  /// Add user function
     72  void addUserFunction(CbcUser * function);
     73  /// Set user call back
     74  void setUserCallBack(CbcStopNow * function);
     75  /// Add cut generator
     76  void addCutGenerator(CglCutGenerator * generator);
    6377  //@}
    6478  ///@name miscellaneous methods to line up with old
     
    7791      0 model and solver untouched - babModel updated
    7892      1 model updated - just with solution basis etc
    79       2 model updated i.e. as babModel (babModel NULL)
     93      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
    8094  */
    8195  void updateModel(ClpSimplex * model2, int returnMode);
     
    95109  inline CbcModel * model()
    96110  { return &model_;}
     111  /// Number of userFunctions
     112  inline int numberUserFunctions() const
     113  { return numberUserFunctions_;}
     114  /// User function array
     115  inline CbcUser ** userFunctionArray() const
     116  { return userFunction_;}
     117  /// Copy of model on initial load (will contain output solutions)
     118  inline OsiClpSolverInterface * originalSolver() const
     119  { return originalSolver_;}
     120  /// Copy of model on initial load
     121  inline CoinModel * originalCoinModel() const
     122  { return originalCoinModel_;}
     123  /// Copy of model on initial load (will contain output solutions)
     124  void setOriginalSolver(OsiClpSolverInterface * originalSolver);
     125  /// Copy of model on initial load
     126  void setOriginalCoinModel(CoinModel * originalCoinModel);
     127  /// Number of cutgenerators
     128  inline int numberCutGenerators() const
     129  { return numberCutGenerators_;}
     130  /// Cut generator array
     131  inline CglCutGenerator ** cutGeneratorArray() const
     132  { return cutGenerator_;}
     133  /// Start time
     134  inline double startTime() const
     135  { return startTime_;}
     136  /// Whether to print to std::cout
     137  inline void setPrinting(bool onOff)
     138  { noPrinting_= !onOff;}
     139  /// Where to start reading commands
     140  inline void setReadMode(int value)
     141  { readMode_ = value;}
    97142  //@}
    98143private:
     
    108153  /// User functions
    109154  CbcUser ** userFunction_;
     155  /** Status of user functions
     156      0 - not used
     157      1 - needs cbc_load
     158      2 - available - data in coinModel
     159      3 - data loaded - can do cbc_save
     160  */
     161  int * statusUserFunction_;
     162  /// Copy of model on initial load (will contain output solutions)
     163  OsiClpSolverInterface * originalSolver_;
     164  /// Copy of model on initial load
     165  CoinModel * originalCoinModel_;
     166  /// Cut generators
     167  CglCutGenerator ** cutGenerator_;
    110168  /// Number of user functions
    111169  int numberUserFunctions_;
     170  /// Number of cut generators
     171  int numberCutGenerators_;
    112172  /// Stop now stuff
    113173  CbcStopNow * callBack_;
     
    122182  /// Whether to print to std::cout
    123183  bool noPrinting_;
    124  
     184  /// Where to start reading commands
     185  int readMode_;
    125186  //@}
    126187};
    127188//#############################################################################
    128 
     189/// Structure to hold useful arrays
     190typedef struct {
     191  // Priorities
     192  int * priorities_;
     193  // SOS priorities
     194  int * sosPriority_;
     195  // Direction to branch first
     196  int * branchDirection_;
     197  // Input solution
     198  double * primalSolution_;
     199  // Down pseudo costs
     200  double * pseudoDown_;
     201  // Up pseudo costs
     202  double * pseudoUp_;
     203} CbcSolverUsefulData;
    129204/** This allows the use of an unknown user stuff including modeling languages
    130205 */
     
    135210  ///@name import/export methods
    136211  //@{
    137   /// Import - 0 if good
    138   virtual int importData(std::string fileName) {return 0;}
    139   /// Export
    140   virtual void exportSolution() {}
     212  /** Import - gets full command arguments
     213      Returns -1 - no action
     214               0 - data read in without error
     215               1 - errors
     216  */
     217  virtual int importData(CbcSolver * model, int & argc, char * argv[]) {return -1;}
     218  /// Export 1 OsiClpSolver, 2 CbcModel - add 10 if infeasible from odd situation
     219  virtual void exportSolution(CbcSolver * model, int mode,const char * message=NULL) {}
     220  /// Export Data (i.e. at very end)
     221  virtual void exportData(CbcSolver * model) {}
     222  /// Get useful stuff
     223  virtual void fillInformation(CbcSolver * model,
     224                               CbcSolverUsefulData & info) {}
     225
    141226  //@}
    142227  ///@name usage methods
     
    151236  { return userName_;}
    152237  /// Solve (whatever that means)
    153   void solve(CbcSolver * model, const char * options) {}
     238  virtual void solve(CbcSolver * model, const char * options) = 0;
     239  /// Returns true if function knows about option
     240  virtual bool canDo(const char * options) = 0;
    154241  //@}
    155242  ///@name Constructors and destructors etc
     
    166253
    167254  /// Clone
    168   CbcUser * clone() const;
     255  virtual CbcUser * clone() const = 0;
    169256 
    170257  /// Destructor
     
    219306
    220307  /// Clone
    221   CbcStopNow * clone() const;
     308  virtual CbcStopNow * clone() const;
    222309 
    223310  /// Destructor
  • trunk/Cbc/src/Cbc_ampl.cpp

    r775 r789  
    753753    priority_(NULL),
    754754    cut_(NULL),
     755    moreInfo_(NULL),
    755756    logLevel_(0),
    756757    type_(-1),
  • trunk/Cbc/src/ClpAmplStuff.cpp

    r700 r789  
    1212#include "ClpAmplObjective.hpp"
    1313#include "ClpConstraintAmpl.hpp"
     14#include "ClpMessage.hpp"
    1415#include "CoinUtilsConfig.h"
    1516#include "CoinHelperFunctions.hpp"
     17#include "CoinWarmStartBasis.hpp"
     18#include "OsiSolverInterface.hpp"
     19#include "CbcSolver.hpp"
     20#include "Cbc_ampl.h"
     21#include "CoinTime.hpp"
     22#include "CglStored.hpp"
     23#include "CoinModel.hpp"
     24#include "CbcLinked.hpp"
     25class CbcAmpl  : public CbcUser {
     26 
     27public:
     28  ///@name usage methods
     29  //@{
     30  /// Solve (whatever that means)
     31  virtual void solve(CbcSolver * model, const char * options);
     32  /// Returns true if function knows about option
     33  virtual bool canDo(const char * options) ;
     34  /** Import - gets full command arguments
     35      Returns -1 - no action
     36               0 - data read in without error
     37               1 - errors
     38  */
     39  virtual int importData(CbcSolver * model, int & argc, char * argv[]);
     40  /// Export 1 OsiClpSolver, 2 CbcModel - add 10 if infeasible from odd situation
     41  virtual void exportSolution(CbcSolver * model, int mode,const char * message=NULL) ;
     42  /// Export Data (i.e. at very end)
     43  virtual void exportData(CbcSolver * model);
     44  /// Get useful stuff
     45  virtual void fillInformation(CbcSolver * model,
     46                               CbcSolverUsefulData & info);
     47  //@}
     48  ///@name Constructors and destructors etc
     49  //@{
     50  /// Default Constructor
     51  CbcAmpl();
     52 
     53  /** Copy constructor .
     54   */ 
     55  CbcAmpl(const CbcAmpl & rhs);
     56 
     57  /// Assignment operator
     58  CbcAmpl & operator=(const CbcAmpl& rhs);
     59
     60  /// Clone
     61  virtual CbcUser * clone() const;
     62 
     63  /// Destructor
     64  virtual ~CbcAmpl ();
     65  //@}
     66private:
     67  ///@name Private member data
     68  //@{
     69  /// AMPL info
     70  ampl_info info_;
     71  //@}
     72};
     73// Mechanicsburg stuff
     74CbcAmpl::CbcAmpl()
     75  : CbcUser()
     76{
     77  userName_ = "mech";
     78  memset(&info_,0,sizeof(info_));
     79}
     80CbcAmpl::~CbcAmpl()
     81{
     82}
     83// Copy constructor
     84CbcAmpl::CbcAmpl ( const CbcAmpl & rhs)
     85  : CbcUser(rhs)
     86{
     87  info_ = rhs.info_;
     88}
     89// Assignment operator
     90CbcAmpl &
     91CbcAmpl::operator=(const CbcAmpl & rhs)
     92{
     93  if (this != &rhs) {
     94    CbcUser::operator=(rhs);
     95    info_ = rhs.info_;
     96  }
     97  return *this;
     98}
     99// Clone
     100CbcUser *
     101CbcAmpl::clone() const
     102{
     103  return new CbcAmpl(*this);
     104}
     105// Solve (whatever that means)
     106void
     107CbcAmpl::solve(CbcSolver * controlModel, const char * options)
     108{
     109  CbcModel * model = controlModel->model();
     110  assert (model);
     111  //OsiClpSolverInterface * clpSolver = dynamic_cast< OsiClpSolverInterface*> (model->solver());
     112  //ClpSimplex * lpSolver = clpSolver->getModelPtr();
     113  if (!strcmp(options,"cbc_load")) {
     114  } else if (!strcmp(options,"cbc_quit")) {
     115  } else {
     116    printf("unknown option for CbcAmpl is %s\n",options);
     117    abort();
     118  }
     119}
     120// Returns true if function knows about option
     121bool
     122CbcAmpl::canDo(const char * options)
     123{
     124  return (!strcmp(options,"cbc_load")||!strcmp(options,"cbc_quit"));
     125}
     126/* Import - gets full command arguments
     127   Returns -1 - no action
     128            0 - data read in without error
     129            1 - errors
     130*/
     131int
     132CbcAmpl::importData(CbcSolver * control, int &argc, char * argv[])
     133{
     134  CbcModel * babModel = control->model();
     135  assert (babModel);
     136  CoinMessageHandler * generalMessageHandler = babModel->messageHandler();
     137  OsiClpSolverInterface * solver = dynamic_cast< OsiClpSolverInterface*> (control->model()->solver());
     138  assert (solver);
     139  CoinMessages generalMessages = solver->getModelPtr()->messages();
     140  char generalPrint[10000];
     141  OsiSolverLink * si = NULL;
     142  ClpSimplex * lpSolver = solver->getModelPtr();
     143  if (argc>2&&!strcmp(argv[2],"-AMPL")) {
     144    // see if log in list
     145    bool printing=false;
     146    for (int i=1;i<argc;i++) {
     147      if (!strncmp(argv[i],"log",3)) {
     148        const char * equals = strchr(argv[i],'=');
     149        if (equals&&atoi(equals+1)>0) {
     150          printing=true;
     151          info_.logLevel=atoi(equals+1);
     152          control->setIntValue(LOGLEVEL,info_.logLevel);
     153          // mark so won't be overWritten
     154          info_.numberRows=-1234567;
     155          break;
     156        }
     157      }
     158    }
     159    CoinModel * coinModel=NULL;
     160    int returnCode = readAmpl(&info_,argc, argv,(void **) (& coinModel));
     161    if (returnCode)
     162      return returnCode;
     163    control->setReadMode(3); // so will start with parameters
     164    // see if log in list (including environment)
     165    for (int i=1;i<info_.numberArguments;i++) {
     166      if (!strcmp(info_.arguments[i],"log")) {
     167        if (i<info_.numberArguments-1&&atoi(info_.arguments[i+1])>0)
     168          printing=true;
     169        break;
     170      }
     171    }
     172    control->setPrinting(printing);
     173    if (printing)
     174      printf("%d rows, %d columns and %d elements\n",
     175             info_.numberRows,info_.numberColumns,info_.numberElements);
     176    if (!coinModel) {
     177      solver->loadProblem(info_.numberColumns,info_.numberRows,info_.starts,
     178                          info_.rows,info_.elements,
     179                          info_.columnLower,info_.columnUpper,info_.objective,
     180                          info_.rowLower,info_.rowUpper);
     181      if (info_.numberSos) {
     182        // SOS
     183        solver->setSOSData(info_.numberSos,info_.sosType,info_.sosStart,
     184                           info_.sosIndices,info_.sosReference);
     185      }
     186    } else {
     187      // save
     188      control->setOriginalCoinModel(coinModel);
     189      // load from coin model
     190      OsiSolverLink solver1;
     191      OsiSolverInterface * solver2 = solver1.clone();
     192      babModel->assignSolver(solver2,false);
     193      si = dynamic_cast<OsiSolverLink *>(babModel->solver()) ;
     194      assert (si != NULL);
     195      si->setDefaultMeshSize(0.001);
     196      // need some relative granularity
     197      si->setDefaultBound(100.0);
     198      double dextra3 = control->doubleValue(DEXTRA3);
     199      if (dextra3)
     200        si->setDefaultMeshSize(dextra3);
     201      si->setDefaultBound(100000.0);
     202      si->setIntegerPriority(1000);
     203      si->setBiLinearPriority(10000);
     204      CoinModel * model2 = (CoinModel *) coinModel;
     205      int logLevel = control->intValue(LOGLEVEL);
     206      si->load(*model2,true,logLevel);
     207      // redo
     208      solver = dynamic_cast< OsiClpSolverInterface*> (control->model()->solver());
     209      lpSolver = solver->getModelPtr();
     210      solver->messageHandler()->setLogLevel(0) ;
     211      control->setIntValue(TESTOSI,0);
     212      if (info_.cut) {
     213        printf("Sorry - can't do cuts with LOS as ruins delicate row order\n");
     214        abort();
     215      }
     216    }
     217    if (info_.cut) {
     218      int numberRows = info_.numberRows;
     219      int * whichRow = new int [numberRows];
     220      // Row copy
     221      const CoinPackedMatrix * matrixByRow = solver->getMatrixByRow();
     222      const double * elementByRow = matrixByRow->getElements();
     223      const int * column = matrixByRow->getIndices();
     224      const CoinBigIndex * rowStart = matrixByRow->getVectorStarts();
     225      const int * rowLength = matrixByRow->getVectorLengths();
     226     
     227      const double * rowLower = solver->getRowLower();
     228      const double * rowUpper = solver->getRowUpper();
     229      int nDelete=0;
     230      CglStored storedAmpl;
     231      for (int iRow=0;iRow<numberRows;iRow++) {
     232        if (info_.cut[iRow]) {
     233          whichRow[nDelete++]=iRow;
     234          int start = rowStart[iRow];
     235          storedAmpl.addCut(rowLower[iRow],rowUpper[iRow],
     236                            rowLength[iRow],column+start,elementByRow+start);
     237        }
     238      }
     239      control->addCutGenerator(&storedAmpl);
     240      solver->deleteRows(nDelete,whichRow);
     241      // and special matrix
     242      si->cleanMatrix()->deleteRows(nDelete,whichRow);
     243      delete [] whichRow;
     244    }
     245    // If we had a solution use it
     246    if (info_.primalSolution) {
     247      solver->setColSolution(info_.primalSolution);
     248    }
     249    // status
     250    if (info_.rowStatus) {
     251      unsigned char * statusArray = lpSolver->statusArray();
     252      memset(statusArray,0,lpSolver->numberColumns()+lpSolver->numberRows());
     253      int i;
     254      for (i=0;i<info_.numberColumns;i++)
     255        statusArray[i]=(char)info_.columnStatus[i];
     256      statusArray+=info_.numberColumns;
     257      for (i=0;i<info_.numberRows;i++)
     258        statusArray[i]=(char)info_.rowStatus[i];
     259      CoinWarmStartBasis * basis = lpSolver->getBasis();
     260      solver->setWarmStart(basis);
     261      delete basis;
     262    }
     263    freeArrays1(&info_);
     264    // modify objective if necessary
     265    solver->setObjSense(info_.direction);
     266    solver->setDblParam(OsiObjOffset,info_.offset);
     267    if (info_.offset) {
     268      sprintf(generalPrint,"Ampl objective offset is %g",
     269              info_.offset);
     270      generalMessageHandler->message(CLP_GENERAL,generalMessages)
     271        << generalPrint
     272        <<CoinMessageEol;
     273    }
     274    // Set integer variables (unless nonlinear when set)
     275    if (!info_.nonLinear) {
     276      for (int i=info_.numberColumns-info_.numberIntegers;
     277           i<info_.numberColumns;i++)
     278        solver->setInteger(i);
     279    }
     280    // change argc etc
     281    argc = info_.numberArguments;
     282    argv = info_.arguments;
     283    return 0;
     284  } else {
     285    return -1;
     286  }
     287  abort();
     288  return -1;
     289}
     290// Export 1 OsiClpSolver, 2 CbcModel - add 10 if infeasible from odd situation
     291void
     292CbcAmpl::exportSolution(CbcSolver * model, int mode,const char * message)
     293{
     294  OsiClpSolverInterface * solver = model->originalSolver();
     295  if (!solver) {
     296    solver = dynamic_cast< OsiClpSolverInterface*> (model->model()->solver());
     297    assert (solver);
     298  }
     299  ClpSimplex * lpSolver = solver->getModelPtr();
     300  int numberColumns=lpSolver->numberColumns();
     301  int numberRows = lpSolver->numberRows();
     302  double totalTime = CoinCpuTime()-model->startTime();
     303  if (mode==1) {
     304    double value = lpSolver->getObjValue()*lpSolver->getObjSense();
     305    char buf[300];
     306    int pos=0;
     307    int iStat = lpSolver->status();
     308    if (iStat==0) {
     309      pos += sprintf(buf+pos,"optimal," );
     310    } else if (iStat==1) {
     311      // infeasible
     312      pos += sprintf(buf+pos,"infeasible,");
     313    } else if (iStat==2) {
     314      // unbounded
     315      pos += sprintf(buf+pos,"unbounded,");
     316    } else if (iStat==3) {
     317      pos += sprintf(buf+pos,"stopped on iterations or time,");
     318    } else if (iStat==4) {
     319      iStat = 7;
     320      pos += sprintf(buf+pos,"stopped on difficulties,");
     321    } else if (iStat==5) {
     322      iStat = 3;
     323      pos += sprintf(buf+pos,"stopped on ctrl-c,");
     324    } else {
     325      pos += sprintf(buf+pos,"status unknown,");
     326      iStat=6;
     327    }
     328    info_.problemStatus=iStat;
     329    info_.objValue = value;
     330    pos += sprintf(buf+pos," objective %.*g",ampl_obj_prec(),
     331                   value);
     332    sprintf(buf+pos,"\n%d iterations",
     333            lpSolver->getIterationCount());
     334    free(info_.primalSolution);
     335    info_.primalSolution = (double *) malloc(numberColumns*sizeof(double));
     336    CoinCopyN(lpSolver->primalColumnSolution(),numberColumns,info_.primalSolution);
     337    free(info_.dualSolution);
     338    info_.dualSolution = (double *) malloc(numberRows*sizeof(double));
     339    CoinCopyN(lpSolver->dualRowSolution(),numberRows,info_.dualSolution);
     340    CoinWarmStartBasis * basis = lpSolver->getBasis();
     341    free(info_.rowStatus);
     342    info_.rowStatus = (int *) malloc(numberRows*sizeof(int));
     343    free(info_.columnStatus);
     344    info_.columnStatus = (int *) malloc(numberColumns*sizeof(int));
     345    // Put basis in
     346    int i;
     347    // free,basic,ub,lb are 0,1,2,3
     348    for (i=0;i<numberRows;i++) {
     349      CoinWarmStartBasis::Status status = basis->getArtifStatus(i);
     350      info_.rowStatus[i]=status;
     351    }
     352    for (i=0;i<numberColumns;i++) {
     353      CoinWarmStartBasis::Status status = basis->getStructStatus(i);
     354      info_.columnStatus[i]=status;
     355    }
     356    // put buffer into info_
     357    strcpy(info_.buffer,buf);
     358    delete basis;
     359  } else if (mode==2) {
     360    CbcModel * babModel = model->model();
     361    int iStat = babModel->status();
     362    int iStat2 = babModel->secondaryStatus();
     363    double value = babModel->getObjValue()*lpSolver->getObjSense();
     364    char buf[300];
     365    int pos=0;
     366    if (iStat==0) {
     367      if (babModel->getObjValue()<1.0e40) {
     368        pos += sprintf(buf+pos,"optimal," );
     369      } else {
     370        // infeasible
     371        iStat=1;
     372        pos += sprintf(buf+pos,"infeasible,");
     373      }
     374    } else if (iStat==1) {
     375      if (iStat2!=6)
     376        iStat=3;
     377      else
     378        iStat=4;
     379      std::string minor[]={"","","gap","nodes","time","","solutions","user ctrl-c"};
     380      pos += sprintf(buf+pos,"stopped on %s,",minor[iStat2].c_str());
     381    } else if (iStat==2) {
     382      iStat = 7;
     383      pos += sprintf(buf+pos,"stopped on difficulties,");
     384    } else if (iStat==5) {
     385      iStat = 3;
     386      pos += sprintf(buf+pos,"stopped on ctrl-c,");
     387    } else {
     388      pos += sprintf(buf+pos,"status unknown,");
     389      iStat=6;
     390    }
     391    info_.problemStatus=iStat;
     392    info_.objValue = value;
     393    if (babModel->getObjValue()<1.0e40) {
     394      int precision = ampl_obj_prec();
     395      if (precision>0)
     396        pos += sprintf(buf+pos," objective %.*g",precision,
     397                       value);
     398      else
     399        pos += sprintf(buf+pos," objective %g",value);
     400    }
     401    sprintf(buf+pos,"\n%d nodes, %d iterations, %g seconds",
     402            babModel->getNodeCount(),
     403            babModel->getIterationCount(),
     404            totalTime);
     405    if (babModel->bestSolution()) {
     406      free(info_.primalSolution);
     407      info_.primalSolution = (double *) malloc(numberColumns*sizeof(double));
     408      CoinCopyN(lpSolver->primalColumnSolution(),numberColumns,info_.primalSolution);
     409      free(info_.dualSolution);
     410      info_.dualSolution = (double *) malloc(numberRows*sizeof(double));
     411      CoinCopyN(lpSolver->dualRowSolution(),numberRows,info_.dualSolution);
     412    } else {
     413      info_.primalSolution=NULL;
     414      info_.dualSolution=NULL;
     415    }
     416    // put buffer into info
     417    strcpy(info_.buffer,buf);
     418  } else if (mode==11||mode==12) {
     419    // infeasible
     420    info_.problemStatus=1;
     421    info_.objValue = 1.0e100;
     422    sprintf(info_.buffer,"%s",message);
     423    info_.primalSolution=NULL;
     424    info_.dualSolution=NULL;
     425  }
     426}
     427// Export Data (i.e. at very end)
     428void
     429CbcAmpl::exportData(CbcSolver * model)
     430{
     431  writeAmpl(&info_);
     432  freeArrays2(&info_);
     433  freeArgs(&info_);
     434}
     435// Get useful stuff
     436void
     437CbcAmpl::fillInformation(CbcSolver * model,
     438                         CbcSolverUsefulData & info)
     439{
     440  memset(&info,0,sizeof(info));
     441  info.priorities_ = info_.priorities;
     442  info.sosPriority_ = info_.sosPriority;
     443  info.branchDirection_ = info_.branchDirection;
     444  info.primalSolution_ = info_.primalSolution;
     445  info.pseudoDown_ = info_.pseudoDown;
     446  info.pseudoUp_ = info_.pseudoUp;
     447}
     448void addAmplToCbc(CbcSolver * control)
     449{
     450  CbcAmpl ampl;
     451  control->addUserFunction(&ampl);
     452}
    16453extern "C" {
    17454  //# include "getstub.h"
  • trunk/Cbc/src/CoinSolve.cpp

    r779 r789  
    244244#else
    245245#include "CbcSolver.hpp"
     246void addAmplToCbc(CbcSolver *);
    246247int main (int argc, const char *argv[])
    247248{
     
    250251  // initialize
    251252  control.fillValuesInSolver();
    252   return control.solve (argc, argv, 2);
     253  addAmplToCbc(&control);
     254  return control.solve (argc, argv, 1);
    253255}
    254256#endif
Note: See TracChangeset for help on using the changeset viewer.