CONOPT
Loading...
Searching...
No Matches
qp2.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 QP2_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;
53
54 if (i < nvar - 1)
55 {
56 Q[j] = 0.1;
57 Qrow[j] = i + 1;
58 Qcol[j] = i;
59 ++j;
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
160 int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[],
161 int *nodrv, int numvar, int numjac, int thread) override
162 {
163 int i, j, k;
164 /* */
165 /* Is only called for rowno = 0, the nonlinear objective */
166 /* Return H*Dx = Q*Dx in d2g */
167 /* */
168 if (rowno == 0)
169 {
170 for (i = 0; i < numvar; i++)
171 d2g[i] = 0.0;
172 for (k = 0; k < nqnz; k++)
173 {
174 i = Qrow[k];
175 j = Qcol[k];
176 if (i == j)
177 d2g[i] += Q[k] * dx[i];
178 else
179 {
180 d2g[i] += Q[k] * dx[j];
181 d2g[j] += Q[k] * dx[i];
182 }
183 }
184 }
185 return 0;
186 }
187};
188
189#include "std.cpp"
190
194int main(int argc, char **argv)
195{
196 int COI_Error = 0;
197
198 // the number of variables in the problem
199 int numvar = 1000;
200
201 // getting the program name from the executable path
202 std::string pname = getProgramName(argv[0]);
203
204 // initialising the Conopt Object
206 QP2_ModelData modeldata(numvar);
207 Tut_MessageHandler msghandler(pname);
208
209 // adding the message handler to the conopt interface
210 conopt.setMessageHandler(msghandler);
211
212 // building the model
213 modeldata.buildModel();
214
215 // loading the model in the conopt object
216 conopt.loadModel(modeldata);
217
218#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
219 std::string license = CONOPT_LICENSE_TEXT;
220 COI_Error += conopt.setLicense(CONOPT_LICENSE_INT_1, CONOPT_LICENSE_INT_2, CONOPT_LICENSE_INT_3, license);
221#endif
222
223 if (COI_Error)
224 {
225 conopt.sendMessage(
226 "Skipping COI_Solve due to setup errors. COI_Error = " + std::to_string(COI_Error));
227 cpp_log(conopt, "Skipping Solve due to setup errors", COI_Error);
228 }
229
230 COI_Error = conopt.solve(); /* Optimize */
231
232 conopt.sendMessage("After solving. COI_Error = " + std::to_string(COI_Error));
233 if (conopt.modelStatus() != 2 || conopt.solutionStatus() != 1)
234 cpp_log(conopt, "Incorrect Model or Solver Status", -1);
235 else if (fabs(conopt.objectiveValue() - 59978.0) > 0.001)
236 cpp_log(conopt, "Incorrect objective returned", -1);
237
238 conopt.printStatus();
239
240 cpp_log(conopt, "Successful Solve", COI_Error);
241}
The Conopt class.
Definition conopt.hpp:27
static constexpr double Infinity
Definition conopt.hpp:30
std::vector< double > Target
Definition qp2.cpp:28
std::vector< int > Qcol
Definition qp2.cpp:27
std::vector< double > Q
Definition qp2.cpp:25
std::vector< int > Qrow
Definition qp2.cpp:26
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 qp2.cpp:194
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 qp2.cpp:111
QP2_ModelData(int numvar)
the constructor for the QP instance model data.
Definition qp2.cpp:39
int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[], int *nodrv, int numvar, int numjac, int thread) override
computes the directional second derivative for a single constraint
Definition qp2.cpp:160
void buildModel()
adds the variables and constraints for the problem
Definition qp2.cpp:67
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