コード例 #1
0
int main(){
  
  // Warning
  casadi_warning("This function will fail.");
  
  casadi_warning("This function will fail as sure as 1+1 ==" << "2");
  
  // No warning here
  casadi_assert_warning(0==0, "Not here.");
  
  // Warning due to failed assert
  casadi_assert_warning(1==0, "I am telling you, it WILL fail.");
  
  // Recursive error
  casadi_assert(bad_test4());

  return 0;
}
コード例 #2
0
ファイル: shared_object.cpp プロジェクト: cfpperche/casadi
 SharedObjectNode::~SharedObjectNode() {
   casadi_assert_warning(count==0,
                         "Reference counting failure. "
                         "Possible cause: Circular dependency in user code.");
   if (weak_ref_!=0) {
     weak_ref_->kill();
     delete weak_ref_;
   }
 }
コード例 #3
0
  void SXFunctionInternal::spFreeOpenCL() {
    // OpenCL return flag
    cl_int ret;

    // Clean up memory for input buffers
    for (vector<cl_mem>::iterator i=sp_input_memobj_.begin(); i!=sp_input_memobj_.end(); ++i) {
      if (*i != 0) {
        ret = clReleaseMemObject(*i);
        casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      }
    }
    sp_input_memobj_.clear();

    // Clean up memory for output buffers
    for (vector<cl_mem>::iterator i=sp_output_memobj_.begin(); i!=sp_output_memobj_.end(); ++i) {
      if (*i != 0) {
        ret = clReleaseMemObject(*i);
        casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      }
    }
    sp_output_memobj_.clear();

    // Free opencl forward propagation kernel
    if (sp_fwd_kernel_!=0) {
      ret = clReleaseKernel(sp_fwd_kernel_);
      casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      sp_fwd_kernel_ = 0;
    }

    // Free opencl backward propagation kernel
    if (sp_adj_kernel_!=0) {
      ret = clReleaseKernel(sp_adj_kernel_);
      casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      sp_adj_kernel_ = 0;
    }

    // Free opencl program
    if (sp_program_!=0) {
      ret = clReleaseProgram(sp_program_);
      casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      sp_program_ = 0;
    }
  }
コード例 #4
0
  void SXFunctionInternal::freeOpenCL() {
    // OpenCL return flag
    cl_int ret;

    // Clean up memory for input buffers
    for (vector<cl_mem>::iterator i=input_memobj_.begin(); i!=input_memobj_.end(); ++i) {
      if (*i != 0) {
        ret = clReleaseMemObject(*i);
        casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      }
    }
    input_memobj_.clear();

    // Clean up memory for output buffers
    for (vector<cl_mem>::iterator i=output_memobj_.begin(); i!=output_memobj_.end(); ++i) {
      if (*i != 0) {
        ret = clReleaseMemObject(*i);
        casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      }
    }
    output_memobj_.clear();

    // Free opencl numerical evaluation kernel
    if (kernel_!=0) {
      ret = clReleaseKernel(kernel_);
      casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      kernel_ = 0;
    }

    // Free opencl program
    if (program_!=0) {
      ret = clReleaseProgram(program_);
      casadi_assert_warning(ret == CL_SUCCESS, "Freeing OpenCL memory failed");
      program_ = 0;
    }
  }
コード例 #5
0
  bool WorhpInternal::eval_h(const double* x, double obj_factor, const double* lambda, double* values){
    try{
      log("eval_h started");
      double time1 = clock();

      // Make sure generated
      casadi_assert_warning(!hessLag_.isNull(),"Hessian function not pregenerated");

      // Get Hessian 
      Function& hessLag = this->hessLag();

      // Pass input
      hessLag.setInput(x,HESSLAG_X);
      hessLag.setInput(input(NLP_SOLVER_P),HESSLAG_P);
      hessLag.setInput(obj_factor,HESSLAG_LAM_F);
      hessLag.setInput(lambda,HESSLAG_LAM_G);

      // Evaluate
      hessLag.evaluate();

      // Get results
      const DMatrix& H = hessLag.output();
      const vector<int>& colind = H.colind();
      const vector<int>& row = H.row();
      const vector<double>& data = H.data();

      // The Hessian values are divided into strictly upper (in WORHP lower) triangular and diagonal
      double* values_upper = values;
      double* values_diagonal = values + (worhp_w_.HM.nnz-nx_);

      // Initialize diagonal to zero
      for(int r=0; r<nx_; ++r){
        values_diagonal[r] = 0.;
      }

      // Upper triangular part of the Hessian (note CCS -> CRS format change)
      for(int c=0; c<nx_; ++c){
        for(int el=colind[c]; el<colind[c+1]; ++el){
          if(row[el]>c){
            // Strictly upper triangular
            *values_upper++ = data[el];
          } else if(row[el]==c){
            // Diagonal separately
            values_diagonal[c] = data[el];
          }
        }
      }
      
      if(monitored("eval_h")){
        std::cout << "x = " <<  hessLag.input(HESSLAG_X) << std::endl;
        std::cout << "obj_factor= " << obj_factor << std::endl;
        std::cout << "lambda = " << hessLag.input(HESSLAG_LAM_G) << std::endl;
        std::cout << "H = " << hessLag.output(HESSLAG_HESS) << std::endl;
      }

      if (regularity_check_ && !isRegular(hessLag.output(HESSLAG_HESS).data())) casadi_error("WorhpInternal::eval_h: NaN or Inf detected.");
      
      double time2 = clock();
      t_eval_h_ += double(time2-time1)/CLOCKS_PER_SEC;
      n_eval_h_ += 1;
      log("eval_h ok");
      return true;
    } catch (exception& ex){
      cerr << "eval_h failed: " << ex.what() << endl;
      return false;
    }
  }
コード例 #6
0
void LiftedSQPInternal::evaluate(int nfdir, int nadir){
  casadi_assert(nfdir==0 && nadir==0);
  checkInitialBounds();
     
  // Objective value
  double f_k = numeric_limits<double>::quiet_NaN();
  
  // Current guess for the primal solution
  DMatrix &x_k = output(NLP_SOLVER_X);
  const DMatrix &x_init = input(NLP_SOLVER_X0);
  copy(x_init.begin(),x_init.end(),x_k.begin());
  
  // Current guess for the dual solution
  DMatrix &lam_x_k = output(NLP_SOLVER_LAM_X);
  DMatrix &lam_g_k = output(NLP_SOLVER_LAM_G);

  // Bounds
  const DMatrix &x_min = input(NLP_SOLVER_LBX);
  const DMatrix &x_max = input(NLP_SOLVER_UBX);
  
  const DMatrix &g_min = input(NLP_SOLVER_LBG);
  const DMatrix &g_max = input(NLP_SOLVER_UBG);
  int k=0;
  
  // Does G depend on the multipliers?
  bool has_lam_x =  !rfcn_.input(G_LAM_X).empty();
  bool has_lam_g =  !rfcn_.input(G_LAM_G).empty();
  bool has_lam_f2 = !efcn_.input(EXP_DLAM_F2).empty();
  
  while(true){
    // Evaluate residual
    rfcn_.setInput(x_k,G_X);
    if(has_lam_x) rfcn_.setInput(lam_x_k,G_LAM_X);
    if(has_lam_g) rfcn_.setInput(lam_g_k,G_LAM_G);
    rfcn_.evaluate();
    rfcn_.getOutput(d_k_,G_D);
    f_k = rfcn_.output(G_F).toScalar();
    const DMatrix& g_k = rfcn_.output(G_G);
    
    // Construct the QP
    lfcn_.setInput(x_k,LIN_X);
    if(has_lam_x) lfcn_.setInput(lam_x_k,LIN_LAM_X);
    if(has_lam_g) lfcn_.setInput(lam_g_k,LIN_LAM_G);
    lfcn_.setInput(d_k_,LIN_D);
    lfcn_.evaluate();
    DMatrix& B1_k = lfcn_.output(LIN_J1);
    const DMatrix& b1_k = lfcn_.output(LIN_F1);
    const DMatrix& B2_k = lfcn_.output(LIN_J2);
    const DMatrix& b2_k = lfcn_.output(LIN_F2);

    // Regularization
    double reg = 0;
    bool regularization = true;
    
    // Check the smallest eigenvalue of the Hessian
    if(regularization && nu==2){
      double a = B1_k.elem(0,0);
      double b = B1_k.elem(0,1);
      double c = B1_k.elem(1,0);
      double d = B1_k.elem(1,1);
      
      // Make sure no not a numbers
      casadi_assert(a==a && b==b && c==c &&  d==d);
      
      // Make sure symmetric
      if(b!=c){
        casadi_assert_warning(fabs(b-c)<1e-10,"Hessian is not symmetric: " << b << " != " << c);
        B1_k.elem(1,0) = c = b;
      }
      
      double eig_smallest = (a+d)/2 - std::sqrt(4*b*c + (a-d)*(a-d))/2;
      double threshold = 1e-8;
      if(eig_smallest<threshold){
        // Regularization
        reg = threshold-eig_smallest;
        std::cerr << "Regularization with " << reg << " to ensure positive definite Hessian." << endl;
        B1_k(0,0) += reg;
        B1_k(1,1) += reg;
      }
    }
    
    
    // Solve the QP
    qp_solver_.setInput(B1_k,QP_H);
    qp_solver_.setInput(b1_k,QP_G);
    qp_solver_.setInput(B2_k,QP_A);
    std::transform(x_min.begin(),x_min.begin()+nu,x_k.begin(),qp_solver_.input(QP_LBX).begin(),std::minus<double>());
    std::transform(x_max.begin(),x_max.begin()+nu,x_k.begin(),qp_solver_.input(QP_UBX).begin(),std::minus<double>());
    std::transform(g_min.begin()+nv,g_min.end(), b2_k.begin(),qp_solver_.input(QP_LBA).begin(),std::minus<double>());
    std::transform(g_max.begin()+nv,g_max.end(), b2_k.begin(),qp_solver_.input(QP_UBA).begin(),std::minus<double>());
    qp_solver_.evaluate();
    const DMatrix& du_k = qp_solver_.output(QP_PRIMAL);
    const DMatrix& dlam_u_k = qp_solver_.output(QP_LAMBDA_X);
    const DMatrix& dlam_f2_k = qp_solver_.output(QP_LAMBDA_A);    
    
    // Expand the step
    for(int i=0; i<LIN_NUM_IN; ++i){
      efcn_.setInput(lfcn_.input(i),i);
    }
    efcn_.setInput(du_k,EXP_DU);
    if(has_lam_f2) efcn_.setInput(dlam_f2_k,EXP_DLAM_F2);
    efcn_.evaluate();
    const DMatrix& dv_k = efcn_.output();
    
    // Expanded primal step
    copy(du_k.begin(),du_k.end(),dx_k_.begin());
    copy(dv_k.begin(),dv_k.begin()+nv,dx_k_.begin()+nu);

    // Expanded dual step
    copy(dlam_u_k.begin(),dlam_u_k.end(),dlam_x_k_.begin());
    copy(dlam_f2_k.begin(),dlam_f2_k.end(),dlam_g_k_.begin()+nv);
    copy(dv_k.rbegin(),dv_k.rbegin()+nv,dlam_g_k_.begin());
    
    // Take a full step
    transform(dx_k_.begin(),dx_k_.end(),x_k.begin(),x_k.begin(),plus<double>());
    copy(dlam_x_k_.begin(),dlam_x_k_.end(),lam_x_k.begin());
    transform(dlam_g_k_.begin(),dlam_g_k_.end(),lam_g_k.begin(),lam_g_k.begin(),plus<double>());

    // Step size
    double norm_step=0;
    for(vector<double>::const_iterator it=dx_k_.begin(); it!=dx_k_.end(); ++it)  norm_step += *it**it;
    if(!gauss_newton_){
      for(vector<double>::const_iterator it=dlam_g_k_.begin(); it!=dlam_g_k_.end(); ++it) norm_step += *it**it;
    }
    norm_step = sqrt(norm_step);
    
    // Constraint violation
    double norm_viol = 0;
    for(int i=0; i<x_k.size(); ++i){
      double d = fmax(x_k.at(i)-x_max.at(i),0.) + fmax(x_min.at(i)-x_k.at(i),0.);
      norm_viol += d*d;
    }
    for(int i=0; i<g_k.size(); ++i){
      double d = fmax(g_k.at(i)-g_max.at(i),0.) + fmax(g_min.at(i)-g_k.at(i),0.);
      norm_viol += d*d;
    }
    norm_viol = sqrt(norm_viol);
    
    // Print progress (including the header every 10 rows)
    if(k % 10 == 0){
      cout << setw(4) << "iter" << setw(20) << "objective" << setw(20) << "norm_step" << setw(20) << "norm_viol" << endl;
    }
    cout   << setw(4) <<     k  << setw(20) <<  f_k        << setw(20) <<  norm_step  << setw(20) <<  norm_viol  << endl;
    
    
    // Check if stopping criteria is satisfied
    if(norm_viol + norm_step < toldx_){
      cout << "Convergence achieved!" << endl;
      break;
    }
    
    // Increase iteration count
    k = k+1;
    
    // Check if number of iterations have been reached
    if(k >= max_iter_){
      cout << "Maximum number of iterations (" << max_iter_ << ") reached" << endl;
      break;
    }
  }
  
  // Store optimal value
  output(NLP_SOLVER_F).set(f_k);
  
  // Save statistics
  stats_["iter_count"] = k;
}