# source:stable/3.9/Ipopt/contrib/RInterface/tests/mynlp.R@1831

Last change on this file since 1831 was 1831, checked in by andreasw, 3 years ago

merged with trunk rev 1830

File size: 2.1 KB
Line
2# This code is published under the Common Public License.
3#
4# File:   mynlp.R
5# Author: Jelmer Ypma
6# Date:   18 April 2010
7#
8# Example NLP for interfacing a problem with IPOPT.
9# This example is adapted from the C++ example that
10# goes along with the Ipopt tutorial document.
11# This example solves the following problem:
12#
13# min_x f(x) = -(x2-2)^2
14#  s.t.
15#       0 = x1^2 + x2 - 1
16#       -1 <= x1 <= 1
17
18library('ipoptr')
19
20eval_f <- function( x ) {
21    print( paste( "In R::eval_f, x = ", paste( x, collapse=', ' ) ) )
22
23    return( -(x[2] - 2.0)*(x[2] - 2.0) )
24}
25
26eval_grad_f <- function( x ) {
27    return( c(0.0, -2.0*(x[2] - 2.0) ) )
28}
29
30eval_g <- function( x ) {
31    return( -(x[1]*x[1] + x[2] - 1.0) );
32}
33
34# list with indices of non-zero elements
35# each element of the list corresponds to the derivative of one constraint
36#
37# e.g.
38#      / 0 x x \
39#      \ x 0 x /
40# would be
41# list( c(2,3), c(1,3) )
42eval_jac_g_structure <- list( c(1,2) )
43
44
45# this should return a vector with all the non-zero elements
46# so, no list here, because that is slower I guess
47# TODO: make an R-function that shows the structure in matrix form
48eval_jac_g <- function( x ) {
49    return ( c ( -2.0 * x[1], -1.0 ) )
50}
51
52
53# diagonal matrix, usually only fill the lower triangle
54eval_h_structure <- list( c(1), c(2) )
55
56eval_h <- function( x, obj_factor, hessian_lambda ) {
57    return ( c( -2.0*hessian_lambda[1], -2.0*obj_factor ) )
58}
59
60x0 <- c(0.5,1.5)
61
62lb <- c( -1, -1.0e19 )
63ub <- c(  1,  1.0e19 )
64
65constraint_lb <- 0
66constraint_ub <- 0
67
68opts <- list("print_level"=0,
69             "file_print_level"=12,
70             "output_file"="ipopttest.out")
71
72print( ipoptr( x0=x0,
73               eval_f=eval_f,