CONOPT
Loading...
Searching...
No Matches
square.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 /* */
34
35 /* Information about Constraints: */
36 /* Default: Rhs = 0 */
37 /* Default: the status information in Esta and the function */
38 /* value in FV are not used. */
39 /* Default: Type: There is no default. */
40 /* 0 = Equality, */
41 /* 1 = Greater than or equal, */
42 /* 2 = Less than or equal, */
43 /* 3 = Non binding. */
44 /* */
45 /* Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear */
46 /* Although the model is linear we label constraint 1 as nonlinear */
47 /* Value (Linear only) */
48 /* X[0] X[1] */
49 /* 0: 1 1 */
50 /* 1: NL NL */
51 /* */
52 /* */
53 /* Constraint 0 */
54 /* Rhs = 10 and type Eguality */
55 /* */
56 addConstraint(ConoptConstraintType::Eq, 10.0, {0, 1}, {1, 1}, {0, 0});
57 /* */
58 /* Constraint 1 */
59 /* Rhs = 0 and type Equality */
60 /* */
61
62 addConstraint(ConoptConstraintType::Eq, 0.0, {0, 1}, {0, 0}, {1, 1});
63 }
64
69 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
70 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
71 {
72 /* */
73 /* Row 0: linear */
74 /* */
75 if (rowno == 0)
76 {
77 return 1; /* This should not happen */
78 }
79 /* */
80 /* Row 1: The production function is nonlinear */
81 /* */
82 else if (rowno == 1)
83 {
84 /* */
85 /* Mode = 1 or 3: Function value */
86 /* */
87 if (mode == 1 || mode == 3)
88 *g = x[0] - x[1];
89 /* */
90 /* Mode = 2 or 3: Derivatives */
91 /* */
92 if (mode == 2 || mode == 3)
93 {
94 jac[0] = 1.;
95 jac[1] = -1.;
96 }
97 }
98 return 0;
99 }
100};
101
102#include "std.cpp"
103
108int main(int argc, char **argv)
109{
110 int COI_Error = 0;
111
112 // getting the program name from the executable path
113 std::string pname = getProgramName(argv[0]);
114
115 // initialising the Conopt Object
117 Sq_ModelData modeldata;
118 Tut_MessageHandler msghandler(pname);
119
120 // adding the message handler to the conopt interface
121 conopt.setMessageHandler(msghandler);
122
123 // building the model
124 modeldata.buildModel();
125
126 // loading the model in the conopt object
127 conopt.loadModel(modeldata);
128
129 // tell conopt this is a square system
130 conopt.squareModel(1);
131
132#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
133 std::string license = CONOPT_LICENSE_TEXT;
134 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
135#endif
136
137 if (COI_Error)
138 cpp_log(
139 conopt, "Skipping COI_Solve due to license error. COI_Error = " + std::to_string(COI_Error), COI_Error);
140
141 COI_Error = conopt.solve(); /* Optimize */
142
143 if (COI_Error)
144 {
145 cpp_log(conopt, "Errors encountered during solution", COI_Error);
146 }
147 // checking the statuses and objective value
148 else if (conopt.modelStatus() != 16 || conopt.solutionStatus() != 1)
149 {
150 cpp_log(conopt, "Solver or Model status not as expected (1,16)", -1);
151 }
152
153 // printing the final status of the optimisation
154 conopt.printStatus();
155
156 cpp_log(conopt, "Successful Solve", COI_Error);
157}
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.
void buildModel()
adds the variables and constraints for the problem
Definition square.cpp:23
int main(int argc, char **argv)
Main program. A simple setup and call of CONOPT.
Definition square.cpp:108
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 square.cpp:69
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