1 # ifndef CPPAD_CPPAD_IPOPT_SRC_CPPAD_IPOPT_NLP_HPP 2 # define CPPAD_CPPAD_IPOPT_SRC_CPPAD_IPOPT_NLP_HPP 3 /* -------------------------------------------------------------------------- 4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell 5 6 CppAD is distributed under the terms of the 7 Eclipse Public License Version 2.0. 8 9 This Source Code may also be made available under the following 10 Secondary License when the conditions for such availability set forth 11 in the Eclipse Public License, Version 2.0 are satisfied: 12 GNU General Public License, Version 2.0 or later. 13 ---------------------------------------------------------------------------- */ 14 /* 15 $begin cppad_ipopt_nlp$$ 16 $dollar @$$ 17 $spell 18 libipopt 19 namespace 20 dir 21 cppad 22 bool 23 doesn't 24 nan 25 inf 26 naninf 27 std 28 maxiter 29 infeasibility 30 obj 31 const 32 optimizer 33 cppad_ipopt_nlp.hpp 34 fg_info.eval 35 retape 36 CppAD 37 config 38 lipopt 39 40 $$ 41 $section Nonlinear Programming Using the CppAD Interface to Ipopt$$ 42 43 44 $head Deprecated 2012-11-28$$ 45 This interface to Ipopt is deprecated, use $cref ipopt_solve$$ instead. 46 47 $head Syntax$$ 48 $codei%# include "cppad_ipopt_nlp.hpp" 49 %$$ 50 $codei%cppad_ipopt_solution %solution%; 51 %$$ 52 $codei%cppad_ipopt_nlp %cppad_nlp%( 53 %n%, %m%, %x_i%, %x_l%, %x_u%, %g_l%, %g_u%, &%fg_info%, &%solution% 54 )%$$ 55 $codei% 56 export LD_LIBRARY_PATH=@LD_LIBRARY_PATH:%ipopt_library_paths%$$ 57 58 $head Purpose$$ 59 The class $code cppad_ipopt_nlp$$ is used to solve nonlinear programming 60 problems of the form 61 $latex \[ 62 \begin{array}{rll} 63 {\rm minimize} & f(x) 64 \\ 65 {\rm subject \; to} & g^l \leq g(x) \leq g^u 66 \\ 67 & x^l \leq x \leq x^u 68 \end{array} 69 \] $$ 70 This is done using 71 $href% 72 http://www.coin-or.org/projects/Ipopt.xml% 73 Ipopt 74 %$$ 75 optimizer and 76 $href% 77 http://www.coin-or.org/CppAD/% 78 CppAD 79 %$$ 80 Algorithmic Differentiation package. 81 82 $head cppad_ipopt namespace$$ 83 All of the declarations for these routines 84 are in the $code cppad_ipopt$$ namespace 85 (not the $code CppAD$$ namespace). 86 For example; $cref/SizeVector/cppad_ipopt_nlp/SizeVector/$$ below 87 actually denotes the type $code cppad_ipopt::SizeVector$$. 88 89 $head ipopt_library_paths$$ 90 If you are linking to a shared version of the Ipopt library, 91 you may have to add a path to the $code LD_LIBRARY_PATH$$. 92 You can determine the directory you need to add using the command 93 $code 94 pkg-config ipopt --libs 95 %$$ 96 The output will have the following form 97 $codei% 98 -L%dir% -lipopt 99 %$$ 100 You may need to add the directory %dir% to $code LD_LIBRARY_PATH%$$; e.g., 101 $codei% 102 export LD_LIBRARY_PATH="%dir%:@LD_LIBRARY_PATH" 103 %$$ 104 105 $head fg(x)$$ 106 The function $latex fg : \B{R}^n \rightarrow \B{R}^{m+1}$$ is defined by 107 $latex \[ 108 \begin{array}{rcl} 109 fg_0 (x) & = & f(x) \\ 110 fg_1 (x) & = & g_0 (x) \\ 111 & \vdots & \\ 112 fg_m (x) & = & g_{m-1} (x) 113 \end{array} 114 \] $$ 115 116 $subhead Index Vector$$ 117 We define an $icode index vector$$ as a vector of non-negative integers 118 for which none of the values are equal; i.e., 119 it is both a vector and a set. 120 If $latex I$$ is an index vector $latex |I|$$ is used to denote the 121 number of elements in $latex I$$ and $latex \| I \|$$ is used 122 to denote the value of the maximum element in $latex I$$. 123 124 $subhead Projection$$ 125 Given an index vector $latex J$$ and a positive integer $latex n$$ 126 where $latex n > \| J \|$$, we use $latex J \otimes n $$ for 127 the mapping $latex ( J \otimes n ) : \B{R}^n \rightarrow \B{R}^{|J|}$$ defined by 128 $latex \[ 129 [ J \otimes n ] (x)_j = x_{J(j)} 130 \] $$ 131 for $latex j = 0 , \ldots |J| - 1$$. 132 133 $subhead Injection$$ 134 Given an index vector $latex I$$ and a positive integer $latex m$$ 135 where $latex m > \| I \|$$, we use $latex m \otimes I$$ for 136 the mapping $latex ( m \otimes I ): \B{R}^{|I|} \rightarrow \B{R}^m$$ defined by 137 $latex \[ 138 [ m \otimes I ] (y)_i = \left\{ \begin{array}{ll} 139 y_k & {\rm if} \; i = I(k) \; {\rm for \; some} \; 140 k \in \{ 0 , \cdots, |I|-1 \} 141 \\ 142 0 & {\rm otherwise} 143 \end{array} \right. 144 \] $$ 145 146 $subhead Representation$$ 147 In many applications, each of the component functions of $latex fg(x)$$ 148 only depend on a few of the components of $latex x$$. 149 In this case, expressing $latex fg(x)$$ in terms of simpler functions 150 with fewer arguments can greatly reduce the amount of work required 151 to compute its derivatives. 152 $pre 153 154 $$ 155 We use the functions 156 $latex r_k : \B{R}^{q(k)} \rightarrow \B{R}^{p(k)}$$ 157 for $latex k = 0 , \ldots , K$$ to express our 158 representation of $latex fg(x)$$ in terms of simpler functions 159 as follows 160 $latex \[ 161 fg(x) = \sum_{k=0}^{K-1} \; \sum_{\ell=0}^{L(k) - 1} 162 [ (m+1) \otimes I_{k,\ell} ] \; \circ 163 \; r_k \; \circ \; [ J_{k,\ell} \otimes n ] \; (x) 164 \] $$ 165 where $latex \circ$$ represents function composition, 166 for $latex k = 0 , \ldots , K - 1$$, and $latex \ell = 0 , \ldots , L(k)$$, 167 $latex I_{k,\ell}$$ and $latex J_{k,\ell}$$ are index vectors with 168 $latex | J_{k,\ell} | = q(k)$$, 169 $latex \| J_{k,\ell} \| < n$$, 170 $latex | I_{k,\ell} | = p(k)$$, and 171 $latex \| I_{k,\ell} \| \leq m$$. 172 173 $head Simple Representation$$ 174 In the simple representation, 175 $latex r_0 (x) = fg(x)$$, 176 $latex K = 1$$, 177 $latex q(0) = n$$, 178 $latex p(0) = m+1$$, 179 $latex L(0) = 1$$, 180 $latex I_{0,0} = (0 , \ldots , m)$$, 181 and $latex J_{0,0} = (0 , \ldots , n-1)$$. 182 183 $head SizeVector$$ 184 The type $codei SizeVector$$ is defined by the 185 $codei cppad_ipopt_nlp.hpp$$ include file to be a 186 $cref SimpleVector$$ class with elements of type 187 $code size_t$$. 188 189 $head NumberVector$$ 190 The type $codei NumberVector$$ is defined by the 191 $codei cppad_ipopt_nlp.hpp$$ include file to be a 192 $cref SimpleVector$$ class with elements of type 193 $code Ipopt::Number$$. 194 195 $head ADNumber$$ 196 The type $codei ADNumber$$ is defined by the 197 $codei cppad_ipopt_nlp.hpp$$ include file to be a 198 an AD type that can be used to compute derivatives. 199 200 $head ADVector$$ 201 The type $codei ADVector$$ is defined by the 202 $codei cppad_ipopt_nlp.hpp$$ include file to be a 203 $cref SimpleVector$$ class with elements of type 204 $code ADNumber$$. 205 206 $head n$$ 207 The argument $icode n$$ has prototype 208 $codei% 209 size_t %n% 210 %$$ 211 It specifies the dimension of the argument space; 212 i.e., $latex x \in \B{R}^n$$. 213 214 $head m$$ 215 The argument $icode m$$ has prototype 216 $codei% 217 size_t %m% 218 %$$ 219 It specifies the dimension of the range space for $latex g$$; 220 i.e., $latex g : \B{R}^n \rightarrow \B{R}^m$$. 221 222 $head x_i$$ 223 The argument $icode x_i$$ has prototype 224 $codei% 225 const NumberVector& %x_i% 226 %$$ 227 and its size is equal to $latex n$$. 228 It specifies the initial point where Ipopt starts the optimization process. 229 230 $head x_l$$ 231 The argument $icode x_l$$ has prototype 232 $codei% 233 const NumberVector& %x_l% 234 %$$ 235 and its size is equal to $latex n$$. 236 It specifies the lower limits for the argument in the optimization problem; 237 i.e., $latex x^l$$. 238 239 $head x_u$$ 240 The argument $icode x_u$$ has prototype 241 $codei% 242 const NumberVector& %x_u% 243 %$$ 244 and its size is equal to $latex n$$. 245 It specifies the upper limits for the argument in the optimization problem; 246 i.e., $latex x^u$$. 247 248 $head g_l$$ 249 The argument $icode g_l$$ has prototype 250 $codei% 251 const NumberVector& %g_l% 252 %$$ 253 and its size is equal to $latex m$$. 254 It specifies the lower limits for the constraints in the optimization problem; 255 i.e., $latex g^l$$. 256 257 $head g_u$$ 258 The argument $icode g_u$$ has prototype 259 $codei% 260 const NumberVector& %g_u% 261 %$$ 262 and its size is equal to $latex n$$. 263 It specifies the upper limits for the constraints in the optimization problem; 264 i.e., $latex g^u$$. 265 266 $head fg_info$$ 267 The argument $icode fg_info$$ has prototype 268 $codei% 269 %FG_info fg_info% 270 %$$ 271 where the class $icode FG_info$$ is derived from the 272 base class $code cppad_ipopt_fg_info$$. 273 Certain virtual member functions of $icode fg_info$$ are used to 274 compute the value of $latex fg(x)$$. 275 The specifications for these member functions are given below: 276 277 $subhead fg_info.number_functions$$ 278 This member function has prototype 279 $codei% 280 virtual size_t cppad_ipopt_fg_info::number_functions(void) 281 %$$ 282 If $icode K$$ has type $code size_t$$, the syntax 283 $codei% 284 %K% = %fg_info%.number_functions() 285 %$$ 286 sets $icode K$$ to the number of functions used in the 287 representation of $latex fg(x)$$; i.e., $latex K$$ in 288 the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 289 $pre 290 291 $$ 292 The $code cppad_ipopt_fg_info$$ implementation of this function 293 corresponds to the simple representation mentioned above; i.e. 294 $icode%K% = 1%$$. 295 296 $subhead fg_info.eval_r$$ 297 This member function has the prototype 298 $codei% 299 virtual ADVector cppad_ipopt_fg_info::eval_r(size_t %k%, const ADVector& %u%) = 0; 300 %$$ 301 Thus it is a pure virtual function and must be defined in the 302 derived class $icode FG_info$$. 303 $pre 304 305 $$ 306 This function computes the value of $latex r_k (u)$$ 307 used in the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ 308 for $latex fg(x)$$. 309 If $icode k$$ in $latex \{0 , \ldots , K-1 \}$$ has type $code size_t$$, 310 $icode u$$ is an $code ADVector$$ of size $icode q(k)$$ 311 and $icode r$$ is an $code ADVector$$ of size $icode p(k)$$ 312 the syntax 313 $codei% 314 %r% = %fg_info%.eval_r(%k%, %u%) 315 %$$ 316 set $icode r$$ to the vector $latex r_k (u)$$. 317 318 $subhead fg_info.retape$$ 319 This member function has the prototype 320 $codei% 321 virtual bool cppad_ipopt_fg_info::retape(size_t %k%) 322 %$$ 323 If $icode k$$ in $latex \{0 , \ldots , K-1 \}$$ has type $code size_t$$, 324 and $icode retape$$ has type $code bool$$, 325 the syntax 326 $codei% 327 %retape% = %fg_info%.retape(%k%) 328 %$$ 329 sets $icode retape$$ to true or false. 330 If $icode retape$$ is true, 331 $code cppad_ipopt_nlp$$ will retape the operation sequence 332 corresponding to $latex r_k (u)$$ for 333 every value of $icode u$$. 334 An $code cppad_ipopt_nlp$$ object 335 should use much less memory and run faster if $icode retape$$ is false. 336 You can test both the true and false cases to make sure 337 the operation sequence does not depend on $icode u$$. 338 $pre 339 340 $$ 341 The $code cppad_ipopt_fg_info$$ implementation of this function 342 sets $icode retape$$ to true 343 (while slower it is also safer to always retape). 344 345 $subhead fg_info.domain_size$$ 346 This member function has prototype 347 $codei% 348 virtual size_t cppad_ipopt_fg_info::domain_size(size_t %k%) 349 %$$ 350 If $icode k$$ in $latex \{0 , \ldots , K-1 \}$$ has type $code size_t$$, 351 and $icode q$$ has type $code size_t$$, the syntax 352 $codei% 353 %q% = %fg_info%.domain_size(%k%) 354 %$$ 355 sets $icode q$$ to the dimension of the domain space for $latex r_k (u)$$; 356 i.e., $latex q(k)$$ in 357 the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 358 359 $pre 360 361 $$ 362 The $code cppad_ipopt_h_base$$ implementation of this function 363 corresponds to the simple representation mentioned above; i.e., 364 $latex q = n$$. 365 366 $subhead fg_info.range_size$$ 367 This member function has prototype 368 $codei% 369 virtual size_t cppad_ipopt_fg_info::range_size(size_t %k%) 370 %$$ 371 If $icode k$$ in $latex \{0 , \ldots , K-1 \}$$ has type $code size_t$$, 372 and $icode p$$ has type $code size_t$$, the syntax 373 $codei% 374 %p% = %fg_info%.range_size(%k%) 375 %$$ 376 sets $icode p$$ to the dimension of the range space for $latex r_k (u)$$; 377 i.e., $latex p(k)$$ in 378 the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 379 $pre 380 381 $$ 382 The $code cppad_ipopt_h_base$$ implementation of this function 383 corresponds to the simple representation mentioned above; i.e., 384 $latex p = m+1$$. 385 386 $subhead fg_info.number_terms$$ 387 This member function has prototype 388 $codei% 389 virtual size_t cppad_ipopt_fg_info::number_terms(size_t %k%) 390 %$$ 391 If $icode k$$ in $latex \{0 , \ldots , K-1 \}$$ has type $code size_t$$, 392 and $icode L$$ has type $code size_t$$, the syntax 393 $codei% 394 %L% = %fg_info%.number_terms(%k%) 395 %$$ 396 sets $icode L$$ to the number of terms in representation 397 for this value of $icode k$$; 398 i.e., $latex L(k)$$ in 399 the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 400 $pre 401 402 $$ 403 The $code cppad_ipopt_h_base$$ implementation of this function 404 corresponds to the simple representation mentioned above; i.e., 405 $latex L = 1$$. 406 407 $subhead fg_info.index$$ 408 This member function has prototype 409 $codei% 410 virtual void cppad_ipopt_fg_info::index( 411 size_t %k%, size_t %ell%, SizeVector& %I%, SizeVector& %J% 412 ) 413 %$$ 414 The argument 415 $icode% 416 k 417 %$$ 418 has type $codei size_t$$ 419 and is a value between zero and $latex K-1$$ inclusive. 420 The argument 421 $icode% 422 ell 423 %$$ 424 has type $codei size_t$$ 425 and is a value between zero and $latex L(k)-1$$ inclusive. 426 The argument 427 $icode% 428 I 429 %$$ is a $cref SimpleVector$$ with elements 430 of type $code size_t$$ and size greater than or equal to $latex p(k)$$. 431 The input value of the elements of $icode I$$ does not matter. 432 The output value of 433 the first $latex p(k)$$ elements of $icode I$$ 434 must be the corresponding elements of $latex I_{k,ell}$$ 435 in the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 436 The argument 437 $icode% 438 J 439 %$$ is a $cref SimpleVector$$ with elements 440 of type $code size_t$$ and size greater than or equal to $latex q(k)$$. 441 The input value of the elements of $icode J$$ does not matter. 442 The output value of 443 the first $latex q(k)$$ elements of $icode J$$ 444 must be the corresponding elements of $latex J_{k,ell}$$ 445 in the $cref/representation/cppad_ipopt_nlp/fg(x)/Representation/$$ above. 446 $pre 447 448 $$ 449 The $code cppad_ipopt_h_base$$ implementation of this function 450 corresponds to the simple representation mentioned above; i.e., 451 for $latex i = 0 , \ldots , m$$, 452 $icode%I%[%i%] = %i%$$, 453 and for $latex j = 0 , \ldots , n-1$$, 454 $icode%J%[%j%] = %j%$$. 455 456 $head solution$$ 457 After the optimization process is completed, $icode solution$$ contains 458 the following information: 459 460 $subhead status$$ 461 The $icode status$$ field of $icode solution$$ has prototype 462 $codei% 463 cppad_ipopt_solution::solution_status %solution%.status 464 %$$ 465 It is the final Ipopt status for the optimizer. 466 Here is a list of the possible values for the status: 467 468 $table 469 $icode status$$ $cnext Meaning 470 $rnext 471 not_defined $cnext 472 The optimizer did not return a final status to this $code cppad_ipopt_nlp$$ 473 object. 474 $rnext 475 unknown $cnext 476 The status returned by the optimizer is not defined in the Ipopt 477 documentation for $code finalize_solution$$. 478 $rnext 479 success $cnext 480 Algorithm terminated successfully at a point satisfying the convergence 481 tolerances (see Ipopt options). 482 $rnext 483 maxiter_exceeded $cnext 484 The maximum number of iterations was exceeded (see Ipopt options). 485 $rnext 486 stop_at_tiny_step $cnext 487 Algorithm terminated because progress was very slow. 488 $rnext 489 stop_at_acceptable_point $cnext 490 Algorithm stopped at a point that was converged, 491 not to the 'desired' tolerances, but to 'acceptable' tolerances 492 (see Ipopt options). 493 $rnext 494 local_infeasibility $cnext 495 Algorithm converged to a non-feasible point 496 (problem may have no solution). 497 $rnext 498 user_requested_stop $cnext 499 This return value should not happen. 500 $rnext 501 diverging_iterates $cnext 502 It the iterates are diverging. 503 $rnext 504 restoration_failure $cnext 505 Restoration phase failed, algorithm doesn't know how to proceed. 506 $rnext 507 error_in_step_computation $cnext 508 An unrecoverable error occurred while Ipopt tried to 509 compute the search direction. 510 $rnext 511 invalid_number_detected $cnext 512 Algorithm received an invalid number (such as $code nan$$ or $code inf$$) 513 from the users function $icode%fg_info%.eval%$$ or from the CppAD evaluations 514 of its derivatives 515 (see the Ipopt option $code check_derivatives_for_naninf$$). 516 $rnext 517 internal_error $cnext 518 An unknown Ipopt internal error occurred. 519 Contact the Ipopt authors through the mailing list. 520 $tend 521 522 $subhead x$$ 523 The $code x$$ field of $icode solution$$ has prototype 524 $codei% 525 NumberVector %solution%.x 526 %$$ 527 and its size is equal to $latex n$$. 528 It is the final $latex x$$ value for the optimizer. 529 530 $subhead z_l$$ 531 The $code z_l$$ field of $icode solution$$ has prototype 532 $codei% 533 NumberVector %solution%.z_l 534 %$$ 535 and its size is equal to $latex n$$. 536 It is the final Lagrange multipliers for the 537 lower bounds on $latex x$$. 538 539 $subhead z_u$$ 540 The $code z_u$$ field of $icode solution$$ has prototype 541 $codei% 542 NumberVector %solution%.z_u 543 %$$ 544 and its size is equal to $latex n$$. 545 It is the final Lagrange multipliers for the 546 upper bounds on $latex x$$. 547 548 $subhead g$$ 549 The $code g$$ field of $icode solution$$ has prototype 550 $codei% 551 NumberVector %solution%.g 552 %$$ 553 and its size is equal to $latex m$$. 554 It is the final value for the constraint function $latex g(x)$$. 555 556 $subhead lambda$$ 557 The $code lambda$$ field of $icode solution$$ has prototype 558 $codei% 559 NumberVector %solution%.lambda 560 %$$ 561 and its size is equal to $latex m$$. 562 It is the final value for the 563 Lagrange multipliers corresponding to the constraint function. 564 565 $subhead obj_value$$ 566 The $code obj_value$$ field of $icode solution$$ has prototype 567 $codei% 568 Number %solution%.obj_value 569 %$$ 570 It is the final value of the objective function $latex f(x)$$. 571 572 573 $end 574 ----------------------------------------------------------------------------- 575 */ 576 # include <cppad/cppad.hpp> 577 # include <coin-or/IpIpoptApplication.hpp> 578 # include <coin-or/IpTNLP.hpp> 579 580 /*! 581 \file cppad_ipopt_nlp.hpp 582 \brief CppAD interface to Ipopt 583 584 \ingroup cppad_ipopt_nlp_cpp 585 */ 586 587 // --------------------------------------------------------------------------- 588 namespace cppad_ipopt { 589 // --------------------------------------------------------------------------- 590 591 /// A scalar value used to record operation sequence. 592 typedef CppAD::AD<Ipopt::Number> ADNumber; 593 /// A simple vector of values used to record operation sequence 594 typedef CppAD::vector<ADNumber> ADVector; 595 /// A simple vector of size_t values. 596 typedef CppAD::vector<size_t> SizeVector; 597 /// A simple vector of values used by Ipopt 598 typedef CppAD::vector<Ipopt::Number> NumberVector; 599 600 /*! 601 Abstract base class user derives from to define the funcitons in the problem. 602 */ 603 class cppad_ipopt_fg_info 604 { 605 /// allow cppad_ipopt_nlp class complete access to this class 606 friend class cppad_ipopt_nlp; 607 private: 608 /// domain space dimension for the functions f(x), g(x) 609 size_t n_; 610 /// range space dimension for the function g(x) 611 size_t m_; 612 /// the cppad_ipopt_nlp constructor uses this method to set n_ set_n(size_t n)613 void set_n(size_t n) 614 { n_ = n; } 615 /// the cppad_ipopt_nlp constructor uses this method to set m_ set_m(size_t m)616 void set_m(size_t m) 617 { m_ = m; } 618 619 public: 620 /// destructor virtual so user derived class destructor gets called ~cppad_ipopt_fg_info(void)621 virtual ~cppad_ipopt_fg_info(void) 622 { } 623 /// number_functions; i.e. K (simple representation uses 1) number_functions(void)624 virtual size_t number_functions(void) 625 { return 1; } 626 /// function that evaluates the users representation for f(x) and 627 /// and g(x) is pure virtual so user must define it in derived class 628 virtual ADVector eval_r(size_t k, const ADVector& u) = 0; 629 /// should the function r_k (u) be retaped when ever the arguemnt 630 /// u changes (default is true which is safe but slow) retape(size_t k)631 virtual bool retape(size_t k) 632 { return true; } 633 /// domain_size q[k] for r_k (u) (simple representation uses n) domain_size(size_t k)634 virtual size_t domain_size(size_t k) 635 { return n_; } 636 /// range_size p[k] for r_k (u) (simple representation uses m+1) range_size(size_t k)637 virtual size_t range_size(size_t k) 638 { return m_ + 1; } 639 /// number_terms that use r_k (u) (simple represenation uses 1) number_terms(size_t k)640 virtual size_t number_terms(size_t k) 641 { return 1; } 642 /// return the index vectors I_{k,ell} and J_{k,ell} 643 /// (simple representation uses I[i] = i and J[j] = j) index(size_t k,size_t ell,SizeVector & I,SizeVector & J)644 virtual void index(size_t k, size_t ell, SizeVector& I, SizeVector& J) 645 { assert( I.size() >= m_ + 1 ); 646 assert( J.size() >= n_ ); 647 for(size_t i = 0; i <= m_; i++) 648 I[i] = i; 649 for(size_t j = 0; j < n_; j++) 650 J[j] = j; 651 } 652 }; 653 654 /*! 655 Class that contains information about the problem solution 656 657 \section Nonlinear_Programming_Problem Nonlinear Programming Problem 658 We are give smooth functions 659 \f$ f : {\bf R}^n \rightarrow {\bf R} \f$ 660 and 661 \f$ g : {\bf R}^n \rightarrow {\bf R}^m \f$ 662 and wish to solve the problem 663 \f[ 664 \begin{array}{rcl} 665 {\rm minimize} & f(x) & {\rm w.r.t.} \; x \in {\bf R}^n 666 \\ 667 {\rm subject \; to} & g^l \leq g(x) \leq g^u 668 \\ 669 & x^l \leq x \leq x^u 670 \end{array} 671 \f] 672 673 674 \section Users_Representation Users Representation 675 The functions 676 \f$ f : {\bf R}^n \rightarrow {\bf R} \f$ and 677 \f$ g : {\bf R}^n \rightarrow {\bf R}^m \f$ are defined by 678 \f[ 679 \left( \begin{array}{c} f(x) \\ g(x) \end{array} \right) 680 = 681 \sum_{k=0}^{K-1} \; \sum_{\ell=0}^{L(k) - 1} 682 [ (m+1) \otimes I_{k,\ell} ] \; \circ 683 \; r_k \; \circ \; [ J_{k,\ell} \otimes n ] \; (x) 684 \f] 685 where for \f$ k = 0 , \ldots , K-1\f$, 686 \f$ r_k : {\bf R}^{q(k)} \rightarrow {\bf R}^{p(k)} \f$. 687 688 \section Deprecated_Evaluation_Methods Evaluation Methods 689 The set of evaluation methods for this class is 690 \verbatim 691 { eval_f, eval_grad_f, eval_g, eval_jac_g, eval_h } 692 \endverbatim 693 Note that the bool return flag for the evaluations methods 694 does not appear in the Ipopt documentation. 695 Looking at the code, it seems to be a flag telling Ipopt to abort 696 when the flag is false. 697 698 */ 699 class cppad_ipopt_solution 700 { 701 public: 702 /// possible values for he solution status 703 enum solution_status { 704 not_defined, 705 success, 706 maxiter_exceeded, 707 stop_at_tiny_step, 708 stop_at_acceptable_point, 709 local_infeasibility, 710 user_requested_stop, 711 feasible_point_found, 712 diverging_iterates, 713 restoration_failure, 714 error_in_step_computation, 715 invalid_number_detected, 716 too_few_degrees_of_freedom, 717 internal_error, 718 unknown 719 } status; 720 /// the approximation solution 721 NumberVector x; 722 /// Lagrange multipliers corresponding to lower bounds on x 723 NumberVector z_l; 724 /// Lagrange multipliers corresponding to upper bounds on x 725 NumberVector z_u; 726 /// value of g(x) 727 NumberVector g; 728 /// Lagrange multipliers correspondiing constraints on g(x) 729 NumberVector lambda; 730 /// value of f(x) 731 Ipopt::Number obj_value; 732 /// constructor initializes solution status as not yet defined cppad_ipopt_solution(void)733 cppad_ipopt_solution(void) 734 { status = not_defined; } 735 }; 736 737 /*! 738 Class connects Ipopt to CppAD for derivative and sparsity pattern calculations. 739 */ 740 class cppad_ipopt_nlp : public Ipopt::TNLP 741 { 742 private: 743 /// A Scalar value used by Ipopt 744 typedef Ipopt::Number Number; 745 /// An index value used by Ipopt 746 typedef Ipopt::Index Index; 747 /// Indexing style used in Ipopt sparsity structure 748 typedef Ipopt::TNLP::IndexStyleEnum IndexStyleEnum; 749 /// A simple vector of boolean values 750 typedef CppAD::vectorBool BoolVector; 751 /// A simple vector of AD function objects 752 typedef CppAD::vector< CppAD::ADFun<Number> > ADFunVector; 753 /// A simple vector of simple vectors of boolean values 754 typedef CppAD::vector<BoolVector> BoolVectorVector; 755 /// A mapping that is dense in i, sparse in j, and maps (i, j) 756 /// to the corresponding sparsity index in Ipopt. 757 typedef CppAD::vector< std::map<size_t,size_t> > IndexMap; 758 759 // ------------------------------------------------------------------ 760 // Values directly passed in to constuctor 761 // ------------------------------------------------------------------ 762 /// dimension of the domain space for f(x) and g(x) 763 /// (passed to ctor) 764 const size_t n_; 765 /// dimension of the range space for g(x) 766 /// (passed to ctor) 767 const size_t m_; 768 /// dimension of the range space for g(x) 769 /// (passed to ctor) 770 const NumberVector x_i_; 771 /// lower limit for x 772 /// (size n_), (passed to ctor) 773 const NumberVector x_l_; 774 /// upper limit for x 775 /// (size n_) (passed to ctor) 776 const NumberVector x_u_; 777 /// lower limit for g(x) 778 /// (size m_) (passed to ctor) 779 const NumberVector g_l_; 780 /// upper limit for g(x) 781 /// (size m_) (passed to ctor) 782 const NumberVector g_u_; 783 /// pointer to base class version of derived class object used to get 784 /// information about the user's representation for f(x) and g(x) 785 /// (passed to ctor) 786 cppad_ipopt_fg_info* const fg_info_; 787 /// pointer to object where final results are stored 788 /// (passed to ctor) 789 cppad_ipopt_solution* const solution_; 790 /// plus infinity as a value of type Number 791 const Number infinity_; 792 793 // ------------------------------------------------------------------ 794 // Effectively const values determined during constructor using calls 795 // to fg_info: 796 // ------------------------------------------------------------------ 797 /// The value of \f$ K \f$ in the representation. 798 /// (effectively const) 799 size_t K_; 800 /// Does operation sequence for \f$ r_k (u) \f$ depend on \f$ u \f$. 801 /// (size K_) (effectively const) 802 BoolVector retape_; 803 /// <tt>q_[k]</tt> is the domain space dimension for \f$ r_k (u) \f$ 804 /// (size K_) (effectively const) 805 SizeVector q_; 806 /// <tt>p_[k]</tt> is the range space dimension for \f$ r_k (u) \f$ 807 /// (size K_) (effectively const) 808 SizeVector p_; 809 /// <tt>L_[k]</tt> is number of times \f$ r_k (u) \f$ appears in 810 /// the representation summation 811 /// (size K_) (effectively const) 812 SizeVector L_; 813 // ------------------------------------------------------------------- 814 // Other effectively const values determined by the constructor: 815 // ------------------------------------------------------------------- 816 /*! 817 CppAD sparsity patterns for \f$ \{ r_k^{(1)} (u) \} \f$ (set by ctor). 818 819 For <tt>k = 0 , ... , K_-1, pattern_jac_r_[k]</tt> 820 is a CppAD sparsity pattern for the Jacobian of \f$ r_k (u) \f$ 821 and as such it has size <tt>p_[k]*q_[k]</tt>. 822 (effectively const) 823 */ 824 BoolVectorVector pattern_jac_r_; 825 826 /*! 827 CppAD sparsity patterns for \f$ \{ r_k^{(2)} (u) \} \f$ (set by ctor). 828 829 For <tt>k = 0 , ... , K_-1, pattern_jac_r_[k]</tt> 830 is a CppAD sparsity pattern for the Hessian of 831 \f[ 832 R(u) = \sum_{i=0}^{p[k]-1} r_k (u)_i 833 \f] 834 and as such it has size <tt>q_[k]*q_[k]</tt>. 835 (effectively const) 836 */ 837 BoolVectorVector pattern_hes_r_; 838 839 /// number non-zero is Ipopt sparsity structor for Jacobian of g(x) 840 /// (effectively const) 841 size_t nnz_jac_g_; 842 /// row indices in Ipopt sparsity structor for Jacobian of g(x) 843 /// (effectively const) 844 SizeVector iRow_jac_g_; 845 /// column indices in Ipopt sparsity structor for Jacobian of g(x) 846 /// (effectively const) 847 SizeVector jCol_jac_g_; 848 849 /// number non-zero is Ipopt sparsity structor for Hessian of Lagragian 850 /// (effectively const) 851 size_t nnz_h_lag_; 852 /// row indices in Ipopt sparsity structor for Hessian of Lagragian 853 /// (effectively const) 854 SizeVector iRow_h_lag_; 855 /// column indices in Ipopt sparsity structor for Hessian of Lagragian 856 /// (effectively const) 857 SizeVector jCol_h_lag_; 858 859 /*! 860 Mapping from (i, j) in Jacobian of g(x) to Ipopt sparsity structure 861 862 For <tt>i = 0 , ... , m_-1, index_jac_g_[i]</tt> 863 is a standard map from column index values j to the corresponding 864 index in the Ipopt sparsity structure for the Jacobian of g(x). 865 */ 866 IndexMap index_jac_g_; 867 868 /*! 869 Mapping from (i, j) in Hessian of fg(x) to Ipopt sparsity structure 870 871 For <tt>i = 0 , ... , n_-1, index_hes_fg_[i]</tt> 872 is a standard map from column index values j to the corresponding 873 index in the Ipopt sparsity structure for the Hessian of the Lagragian. 874 */ 875 IndexMap index_hes_fg_; 876 // ----------------------------------------------------------------- 877 // Values that are changed by routine other than the constructor: 878 // ----------------------------------------------------------------- 879 880 /// For <tt>k = 0 , ... , K_-1, r_fun_[k]</tt> 881 /// is a the CppAD function object corresponding to \f$ r_k (u) \f$. 882 ADFunVector r_fun_; 883 /*! 884 Is r_fun[k] OK for current x. 885 886 For <tt>k = 0 , ... , K_-1, tape_ok_[k]</tt> 887 is true if current operations sequence in <tt>r_fun_[k]</tt> 888 OK for this value of \f$ x \f$. 889 Note that \f$ u = [ J_{k,\ell} \otimes n ] (x) \f$ may depend on the 890 value of \f$ \ell \f$. 891 */ 892 BoolVector tape_ok_; 893 894 /// work space of size equal maximum of <tt>q[k]</tt> w.r.t k. 895 SizeVector J_; 896 /// work space of size equal maximum of <tt>p[k]</tt> w.r.t k. 897 SizeVector I_; 898 // ------------------------------------------------------------ 899 // Private Methods 900 // ------------------------------------------------------------ 901 /// block the default constructor from use 902 cppad_ipopt_nlp(const cppad_ipopt_nlp&); 903 /// blocks the assignment operator from use 904 cppad_ipopt_nlp& operator=(const cppad_ipopt_nlp&); 905 public: 906 // ---------------------------------------------------------------- 907 // See cppad_ipopt_nlp.cpp for doxygen documentation of these methods 908 // ---------------------------------------------------------------- 909 910 /// only constructor for cppad_ipopot_nlp 911 cppad_ipopt_nlp( 912 size_t n , 913 size_t m , 914 const NumberVector &x_i , 915 const NumberVector &x_l , 916 const NumberVector &x_u , 917 const NumberVector &g_l , 918 const NumberVector &g_u , 919 cppad_ipopt_fg_info* fg_info , 920 cppad_ipopt_solution* solution 921 ); 922 923 // use virtual so that derived class destructor gets called. 924 virtual ~cppad_ipopt_nlp(); 925 926 // return info about the nlp 927 virtual bool get_nlp_info( 928 Index& n , 929 Index& m , 930 Index& nnz_jac_g , 931 Index& nnz_h_lag , 932 IndexStyleEnum& index_style 933 ); 934 935 // return bounds for my problem 936 virtual bool get_bounds_info( 937 Index n , 938 Number* x_l , 939 Number* x_u , 940 Index m , 941 Number* g_l , 942 Number* g_u 943 ); 944 945 // return the starting point for the algorithm 946 virtual bool get_starting_point( 947 Index n , 948 bool init_x , 949 Number* x , 950 bool init_z , 951 Number* z_L , 952 Number* z_U , 953 Index m , 954 bool init_lambda , 955 Number* lambda 956 ); 957 958 // return the objective value 959 virtual bool eval_f( 960 Index n , 961 const Number* x , 962 bool new_x , 963 Number& obj_value 964 ); 965 966 // Method to return the gradient of the objective 967 virtual bool eval_grad_f( 968 Index n , 969 const Number* x , 970 bool new_x , 971 Number* grad_f 972 ); 973 974 // return the constraint residuals 975 virtual bool eval_g( 976 Index n , 977 const Number* x , 978 bool new_x , 979 Index m , 980 Number* g 981 ); 982 983 // Method to return: 984 // 1) The structure of the jacobian (if "values" is NULL) 985 // 2) The values of the jacobian (if "values" is not NULL) 986 virtual bool eval_jac_g( 987 Index n , 988 const Number* x , 989 bool new_x , 990 Index m , 991 Index nele_jac , 992 Index* iRow , 993 Index* jCol , 994 Number* values 995 ); 996 997 // Method to return: 998 // 1) structure of hessian of the lagrangian (if "values" is NULL) 999 // 2) values of hessian of the lagrangian (if "values" is not NULL) 1000 virtual bool eval_h( 1001 Index n , 1002 const Number* x , 1003 bool new_x , 1004 Number obj_factor , 1005 Index m , 1006 const Number* lambda , 1007 bool new_lambda , 1008 Index nele_hess , 1009 Index* iRow , 1010 Index* jCol , 1011 Number* values 1012 ); 1013 1014 // called when the algorithm is completed so the TNLP can 1015 // store/write the solution 1016 virtual void finalize_solution( 1017 Ipopt::SolverReturn status , 1018 Index n , 1019 const Number* x , 1020 const Number* z_L , 1021 const Number* z_U , 1022 Index m , 1023 const Number* g , 1024 const Number* lambda , 1025 Number obj_value , 1026 const Ipopt::IpoptData* ip_data , 1027 Ipopt::IpoptCalculatedQuantities* ip_cq 1028 ); 1029 1030 virtual bool intermediate_callback( 1031 Ipopt::AlgorithmMode mode, 1032 Index iter, 1033 Number obj_value, 1034 Number inf_pr, 1035 Number inf_du, 1036 Number mu, 1037 Number d_norm, 1038 Number regularization_size, 1039 Number alpha_du, 1040 Number alpha_pr, 1041 Index ls_trials, 1042 const Ipopt::IpoptData* ip_data, 1043 Ipopt::IpoptCalculatedQuantities* ip_cq 1044 ); 1045 1046 }; 1047 1048 1049 // --------------------------------------------------------------------------- 1050 } // end namespace cppad_ipopt 1051 // --------------------------------------------------------------------------- 1052 1053 # endif 1054