Changeset 419


Ignore:
Timestamp:
Aug 1, 2005 2:34:28 PM (14 years ago)
Author:
andreasw
Message:
  • ran astyle
  • corrected definition of finalize_solution
Location:
branches/dev/Examples/Cpp_example
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/dev/Examples/Cpp_example/MyNLP.cpp

    r56 r419  
    1313/* Constructor. */
    1414MyNLP::MyNLP()
    15 {
    16 }
     15{}
    1716
    1817MyNLP::~MyNLP()
    19 {
    20 }
     18{}
    2119
    2220bool MyNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag)
     
    4038
    4139bool MyNLP::get_bounds_info(Index n, Number* x_l, Number* x_u,
    42                                Index m, Number* g_l, Number* g_u)
     40                            Index m, Number* g_l, Number* g_u)
    4341{
    44   // here, the n and m we gave IPOPT in get_nlp_info are passed back to us. 
     42  // here, the n and m we gave IPOPT in get_nlp_info are passed back to us.
    4543  // If desired, we could assert to make sure they are what we think they are.
    4644  assert(n == 2);
     
    5351  // x2 has no upper or lower bound, so we set them to
    5452  // a large negative and a large positive number.
    55   // The value that is interpretted as -/+infinity can be 
     53  // The value that is interpretted as -/+infinity can be
    5654  // set in the options, but it defaults to -/+1e19
    5755  x_l[1] = -1.0e19;
    5856  x_u[1] = +1.0e19;
    5957
    60   // we have one equality constraint, so we set the bounds on this constraint 
     58  // we have one equality constraint, so we set the bounds on this constraint
    6159  // to be equal (and zero).
    6260  g_l[0] = g_u[0] = 0.0;
     
    6664
    6765bool MyNLP::get_starting_point(Index n, bool init_x, Number* x,
    68                                bool init_z, Number* z_L, Number* z_U,
    69                                Index m, bool init_lambda,
    70                                Number* lambda)
     66                               bool init_z, Number* z_L, Number* z_U,
     67                               Index m, bool init_lambda,
     68                               Number* lambda)
    7169{
    7270  // Here, we assume we only have starting values for x, if you code
     
    113111
    114112  g[0] = -(x1*x1 + x2 - 1.0);
    115  
     113
    116114  return true;
    117115}
    118116
    119117bool MyNLP::eval_jac_g(Index n, const Number* x, bool new_x,
    120                        Index m, Index nele_jac, Index* iRow, Index *jCol,
    121                        Number* values)
     118                       Index m, Index nele_jac, Index* iRow, Index *jCol,
     119                       Number* values)
    122120{
    123   if (values == NULL)
    124     {
    125       // return the structure of the jacobian of the constraints
    126      
    127       // element at 1,1: grad_{x1} g_{1}(x)
    128       iRow[0] = 1; jCol[0] = 1;
     121  if (values == NULL) {
     122    // return the structure of the jacobian of the constraints
    129123
    130       // element at 1,2: grad_{x2} g_{1}(x)
    131       iRow[1] = 1; jCol[1] = 2;
    132     }
    133   else
    134     {
    135       // return the values of the jacobian of the constraints
    136       Number x1 = x[0];
     124    // element at 1,1: grad_{x1} g_{1}(x)
     125    iRow[0] = 1;
     126    jCol[0] = 1;
    137127
    138       // element at 1,1: grad_{x1} g_{1}(x)
    139       values[0] = -2.0 * x1;
     128    // element at 1,2: grad_{x2} g_{1}(x)
     129    iRow[1] = 1;
     130    jCol[1] = 2;
     131  }
     132  else {
     133    // return the values of the jacobian of the constraints
     134    Number x1 = x[0];
    140135
    141       // element at 1,2: grad_{x1} g_{1}(x)
    142       values[1] = -1.0;
    143     }
     136    // element at 1,1: grad_{x1} g_{1}(x)
     137    values[0] = -2.0 * x1;
     138
     139    // element at 1,2: grad_{x1} g_{1}(x)
     140    values[1] = -1.0;
     141  }
    144142
    145143  return true;
     
    147145
    148146bool MyNLP::eval_h(Index n, const Number* x, bool new_x,
    149                    Number obj_factor, Index m, const Number* lambda,
    150                    bool new_lambda, Index nele_hess, Index* iRow,
    151                    Index* jCol, Number* values)
     147                   Number obj_factor, Index m, const Number* lambda,
     148                   bool new_lambda, Index nele_hess, Index* iRow,
     149                   Index* jCol, Number* values)
    152150{
    153   if (values == NULL)
    154     {
    155       // return the structure. This is a symmetric matrix, fill the lower left
    156       // triangle only.
    157      
    158       // element at 1,1: grad^2_{x1,x1} L(x,lambda)
    159       iRow[0] = 1; jCol[0] = 1;
     151  if (values == NULL) {
     152    // return the structure. This is a symmetric matrix, fill the lower left
     153    // triangle only.
    160154
    161       // element at 2,2: grad^2_{x2,x2} L(x,lambda)
    162       iRow[1] = 2; jCol[1] = 2;
    163      
    164       // Note: off-diagonal elements are zero for this problem
    165     }
    166   else
    167     {
    168       // return the values
     155    // element at 1,1: grad^2_{x1,x1} L(x,lambda)
     156    iRow[0] = 1;
     157    jCol[0] = 1;
    169158
    170       // element at 1,1: grad^2_{x1,x1} L(x,lambda)
    171       values[0] = -2.0 * lambda[0];
     159    // element at 2,2: grad^2_{x2,x2} L(x,lambda)
     160    iRow[1] = 2;
     161    jCol[1] = 2;
    172162
    173       // element at 2,2: grad^2_{x2,x2} L(x,lambda)
    174       values[1] = -2.0;
     163    // Note: off-diagonal elements are zero for this problem
     164  }
     165  else {
     166    // return the values
    175167
    176       // Note: off-diagonal elements are zero for this problem
    177     }
     168    // element at 1,1: grad^2_{x1,x1} L(x,lambda)
     169    values[0] = -2.0 * lambda[0];
     170
     171    // element at 2,2: grad^2_{x2,x2} L(x,lambda)
     172    values[1] = -2.0;
     173
     174    // Note: off-diagonal elements are zero for this problem
     175  }
    178176
    179177  return true;
    180178}
    181179
    182 void MyNLP::finalize_solution(ApplicationReturnStatus status,
    183                               Index n, const Number* x, const Number* z_L, const Number* z_U,
    184                               Index m, const Number* g, const Number* lambda,
    185                               Number obj_value)
     180void MyNLP::finalize_solution(SolverReturn status,
     181                              Index n, const Number* x, const Number* z_L, const Number* z_U,
     182                              Index m, const Number* g, const Number* lambda,
     183                              Number obj_value)
    186184{
    187185  // here is where we would store the solution to variables, or write to a file, etc
  • branches/dev/Examples/Cpp_example/MyNLP.hpp

    r56 r419  
    4040  /** Method to return some info about the nlp */
    4141  virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
    42                             Index& nnz_h_lag);
     42                            Index& nnz_h_lag);
    4343
    4444  /** Method to return the bounds for my problem */
    4545  virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
    46                                Index m, Number* g_l, Number* g_u);
     46                               Index m, Number* g_l, Number* g_u);
    4747
    4848  /** Method to return the starting point for the algorithm */
    4949  virtual bool get_starting_point(Index n, bool init_x, Number* x,
    50                                     bool init_z, Number* z_L, Number* z_U,
    51                                     Index m, bool init_lambda,
    52                                   Number* lambda);
     50                                  bool init_z, Number* z_L, Number* z_U,
     51                                  Index m, bool init_lambda,
     52                                  Number* lambda);
    5353
    5454  /** Method to return the objective value */
     
    6666   */
    6767  virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
    68                           Index m, Index nele_jac, Index* iRow, Index *jCol,
    69                           Number* values);
     68                          Index m, Index nele_jac, Index* iRow, Index *jCol,
     69                          Number* values);
    7070
    7171  /** Method to return:
     
    7474   */
    7575  virtual bool eval_h(Index n, const Number* x, bool new_x,
    76                       Number obj_factor, Index m, const Number* lambda,
    77                       bool new_lambda, Index nele_hess, Index* iRow,
    78                       Index* jCol, Number* values);
    79  
     76                      Number obj_factor, Index m, const Number* lambda,
     77                      bool new_lambda, Index nele_hess, Index* iRow,
     78                      Index* jCol, Number* values);
     79
    8080  //@}
    8181
     
    8383  //@{
    8484  /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
    85   virtual void finalize_solution(ApplicationReturnStatus status,
    86                                  Index n, const Number* x, const Number* z_L, const Number* z_U,
    87                                 Index m, const Number* g, const Number* lambda,
    88                                 Number obj_value);
     85  virtual void finalize_solution(SolverReturn status,
     86                                 Index n, const Number* x, const Number* z_L, const Number* z_U,
     87                                Index m, const Number* g, const Number* lambda,
     88                                Number obj_value);
    8989  //@}
    9090
  • branches/dev/Examples/Cpp_example/cpp_example.cpp

    r266 r419  
    1616int main(int argv, char* argc[])
    1717{
    18    // Create an instance of your nlp...
    19    SmartPtr<TNLP> mynlp = new MyNLP();
    20  
    21    // Create an instance of the IpoptApplication
    22    SmartPtr<IpoptApplication> app = new IpoptApplication();
     18  // Create an instance of your nlp...
     19  SmartPtr<TNLP> mynlp = new MyNLP();
    2320
    24    // To illustrate the use of ip_data and ip_cq, we will use the
    25    //  version of OptimzeTNLP that returns pointers to these objects
    26    // Usually, the standard IpoptApplication output is sufficient
    27    //  and you would only need to call OptimizeTNLP(mynlp);
    28    SmartPtr<IpoptData> ip_data = NULL;
    29    SmartPtr<IpoptCalculatedQuantities> ip_cq = NULL;
    30    ApplicationReturnStatus status = app->OptimizeTNLP(mynlp, ip_data, ip_cq);
     21  // Create an instance of the IpoptApplication
     22  SmartPtr<IpoptApplication> app = new IpoptApplication();
    3123
    32    if (status == Solve_Succeeded) {
    33      // Retrieve some information from ip_data
    34      printf("\n\n*** The problem solved in %d iterations!\n", ip_data->iter_count());
    35      // Retrieve some information from ip_cq
    36      printf("\n\n*** The current value of the objective function is %g.\n", ip_cq->curr_f());
    37    }
     24  // To illustrate the use of ip_data and ip_cq, we will use the
     25  //  version of OptimzeTNLP that returns pointers to these objects
     26  // Usually, the standard IpoptApplication output is sufficient
     27  //  and you would only need to call OptimizeTNLP(mynlp);
     28  SmartPtr<IpoptData> ip_data = NULL;
     29  SmartPtr<IpoptCalculatedQuantities> ip_cq = NULL;
     30  ApplicationReturnStatus status = app->OptimizeTNLP(mynlp, ip_data, ip_cq);
    3831
    39    return (int) status;
     32  if (status == Solve_Succeeded) {
     33    // Retrieve some information from ip_data
     34    printf("\n\n*** The problem solved in %d iterations!\n", ip_data->iter_count());
     35    // Retrieve some information from ip_cq
     36    printf("\n\n*** The current value of the objective function is %g.\n", ip_cq->curr_f());
     37  }
     38
     39  return (int) status;
    4040}
Note: See TracChangeset for help on using the changeset viewer.