/home/coin/SVN-release/OS-2.0.1/Bonmin/experimental/FP/FP.cpp

Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006 
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Authors :
00006 // Pierre Bonami, Carnegie Mellon University
00007 // Date:
00008 // 06/29/2006
00009 
00010 // Driver for Feasibility pump
00011 
00012 
00013 #if defined(_MSC_VER)
00014 // Turn off compiler warning about long names
00015 #  pragma warning(disable:4786)
00016 #endif
00017 #include <iomanip>
00018 #include <fstream>
00019 
00020 #include "CoinTime.hpp"
00021 
00022 #include "BonAmplInterface.hpp"
00023 #include "BonCbcParam.hpp"
00024 #include "BonCbc.hpp"
00025 #include "BonAmplTMINLP.hpp"
00026 #include "AmplTNLP.hpp"
00027 #include "FP.hpp"
00028 #include "BonIpoptSolver.hpp"
00029 
00030 void register_ALL_options( SmartPtr<RegisteredOptions> roptions );
00031 void set_ipopt_minlp_default(SmartPtr<OptionsList> Option);
00032 
00033 
00034 
00035 class AmplFP : public Bonmin::AmplTMINLP
00036 {
00037   public:
00038   virtual void fillApplicationOptions2(Ipopt::AmplOptionsList* amplOptList)
00039 {
00040     
00041     amplOptList->AddAmplOption("FP.Algo","FP.Algo",
00042                                AmplOptionsList::String_Option,
00043                                "specify minimal investment in share");
00044     amplOptList->AddAmplOption("FP.time_limit","FP.time_limit",
00045                                AmplOptionsList::Number_Option,
00046                                "Give time limit");
00047 }
00048 
00049 AmplFP(const SmartPtr<const Journalist>& jnlst, 
00050            const SmartPtr<OptionsList> options,
00051            char**& argv, 
00052            AmplSuffixHandler* suffix_handler /*=NULL*/,
00053            const std::string& appName,
00054            std::string* nl_file_content /* = NULL */):AmplTMINLP()
00055            {
00056                Initialize2(jnlst, options, argv, suffix_handler,
00057                appName, nl_file_content);
00058           }
00059 virtual void
00060 Initialize2(const SmartPtr<const Journalist>& jnlst, 
00061            const SmartPtr<OptionsList> options,
00062            char**& argv, 
00063            AmplSuffixHandler* suffix_handler =NULL,
00064            const std::string& appName = "fp",
00065            std::string* nl_file_content  = NULL )
00066            {
00067   SmartPtr<AmplOptionsList> ampl_options_list = new AmplOptionsList();
00068   fillAmplOptionList(GetRawPtr(ampl_options_list));
00069   fillApplicationOptions2(GetRawPtr(ampl_options_list) );
00070   std::string options_id = appName + "_options";
00071   ampl_tnlp_ = new AmplTNLP(jnlst, options, argv, suffix_handler, true,
00072                             ampl_options_list, options_id.c_str(),
00073                             appName.c_str(), appName.c_str(), nl_file_content);
00074            }
00075            public:
00076            AmplFP():AmplTMINLP(){}
00077            virtual AmplTMINLP * createEmpty(){return new AmplFP;}
00078 };
00079 
00080 class FPInterface : public Bonmin::AmplInterface
00081 {
00082 public:
00083   FPInterface(char **& argv):Bonmin::AmplInterface()
00084   {
00085     readAmplNlFile2(argv);
00086   }
00087   FPInterface(const FPInterface &other):
00088   Bonmin::AmplInterface(other)
00089   {}
00090   virtual OsiSolverInterface * clone()
00091   { return new FPInterface(*this);} 
00092 protected:
00093   virtual std::string appName() {return "FP";}
00094   virtual void registerApplicationOptions(Ipopt::SmartPtr<Ipopt::RegisteredOptions> roptions)
00095   {
00096     roptions->SetRegisteringCategory("FP options");
00097     roptions->AddStringOption4("Algo","type of FP algorithm",
00098                                "FP",
00099                                "FP", "Stand-alone FP",
00100                                "iFP", "iterated FP",
00101                                "OA", "Classical OA",
00102                                "eOA","Enhanced OA"
00103                                "");
00104   }
00105   void 
00106   readAmplNlFile2(char**& filename
00107                   )
00108   {
00109     app_ = new Bonmin::IpoptSolver();
00110     SmartPtr<RegisteredOptions> roptions = app_->RegOptions();
00111     register_ALL_options(roptions);
00112     registerApplicationOptions(roptions);
00113     
00114     // Call initalize to open output
00115     app_->Initialize("");
00116     // Read the bonmin.opt input file
00117     app_->Initialize("FP.opt");
00118     
00119     char * pbName = new char[strlen(filename[1])+1];
00120     strcpy(pbName, filename[1]);
00121     
00122     setStrParam(OsiProbName,std::string(pbName));
00123     delete [] pbName;
00124     
00125     
00126     if(!IsValid(tminlp_)) {
00127       amplTminlp_ = new AmplFP(ConstPtr(app_->Jnlst()), app_->Options(), filename,
00128                                NULL, appName() , NULL);
00129       tminlp_ = GetRawPtr(amplTminlp_);
00130     }
00131     else {
00132       Bonmin::AmplTMINLP * amplTMINLP = dynamic_cast<Bonmin::AmplTMINLP *> (GetRawPtr(tminlp_));
00133       if(amplTMINLP) {
00134         Bonmin::AmplTMINLP * newAmpl = amplTMINLP->createEmpty();
00135         newAmpl->Initialize(ConstPtr(app_->Jnlst()), app_->Options(), filename,
00136                             NULL, appName() , NULL);
00137         amplTminlp_ = newAmpl;
00138         tminlp_ = GetRawPtr(amplTminlp_);
00139       }
00140       else {
00141         amplTminlp_ = new AmplFP(ConstPtr(app_->Jnlst()), app_->Options(), filename,
00142                                      NULL, appName() , NULL);
00143         tminlp_ = GetRawPtr(amplTminlp_);
00144       }
00145     }
00146     problem_ = new Bonmin::TMINLP2TNLP(tminlp_);
00147     
00148     bool print_options_documentation;
00149     app_->Options()->GetBoolValue("print_options_documentation",
00150                                   print_options_documentation, "");
00151     if (print_options_documentation) {
00152       std::list<std::string> categories;
00153       categories.push_back("FP options");
00154       //    roptions->OutputLatexOptionDocumentation2(*app_->Jnlst(),categories);
00155       roptions->OutputOptionDocumentation(*app_->Jnlst(),categories);
00156     }
00157     
00158     int numcols = getNumCols();
00159     if(obj_)
00160       delete [] obj_;
00161     obj_ = new double[numcols];
00162     CoinFillN(obj_,numcols,1.);
00163     setStrParam(OsiProbName, std::string(filename[1]));
00164     extractInterfaceParams();
00165     hasBeenOptimized_ = false;
00166     feasibilityProblem_ = new Bonmin::TNLP2FPNLP
00167       (Ipopt::SmartPtr<Ipopt::TNLP>(Ipopt::GetRawPtr(problem_)));
00168   }
00169 };
00170 
00171 int iteratedFP(Bonmin::AmplInterface &nlpSolver, 
00172                bool standAlone, 
00173                double *&solution);
00174 
00175 int enhancedOA(Bonmin::AmplInterface &nlpSolver, 
00176                bool doFP, 
00177                double *&solution);
00178 
00179 int main (int argc, char *argv[])
00180 {
00181   using namespace Ipopt;
00182   
00183   FPInterface nlpSolver(argv);
00184   
00185                            
00186   //Set up done, now let's branch and bound
00187   //double time1 = CoinCpuTime();
00188   try {
00189     Ipopt::SmartPtr<Ipopt::OptionsList> Options = nlpSolver.retrieve_options();
00190     
00191     int algo;
00192     
00193     Options->GetEnumValue("Algo", algo, "FP.");
00194     Options->GetNumericValue("time_limit",params.maxTime_,"FP.");
00195     double * solution = NULL;
00196     if(algo==0)
00197       iteratedFP(nlpSolver,1, solution);
00198     else if (algo==1)
00199       iteratedFP(nlpSolver,0, solution);
00200     else if(algo==2)
00201       enhancedOA(nlpSolver,0, solution);
00202     else if(algo==3)
00203       enhancedOA(nlpSolver,1, solution);
00204     
00205     std::string message;
00206     if(solution==NULL) message="No solution";
00207       else message="Solution found";
00208     nlpSolver.writeAmplSolFile(message,solution,NULL);
00209     
00210   }
00211   catch(Bonmin::TNLPSolver::UnsolvedError *E) {
00212      E->printError(std::cerr);
00213     //There has been a failure to solve a problem with Ipopt.
00214     //And we will output file with information on what has been changed in the problem to make it fail.
00215     //Now depending on what algorithm has been called (B-BB or other) the failed problem may be at different place.
00216     //    const OsiSolverInterface &si1 = (algo > 0) ? nlpSolver : *model.solver();
00217 
00218   }
00219   catch(Bonmin::OsiTMINLPInterface::SimpleError &E) {
00220     std::cerr<<E.className()<<"::"<<E.methodName()
00221     <<std::endl
00222     <<E.message()<<std::endl;
00223   }
00224   catch(CoinError &E) {
00225     std::cerr<<E.className()<<"::"<<E.methodName()
00226     <<std::endl
00227     <<E.message()<<std::endl;
00228   }
00229   catch(...) {
00230     std::string pbName;
00231     
00232     nlpSolver.getStrParam(OsiProbName, pbName);
00233 
00234     std::cerr<<pbName<<" unrecognized excpetion"<<std::endl;
00235     std::cerr<<pbName<<"\t Finished \t exception"<<std::endl;
00236     throw;
00237   }
00238   
00239   return 0;
00240 }
00241 
00242 

Generated on Thu Oct 8 03:02:53 2009 by  doxygen 1.4.7