-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathumain.cc
201 lines (156 loc) · 4.79 KB
/
umain.cc
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
File main.cc
This file contains just an example on how to set-up the matrices for using with
the solve_quadprog() function.
The test problem is the following:
Given:
G = 4 -2 g0^T = [6 0]
-2 4
Solve:
min f(x) = 1/2 x G x + g0 x
s.t.
x_1 + x_2 = 3
x_1 >= 0
x_2 >= 0
v x_1 + x_2 >= 2
The solution is x^T = [1 2] and f(x) = 12
Author: Luca Di Gaspero
DIEGM - University of Udine, Italy
http://www.diegm.uniud.it/digaspero/
LICENSE
This file is part of QuadProg++: a C++ library implementing
the algorithm of Goldfarb and Idnani for the solution of a (convex)
Quadratic Programming problem by means of an active-set dual method.
Copyright (C) 2007-2009 Luca Di Gaspero.
Copyright (C) 2009 Eric Moyer.
QuadProg++ is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QuadProg++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with QuadProg++. If not, see <http://www.gnu.org/licenses/>.
*/
#define DNDEBUG
#include <iostream>
#include <sstream>
#include <string>
#include "uQuadProg++.hh"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <Eigen/Eigen>
#include <EigenQP.h>
#include <boost/progress.hpp>
using boost::timer;
using namespace Eigen;
namespace ublas = boost::numeric::ublas;
using std::endl;
using std::cout;
int main (int argc, char *const argv[]) {
timer clock;
int n, m, p;
n = 1000;
m = 505;
p = 703;
double sum = 0.0;
char ch;
ublas::matrix<double> G, CE, CI;
ublas::vector<double> g0, ce0, ci0, x;
MatrixXd eG(n,n), eCi(n, p), eCe(n, m);
VectorXd eg0(n), ece(m), eci(p), ex(n);
G.resize(n, n);
{
// std::istringstream is("4, -2,"
// "-2, 4 ");
/*
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
is >> G(i, j) >> ch;
eG(i, j) = G(i, j);
*/
for (int i = 0; i < n; ++i)
for(int j = 0; j <= i; ++j){
eG(i, j) = G(i, j) = (i== j)? i+j+1 : pow((-1), (i+j)) * (double)i/(j+1)*(rand()/RAND_MAX);
}
eG = (eG.transpose() * eG).eval();
G = prod(trans(G) , G);
// cout << "G" << G << endl << endl << eG << endl;
}
g0.resize(n);
{
std::istringstream is("6.0, 0.0 ");
for (int i = 0; i < n; i++){
eg0(i)=g0(i) = (double)rand()/RAND_MAX;
}
// cout << "g0" << g0 << endl << endl << eg0 << endl;
}
CE.resize(n, m);
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++){
eCe(i, j) = CE(i, j) = (double) rand()/RAND_MAX;
}
// cout << "CE" << CE << endl << endl << eCe << endl;
}
ce0.resize(m);
{
std::istringstream is("-3.0 ");
for (int j = 0; j < m; j++){
is >> ce0(j) >> ch;
ece(j) = ce0(j);
}
// cout << "ce" << ce0 << endl << endl << ece << endl;
}
CI.resize(n, p);
{
std::istringstream is("1.0, 0.0, 1.0, "
"0.0, 1.0, 1.0 ");
for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++){
eCi(i, j) = CI(i, j) = (double) (rand()) /RAND_MAX;
}
// cout << "CI" << CI << endl << endl << eCi << endl;
}
ci0.resize(p);
{
std::istringstream is("0.0, 0.0, -2.0 ");
for (int j = 0; j < p; j++)
{
eci(j) = ci0(j) = (double) rand() / RAND_MAX;
}
// cout << "ci" << ci0 << endl << endl << eci << endl;
}
x.resize(n);
clock.restart();
std::cout << "f: " << uQuadProgPP::solve_quadprog(G, g0, CE, ce0, CI, ci0, x) << std::endl;
std::cout << "Elapse Time:" << clock.elapsed() << std::endl;
std::cout << "x: " << x << std::endl;
/* for (int i = 0; i < n; i++)
std::cout << x[i] << ' ';
std::cout << std::endl; */
/* FOR DOUBLE CHECKING COST since in the solve_quadprog routine the matrix G is modified */
{
std::istringstream is("4, -2,"
"-2, 4 ");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
is >> G(i, j) >> ch;
}
std::cout << "Double checking cost: ";
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += x(i) * G(i, j) * x(j);
sum *= 0.5;
for (int i = 0; i < n; i++)
sum += g0(i) * x(i);
std::cout << sum << std::endl;
clock.restart();
std::cout << QP::solve_quadprog(eG, eg0, eCe, ece, eCi, eci, ex) << std::endl;
std::cout << "Elapsed Time:" << clock.elapsed() << std::endl;
std::cout <<"Solution:" << endl << ex << std::endl;
}