CONOPT
Loading...
Searching...
No Matches
tutorial.cpp
Go to the documentation of this file.
1
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <math.h>
11#include <string.h>
12#include <iostream>
13#include "conopticpp.h"
14
16{
17public:
20 {
21 }
22
27 {
28 /* */
29 /* Information about Variables: */
30 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
31 /* Default: the status information in Vsta is not used. */
32 /* */
33 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
34 /* */
35 addVariable(0.1, CONOPT_INF, 0.5);
36 /* */
37 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
38 /* */
39 addVariable(0.1, CONOPT_INF, 0.5);
40 /* */
41 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
42 /* default initial value of 0 is used: */
43 /* */
46 /* */
47 /* Information about Constraints: */
48 /* Default: Rhs = 0 */
49 /* Default: the status information in Esta and the function */
50 /* value in FV are not used. */
51 /* Default: Type: There is no default. */
52 /* 0 = Equality, */
53 /* 1 = Greater than or equal, */
54 /* 2 = Less than or equal, */
55 /* 3 = Non binding. */
56 /* */
57 /* Constraint 0 (Objective) */
58 /* Rhs = -0.1 and type Non binding */
59 /* */
60 addConstraint(ConoptConstraintType::Free, -0.1, {0, 1, 2, 3}, {-1, -1, 0, 0}, {0, 0, 1, 1});
61 /* */
62 /* Constraint 1 (Production Function) */
63 /* Rhs = 0 and type Equality */
64 /* */
65 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1, 2}, {0, 0, -1}, {1, 1, 0});
66 /* */
67 /* Constraint 2 (Price equation) */
68 /* Rhs = 4.0 and type Equality */
69 /* */
70 addConstraint(ConoptConstraintType::Eq, 4.0, {2, 3}, {1, 2}, {0, 0});
71
72 /* setting the objective constraint */
74
75 /* setting the optimisation direction */
77 }
78
83 int FDEval(const double x[], double* g, double jac[], int rowno, const int jacnum[], int mode, int ignerr,
84 int* errcnt, int numvar, int numjac, int thread)
85 {
86 /* */
87 /* Declare local copies of the optimization variables. This is */
88 /* just for convenience to make the expressions easier to read. */
89 /* */
90 double L, Inp, Out, P;
91 /* */
92 /* Declare parameters and their data values. */
93 /* */
94 double Al = 0.16;
95 double Ak = 2.0;
96 double Ainp = 0.16;
97 double Rho = 1.0;
98 double K = 4.0;
99 double hold1, hold2, hold3;
100
101 /* */
102 /* Move the optimization variables from the X vector to a set */
103 /* of local variables with the same names as the variables in */
104 /* the model description. This is not necessary, but it should make*/
105 /* the equations easier to recognize. */
106 /* This time we work with the C numbering convention */
107 /* */
108 L = x[0];
109 Inp = x[1];
110 Out = x[2];
111 P = x[3];
112 /* */
113 /* Row 0: the objective function is nonlinear */
114 /* */
115
116 if ( rowno == 0 ) {
117 /* */
118 /* Mode = 1 or 3. Function value: G = P * Out */
119 /* */
120 if ( mode == 1 || mode == 3 )
121 *g = P * Out;
122 /* */
123 /* Mode = 2 or 3: Derivative values: */
124 /* */
125 if ( mode == 2 || mode == 3 ) {
126 jac[2] = P; /* derivative w.r.t. Out = x[2] */
127 jac[3] = Out; /* derivative w.r.t. P = x[3] */
128 }
129 }
130 /* */
131 /* Row 1: The production function is nonlinear */
132 /* */
133 else if ( rowno == 1 ) {
134 /* */
135 /* Compute some common terms */
136 /* */
137 hold1 = (Al*pow(L,(-Rho)) + Ak*pow(K,(-Rho)) + Ainp*pow(Inp,(-Rho)));
138 hold2 = pow(hold1,( -1./Rho ));
139 /* */
140 /* Mode = 1 or 3: Function value */
141 /* */
142 if ( mode == 1 || mode == 3 )
143 *g = hold2;
144 /* */
145 /* Mode = 2 or 3: Derivatives */
146 /* */
147 if ( mode == 2 || mode == 3 ) {
148 hold3 = hold2 / hold1;
149 jac[0] = hold3 * Al * pow(L ,(-Rho-1.)); /* derivative w.r.t. L = x[0] */
150 jac[1] = hold3 * Ainp * pow(Inp,(-Rho-1.)); /* derivative w.r.t. Inp = x[1] */
151 }
152 }
153 /* */
154 /* Row = 2: The row is linear and will not be called. */
155 /* */
156
157 return 0;
158 }
159};
160
161#include "std.cpp"
162
167int main(int argc, char** argv)
168{
169 int COI_Error = 0;
170
171 // getting the program name from the executable path
172 std::string pname = getProgramName(argv[0]);
173
174 // initialising the Conopt Object
175 ConoptCpp conopt(pname);
176 Tut_ModelData modeldata;
177 Tut_MessageHandler msghandler(pname);
178
179 // adding the message handler to the conopt interface
180 conopt.setMessageHandler(msghandler);
181
182 // building the model
183 modeldata.buildModel();
184
185 // loading the model in the conopt object
186 conopt.loadModel(modeldata);
187
188#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
189 std::string license = LICENSE_TEXT;
190 COI_Error += conopt.setLicense(LICENSE_INT_1, LICENSE_INT_2, LICENSE_INT_3, license);
191#endif
192
193 if ( COI_Error )
194 cpp_log(conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
195
196 COI_Error = conopt.solve(); /* Optimize */
197
198 // checking the statuses and objective value
199 if ( conopt.modelStatus() != 2 || conopt.solutionStatus() != 1 )
200 {
201 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
202 }
203 else if ( fabs( conopt.objectiveValue() - 0.572943 ) > 0.000001 )
204 {
205 cpp_log(conopt, "Incorrect objective returned", -1);
206 }
207
208 // printing the final status of the optimisation
209 conopt.printStatus();
210
211 cpp_log(conopt, "Successful Solve", COI_Error);
212}
The Conopt class.
Definition conopticpp.h:27
char pname[MAXLINE]
Definition comdecl.h:10
int COI_Error
Definition comdecl.h:15
CONOPT C++ interface header file. This is the main object for the CONOPT C++ interface.
#define CONOPT_INF
Definition defines.h:18
COI_API void setMessageHandler(ConoptMessageHandler &msghandler)
sets the message handler to the user supplied handler.
COI_API void loadModel(ConoptModelData &modeldata)
loads the model and stores the pointer in the interface
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition tutorial.cpp:167
int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode, int ignerr, int *errcnt, int numvar, int numjac, int thread)
defines the nonlinearities of the model by returning numerical values.
Definition tutorial.cpp:83
void buildModel()
adds the variables and constraints for the problem
Definition tutorial.cpp:26
int addVariable(double lower, double upper, double curr=0, int varstatus=-1)
adds a variable to the model. The non-zero coefficients are added later.
void setObjectiveElement(ConoptObjectiveElement elem, int elemindex)
sets the index for the objective variable or constraint
int addConstraint(ConoptConstraintType constype, double rhs, int slackstatus=-1)
adds a constraint to the problem. The non-zero coefficients are added later
void setOptimizationSense(ConoptSense sense)
sets the optimisation direction.
COI_API int setLicense(int licint1, int licint2, int licint3, std::string licstring)
define the License Information.
COI_API int modelStatus()
returns the model status
COI_API double objectiveValue()
returns the objective value
COI_API int solutionStatus()
return the solution status
COI_API void printStatus()
prints the status of the optimisation
COI_API int solve()
method for starting the solving process of CONOPT.
void cpp_log(ConoptCpp &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95
double Ak
Definition tutoriali.c:24
double L
Definition tutoriali.c:16
double Ainp
Definition tutoriali.c:25
double K
Definition tutoriali.c:27
double Al
Definition tutoriali.c:23
double P
Definition tutoriali.c:16
double hold2
Definition tutoriali.c:28
double hold3
Definition tutoriali.c:28
double Rho
Definition tutoriali.c:26
double hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16