1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2007 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*//** |
19 |
@file |
20 |
Connection of the IPOPT optimisation solver into ASCEND. |
21 |
|
22 |
THIS IS STILL VERY MUCH UNDER DEVELOPMENT AND INCOMPLETE. I'VE ACTUALLY |
23 |
ONLY JUST STARTED WRITING IT by starting with asc_tron.c and modifying. |
24 |
|
25 |
The IPOPT solver is documented at http://www.coin-or.org/Ipopt/ |
26 |
*//* |
27 |
ASCEND wrapper for IPOPT originally by John Pye, Jun 2007. |
28 |
*/ |
29 |
|
30 |
#include <solver/solver.h> |
31 |
|
32 |
#include <ipopt/IpStdCInterface.h> |
33 |
|
34 |
ASC_DLLSPEC SolverRegisterFn ipopt_register; |
35 |
|
36 |
#if 0 |
37 |
// sample code for the C interface |
38 |
|
39 |
int main(){ |
40 |
Index n=-1; /* number of variables */ |
41 |
Index m=-1; /* number of constraints */ |
42 |
Number* x_L = NULL; /* lower bounds on x */ |
43 |
Number* x_U = NULL; /* upper bounds on x */ |
44 |
Number* g_L = NULL; /* lower bounds on g */ |
45 |
Number* g_U = NULL; /* upper bounds on g */ |
46 |
IpoptProblem nlp = NULL; /* IpoptProblem */ |
47 |
enum ApplicationReturnStatus status; /* Solve return code */ |
48 |
Number* x = NULL; /* starting point and solution vector */ |
49 |
Number* mult_x_L = NULL; /* lower bound multipliers |
50 |
at the solution */ |
51 |
Number* mult_x_U = NULL; /* upper bound multipliers |
52 |
at the solution */ |
53 |
Number obj; /* objective value */ |
54 |
Index i; /* generic counter */ |
55 |
|
56 |
/* set the number of variables and allocate space for the bounds */ |
57 |
n=4; |
58 |
x_L = (Number*)malloc(sizeof(Number)*n); |
59 |
x_U = (Number*)malloc(sizeof(Number)*n); |
60 |
/* set the values for the variable bounds */ |
61 |
for (i=0; i<n; i++) { |
62 |
x_L[i] = 1.0; |
63 |
x_U[i] = 5.0; |
64 |
} |
65 |
|
66 |
/* set the number of constraints and allocate space for the bounds */ |
67 |
m=2; |
68 |
g_L = (Number*)malloc(sizeof(Number)*m); |
69 |
g_U = (Number*)malloc(sizeof(Number)*m); |
70 |
/* set the values of the constraint bounds */ |
71 |
g_L[0] = 25; g_U[0] = 2e19; |
72 |
g_L[1] = 40; g_U[1] = 40; |
73 |
|
74 |
/* create the IpoptProblem */ |
75 |
nlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, 8, 10, 0, |
76 |
&eval_f, &eval_g, &eval_grad_f, |
77 |
&eval_jac_g, &eval_h); |
78 |
|
79 |
/* We can free the memory now - the values for the bounds have been |
80 |
copied internally in CreateIpoptProblem */ |
81 |
free(x_L); |
82 |
free(x_U); |
83 |
free(g_L); |
84 |
free(g_U); |
85 |
|
86 |
/* set some options */ |
87 |
AddIpoptNumOption(nlp, "tol", 1e-9); |
88 |
AddIpoptStrOption(nlp, "mu_strategy", "adaptive"); |
89 |
|
90 |
/* allocate space for the initial point and set the values */ |
91 |
x = (Number*)malloc(sizeof(Number)*n); |
92 |
x[0] = 1.0; |
93 |
x[1] = 5.0; |
94 |
x[2] = 5.0; |
95 |
x[3] = 1.0; |
96 |
|
97 |
/* allocate space to store the bound multipliers at the solution */ |
98 |
mult_x_L = (Number*)malloc(sizeof(Number)*n); |
99 |
mult_x_U = (Number*)malloc(sizeof(Number)*n); |
100 |
|
101 |
/* solve the problem */ |
102 |
status = IpoptSolve(nlp, x, NULL, &obj, NULL, mult_x_L, mult_x_U, NULL); |
103 |
|
104 |
if (status == Solve_Succeeded) { |
105 |
printf("\n\nSolution of the primal variables, x\n"); |
106 |
for (i=0; i<n; i++) { |
107 |
printf("x[%d] = %e\n", i, x[i]); |
108 |
} |
109 |
|
110 |
printf("\n\nSolution of the bound multipliers, z_L and z_U\n"); |
111 |
for (i=0; i<n; i++) { |
112 |
printf("z_L[%d] = %e\n", i, mult_x_L[i]); |
113 |
} |
114 |
for (i=0; i<n; i++) { |
115 |
printf("z_U[%d] = %e\n", i, mult_x_U[i]); |
116 |
} |
117 |
|
118 |
printf("\n\nObjective value\n"); |
119 |
printf("f(x*) = %e\n", obj); |
120 |
} |
121 |
|
122 |
/* free allocated memory */ |
123 |
FreeIpoptProblem(nlp); |
124 |
free(x); |
125 |
free(mult_x_L); |
126 |
free(mult_x_U); |
127 |
|
128 |
return 0; |
129 |
} |
130 |
#endif |
131 |
|
132 |
int ipopt_register(void){ |
133 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to load IPOPT"); |
134 |
return 1; |
135 |
/* return solver_register(&tron_internals); */ |
136 |
} |