Ejemplo n.º 1
0
void loadProblem(EnvPtr env, MINOTAUR_AMPL::AMPLInterface* iface,
                 ProblemPtr &oinst, double *obj_sense)
{
  Timer *timer = env->getNewTimer();
  OptionDBPtr options = env->getOptions();
  JacobianPtr jac;
  HessianOfLagPtr hess;
  const std::string me("qg: ");

  timer->start();
  oinst = iface->readInstance(options->findString("problem_file")->getValue());
  env->getLogger()->msgStream(LogInfo) << me 
    << "time used in reading instance = " << std::fixed 
    << std::setprecision(2) << timer->query() << std::endl;

  // display the problem
  oinst->calculateSize();
  if (options->findBool("display_problem")->getValue()==true) {
    oinst->write(env->getLogger()->msgStream(LogNone), 12);
  }
  if (options->findBool("display_size")->getValue()==true) {
    oinst->writeSize(env->getLogger()->msgStream(LogNone));
  }
  // create the jacobian
  if (false==options->findBool("use_native_cgraph")->getValue()) {
    jac = (MINOTAUR_AMPL::AMPLJacobianPtr) 
      new MINOTAUR_AMPL::AMPLJacobian(iface);
    oinst->setJacobian(jac);

    // create the hessian
    hess = (MINOTAUR_AMPL::AMPLHessianPtr)
      new MINOTAUR_AMPL::AMPLHessian(iface);
    oinst->setHessian(hess);
  }

  // set initial point
  oinst->setInitialPoint(iface->getInitialPoint(), 
      oinst->getNumVars()-iface->getNumDefs());

  if (oinst->getObjective() &&
      oinst->getObjective()->getObjectiveType()==Maximize) {
    *obj_sense = -1.0;
    env->getLogger()->msgStream(LogInfo) << me 
      << "objective sense: maximize (will be converted to Minimize)"
      << std::endl;
  } else {
    *obj_sense = 1.0;
    env->getLogger()->msgStream(LogInfo) << me 
      << "objective sense: minimize" << std::endl;
  }

  delete timer;
}
Ejemplo n.º 2
0
//! main routine implementing outer approximation
int main(int argc, char** argv)
{
  //! Declaration of Variables ============================================
  //! interface to AMPL (NULL):
  MINOTAUR_AMPL::AMPLInterfacePtr iface = MINOTAUR_AMPL::AMPLInterfacePtr();  
  MINOTAUR_AMPL::JacobianPtr jPtr;            //! Jacobian read from AMPL
  MINOTAUR_AMPL::HessianOfLagPtr hPtr;        //! Hessian read from AMPL

  //! environment, timers, options:
  EnvPtr env = (EnvPtr) new Environment();
  TimerFactory *tFactory = new TimerFactory();     
  Timer *timer=tFactory->getTimer();
  OptionDBPtr options;                        //! AMPL and MINOTAUR options

  //! problem pointers (MINLP, NLP, MILP):
  ProblemPtr minlp;                           //! MINLP instance to be solved
  ProblemPtr milp;                            //! MILP master problem

  //! solver pointers, including status
  FilterSQPEngine e(env);                     //! NLP engine: FilterSQP
  EngineStatus status;

  //! pointers to manipulate variables & constraints
  VariablePtr v, objVar;                      //! variable pointer (objective)
  ObjectivePtr objFun;                        //! remember objective function pt.

  //! local variables
  Double *x, *xsoln;
  Double *lobnd, *upbnd;
  Double objfLo = -INFINITY, objfUp = INFINITY, objfNLP, objfMIP;
  Double tol = 1E-2;
  Int    feasibleNLP = 0, iterOA = 1, n;
  // ======================================================================

  //! start the timer
  timer->start();

  //! make sure solver is used correctly
  if (argc < 2) {
    usage();
    return -1;
  }

  //! add AMPL options & flags to environment: flags (options without values)
  options = env->getOptions();
  add_ampl_flags(options);
  env->readOptions(argc, argv);                  //! parse options
  if (!checkUserOK(options,env)) goto CLEANUP;   //! check if user needs help

  //! read MINLP from AMPL & create Hessian/Jacobian for NLP solves
  iface = (MINOTAUR_AMPL::AMPLInterfacePtr) new MINOTAUR_AMPL::AMPLInterface(env);
  minlp = iface->readInstance(options->findString("problem_file")->getValue(),false);
  jPtr  = (MINOTAUR_AMPL::JacobianPtr)      new MINOTAUR_AMPL::Jacobian(iface);
  hPtr  = (MINOTAUR_AMPL::HessianOfLagPtr)  new MINOTAUR_AMPL::HessianOfLag(iface);
  minlp->setJacobian(jPtr);
  minlp->setHessian(hPtr);
    
  //! Display number of constraints and variables, and MINLP problem
  minlp->calculateSize();
  n = minlp->getNumVars();
  std::cout << "No. of vars, cons = " << minlp->getNumVars() 
            << minlp->getNumCons() << std::endl;
  std::cout << std::endl << "The MINLP problem is: " << std::endl;
  minlp->write(std::cout);

  //! load the MINLP into the NLP solver (FilterSQP)
  e.load(minlp);

  //! get initial point & save original bounds
  x     = new Double[n];
  xsoln = new Double[n];
  lobnd = new Double[n];
  upbnd = new Double[n];
  std::copy(iface->getInitialPoint(),iface->getInitialPoint()+n,x);
  for (VariableConstIterator i=minlp->varsBegin(); i!=minlp->varsEnd(); ++i) {
    v = *i;
    lobnd[v->getId()] = v->getLb();
    upbnd[v->getId()] = v->getUb();
  }

  //! initialize the MILP master problem by copying variables & linear c/s
  milp   = (ProblemPtr) new Problem();
  objFun = minlp->getObjective();
  objVar = VariablePtr();
  initMaster(minlp, milp, objVar, objFun, x);

  while ((objfLo <= objfUp)&&(iterOA<4)){

    std::cout << "Iteration " << iterOA << std::endl 
              << "===============" << std::endl << std::endl;

    //! set-up and solve NLP(y) with fixed integers
    solveNLP(minlp, e, x, objfNLP, feasibleNLP, n);
    std::cout << "Solved NLP " << iterOA << "  objective = " << objfNLP 
              << "  NLPfeasible = " << feasibleNLP << std::endl;
    if (feasibleNLP && (objfNLP-tol < objfUp)) {
      objfUp = objfNLP - tol;
      std::copy(x,x+n,xsoln);
    }
        
    //! update MILP master problem by adding outer approximations
    updateMaster(minlp, milp, objVar, objFun, objfUp, x, n);

    //! solve MILP master problem
    solveMaster(env, milp, x, &objfMIP, n);
    objfLo = objfMIP;

    iterOA = iterOA + 1;

  } // end while (objfLo <= objfUp) 

  //! output final result & timing
  std::cout << std::endl << "END outer-approximation: f(x) = " << objfUp 
            << "   time used = " << timer->query() << std::endl;

CLEANUP:
  //! free storage 
   delete timer;
   delete tFactory;
   if (minlp) {
     minlp->clear();
     delete [] x;
     delete [] xsoln;
     delete [] lobnd;
     delete [] upbnd;
   }
   if (milp) {
     milp->clear();
   }
  return 0;
} // end outer approximation main