00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "BonTNLPSolver.hpp"
00012 #include "BonColReader.hpp"
00013
00014
00015 #include "IpBlas.hpp"
00016
00017
00018 #include <fstream>
00019
00020 namespace Bonmin{
00021 using namespace Ipopt;
00022
00023
00024 #if 0
00025 string TNLPSolver::returnCodes[TNLPSolver::numReturnCodes] = {
00026 "Hit iteration limit" ,
00027 "Some error was made in computations." ,
00028 "Problem has more equations than free variables." ,
00029 "Problem is not well defined.",
00030 "Illegal option set.",
00031 "Exception in some the third-party code used by solver." ,
00032 "Unrecognized exception.",
00033 "Problem solved to optimality",
00034 "Problem solvedd to acceptable level of tolerance",
00035 "Problem is infeasible" ,
00036 "Problem is unbounded", };
00037 #endif
00038
00039
00040 TNLPSolver::TNLPSolver()
00041 {
00042 initializeOptionsAndJournalist();
00043 }
00044
00045 TNLPSolver::TNLPSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
00046 Ipopt::SmartPtr<Ipopt::OptionsList> options,
00047 Ipopt::SmartPtr<Ipopt::Journalist> journalist):
00048 journalist_(journalist),
00049 options_(options),
00050 roptions_(roptions)
00051 {
00052 }
00053
00054 TNLPSolver::~TNLPSolver()
00055 {}
00056
00057
00058 bool
00059 TNLPSolver::zeroDimension(const Ipopt::SmartPtr<Ipopt::TNLP>& tnlp, ReturnStatus &optimizationStatus)
00060 {
00061
00062 int n,m,dum1, dum2;
00063 Ipopt::TNLP::IndexStyleEnum dum3;
00064 tnlp->get_nlp_info(n,m,dum1, dum2, dum3);
00065 double * x_l = new double[n];
00066 double * x_u = new double[n];
00067 double * g_l = new double[m];
00068 double * g_u = new double[m];
00069
00070 tnlp->get_bounds_info(n, x_l, x_u, m, g_l , g_u);
00071
00072
00073 for(int i = 0 ; i < n ; i++) {
00074 if(x_u[i] - x_l[i] > 1e-5)
00075 {
00076 delete [] x_l;
00077 delete [] x_u;
00078 delete [] g_l;
00079 delete [] g_u;
00080 return 0;
00081 }
00082 }
00083
00084
00085
00086 double obj_value;
00087
00088 tnlp->eval_f(n, x_l, true, obj_value);
00089
00090 double * x_sol = new double[n];
00091
00092
00093 IpBlasDcopy(n, x_l, 1, x_sol, 1);
00094
00095 delete [] x_l;
00096 delete [] x_u;
00097
00098 double * g_sol = new double [m];
00099
00100 tnlp->eval_g(n, x_sol, true, m, g_sol);
00101
00102 optimizationStatus = solvedOptimal;
00103 for(int i = 0 ; i < m ; i++) {
00104 if(g_sol[i] - g_l[i] < - 1e-07 || g_sol[i] - g_u[i] > 1e-07) {
00105 optimizationStatus = provenInfeasible;
00106
00107 delete [] g_l;
00108 delete [] g_u;
00109 double * lam = new double[m];
00110 CoinFillN(lam,m,0.);
00111 double * z = new double[n];
00112 CoinFillN(z,n,0.);
00113 tnlp->finalize_solution(Ipopt::LOCAL_INFEASIBILITY,
00114 n, x_sol, NULL, NULL,
00115 m, g_sol, NULL, obj_value, NULL, NULL);
00116 delete [] lam;
00117 delete [] z;
00118 delete [] g_sol;
00119 delete [] x_sol;
00120
00121 return 1;
00122 }
00123 }
00124 delete [] g_l;
00125 delete [] g_u;
00126
00127 double * lam = new double[m];
00128 CoinFillN(lam,m,0.);
00129 double * z = new double[n];
00130 CoinFillN(z,n,0.);
00131 tnlp->finalize_solution(Ipopt::SUCCESS,
00132 n, x_sol, z, z,
00133 m, g_sol, lam, obj_value, NULL, NULL);
00134 delete [] lam;
00135 delete [] z;
00136 delete [] g_sol;
00137 delete [] x_sol;
00138 return 1;
00139 }
00140
00141 void
00142 TNLPSolver::UnsolvedError::printError(std::ostream &os)
00143 {
00144 os<<solverName()<<" exited with error code "<<errorNum_<<" "<<errorName()<<std::endl;
00145 }
00146
00147 void
00148 TNLPSolver::UnsolvedError::writeDiffFiles(const std::string prefix) const{
00149 const int numcols = model_->num_variables();
00150 const int numrows = model_->num_constraints();
00151
00152 const double * currentLower = model_->x_l();
00153 const double * currentUpper = model_->x_u();
00154
00155 const double * originalLower = model_->orig_x_l();
00156 const double * originalUpper = model_->orig_x_u();
00157 CoinRelFltEq eq;
00158 std::string fBoundsName = prefix + name_;
00159 fBoundsName+="_bounds";
00160
00161 std::string fModName = fBoundsName + ".mod";
00162 std::ofstream fBounds;
00163 std::ofstream fMod;
00164
00166 bool hasVarNames = 0;
00167 NamesReader reader(name_,".col");
00168
00169 if(reader.readFile())
00170 hasVarNames=1;
00171 if(hasVarNames)
00172 fMod.open(fModName.c_str());
00173 fBounds.open(fBoundsName.c_str());
00174
00175 for(int i = 0 ; i < numcols ; i++)
00176 {
00177 if(!eq(currentLower[i],originalLower[i]))
00178 {
00179 if(hasVarNames)
00180 fMod<<"bounds"<<i<<": "
00181 <<reader.name(i)<<" >= "
00182 <<currentLower[i]<<";\n";
00183
00184
00185 fBounds<<"LO"<<"\t"<<i<<"\t"<<currentLower[i]<<std::endl;
00186 }
00187 if(!eq(currentUpper[i],originalUpper[i]))
00188 {
00189 if(hasVarNames)
00190 fMod<<"bounds"<<i<<": "
00191 <<reader.name(i)<<" <= "
00192 <<currentUpper[i]<<";\n";
00193
00194 fBounds<<"UP"<<"\t"<<i<<"\t"<<currentUpper[i]<<std::endl;
00195 }
00196 }
00197
00198
00199 std::string fStartPointName = name_;
00200 fStartPointName+="_start";
00201
00202
00203
00204 const double * primals = model_->x_init();
00205 const double * duals = model_->duals_init();
00206
00207 if(!primals)
00208 {
00209 std::cerr<<"A failure has occured but no starting point exists"<<std::endl;
00210 return;
00211 }
00212
00213 std::ofstream fStartPoint(fStartPointName.c_str());
00214 fStartPoint.precision(17);
00215 fStartPoint<<numcols<<"\t"<<2*numcols+numrows<<std::endl;
00216 for(int i = 0 ; i < numcols ; i++)
00217 fStartPoint<<primals[i]<<std::endl;
00218 int end = 2*numcols + numrows;
00219 if(duals)
00220 {
00221 for(int i = 0 ; i < end; i++)
00222 fStartPoint<<duals[i]<<std::endl;
00223 }
00224
00225
00226 }
00227
00230 bool
00231 TNLPSolver::isRecoverable(ReturnStatus &r){
00232 return (r >=0 || (r != illDefinedProblem && r != illegalOption && r != computationError) );
00233 }
00234
00236 void
00237 TNLPSolver::initializeOptionsAndJournalist(){
00238 options_ = new Ipopt::OptionsList();
00239
00240 journalist_= new Ipopt::Journalist();
00241 roptions_ = new Bonmin::RegisteredOptions();
00242
00243 try{
00244 Ipopt::SmartPtr<Ipopt::Journal> stdout_journal =
00245 journalist_->AddFileJournal("console", "stdout", Ipopt::J_ITERSUMMARY);
00246
00247 options_->SetJournalist(journalist_);
00248 options_->SetRegisteredOptions(GetRawPtr(roptions_));
00249 }
00250 catch (Ipopt::IpoptException &E){
00251 E.ReportException(*journalist_);
00252 throw E;
00253 }
00254 catch(std::bad_alloc){
00255 journalist_->Printf(Ipopt::J_ERROR, Ipopt::J_MAIN, "\n Not enough memory .... EXIT\n");
00256 throw -1;
00257 }
00258 catch(...){
00259 Ipopt::IpoptException E("Uncaught exception in FilterSolver::FilterSolver()",
00260 "BonFilterSolver.cpp",-1);
00261 throw E;
00262 }
00263
00264 }
00265
00266
00267 }
00268