// Copyright (C) 2006, 2007 International Business Machines and others. // All Rights Reserved. // This code is published under the Eclipse Public License. // // $Id$ // // Author: Andreas Waechter IBM 2006-10-11 #include "BonCurvatureEstimator.hpp" #include "IpTSymLinearSolver.hpp" #include "IpGenTMatrix.hpp" #include "IpIdentityMatrix.hpp" #include "IpZeroMatrix.hpp" #include "IpDenseVector.hpp" #include "IpBlas.hpp" #include #ifdef HAVE_MA27 # include "IpMa27TSolverInterface.hpp" #endif #ifdef HAVE_MA57 # include "IpMa57TSolverInterface.hpp" #endif #ifdef HAVE_MC19 # include "IpMc19TSymScalingMethod.hpp" #endif #ifdef HAVE_PARDISO # include "IpPardisoSolverInterface.hpp" #endif #ifdef HAVE_TAUCS # include "IpTAUCSSolverInterface.hpp" #endif #ifdef HAVE_WSMP # include "IpWsmpSolverInterface.hpp" #endif #ifdef COIN_HAS_MUMPS # include "IpMumpsSolverInterface.hpp" #endif namespace Bonmin { using namespace Ipopt; // ToDo: Consider NLP scaling? CurvatureEstimator::CurvatureEstimator (SmartPtr jnlst, SmartPtr options, SmartPtr tnlp) : jnlst_(jnlst), options_(options), prefix_(""), tnlp_(tnlp), grad_f_(NULL), irows_jac_(NULL), jcols_jac_(NULL), jac_vals_(NULL), irows_hess_(NULL), jcols_hess_(NULL), hess_vals_(NULL), eq_x_free_map_(NULL), eq_g_fixed_map_(NULL), all_x_free_map_(NULL), all_g_fixed_map_(NULL), lambda_(NULL), eq_projected_d_(NULL), initialized_(false) { DBG_ASSERT(IsValid(jnlst)); DBG_ASSERT(IsValid(options)); DBG_ASSERT(IsValid(tnlp)); //////////////////////////////////////////////////////////////////// // Create a strategy object for solving the linear system for the // // projection matrix // //////////////////////////////////////////////////////////////////// // The following linear are taken from AlgBuilder in Ipopt SmartPtr SolverInterface1; SmartPtr SolverInterface2; std::string linear_solver; options->GetStringValue("linear_solver", linear_solver, prefix_); if (linear_solver=="ma27") { #ifdef HAVE_MA27 SolverInterface1 = new Ma27TSolverInterface(); SolverInterface2 = new Ma27TSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver MA27 not available."); #endif } else if (linear_solver=="ma57") { #ifdef HAVE_MA57 SolverInterface1 = new Ma57TSolverInterface(); SolverInterface2 = new Ma57TSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver MA57 not available."); #endif } else if (linear_solver=="pardiso") { #ifdef HAVE_PARDISO SolverInterface1 = new PardisoSolverInterface(); SolverInterface2 = new PardisoSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver Pardiso not available."); #endif } else if (linear_solver=="taucs") { #ifdef HAVE_TAUCS SolverInterface1 = new TAUCSSolverInterface(); SolverInterface2 = new TAUCSSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver TAUCS not available."); #endif } else if (linear_solver=="wsmp") { #ifdef HAVE_WSMP SolverInterface1 = new WsmpSolverInterface(); SolverInterface2 = new WsmpSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver WSMP not available."); #endif } else if (linear_solver=="mumps") { #ifdef COIN_HAS_MUMPS SolverInterface1 = new MumpsSolverInterface(); SolverInterface2 = new MumpsSolverInterface(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear solver MUMPS not available."); #endif } SmartPtr ScalingMethod1; SmartPtr ScalingMethod2; std::string linear_system_scaling; if (!options->GetStringValue("linear_system_scaling", linear_system_scaling, prefix_)) { // By default, don't use mc19 for non-HSL solvers if (linear_solver!="ma27" && linear_solver!="ma57") { linear_system_scaling="none"; } } if (linear_system_scaling=="mc19") { #ifdef HAVE_MC19 ScalingMethod1 = new Mc19TSymScalingMethod(); ScalingMethod2 = new Mc19TSymScalingMethod(); #else THROW_EXCEPTION(OPTION_INVALID, "Selected linear system scaling method MC19 not available."); #endif } eq_tsymlinearsolver_ = new TSymLinearSolver(SolverInterface1, ScalingMethod1); all_tsymlinearsolver_ = new TSymLinearSolver(SolverInterface2, ScalingMethod2); // End of lines from AlgBuilder } CurvatureEstimator::~CurvatureEstimator() { if (initialized_) { delete [] grad_f_; delete [] irows_jac_; delete [] jcols_jac_; delete [] jac_vals_; delete [] irows_hess_; delete [] jcols_hess_; delete [] hess_vals_; delete [] eq_x_free_map_; delete [] eq_g_fixed_map_; delete [] all_x_free_map_; delete [] all_g_fixed_map_; delete [] eq_projected_d_; delete [] lambda_; } } bool CurvatureEstimator::Initialize() { DBG_ASSERT(!initialized_); ////////////////////////////////////// // Prepare internal data structures // ////////////////////////////////////// // Get sizes TNLP::IndexStyleEnum index_style; if (!tnlp_->get_nlp_info(n_, m_, nnz_jac_, nnz_hess_, index_style)) { return false; } // Space for gradient delete [] grad_f_; grad_f_ = NULL; grad_f_ = new Number[n_]; // Get nonzero entries in the matrices delete [] irows_jac_; delete [] jcols_jac_; irows_jac_ = NULL; jcols_jac_ = NULL; irows_jac_ = new Index[nnz_jac_]; jcols_jac_ = new Index[nnz_jac_]; if (!tnlp_->eval_jac_g(n_, NULL, false, m_, nnz_jac_, irows_jac_, jcols_jac_, NULL)) { return false; } if (index_style == TNLP::FORTRAN_STYLE) { for (Index i=0; ieval_h(n_, NULL, false, 1., m_, NULL, false, nnz_hess_, irows_hess_, jcols_hess_, NULL)) { return false; } if (index_style == TNLP::FORTRAN_STYLE) { for (Index i=0; ieval_jac_g(n_, x, new_x, m_, nnz_jac_, NULL, NULL, jac_vals_)) { return false; } } // First we compute the direction projected into the space of only // the equality constraints if (new_x) { std::vector dummy_active_x; std::vector dummy_active_g; if (!PrepareNewMatrixStructure(x_l, x_u, g_l, g_u, dummy_active_x, dummy_active_g, eq_nx_free_, eq_x_free_map_, eq_ng_fixed_, eq_g_fixed_map_, eq_comp_proj_matrix_space_, eq_comp_vec_space_)) { return false; } if (!PrepareNewMatrixValues(eq_x_free_map_, eq_g_fixed_map_, eq_comp_proj_matrix_space_, eq_comp_proj_matrix_, eq_tsymlinearsolver_)) { return false; } } if (eq_ng_fixed_>0) { // Compute the projection of the direction if (!SolveSystem(orig_d, NULL, eq_projected_d_, NULL, eq_x_free_map_, eq_g_fixed_map_, eq_comp_vec_space_, eq_comp_proj_matrix_, eq_tsymlinearsolver_)) { return false; } orig_d = eq_projected_d_; // This way we don't need to rememeber // how the direction was projected; } // If necessary, determine new activities bool new_activities = false; if (new_bounds || new_mults) { new_activities = true; active_x_.clear(); if (eq_ng_fixed_>0) { const Number zTol = 1e-4; jnlst_->Printf(J_MOREDETAILED, J_NLP, "List of variables considered fixed (with orig_d and z)\n"); for (Index i=0; i0. && z_U[i]*orig_d[i]>zTol) { active_x_.push_back(i+1); jnlst_->Printf(J_MOREDETAILED, J_NLP, "x[%5d] (%e,%e)\n", i, orig_d[i], z_U[i]); DBG_ASSERT(x_u[i] < 1e19); } else if (orig_d[i]<0. && -z_L[i]*orig_d[i]>zTol) { active_x_.push_back(-(i+1)); jnlst_->Printf(J_MOREDETAILED, J_NLP, "x[%5d] (%e,%e)\n", i, orig_d[i], z_L[i]); DBG_ASSERT(x_l[i] > -1e19); } } } } active_g_.clear(); // Compute the product of the direction with the constraint Jacobian // This could be done more efficient if we have d in sparse format Number* jacTd = new Number[m]; const Number zero = 0.; IpBlasDcopy(m, &zero, 0, jacTd, 1); for (Index i=0; iPrintf(J_MOREDETAILED, J_NLP, "List of constraints considered fixed (with lam and jacTd)\n"); for (Index j=0; j lamTol) { if (lam[j]*jacTd[j] > 0) { if (lam[j] < 0.) { active_g_.push_back(-(j+1)); DBG_ASSERT(g_l[j] > -1e19); } else { active_g_.push_back(j+1); DBG_ASSERT(g_u[j] < 1e19); } // active_g_.push_back(j+1); jnlst_->Printf(J_MOREDETAILED, J_NLP, "g[%5d] (%e,%e)\n", j, lam[j], jacTd[j]); } } } delete [] jacTd; } // Check if the structure of the matrix has changed if (new_activities) { if (!PrepareNewMatrixStructure(x_l, x_u, g_l, g_u, active_x_, active_g_, all_nx_free_, all_x_free_map_, all_ng_fixed_, all_g_fixed_map_, all_comp_proj_matrix_space_, all_comp_vec_space_)) { return false; } } bool new_lambda = false; if (new_x || new_activities) { if (!PrepareNewMatrixValues(all_x_free_map_, all_g_fixed_map_, all_comp_proj_matrix_space_, all_comp_proj_matrix_, all_tsymlinearsolver_)) { return false; } #ifdef lambdas // Compute least square multipliers for the given activities if (!tnlp_->eval_grad_f(n_, x, new_x, grad_f_)) { return false; } if (!SolveSystem(grad_f_, NULL, NULL, lambda_)) { return false; } IpBlasDscal(m_, -1., lambda_, 1); if (jnlst_->ProduceOutput(J_MOREVECTOR, J_NLP)) { jnlst_->Printf(J_MOREVECTOR, J_NLP, "Curvature Estimator: least square multiplier:\n"); for (Index i=0; iPrintf(J_MOREVECTOR, J_NLP, "lambda[%5d] = %23.16e\n", i, lambda_[i]); } } new_lambda = true; #endif } // Compute the projection of the direction if (!SolveSystem(orig_d, NULL, projected_d, NULL, all_x_free_map_, all_g_fixed_map_, all_comp_vec_space_, all_comp_proj_matrix_, all_tsymlinearsolver_)) { return false; } // Sanity check to see if the thing is indeed in the null space // (if the constraint gradients are rank-deficient, the solver // might not have done a good job) Number* trash = new Number[m_]; for (Index j=0; j= 0 && all_g_fixed_map_[irow] >= 0) { trash[irow] += jac_vals_[i]*projected_d[jcol]; } } if (jnlst_->ProduceOutput(J_MOREVECTOR, J_NLP)) { for (Index j=0; jPrintf(J_MOREVECTOR, J_NLP, "nullspacevector[%5d] = %e\n", j, trash[j]); } } Index imax = IpBlasIdamax(m_, trash, 1)-1; Number max_trash = trash[imax]; delete [] trash; const Number max_trash_tol = 1e-6; if (max_trash > max_trash_tol) { jnlst_->Printf(J_WARNING, J_NLP, "Curvature Estimator: Bad solution from linear solver with max_red = %e:\n", max_trash); return false; } if (jnlst_->ProduceOutput(J_MOREVECTOR, J_NLP)) { jnlst_->Printf(J_MOREVECTOR, J_NLP, "Curvature Estimator: original and projected directions are:\n"); for (Index i=0; iPrintf(J_MOREVECTOR, J_NLP, "orig_d[%5d] = %23.16e proj_d[%5d] = %23.16e\n", i, orig_d[i], i, projected_d[i]); } } gradLagTd = 0.; #ifdef lambdas // Compute the product with the gradient of the Lagrangian gradLagTd = IpBlasDdot(n, projected_d, 1, grad_f_, 1); for (Index i=0; i& active_x, std::vector& active_g, Index& nx_free, Index* x_free_map, Index& ng_fixed, Index* g_fixed_map, SmartPtr& comp_proj_matrix_space, SmartPtr& comp_vec_space) { // Deterimine which variables are free for (Index i=0; i::iterator i=active_x.begin(); i != active_x.end(); i++) { DBG_ASSERT(*i != 0 && *i<=n_ && *i>=-n_); if (*i<0) { x_free_map[(-*i)-1] = -1; DBG_ASSERT(x_l[(-*i)-1] > -1e19); } else { x_free_map[(*i)-1] = -1; DBG_ASSERT(x_u[(*i)-1] < 1e19); } } // fixed by bounds for (Index i=0; i::iterator i=active_g.begin(); i != active_g.end(); i++) { DBG_ASSERT(*i != 0 && *i<=m_ && *i>=-m_); if (*i<0) { g_fixed_map[(-*i)-1] = 0; DBG_ASSERT(g_l[(-*i)-1] > -1e19); //ToDo look at option? } else { g_fixed_map[(*i)-1] = 0; DBG_ASSERT(g_u[(*i)-1] < 1e19); } } // fixed by bounds for (Index j=0; j= 0 && g_fixed_map[irow] >= 0) { iRows[nnz_proj_jac] = g_fixed_map[irow] + 1; jCols[nnz_proj_jac] = x_free_map[jcol] + 1; nnz_proj_jac++; } } // Create the matrix space for the Jacobian matrices SmartPtr proj_jac_space = new GenTMatrixSpace(ng_fixed, nx_free, nnz_proj_jac, iRows, jCols); delete [] iRows; delete [] jCols; // Create Matrix space for the projection matrix comp_proj_matrix_space = new CompoundSymMatrixSpace(2, nx_free+ng_fixed); comp_proj_matrix_space->SetBlockDim(0, nx_free); comp_proj_matrix_space->SetBlockDim(1, ng_fixed); SmartPtr identity_space = new IdentityMatrixSpace(nx_free); comp_proj_matrix_space->SetCompSpace(0, 0, *identity_space, true); comp_proj_matrix_space->SetCompSpace(1, 0, *proj_jac_space, true); // Create a Vector space for the rhs and sol comp_vec_space = new CompoundVectorSpace(2, nx_free+ng_fixed); SmartPtr x_space = new DenseVectorSpace(nx_free); comp_vec_space->SetCompSpace(0, *x_space); SmartPtr g_space = new DenseVectorSpace(ng_fixed); comp_vec_space->SetCompSpace(1, *g_space); return true; } bool CurvatureEstimator::PrepareNewMatrixValues( const Index* x_free_map, const Index* g_fixed_map, SmartPtr& comp_proj_matrix_space, SmartPtr& comp_proj_matrix, SmartPtr& tsymlinearsolver) { comp_proj_matrix = comp_proj_matrix_space->MakeNewCompoundSymMatrix(); SmartPtr jac = comp_proj_matrix->GetCompNonConst(1, 0); SmartPtr tjac = static_cast (GetRawPtr(jac)); Number* vals = tjac->Values(); Index inz=0; for (Index i=0; i= 0 && g_fixed_map[irow] >= 0) { vals[inz++] = jac_vals_[i]; } } DBG_ASSERT(inz == tjac->Nonzeros()); // We need to reset the linear solver object, so that it knows // that the structure of the linear system has changed tsymlinearsolver->ReducedInitialize(*jnlst_, *options_, prefix_); return true; } bool CurvatureEstimator::SolveSystem( const Number* rhs_x, const Number* rhs_g, Number* sol_x, Number* sol_g, const Index* x_free_map, const Index* g_fixed_map, SmartPtr& comp_vec_space, SmartPtr& comp_proj_matrix, SmartPtr& tsymlinearsolver) { // Create a vector for the RHS SmartPtr rhs = comp_vec_space->MakeNewCompoundVector(); SmartPtr vrhs_x = rhs->GetCompNonConst(0); SmartPtr vrhs_g = rhs->GetCompNonConst(1); // Now fill this vector with the values, extracting the relevant entries if (rhs_x) { SmartPtr drhs_x = static_cast (GetRawPtr(vrhs_x)); Number* xvals = drhs_x->Values(); for (Index i=0; i=0) { xvals[ix] = rhs_x[i]; } } } else { vrhs_x->Set(0.); } if (rhs_g) { SmartPtr drhs_g = static_cast (GetRawPtr(vrhs_g)); Number* gvals = drhs_g->Values(); for (Index j=0; j=0) { gvals[jg] = rhs_g[j]; } } } else { vrhs_g->Set(0.); } // Solve the linear system SmartPtr sol = comp_vec_space->MakeNewCompoundVector(); ESymSolverStatus solver_retval = tsymlinearsolver->Solve(*comp_proj_matrix, *rhs, *sol, false, 0); // For now we try, if the system is reported to be singular, to // still get a solution; ToDo if (solver_retval == SYMSOLVER_SINGULAR) { jnlst_->Printf(J_DETAILED, J_NLP, "Projection matrix reported to be singular, but we try to obtain a solution anyway.\n"); solver_retval = tsymlinearsolver->Solve(*comp_proj_matrix, *rhs, *sol, false, 0); } if (solver_retval != SYMSOLVER_SUCCESS) { // DELETEME //printf("Return code from Solver is %d\n", solver_retval); return false; } // and get the solution out of it if (sol_x) { SmartPtr vsol_x = sol->GetCompNonConst(0); SmartPtr dsol_x = static_cast (GetRawPtr(vsol_x)); const Number* xvals = dsol_x->Values(); for (Index i=0; i=0) { sol_x[i] = xvals[ix]; } else { sol_x[i] = 0.; } } } if (sol_g) { SmartPtr vsol_g = sol->GetCompNonConst(1); SmartPtr dsol_g = static_cast (GetRawPtr(vsol_g)); const Number* gvals = dsol_g->Values(); for (Index j=0; j=0) { sol_g[j] = gvals[ig]; } else { sol_g[j] = 0.; } } } return true; } bool CurvatureEstimator::Compute_dTHLagd( const Number* d, const Number* x, bool new_x, const Number* lambda, bool new_lambda, Number& dTHLagd) { if (new_x || new_lambda || !hess_vals_) { if (!hess_vals_) { hess_vals_ = new Number[nnz_hess_]; } if (!tnlp_->eval_h(n_, x, new_x, 1., m_, lambda, new_lambda, nnz_hess_, NULL, NULL, hess_vals_)) { return false; } } dTHLagd = 0.; for (Index i=0; i