/* Copyright (C) 2006 International Business Machines and others. * All Rights Reserved. * This code is published under the Common Public License. * * $Id: TutorialC.c 1097 2006-07-17 08:21:46Z andreasw $ * * Author: Andreas Waechter IBM 2006-07-16 */ /* // This is part of the Ipopt tutorial at DIMACS's COIN-OR workshop. // // Implementation of the main program for execise problem // // param n := 4; // // var x {1..n} <= 0, >= -1.5, := -0.5; // // minimize obj: // sum{i in 1..n} (x[i]-1)^2; // ; // // subject to constr {i in 2..n-1}: // (x[i]^2+1.5*x[i]-i/n)*cos(x[i+1]) - x[i-1] = 0; // // The constant term "i/n" in the constraint is supposed to be input data */ #include "IpStdCInterface.h" #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); /* Structure to communicate problem data */ struct _ProblemData { int N; double* a; }; typedef struct _ProblemData* ProblemData; /* Main Program */ int main() { Index n=-1; /* number of variables */ Index m=-1; /* number of constraints */ Index nele_jac; /* number of nonzeros in Jacobian */ Index nele_hess; /* number of nonzeros in Hessian */ Index index_style; /* indexing style for matrices */ 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 */ int size; /* Size of the problem */ ProblemData PD; /* Pointer to structure with problem data */ /* Specify size of the problem */ size = 300; /* Set the problem data */ PD = (ProblemData)malloc(sizeof(struct _ProblemData)); PD->N = size; PD->a = malloc(sizeof(double)*(size-2)); for (i=0; ia[i] = ((double)(i+2))/(double)size; } /* set the number of variables and allocate space for the bounds */ n=size; x_L = (Number*)malloc(sizeof(Number)*n); x_U = (Number*)malloc(sizeof(Number)*n); /* set the values for the variable bounds */ for (i=0; ia); free(PD); return 0; } /* Function Implementations */ Bool eval_f(Index n, Number* x, Bool new_x, Number* obj_value, UserDataPtr user_data) { int i; ProblemData PD = (ProblemData)user_data; assert(n == PD->N); *obj_value = 0.; for (i=0; iN); for (i=0; ia; assert(n == PD->N); assert(m == PD->N-2); for (j=0; ja; if (values == NULL) { /* return the structure of the jacobian */ inz = 0; for (j=0; ja; if (values == NULL) { /* return the structure. This is a symmetric matrix, fill the * upper right triangle only. */ inz = 0; /* First variable has only a diagonal entry */ iRow[inz] = 0; jCol[inz] = 0; inz++; /* Next ones have first off-diagonal and diagonal */ for (i=1; i1) { values[inz] -= lambda[i-2]*(x[i]*x[i] + 1.5*x[i] -a[i-2])*cos(x[i]); } inz++; values[inz] = -lambda[i-1]*(2.*x[i]+1.5)*sin(x[i+1]); inz++; } values[inz] = obj_factor*2.; values[inz] -= lambda[n-3]*(x[n-1]*x[n-1] + 1.5*x[n-1] -a[n-3])*cos(x[n-1]); inz++; assert(inz == nele_hess); } return TRUE; }