CONOPT
Loading...
Searching...
No Matches
square3.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 Sq2_ModelData : public ConoptModelData
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 /* */
55 /* Constraint 0 */
56 /* Rhs = 10 and type Eguality */
57 /* */
58 addConstraint(ConoptConstraintType::Eq, 10.0, {0, 1}, {1, 1}, {0, 0});
59
60 /* */
61 /* Constraint 1 */
62 /* Rhs = 0 and type Equality */
63 /* */
64 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1}, {0, 0}, {1, 1});
65
66 /* */
67 /* Constraint 2 */
68 /* Rhs = 9 and type Less Than Inequality */
69 /* */
70 addConstraint(ConoptConstraintType::LtEq, 9.0, {0, 1}, {0, 0}, {1, 1});
71 }
72
77 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
78 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
79 {
80 /* */
81 /* Row 0: linear */
82 /* */
83 if (rowno == 0)
84 {
85 return 1; /* This should not happen */
86 }
87 /* */
88 /* Row 1: The function appers as nonlinear */
89 /* */
90 else if (rowno == 1)
91 {
92 /* */
93 /* Mode = 1 or 3: Function value */
94 /* */
95 if (mode == 1 || mode == 3)
96 *g = x[0] - x[1];
97 /* */
98 /* Mode = 2 or 3: Derivatives */
99 /* */
100 if (mode == 2 || mode == 3)
101 {
102 jac[0] = 1.;
103 jac[1] = -1.;
104 }
105 }
106 /* */
107 /* Row 2: The function appers as nonlinear */
108 /* */
109 else if (rowno == 2)
110 {
111 /* */
112 /* Mode = 1 or 3: Function value */
113 /* */
114 if (mode == 1 || mode == 3)
115 *g = x[0] + x[1];
116 /* */
117 /* Mode = 2 or 3: Derivatives */
118 /* */
119 if (mode == 2 || mode == 3)
120 {
121 jac[0] = 1.;
122 jac[1] = 1.;
123 }
124 }
125 return 0;
126 }
127};
128
129#include "std.cpp"
130
135int main(int argc, char **argv)
136{
137 int COI_Error = 0;
138
139 // getting the program name from the executable path
140 std::string pname = getProgramName(argv[0]);
141
142 // initialising the Conopt Object
144 Sq2_ModelData modeldata;
145 Tut_MessageHandler msghandler(pname);
146
147 // adding the message handler to the conopt interface
148 conopt.setMessageHandler(msghandler);
149
150 // building the model
151 modeldata.buildModel();
152
153 // loading the model in the conopt object
154 conopt.loadModel(modeldata);
155
156 // tell conopt this is a square system (critical!)
157 conopt.squareModel(1);
158
159#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
160 std::string license = CONOPT_LICENSE_TEXT;
161 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
162#endif
163
164 if (COI_Error)
165 cpp_log(
166 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
167
168 COI_Error = conopt.solve(); /* Optimize */
169
170 if (COI_Error)
171 {
172 cpp_log(conopt, "Errors encountered during solution", COI_Error);
173 }
174 // checking the statuses and objective value
175 else if (conopt.solutionStatus() != 1 || conopt.modelStatus() < 4 || conopt.modelStatus() > 5)
176 {
177 cpp_log(conopt, "Solver or Model status not as expected (1,4) or (1,5)", -1);
178 }
179
180 // printing the final status of the optimisation
181 conopt.printStatus();
182
183 cpp_log(conopt, "Successful Solve", COI_Error);
184}
The Model Data class.
Definition conopt.hpp:604
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
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 square3.cpp:77
void buildModel()
adds the variables and constraints for the problem
Definition square3.cpp:23
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.
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
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition tutorial.c:218