<body bgcolor="#ffffff" background="https://img.web.de/v/p.gif" class="bgRepeatYes" style="background-repeat: repeat; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-size: 9pt; padding-left: 0px;"><div style="min-height: 200px; background-image: url(https://img.web.de/v/p.gif); background-repeat: repeat; background-color: #ffffff; font-size: 9pt; padding-left: 0px;"><span style="font-family: courier new,courier;"><span style="font-size: 9pt;"><span style="background-color: transparent;"><span style="color: #000000;"><span style="color: #000000;">Dear Ipopt-community,<br /><br />I am having problems solving a simple optimization problem with only two variables.<br /></span></span></span></span>The problem is a very much simplified dynamic optimization problem, where <span style="font-size: 9pt;"><span style="background-color: transparent;"><span style="color: #000000;"><span style="color: #000000;">the number of<br />variables n can be varied from 2 to an arbitrary number:<br /><br /></span></span></span></span>f=(x_n-2)^2<br /><br />grad_f= <br />( 0 )<br />( ... )<br />( 2*(x_n-2) )<br /><br />g= <br />( x_1+x_2*(h-1) )<br />( ... )<br />( x_(n-1)+x_n*(h-1) )<br /><br />jac_g=<br />( 1 h-1 ... 0 )<br />( ... )<br />( 0 ... 1 h-1 )<br /><br />h= <br />( 0 ... 0 )<br />( ... )<br />( 0 ... 2 )<br /><br />This is a very much simplified dynamic optimization problem. I modified the hs071.c example<br />code to solve this problem (the code is pasted below; it can be compiled with the makefile of <br />the hs071.c example); however, Ipopt finds it infeasible. I conjecture there is a problem<br />with the variable bounds since the Ipopt output says the total number of variables is one when<br />it is set to n=2. When n is set to 3, the message '"Too few degrees of freedom (n_x = 1, n_c = 2)."<br />is displayed after starting the optimization. For larger n, the same message with n_x = 1, n_c = (n-1)<br />appears. It therefore seems that the upper and lower variable bounds for all but one variables are<br />fixed to 0. This is also what I observe when displaying the variables during the iterations: only <br />variable n (n-1 in C-style counting) is changing its value.<br />It might well be that the problem lies somewhere else or in some programming mistake I made. I would<br />be thankful for any help or idea on this problem.<br /><br />Best regards,<br />Andreas<br /><br /><br /><br />#include "IpStdCInterface.h"<br />#include <stdlib.h><br />#include <assert.h><br />#include <stdio.h><br /><br />/* Function Declarations */<br />Bool eval_f(Index n, Number* x, Bool new_x,<br /> Number* obj_value, UserDataPtr user_data);<br /><br />Bool eval_grad_f(Index n, Number* x, Bool new_x,<br /> Number* grad_f, UserDataPtr user_data);<br /><br />Bool eval_g(Index n, Number* x, Bool new_x,<br /> Index m, Number* g, UserDataPtr user_data);<br /><br />Bool eval_jac_g(Index n, Number *x, Bool new_x,<br /> Index m, Index nele_jac,<br /> Index *iRow, Index *jCol, Number *values,<br /> UserDataPtr user_data);<br /><br />Bool eval_h(Index n, Number *x, Bool new_x, Number obj_factor,<br /> Index m, Number *lambda, Bool new_lambda,<br /> Index nele_hess, Index *iRow, Index *jCol,<br /> Number *values, UserDataPtr user_data);<br /> <br />double rhs(double x);<br />double dfdx(double x);<br />double d2fdx2(double x);<br /><br />int N=2;<br />double h=0.2;<br /><br />/* Main Program */<br />int main()<br />{<br /> Index n=-1; /* number of variables */<br /> Index m=-1; /* number of constraints */<br /> Number* x_L = NULL; /* lower bounds on x */<br /> Number* x_U = NULL; /* upper bounds on x */<br /> Number* g_L = NULL; /* lower bounds on g */<br /> Number* g_U = NULL; /* upper bounds on g */<br /> IpoptProblem nlp = NULL; /* IpoptProblem */<br /> enum ApplicationReturnStatus status; /* Solve return code */<br /> Number* x = NULL; /* starting point and solution vector */<br /> Number* mult_x_L = NULL; /* lower bound multipliers<br /> at the solution */<br /> Number* mult_x_U = NULL; /* upper bound multipliers<br /> at the solution */<br /> Number obj; /* objective value */<br /> Index i; /* generic counter */<br /><br /> /* Number of nonzeros in the Jacobian of the constraints */<br /> Index nele_jac = 2*(N-1);<br /> /* Number of nonzeros in the Hessian of the Lagrangian (lower or<br /> upper triangual part only) */<br /> Index nele_hess = 1;<br /> /* indexing style for matrices */<br /> Index index_style = 0; /* C-style; start counting of rows and column<br /> indices at 0 */<br /><br /> /* set the number of variables and allocate space for the bounds */<br /> n=N;<br /> x_L = (Number*)malloc(sizeof(Number)*n);<br /> x_U = (Number*)malloc(sizeof(Number)*n);<br /> /* set the values for the variable bounds */<br /> for (i=0; i<n; i++) {<br /> x_L[i] = 0.1;<br /> x_U[i] = 3.0;<br /> }<br /><br /> /* set the number of constraints and allocate space for the bounds */<br /> m=N-1;<br /> g_L = (Number*)malloc(sizeof(Number)*m);<br /> g_U = (Number*)malloc(sizeof(Number)*m);<br /> /* set the values of the constraint bounds */<br /> for (i=0; i<m; i++) {<br /> x_L[i] = 0;<br /> x_U[i] = 0;<br /> } <br /><br /> /* create the IpoptProblem */<br /> nlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess,<br /> index_style, &eval_f, &eval_g, &eval_grad_f,<br /> &eval_jac_g, &eval_h);<br /><br /> /* We can free the memory now - the values for the bounds have been<br /> copied internally in CreateIpoptProblem */<br /> free(x_L);<br /> free(x_U);<br /> free(g_L);<br /> free(g_U);<br /><br /> /* Set some options. Note the following ones are only examples,<br /> they might not be suitable for your problem. */<br /> AddIpoptNumOption(nlp, "tol", 1e-7);<br /> AddIpoptStrOption(nlp, "mu_strategy", "adaptive");<br /> AddIpoptStrOption(nlp, "output_file", "ipopt.out");<br /><br /> /* allocate space for the initial point and set the values */<br /> x = (Number*)malloc(sizeof(Number)*n);<br /> <br /> for (i=0; i<n; i++) {<br /> x[i] = 1;<br /> }<br /><br /> /* allocate space to store the bound multipliers at the solution */<br /> mult_x_L = (Number*)malloc(sizeof(Number)*n);<br /> mult_x_U = (Number*)malloc(sizeof(Number)*n);<br /><br /> /* solve the problem */<br /> status = IpoptSolve(nlp, x, NULL, &obj, NULL, mult_x_L, mult_x_U, NULL);<br /><br /> if (status == Solve_Succeeded) {<br /> printf("\n\nSolution of the primal variables, x\n");<br /> for (i=0; i<n; i++) {<br /> printf("x[%d] = %e\n", i, x[i]);<br /> }<br /><br /> printf("\n\nSolution of the bound multipliers, z_L and z_U\n");<br /> for (i=0; i<n; i++) {<br /> printf("z_L[%d] = %e\n", i, mult_x_L[i]);<br /> }<br /> for (i=0; i<n; i++) {<br /> printf("z_U[%d] = %e\n", i, mult_x_U[i]);<br /> }<br /><br /> printf("\n\nObjective value\n");<br /> printf("f(x*) = %e\n", obj);<br /> }<br /><br /> /* free allocated memory */<br /> FreeIpoptProblem(nlp);<br /> free(x);<br /> free(mult_x_L);<br /> free(mult_x_U);<br /><br /> return 0;<br />}<br /><br /><br />/* Function Implementations */<br />Bool eval_f(Index n, Number* x, Bool new_x,<br /> Number* obj_value, UserDataPtr user_data)<br />{<br /> assert(n == N);<br /> *obj_value = (x[N-1]-2)*(x[N-1]-2);<br /><br /> return TRUE;<br />}<br /><br />Bool eval_grad_f(Index n, Number* x, Bool new_x,<br /> Number* grad_f, UserDataPtr user_data)<br />{<br /> int i;<br /> assert(n == N);<br /><br /> for (i=0;i<N-1;i++) grad_f[i]=0;<br /> grad_f[N-1]=2*(x[N-1]-2);<br /><br /> return TRUE;<br />}<br /><br />Bool eval_g(Index n, Number* x, Bool new_x,<br /> Index m, Number* g, UserDataPtr user_data)<br />{<br /> int i;<br /> assert(n == N);<br /> assert(m == N-1);<br /><br /> for (i=0;i<N-1;i++){<br /> g[i]=x[i]+h*x[i+1]-x[i+1];<br /> }<br /><br /> return TRUE;<br />}<br /><br />Bool eval_jac_g(Index n, Number *x, Bool new_x,<br /> Index m, Index nele_jac,<br /> Index *iRow, Index *jCol, Number *values,<br /> UserDataPtr user_data)<br />{<br /> if (values == NULL) {<br /> /* return the structure of the jacobian */<br /><br /> /* this particular jacobian is dense */<br /> int i;<br /> int c=0;<br /> for (i=0;i<N-1;i++){<br /> iRow[c]=i;<br /> jCol[c]=i;<br /> c++;<br /> iRow[c]=i;<br /> jCol[c]=i+1;<br /> c++;<br /> }<br /> assert(nele_jac==c);<br /> }<br /> else {<br /> /* return the values of the jacobian of the constraints */<br /><br /> int i;<br /> int c=0;<br /> for (i=0;i<N-1;i++){<br /> values[c]=1;<br /> c++;<br /> values[c]=h-1;<br /> c++;<br /> }<br /> }<br /><br /> return TRUE;<br />}<br /><br />Bool eval_h(Index n, Number *x, Bool new_x, Number obj_factor,<br /> Index m, Number *lambda, Bool new_lambda,<br /> Index nele_hess, Index *iRow, Index *jCol,<br /> Number *values, UserDataPtr user_data)<br />{<br /><br /> if (values == NULL) {<br /> /* return the structure. This is a symmetric matrix, fill the lower left<br /> * triangle only. */<br /><br /> /* the hessian for this problem is actually dense */<br /><br /> iRow[0] = N-1;<br /> jCol[0] = N-1;<br /><br /> assert(1 == nele_hess);<br /> }<br /> else {<br /> /* return the values. This is a symmetric matrix, fill the lower left<br /> * triangle only */<br /> <br /> values[0]=obj_factor *2;<br /><br /> /* fill the objective portion */<br /><br /> }<br /><br /> return TRUE;<br />}<br /></span></div> <br><br><table cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#000000"><img src="https://img.web.de/p.gif" width="1" height="1" border="0" alt="" /></td></tr><tr><td style="font-family:verdana; font-size:12px; line-height:17px;">WEB.DE DSL Doppel-Flat ab 19,99 €/mtl.! Jetzt auch mit <br>gratis Notebook-Flat! <a href="http://produkte.web.de/go/DSL_Doppel_Flatrate/2"><b>http://produkte.web.de/go/DSL_Doppel_Flatrate/2</b></a></td></tr></table>
</body>