CONOPT
Loading...
Searching...
No Matches
square2.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
16{
17public:
19
24 {
25 /* Information about Variables: */
26 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
27 /* Default: the status information in Vsta is not used. */
28 /* */
29 /* Model x0 + x1 = 10 */
30 /* x0 - x1 = 0 */
31 /* x0 + x1 <= 9 */
32 /* */
35
36 /* Information about Constraints: */
37 /* Default: Rhs = 0 */
38 /* Default: the status information in Esta and the function */
39 /* value in FV are not used. */
40 /* Default: Type: There is no default. */
41 /* 0 = Equality, */
42 /* 1 = Greater than or equal, */
43 /* 2 = Less than or equal, */
44 /* 3 = Non binding. */
45 /* */
46 /* Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear */
47 /* Although the model is linear we label constraint 1 and 2 as nonlinear */
48 /* Value (Linear only) */
49 /* X[0] X[1] */
50 /* 0: 1 1 */
51 /* 1: NL NL */
52 /* 2: NL NL */
53 /* */
54 /* Constraint 0 */
55 /* Rhs = 10 and type Eguality */
56 /* */
57 addConstraint(ConoptConstraintType::Eq, 10.0, {0, 1}, {1, 1}, {0, 0});
58
59 /* */
60 /* Constraint 1 */
61 /* Rhs = 0 and type Equality */
62 /* */
63 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1}, {0, 0}, {1, 1});
64
65 /* */
66 /* Constraint 2 */
67 /* Rhs = 9 and type Less Than Inequality */
68 /* */
69 addConstraint(ConoptConstraintType::LtEq, 9.0, {0, 1}, {0, 0}, {1, 1});
70 }
71
76 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
77 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
78 {
79 /* */
80 /* Row 0: linear */
81 /* */
82 if (rowno == 0)
83 {
84 return 1; /* This should not happen */
85 }
86 /* */
87 /* Row 1: The function appers as nonlinear */
88 /* */
89 else if (rowno == 1)
90 {
91 /* */
92 /* Mode = 1 or 3: Function value */
93 /* */
94 if (mode == 1 || mode == 3)
95 *g = x[0] - x[1];
96 /* */
97 /* Mode = 2 or 3: Derivatives */
98 /* */
99 if (mode == 2 || mode == 3)
100 {
101 jac[0] = 1.;
102 jac[1] = -1.;
103 }
104 }
105 /* */
106 /* Row 2: The function appers as nonlinear */
107 /* */
108 else if (rowno == 2)
109 {
110 /* */
111 /* Mode = 1 or 3: Function value */
112 /* */
113 if (mode == 1 || mode == 3)
114 *g = x[0] + x[1];
115 /* */
116 /* Mode = 2 or 3: Derivatives */
117 /* */
118 if (mode == 2 || mode == 3)
119 {
120 jac[0] = 1.;
121 jac[1] = 1.;
122 }
123 }
124 return 0;
125 }
126};
127
128#include "std.cpp"
129
134int main(int argc, char **argv)
135{
136 int COI_Error = 0;
137
138 // getting the program name from the executable path
139 std::string pname = getProgramName(argv[0]);
140
141 // initialising the Conopt Object
143 Sq2_ModelData modeldata;
144 Tut_MessageHandler msghandler(pname);
145
146 // adding the message handler to the conopt interface
147 conopt.setMessageHandler(msghandler);
148
149 // building the model
150 modeldata.buildModel();
151
152 // loading the model in the conopt object
153 conopt.loadModel(modeldata);
154
155 // tell conopt this is a square system (critical!)
156 conopt.squareModel(1);
157
158#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
159 std::string license = CONOPT_LICENSE_TEXT;
160 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
161#endif
162
163 if (COI_Error)
164 cpp_log(
165 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
166
167 COI_Error = conopt.solve(); /* Optimize */
168
169 if (COI_Error)
170 {
171 cpp_log(conopt, "Errors encountered during solution", COI_Error);
172 }
173 // checking the statuses and objective value
174 else if (conopt.solutionStatus() != 1 || conopt.modelStatus() < 4 || conopt.modelStatus() > 5)
175 {
176 cpp_log(conopt, "Solver or Model status not as expected (1,4) or (1,5)", -1);
177 }
178
179 // printing the final status of the optimisation
180 conopt.printStatus();
181
182 cpp_log(conopt, "Successful Solve", COI_Error);
183}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
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 square2.cpp:134
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 square2.cpp:76
void buildModel()
adds the variables and constraints for the problem
Definition square2.cpp:23
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.
int addConstraint(ConoptConstraintType constype, double rhs, int slackstatus=-1)
adds a constraint to the problem. The non-zero coefficients are added later
void cpp_log(Conopt &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95