CONOPT
Loading...
Searching...
No Matches
largebnd.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 "conopt.hpp"
14
15class Tut_ModelData : public ConoptModelData
16{
17public:
19
24 {
25 /* */
26 /* Information about Variables: */
27 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
28 /* Default: the status information in Vsta is not used. */
29 /* */
30 /* Lower bound on L = X[0] = 0.1 and initial value = 0.5: */
31 /* */
32 /* ****SPECIAL***** Try to assign the upper bounds a value above */
33 /* Rtmax. */
34 /* */
35
36 addVariable(0.1, 1.0e20, 0.5);
37 /* */
38 /* Lower bound on INP = X[1] = 0.1 and initial value = 0.5: */
39 /* */
40 addVariable(0.1, 1.0e21, 0.5);
41 /* */
42 /* Lower bound on OUT = X[2] and P = X[3] are both 0 and the */
43 /* default initial value of 0 is used: */
44 /* */
45 addVariable(0., 1.0e22);
46 addVariable(0., 1.0e23);
47 /* */
48 /* Information about Constraints: */
49 /* Default: Rhs = 0 */
50 /* Default: the status information in Esta and the function */
51 /* value in FV are not used. */
52 /* Default: Type: There is no default. */
53 /* 0 = Equality, */
54 /* 1 = Greater than or equal, */
55 /* 2 = Less than or equal, */
56 /* 3 = Non binding. */
57 /* */
58 /* Constraint 0 (Objective) */
59 /* Rhs = -0.1 and type Non binding */
60 /* */
61 addConstraint(ConoptConstraintType::Free, -0.1, {0, 1, 2, 3}, {-1, -1, 0, 0}, {0, 0, 1, 1});
62 /* */
63 /* Constraint 1 (Production Function) */
64 /* Rhs = 0 and type Equality */
65 /* */
66 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1, 2}, {0, 0, -1}, {1, 1, 0});
67 /* */
68 /* Constraint 2 (Price equation) */
69 /* Rhs = 4.0 and type Equality */
70 /* */
71 addConstraint(ConoptConstraintType::Eq, 4.0, {2, 3}, {1, 2}, {0, 0});
72
73 /* setting the objective constraint */
75
76 /* setting the optimisation direction */
78 }
79
84 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
85 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
86 {
87 /* */
88 /* Declare local copies of the optimization variables. This is */
89 /* just for convenience to make the expressions easier to read. */
90 /* */
91 double L, Inp, Out, P;
92 /* */
93 /* Declare parameters and their data values. */
94 /* */
95 double Al = 0.16;
96 double Ak = 2.0;
97 double Ainp = 0.16;
98 double Rho = 1.0;
99 double K = 4.0;
100 double hold1, hold2, hold3;
101
102 /* */
103 /* Move the optimization variables from the X vector to a set */
104 /* of local variables with the same names as the variables in */
105 /* the model description. This is not necessary, but it should make*/
106 /* the equations easier to recognize. */
107 /* This time we work with the C numbering convention */
108 /* */
109 L = x[0];
110 Inp = x[1];
111 Out = x[2];
112 P = x[3];
113 /* */
114 /* Row 0: the objective function is nonlinear */
115 /* */
116
117 if (rowno == 0)
118 {
119 /* */
120 /* Mode = 1 or 3. Function value: G = P * Out */
121 /* */
122 if (mode == 1 || mode == 3)
123 *g = P * Out;
124 /* */
125 /* Mode = 2 or 3: Derivative values: */
126 /* */
127 if (mode == 2 || mode == 3)
128 {
129 jac[2] = P; /* derivative w.r.t. Out = x[2] */
130 jac[3] = Out; /* derivative w.r.t. P = x[3] */
131 }
132 }
133 /* */
134 /* Row 1: The production function is nonlinear */
135 /* */
136 else if (rowno == 1)
137 {
138 /* */
139 /* Compute some common terms */
140 /* */
141 hold1 = (Al * pow(L, (-Rho)) + Ak * pow(K, (-Rho)) + Ainp * pow(Inp, (-Rho)));
142 hold2 = pow(hold1, (-1. / Rho));
143 /* */
144 /* Mode = 1 or 3: Function value */
145 /* */
146 if (mode == 1 || mode == 3)
147 *g = hold2;
148 /* */
149 /* Mode = 2 or 3: Derivatives */
150 /* */
151 if (mode == 2 || mode == 3)
152 {
153 hold3 = hold2 / hold1;
154 jac[0] = hold3 * Al * pow(L, (-Rho - 1.)); /* derivative w.r.t. L = x[0] */
155 jac[1] = hold3 * Ainp * pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = x[1] */
156 }
157 }
158 /* */
159 /* Row = 2: The row is linear and will not be called. */
160 /* */
161
162 return 0;
163 }
164};
165
166#include "std.cpp"
167
172int main(int argc, char **argv)
173{
174 int COI_Error = 0;
175
176 // getting the program name from the executable path
177 std::string pname = getProgramName(argv[0]);
178
179 // initialising the Conopt Object
181 Tut_ModelData modeldata;
182 Tut_MessageHandler msghandler(pname);
183
184 // adding the message handler to the conopt interface
185 conopt.setMessageHandler(msghandler);
186
187 // building the model
188 modeldata.buildModel();
189
190 // loading the model in the conopt object
191 conopt.loadModel(modeldata);
192
193#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
194 std::string license = CONOPT_LICENSE_TEXT;
195 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
196#endif
197
198 /*
199 The large bound should now solve and behave like the tutorial example.
200 */
201
202 if (COI_Error)
203 cpp_log(
204 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
205
206 COI_Error = conopt.solve(); /* Optimize */
207
208 // checking the statuses and objective value
209 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
210 {
211 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
212 }
213 else if (fabs(conopt.objectiveValue() - 0.572943) > 0.000001)
214 {
215 cpp_log(conopt, "Incorrect objective returned", -1);
216 }
217
218 // printing the final status of the optimisation
219 conopt.printStatus();
220
221 cpp_log(conopt, "Successful Solve", COI_Error);
222}
The Model Data class.
Definition conopt.hpp:604
The Conopt class.
Definition conopt.hpp: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.
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition largebnd.cpp:172
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) override
defines the nonlinearities of the model by returning numerical values.
Definition largebnd.cpp:84
void buildModel()
adds the variables and constraints for the problem
Definition largebnd.cpp:23
void buildModel()
adds the variables and constraints for the problem
Definition tutorial.cpp:42
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.
void cpp_log(Conopt &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95
double L
Definition tutoriali.c:16
double P
Definition tutoriali.c:16
double hold2
Definition tutoriali.c:28
double hold3
Definition tutoriali.c:28
double hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16