-
Notifications
You must be signed in to change notification settings - Fork 7
/
driver.cpp
168 lines (148 loc) · 4.71 KB
/
driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <Eigen/Eigenvalues>
#include <boost/timer/timer.hpp>
#include <iostream>
#include "eigen-qp.hpp"
using namespace std;
using namespace Eigen;
using namespace EigenQP;
/*
* min 0.5 x.Q.x + c.x
*
* Ax <= b
* Ex = d
*
* See: http://etd.dtu.dk/thesis/220437/ep08_19.pdf
*/
// #define NV_FIXED 8
// #define NC_FIXED 16
// #define NE_FIXED 3
#define PROB_VARS 8
#define PROB_INEQ 16
#define PROB_EQ 3
#define N_TEST (1<<16)
template<typename Scalar, int NV_FIXED, int NC_FIXED, int NE_FIXED>
void test()
{
typedef Eigen::Matrix<Scalar,-1,-1> MatrixXs;
typedef Eigen::Matrix<Scalar,-1,1> VectorXs;
// Make a random problem
int num_vars = NV_FIXED;
int num_ineq = NC_FIXED;
int num_eq = NE_FIXED;
// Random matrices
MatrixXs Q = MatrixXs::Random(num_vars,num_vars);
Q *= Q.adjoint()/sqrt(num_vars); // Make it pos def
VectorXs c = VectorXs::Random(num_vars);
MatrixXs A = MatrixXs::Random(num_ineq,num_vars);
VectorXs b = VectorXs::Random(num_ineq);
MatrixXs E = MatrixXs::Random(num_eq,num_vars);
VectorXs f = VectorXs::Random(num_eq);
VectorXs x_unc;
// Solve unconstrainted system
cout << "Unconstrained..." << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
x_unc = -Q.ldlt().solve(c);
}
VectorXs x(num_vars);
// Generate inequality constraints
b.array() = (A*x_unc).array() - 0.15;
// Inequality constrained problem
cout << "quadprog, dynamic code" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
quadprog(Q,c,A,b,x);
}
cout << " error: " << (x - x_unc).norm() << endl;
{
QPIneqSolver<Scalar,-1,-1> *solver;
cout <<"QPIneqSolver, dynamic, obj creation" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
{
solver = new QPIneqSolver<Scalar,-1,-1>(num_vars,num_ineq);
solver->solve(Q,c,A,b,x);
}
}
cout << " error: " << (x - x_unc).norm() << endl;
cout << "QPIneqSolver, dynamic, object reuse" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
solver->solve(Q,c,A,b,x);
}
cout << " error: " << (x - x_unc).norm() << endl;
}
// Fixed size
Matrix<Scalar, NV_FIXED,NV_FIXED> Q_fixed(Q);
Matrix<Scalar, NV_FIXED, 1> c_fixed(c);
Matrix<Scalar, NC_FIXED,NV_FIXED> A_fixed(A);
Matrix<Scalar, NC_FIXED,1> b_fixed(b);
// Q_fixed.setRandom();
// c_fixed.setRandom();
// A_fixed.setRandom();
x_unc = -Q_fixed.ldlt().solve(c_fixed);
b_fixed.array() = (A_fixed*x_unc).array() - 0.12;
Matrix<Scalar, NV_FIXED, 1> x_fixed;
cout << "quadprog, fixed code" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
quadprog(Q_fixed,c_fixed,A_fixed,b_fixed,x_fixed);
}
cout << " error: " << (x_fixed - x_unc).norm() << endl;
{
QPIneqSolver<Scalar,NV_FIXED,NC_FIXED> *solver;
cout << "QPIneqSolver, fixed" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
{
solver = new QPIneqSolver<Scalar,NV_FIXED,NC_FIXED>(num_vars,num_ineq);
solver->solve(Q_fixed,c_fixed,A_fixed,b_fixed,x_fixed);
}
}
cout << " error: " << (x_fixed - x_unc).norm() << endl;
cout << "QPIneqSolver, fixed, object reuse" << endl;
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
solver->solve(Q_fixed,c_fixed,A_fixed,b_fixed,x_fixed);
}
cout << " error: " << (x_fixed - x_unc).norm() << endl;
}
cout << "QPEqSolver, equality constraints, dynamic" << endl;
{
QPEqSolver<Scalar> solver(num_vars,num_eq);
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
solver.solve(Q,c,E,f,x);
}
}
/*
// known to be broken
cout << "quadprog, ineq/eq constraints, dynamic" << endl;
{
//b = A*x - 0.12;
QPGenSolver<Scalar> solver(num_vars,num_ineq,num_eq);
{
boost::timer::auto_cpu_timer t;
for (int ii=0; ii < N_TEST; ii++)
solver.solve(Q,c,A,b,E,f,x);
}
}
*/
}
int main(int argc, char ** argv)
{
srand((unsigned int) time(0));
cout << "TESTING DOUBLE" << endl;
test<double,PROB_VARS,PROB_INEQ,PROB_EQ>();
cout << "TESTING FLOAT" << endl;
test<float,PROB_VARS,PROB_INEQ,PROB_EQ>();
return 0;
}