/* Copyright (C) 2005, 2006 International Business Machines and others. * All Rights Reserved. * This code is published under the Common Public License. * * $Id: hs071_c.c 1324 2008-09-16 14:19:26Z andreasw $ * * Authors: Carl Laird, Andreas Waechter IBM 2005-08-17 */ #include "IpStdCInterface.h" #include #include #include #include /* Function Declarations */ Bool eval_f(Index n, Number* x, Bool new_x, Number* obj_value, UserDataPtr user_data); Bool eval_grad_f(Index n, Number* x, Bool new_x, Number* grad_f, UserDataPtr user_data); Bool eval_g(Index n, Number* x, Bool new_x, Index m, Number* g, UserDataPtr user_data); Bool eval_jac_g(Index n, Number *x, Bool new_x, Index m, Index nele_jac, Index *iRow, Index *jCol, Number *values, UserDataPtr user_data); Bool eval_h(Index n, Number *x, Bool new_x, Number obj_factor, Index m, Number *lambda, Bool new_lambda, Index nele_hess, Index *iRow, Index *jCol, Number *values, UserDataPtr user_data); /* Main Program */ int main() { Index n=-1; /* number of variables */ Index m=-1; /* number of constraints */ Number* x_L = NULL; /* lower bounds on x */ Number* x_U = NULL; /* upper bounds on x */ Number* g_L = NULL; /* lower bounds on g */ Number* g_U = NULL; /* upper bounds on g */ IpoptProblem nlp = NULL; /* IpoptProblem */ enum ApplicationReturnStatus status; /* Solve return code */ Number* x = NULL; /* starting point and solution vector */ Number* mult_x_L = NULL; /* lower bound multipliers at the solution */ Number* mult_x_U = NULL; /* upper bound multipliers at the solution */ Number obj; /* objective value */ Index i; /* generic counter */ /* Number of nonzeros in the Jacobian of the constraints */ Index nele_jac = 18;//attention /* Number of nonzeros in the Hessian of the Lagrangian (lower or upper triangual part only) */ Index nele_hess = 12;//attention /* indexing style for matrices */ Index index_style = 0; /* C-style; start counting of rows and column indices at 0 */ double PI=3.141592653; /* set the number of variables and allocate space for the bounds */ n=12; x_L = (Number*)malloc(sizeof(Number)*n); x_U = (Number*)malloc(sizeof(Number)*n); /* set the values for the variable bounds */ for (i=0; i<2; i++) { x_L[i] = -PI/2; x_U[i] = PI/2; } for (i = 2; i<4; i++) { x_L[i] = 0.95; x_U[i] = 1.05; } for (i = 4; i<6; i++) { x_L[i] = 0.9; x_U[i] = 1.1; } for (i = 6; i<12; i++) { x_L[i] = 0; x_U[i] = pow(10,7); } /* set the number of constraints and allocate space for the bounds */ m=3; g_L = (Number*)malloc(sizeof(Number)*m); g_U = (Number*)malloc(sizeof(Number)*m); /* set the values of the constraint bounds */ g_L[0] = 0; g_U[0] = 0; g_L[1] = 0; g_U[1] = 0; g_L[2] = 0.1; g_U[2] = 0.3099; /* create the IpoptProblem */ nlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess, index_style, &eval_f, &eval_g, &eval_grad_f, &eval_jac_g, &eval_h); /* We can free the memory now - the values for the bounds have been copied internally in CreateIpoptProblem */ free(x_L); free(x_U); free(g_L); free(g_U); /* Set some options. Note the following ones are only examples, they might not be suitable for your problem. */ //attention AddIpoptNumOption(nlp, "tol", 1e-7); AddIpoptStrOption(nlp, "mu_strategy", "adaptive"); AddIpoptStrOption(nlp, "output_file", "ipopt.out"); /* allocate space for the initial point and set the values */ x = (Number*)malloc(sizeof(Number)*n); for (i = 0; i<2; i++) x[i] = 0; for (i = 2; i<4; i++) x[i] = 1; for (i = 4; i<6; i++) x[i] = 1.026087; for (i = 6; i<12; i++) x[i] = 0; /* allocate space to store the bound multipliers at the solution */ mult_x_L = (Number*)malloc(sizeof(Number)*n); mult_x_U = (Number*)malloc(sizeof(Number)*n); /* solve the problem */ status = IpoptSolve(nlp, x, NULL, &obj, NULL, mult_x_L, mult_x_U, NULL); if (status == Solve_Succeeded) { printf("\n\nSolution of the primal variables, x\n"); for (i=0; i