Changeset 893 for trunk


Ignore:
Timestamp:
Aug 9, 2012 10:48:19 AM (8 years ago)
Author:
pbelotti
Message:

fixed solution size in FP that caused some segfaults. Changed interface to make some vectors read only

Location:
trunk/Couenne/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Couenne/src/heuristics/CouenneFPFindSolution.cpp

    r889 r893  
    2525
    2626/// find a feasible or optimal solution of MILP
    27 double CouenneFeasPump::findSolution (double* &sol, int niter, int* nsuciter) {
     27double CouenneFeasPump::findSolution (double* &iSol, int niter, int* nsuciter) {
    2828
    2929  /// As found on the notes, these methods can be used, from the most
     
    6363  if (useSCIP_ && problem_ -> nIntVars () > 0) { // if LP, use Clp below
    6464
    65     SCIP_RETCODE retcode = ScipSolve (sol, niter, nsuciter, obj);
     65    SCIP_RETCODE retcode = ScipSolve (iSol, niter, nsuciter, obj);
    6666
    6767    if (retcode != SCIP_OKAY) {
     
    7878     else                             milp_ -> initialSolve ();
    7979
    80      if (!sol)
    81        sol = new CouNumber [problem_ -> nVars ()];
     80     if (!iSol)
     81       iSol = new CouNumber [problem_ -> nVars ()];
    8282
    8383     if (milp_ -> getColSolution ())
    84        CoinCopyN (milp_ -> getColSolution (), problem_ -> nVars (), sol);
     84       CoinCopyN (milp_ -> getColSolution (), problem_ -> nVars (), iSol);
    8585     else {
    8686
    87        if (sol)
    88          delete [] sol;
    89        sol = NULL;
     87       if (iSol)
     88         delete [] iSol;
     89       iSol = NULL;
    9090     }
    9191
  • trunk/Couenne/src/heuristics/CouenneFPSolveMILP.cpp

    r891 r893  
    5151/// (according to the l-1 norm of the Hessian) to the current
    5252/// NLP-feasible (but fractional) solution nsol
    53 CouNumber CouenneFeasPump::solveMILP (CouNumber *nSol0, CouNumber *&iSol, int niter, int* nsuciter) {
     53CouNumber CouenneFeasPump::solveMILP (const CouNumber *nSol0, CouNumber *&iSol, int niter, int* nsuciter) {
    5454
    5555  // The problem is of the form
  • trunk/Couenne/src/heuristics/CouenneFPSolveNLP.cpp

    r883 r893  
    2525
    2626/// obtain continuous (if fractional) solution
    27 CouNumber CouenneFeasPump::solveNLP (CouNumber *iSol, CouNumber *&nSol) {
     27CouNumber CouenneFeasPump::solveNLP (const CouNumber *iSol, CouNumber *&nSol) {
    2828
    2929  // Solve the continuous nonlinear programming problem
  • trunk/Couenne/src/heuristics/CouenneFPpool.cpp

    r890 r893  
    217217/// finds, in pool, solution x closest to nSol; removes it from the
    218218/// pool and overwrites it to sol
    219 void CouenneFPpool::findClosestAndReplace (double *&sol, double *nSol, int nvars)  {
     219void CouenneFPpool::findClosestAndReplace (double *&sol, const double *nSol, int nvars)  {
    220220
    221221   double bestdist = COIN_DBL_MAX;
  • trunk/Couenne/src/heuristics/CouenneFPpool.hpp

    r890 r893  
    121121    /// finds, in pool, solution x closest to sol; removes it from the
    122122    /// pool and overwrites it to sol
    123     void findClosestAndReplace (double *&sol, double *nSol, int nvars) ;
     123    void findClosestAndReplace (double *&sol, const double *nSol, int nvars) ;
    124124  };
    125125}
  • trunk/Couenne/src/heuristics/CouenneFPscipSolve.cpp

    r892 r893  
    137137    (void) SCIPsnprintf(varname, SCIP_MAXSTRLEN, "x_%d", i);
    138138    SCIP_CALL( SCIPcreateVar(scip, &vars[i], varname,
    139                              neglect ? 0. : lbs  [i],
    140                              neglect ? 0. : ubs  [i],
    141                              neglect ? 0. : objs [i],
     139                             lbs  [i],
     140                             ubs  [i],
     141                             objs [i],
    142142                             vartypes[i] == 0 ? SCIP_VARTYPE_CONTINUOUS : (vartypes[i] == 1 ? SCIP_VARTYPE_BINARY : SCIP_VARTYPE_INTEGER),
    143143                             TRUE, FALSE, NULL, NULL, NULL, NULL, NULL) );
     
    162162          problem_ -> Ub (j) - problem_ -> Lb (j) > .5) {
    163163
    164         assert (fabs (lbs [j] - problem_ -> Lb (j)) < COUENNE_EPS);
    165         assert (fabs (ubs [j] - problem_ -> Ub (j)) < COUENNE_EPS);
    166         assert (fabs (x [j] - floor (x [j] + .5))   < COUENNE_EPS);
     164        assert (fabs (lbs [j] - problem_ -> Lb (j)) < SCIPfeastol (scip));
     165        assert (fabs (ubs [j] - problem_ -> Ub (j)) < SCIPfeastol (scip));
     166        assert (fabs (x [j] - floor (x [j] + .5))   < SCIPfeastol (scip));
    167167
    168168        assert (nEntries <= 2*nvars - 2);
     
    409409         
    410410    // solve the MILP
    411     SCIP_CALL( SCIPsolve(scip) );
     411
     412    SCIP_RETCODE retcode = SCIPsolve(scip);
     413
     414    if (retcode != SCIP_OKAY) {
     415
     416      printf ("SCIPsolve did not succeed\n");
     417      goto TERMINATION;
     418    }
    412419
    413420    nscipsols =  SCIPgetNSols(scip);
     
    431438
    432439        if (!sol)
    433           sol = new CouNumber [nvars];
     440          sol = new CouNumber [problem_ -> nVars ()];
    434441
    435442        // get solution values and objective of incumbent
    436         SCIP_CALL( SCIPgetSolVals(scip, bestsol, nvars, vars, sol) );
     443        SCIP_CALL( SCIPgetSolVals(scip, bestsol, problem_ -> nVars (), vars, sol) );
    437444        obj = SCIPgetSolOrigObj(scip, bestsol);
    438445
     
    451458           
    452459          // get solution values
    453           SCIP_CALL( SCIPgetSolVals(scip, scipsols[i], nvars, vars, tmpsol) );
     460          SCIP_CALL( SCIPgetSolVals(scip, scipsols[i], problem_ -> nVars (), vars, tmpsol) );
    454461          CouenneFPsolution couennesol = CouenneFPsolution (problem_, tmpsol);
    455462
     
    490497
    491498  } while (solveagain);
     499
     500  ////////////////////////////////////////////////////////////////
     501
     502 TERMINATION:
    492503   
    493504  // release variables before freeing them
  • trunk/Couenne/src/heuristics/CouenneFeasPump.cpp

    r890 r893  
    3333using namespace Couenne;
    3434
    35 void printDist   (CouenneProblem *p, double *iSol, double *nSol);
    36 void printCmpSol (CouenneProblem *p, double *iSol, double *nSol, int direction);
     35void printDist   (CouenneProblem *p, const double *iSol, double *nSol);
     36void printCmpSol (CouenneProblem *p, const double *iSol, double *nSol, int direction);
    3737
    3838// Solve
     
    523523
    524524        if (e -> isInteger () &&
    525             (fabs (iSol [i] - ceil (iSol [i] - .5)) > 1e-4))
     525            (fabs (iSol [i] - ceil (iSol [i] - .5)) > COUENNE_EPS))
    526526          ++nNonint;
    527527
     
    536536    if (problem_ -> Jnlst () -> ProduceOutput (J_ERROR, J_NLPHEURISTIC)) {
    537537
    538       printDist   (problem_,             iSol, nSol);
     538      printDist   (problem_, iSol, nSol);
    539539      printCmpSol (problem_, iSol, nSol, 0);
    540540    }
     
    553553                                       true, // stopAtFirstViol
    554554                                       true); // checkALL
    555 
    556 // #ifdef FM_CHECKNLP2
    557 //       isChecked = problem_->checkNLP2(nSol, 0, false, // do not care about obj
    558 //                                    true, // stopAtFirstViol
    559 //                                    true, // checkALL
    560 //                                    problem_->getFeasTol());
    561 //       if(isChecked) {
    562 //      z = problem_->getRecordBestSol()->getModSolVal();
    563 //       }
    564 // #else /* not FM_CHECKNLP2 */
    565 //       isChecked = problem_ -> checkNLP (nSol, z, true);
    566 // #endif  /* not FM_CHECKNLP2 */
    567555    }
    568556
     
    591579#endif                 // - endif -------------------------
    592580
    593 // #ifdef FM_CHECKNLP2
    594 // #ifdef FM_TRACE_OPTSOL
    595 //       problem_->getRecordBestSol()->update();
    596 //       best = problem_->getRecordBestSol()->getSol();
    597 //       objVal = problem_->getRecordBestSol()->getVal();
    598 // #else /* not FM_TRACE_OPTSOL */
    599 //       best = problem_->getRecordBestSol()->getModSol(problem_ -> nVars ());
    600 //       objVal = z;
    601 // #endif /* not FM_TRACE_OPTSOL */
    602 // #else /* not FM_CHECKNLP2 */
    603 // #ifdef FM_TRACE_OPTSOL
    604 //       problem_->getRecordBestSol()->update(nSol, problem_->nVars(),
    605 //                                         z, problem_->getFeasTol());
    606 //       best = problem_->getRecordBestSol()->getSol();
    607 //       objVal = problem_->getRecordBestSol()->getVal();
    608 // #else /* not FM_TRACE_OPTSOL */
    609 //       best   = nSol;
    610 //       objVal = z;
    611 // #endif /* not FM_TRACE_OPTSOL */
    612 // #endif /* not FM_CHECKNLP2 */
    613      
    614581      if (z < problem_ -> getCutOff ()) {
    615582
     
    661628
    662629    // fix integer coordinates of current (MINLP feasible!) solution
    663     // and set it as initial (obviously NLP feasible) solution
    664 
    665     fixIntVariables (best);
    666     nlp_ -> setInitSol (best);
    667 
    668     ////////////////////////////////////////////////////////////////
    669 
    670     //app_ -> Options () -> SetStringValue ("fixed_variable_treatment", "make_parameter");
    671 
    672     // Solve with original objective function
    673     status = app_ -> OptimizeTNLP (nlp_);
    674 
    675     ////////////////////////////////////////////////////////////////
     630    // and, if feasible, set it as initial (obviously NLP feasible)
     631    // solution
     632
     633    if (fixIntVariables (best)) {
     634
     635      nlp_ -> setInitSol (best);
     636
     637      ////////////////////////////////////////////////////////////////
     638
     639      //app_ -> Options () -> SetStringValue ("fixed_variable_treatment", "make_parameter");
     640
     641      // Solve with original objective function
     642      status = app_ -> OptimizeTNLP (nlp_);
     643
     644      ////////////////////////////////////////////////////////////////
     645
     646      if ((status != Solve_Succeeded) &&
     647          (status != Solved_To_Acceptable_Level))
     648 
     649        problem_ -> Jnlst () -> Printf (J_ERROR, J_NLPHEURISTIC,
     650                                        "Feasibility Pump: error in final NLP problem (due to fixing integer variables?)\n");
     651
     652      // if found a solution with the last NLP, check & save it
     653
     654      double z = nlp_ -> getSolValue ();
     655
     656      // check if newly found NLP solution is also integer (unlikely...)
     657      bool isChecked = false;
     658
     659      if (nSol) {
     660
     661        problem_ -> Jnlst () -> Printf (J_WARNING, J_NLPHEURISTIC, "FP: found nlp solution, check it\n");
     662
     663        isChecked = problem_ -> checkNLP0 (nSol, z, true,
     664                                           false,
     665                                           true,
     666                                           true);
     667      }
     668
     669      if (nSol &&
     670          isChecked &&
     671          (z < problem_ -> getCutOff ())) {
     672
     673        problem_ -> Jnlst () -> Printf (J_WARNING, J_NLPHEURISTIC, "FP: feasible solution is improving\n");
     674
     675#ifdef FM_TRACE_OPTSOL
     676
     677#ifdef FM_CHECKNLP2
     678        problem_->getRecordBestSol()->update();
     679#else
     680        problem_->getRecordBestSol()->update(nSol, problem_->nVars(), z, problem_->getFeasTol());
     681#endif
     682        best = problem_->getRecordBestSol()->getSol();
     683        objVal = problem_->getRecordBestSol()->getVal();
     684#else
     685
     686#ifdef FM_CHECKNLP2
     687        best = problem_->getRecordBestSol()->getModSol(problem_ -> nVars ());
     688#else
     689        best   = nSol;
     690#endif
     691        objVal = z;
     692#endif
     693
     694        problem_ -> setCutOff (objVal);
     695      }
     696    }
    676697
    677698    problem_ -> domain () -> pop ();
    678699
    679     if ((status != Solve_Succeeded) &&
    680         (status != Solved_To_Acceptable_Level))
    681  
    682       problem_ -> Jnlst () -> Printf (J_ERROR, J_NLPHEURISTIC,
    683                                       "Feasibility Pump: error in final NLP problem (due to fixing integer variables?)\n");
    684 
    685     // if found a solution with the last NLP, check & save it
    686 
    687     double z = nlp_ -> getSolValue ();
    688 
    689     // check if newly found NLP solution is also integer (unlikely...)
    690     bool isChecked = false;
    691 
    692     if (nSol) {
    693 
    694       problem_ -> Jnlst () -> Printf (J_WARNING, J_NLPHEURISTIC, "FP: found nlp solution, check it\n");
    695 
    696       isChecked = problem_ -> checkNLP0 (nSol, z, true,
    697                                          false,
    698                                          true,
    699                                          true);
    700      
    701 // #ifdef FM_CHECKNLP2
    702 //       isChecked = problem_->checkNLP2(nSol, 0, false, // do not care about obj
    703 //                                    true, // stopAtFirstViol
    704 //                                    true, // checkALL
    705 //                                    problem_->getFeasTol());
    706 //       if (isChecked) {
    707 //      z = problem_->getRecordBestSol()->getModSolVal();
    708 //       }
    709 // #else /* not FM_CHECKNLP2 */
    710 //       isChecked = problem_ -> checkNLP (nSol, z, true);
    711 // #endif  /* not FM_CHECKNLP2 */
    712 
    713     }
    714 
    715     if (nSol &&
    716         isChecked &&
    717         (z < problem_ -> getCutOff ())) {
    718 
    719       problem_ -> Jnlst () -> Printf (J_WARNING, J_NLPHEURISTIC, "FP: feasible solution is improving\n");
    720 
    721 #ifdef FM_TRACE_OPTSOL
    722 
    723 #ifdef FM_CHECKNLP2
    724       problem_->getRecordBestSol()->update();
    725 #else
    726       problem_->getRecordBestSol()->update(nSol, problem_->nVars(), z, problem_->getFeasTol());
    727 #endif
    728       best = problem_->getRecordBestSol()->getSol();
    729       objVal = problem_->getRecordBestSol()->getVal();
    730 #else
    731 
    732 #ifdef FM_CHECKNLP2
    733       best = problem_->getRecordBestSol()->getModSol(problem_ -> nVars ());
    734 #else
    735       best   = nSol;
    736 #endif
    737       objVal = z;
    738 #endif
    739 
    740 // #ifdef FM_CHECKNLP2
    741 // #ifdef FM_TRACE_OPTSOL
    742 //       problem_->getRecordBestSol()->update();
    743 //       best = problem_->getRecordBestSol()->getSol();
    744 //       objVal = problem_->getRecordBestSol()->getVal();
    745 // #else /* not FM_TRACE_OPTSOL */
    746 //       best = problem_->getRecordBestSol()->getModSol(problem_ -> nVars ());
    747 //       objVal = z;
    748 // #endif /* not FM_TRACE_OPTSOL */
    749 // #else /* not FM_CHECKNLP2 */
    750 // #ifdef FM_TRACE_OPTSOL
    751 //       problem_->getRecordBestSol()->update(nSol, problem_->nVars(),
    752 //                                         z, problem_->getFeasTol());
    753 //       best = problem_->getRecordBestSol()->getSol();
    754 //       objVal = problem_->getRecordBestSol()->getVal();
    755 // #else /* not FM_TRACE_OPTSOL */
    756 //       best   = nSol;
    757 //       objVal = z;
    758 // #endif /* not FM_TRACE_OPTSOL */
    759 // #endif /* not FM_CHECKNLP2 */
    760 
    761       problem_ -> setCutOff (objVal);
    762     }
    763   }
    764 
    765   if (retval > 0) {
    766700    if (problem_ -> Jnlst () -> ProduceOutput (J_ERROR, J_NLPHEURISTIC)) {
    767701      printf ("FP: returning MINLP feasible solution:\n");
    768702      printDist (problem_, best, nSol);
    769703    }
     704
    770705    CoinCopyN (best, problem_ -> nVars (), newSolution);
    771706  }
     
    793728// gather data on single solution for later printout
    794729void compDistSingle (CouenneProblem *p,
    795                      int n, double *v,
     730                     int n,
     731                     const double *v,
    796732                     double &norm,
    797733                     int &nInfI,
     
    855791
    856792// print solutions and distances
    857 void printDist (CouenneProblem *p, double *iSol, double *nSol) {
     793void printDist (CouenneProblem *p, const double *iSol, double *nSol) {
    858794
    859795  int nInfII = -1, nInfNI = -1, nInfIN = -1, nInfNN = -1;
     
    893829#define WRAP 3
    894830
    895 void printCmpSol (CouenneProblem *p, double *iSol, double *nSol, int direction) {
     831void printCmpSol (CouenneProblem *p, const double *iSol, double *nSol, int direction) {
    896832
    897833  int n = p -> nVars ();
  • trunk/Couenne/src/heuristics/CouenneFeasPump.hpp

    r889 r893  
    9999    /// (according to the l-1 norm of the hessian) to the current
    100100    /// NLP-feasible (but fractional) solution nsol
    101     virtual CouNumber solveMILP (CouNumber *nSol, CouNumber *&iSol, int niter, int* nsuciter);
     101    virtual CouNumber solveMILP (const CouNumber *nSol, CouNumber *&iSol, int niter, int* nsuciter);
    102102
    103103    /// obtain solution to NLP
    104     virtual CouNumber solveNLP  (CouNumber *nSol, CouNumber *&iSol);
     104    virtual CouNumber solveNLP  (const CouNumber *nSol, CouNumber *&iSol);
    105105
    106106    /// set new expression as the NLP objective function using
     
    113113    /// re-solve. Returns false if restriction infeasible, true
    114114    /// otherwise
    115     bool fixIntVariables (double *sol);
     115    bool fixIntVariables (const double *sol);
    116116
    117117    /// initialize options to be read later
  • trunk/Couenne/src/heuristics/CouenneFeasPumpConstructors.cpp

    r890 r893  
    392392/// Reads a (possibly fractional) solution and fixes the integer
    393393/// components in the nonlinear problem for later re-solve
    394 bool CouenneFeasPump::fixIntVariables (double *sol) {
     394bool CouenneFeasPump::fixIntVariables (const double *sol) {
    395395
    396396  assert (sol);
  • trunk/Couenne/src/interfaces/CouenneTNLP.cpp

    r884 r893  
    175175
    176176/// set initial solution
    177 void CouenneTNLP::setInitSol (double *sol) {
     177void CouenneTNLP::setInitSol (const double *sol) {
    178178
    179179  if (sol) {
  • trunk/Couenne/src/interfaces/CouenneTNLP.hpp

    r813 r893  
    4848
    4949    /// set initial solution
    50     void setInitSol (double *sol);
     50    void setInitSol (const double *sol);
    5151
    5252    /// returns best solution (if it exists)
Note: See TracChangeset for help on using the changeset viewer.