CONOPT
Loading...
Searching...
No Matches
qp5.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
15/* QP Model: Min (x-target)*Q*(x-target)/2 s.t.
16 sum( x ) = 1;
17 where x is a vector of non-negative variables of length nvar
18 target is a vector of +10, and
19 Q is a matrix with +1 on the diagonal and 0.1 on the
20 first upper and lower bi-diagonal */
21
23{
24public:
25 std::vector<double> Q; // Q matrix values (diagonal and off-diagonal)
26 std::vector<int> Qrow; // Row indices of Q matrix
27 std::vector<int> Qcol; // Column indices of Q matrix
28 std::vector<double> Target; // Target vector
29 int nvar;
30 int nqnz;
31
39 QP5_ModelData(int numvar) : nvar(numvar), nqnz(2 * numvar - 1)
40 {
41 Q.resize(nqnz);
42 Qrow.resize(nqnz);
43 Qcol.resize(nqnz);
44 Target.resize(nvar, 10.0); // all targets set to 10.0
45
46 int j = 0;
47 for (int i = 0; i < nvar; ++i)
48 {
49 Q[j] = 1.0;
50 Qrow[j] = i;
51 Qcol[j] = i;
52 j = j + 1;
53
54 if (i < nvar - 1)
55 {
56 Q[j] = 0.1;
57 Qrow[j] = i + 1;
58 Qcol[j] = i;
59 j = j + 1;
60 }
61 }
62 }
63
68 {
69 /* */
70 /* Information about Variables: */
71 /* Default: Lower = -Inf, Curr = 0, and Upper = +inf. */
72 /* Default: the status information in Vsta is not used. */
73 /* */
74 /* Lower bound = 0 on all variables: */
75 /* */
76 for (int i = 0; i < nvar; ++i)
77 addVariable(0.0, Conopt::Infinity); // lower = 0, upper = +∞
78
79 /* Build common index/coeff lists */
80 std::vector<int> varIdx(nvar);
81 std::vector<double> ones(nvar, 1.0); // coefficients
82 std::vector<int> nlflags_free(nvar, 1); // nonlinear for objective
83 std::vector<int> nlflags_sum(nvar, 0); // linear for sum-to-one constraint
84
85 for (int i = 0; i < nvar; ++i)
86 varIdx[i] = i;
87
88 /* */
89 /* Constraint 0: Objective */
90 /* Rhs = 0 and type Non binding */
91 /* */
92 addConstraint(ConoptConstraintType::Free, 0.0, varIdx, ones, nlflags_free);
93
94 /* */
95 /* Constraint 1: Objective */
96 /* Rhs = 1 and type Equality */
97 /* */
98 addConstraint(ConoptConstraintType::Eq, 1.0, varIdx, ones, nlflags_sum);
99
100 /* setting the objective constraint */
102
103 /* setting the optimisation direction */
105 }
106
111 int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode,
112 int ignerr, int *errcnt, int numvar, int numjac, int thread) override
113 {
114 if (rowno == 0)
115 {
116 // Mode 1 or 3: Evaluate function value
117 if (mode == 1 || mode == 3)
118 {
119 double sum = 0.0;
120 for (int k = 0; k < nqnz; ++k)
121 {
122 int i = Qrow[k], j = Qcol[k];
123 double qi = Q[k];
124 if (i == j)
125 sum += (x[i] - Target[i]) * qi * (x[i] - Target[i]);
126 else
127 sum += 2.0 * (x[i] - Target[i]) * qi * (x[j] - Target[j]);
128 }
129 *g = sum / 2.0;
130 }
131
132 // Mode 2 or 3: Evaluate gradient
133 if (mode == 2 || mode == 3)
134 {
135 for (int i = 0; i < numvar; ++i)
136 jac[i] = 0.0;
137
138 for (int k = 0; k < nqnz; ++k)
139 {
140 int i = Qrow[k], j = Qcol[k];
141 double qi = Q[k];
142 if (i == j)
143 jac[i] += qi * (x[i] - Target[i]);
144 else
145 {
146 jac[i] += qi * (x[j] - Target[j]);
147 jac[j] += qi * (x[i] - Target[i]);
148 }
149 }
150 }
151 }
152
153 return 0;
154 }
155};
156
157#include "std.cpp"
158
162int main(int argc, char **argv)
163{
164 int COI_Error = 0;
165
166 // the number of variables in the problem
167 int numvar = 1000;
168
169 // getting the program name from the executable path
170 std::string pname = getProgramName(argv[0]);
171
172 // initialising the Conopt Object
174 QP5_ModelData modeldata(numvar);
175 Tut_MessageHandler msghandler(pname);
176
177 // adding the message handler to the conopt interface
178 conopt.setMessageHandler(msghandler);
179
180 // building the model
181 modeldata.buildModel();
182
183 // loading the model in the conopt object
184 conopt.loadModel(modeldata);
185
186#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
187 std::string license = CONOPT_LICENSE_TEXT;
188 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
189#endif
190
191 if (COI_Error)
192 {
193 conopt.sendMessage(
194 "Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
195 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error);
196 }
197
198 COI_Error = conopt.solve(); /* Optimize */
199
200 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
201 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
202 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
203 else if (fabs(conopt.objectiveValue() - 59978.0) > 0.001)
204 cpp_log(conopt, "Incorrect objective returned", -1);
205
206 conopt.printStatus();
207
208 cpp_log(conopt, "Successful Solve", COI_Error);
209}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
std::vector< int > Qrow
Definition qp5.cpp:26
std::vector< double > Target
Definition qp5.cpp:28
std::vector< int > Qcol
Definition qp5.cpp:27
std::vector< double > Q
Definition qp5.cpp:25
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 qp5.cpp:162
QP5_ModelData(int numvar)
the constructor for the QP instance model data.
Definition qp5.cpp:39
void buildModel()
adds the variables and constraints for the problem
Definition qp5.cpp:67
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 qp5.cpp:111
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