-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_case_lcp_pgs.h
214 lines (163 loc) · 6.23 KB
/
test_case_lcp_pgs.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef __TEST_CASE_LCP_PGS_H__
#define __TEST_CASE_LCP_PGS_H__
#include <iostream>
#include <vector>
#include <limits>
#include <string>
#include <Accelerate/Accelerate.h>
#include "test_case_lcp.h"
template<class T, bool IS_COL_MAJOR>
class TestCaseLCP_pgs : public TestCaseLCP<T, IS_COL_MAJOR> {
// Type of problem that can be solved:
//
// M z + q = w
//
// s.t. 0 <= z cmpl. w >= 0
//
// M must be symmetric ( For the precise list of the types of matrices accepted.
//
// The iteration formula
// z^{r+1} = - (q + L z^{r+1} + U z^r) / D
const int m_max_num_iterations;
std::vector<double> m_error_history;
T* m_z_lo;
T* m_z_hi;
T* m_z_abs;
T* m_w_abs;
T* m_Mz;
public:
TestCaseLCP_pgs( const int dim, const T condition_num, const int max_num_iterations, const T epsilon, const LCPTestPatternType p_type )
:TestCaseLCP<T, IS_COL_MAJOR>( dim, condition_num, epsilon, p_type )
,m_max_num_iterations ( max_num_iterations )
,m_z_lo ( nullptr )
,m_z_hi ( nullptr )
{
static_assert( is_same< float,T >::value || is_same< double,T >::value );
this->setImplementationType( PGS_VDSP );
m_z_lo = new T[ this->m_dim ];
m_z_hi = new T[ this->m_dim ];
memset( m_z_lo, 0, sizeof(T)*this->m_dim );
for ( int i = 0; i < this->m_dim; i++ ){
m_z_hi[i] = std::numeric_limits<T>::max();
}
m_z_abs = new T[ this->m_dim ];
m_w_abs = new T[ this->m_dim ];
m_Mz = new T[ this->m_dim ];
}
virtual ~TestCaseLCP_pgs(){
delete[] m_z_abs;
delete[] m_w_abs;
delete[] m_z_lo;
delete[] m_z_hi;
delete[] m_Mz;
}
virtual void setBoxConstraints( const T* lo, const T*hi ) {
memcpy( m_z_lo, lo, sizeof(T)*this->m_dim );
memcpy( m_z_hi, hi, sizeof(T)*this->m_dim );
}
virtual T* getActiveZ() {
return this->m_z;
}
virtual void run() {
this->m_error_history.clear();
memset( this->m_z, 0, sizeof(T) * this->m_dim );
int num_iterations;
for ( num_iterations = 0; num_iterations < m_max_num_iterations; num_iterations++ ) {
if constexpr ( is_same<float, T>::value ) {
calcZ_f();
}
else {
calcZ_d();
}
T error;
if constexpr ( is_same<float, T>::value ) {
error = getErrorvDSP_f();
}
else {
error = getErrorvDSP_d();
}
m_error_history.push_back( error );
if (num_iterations >= 5 ) {
double past_5 = 0.0;
for ( int i = num_iterations - 5 ; i < num_iterations ; i++ ) {
past_5 += m_error_history[i];
}
past_5 /= 5.0;
if ( error >= past_5 - this->m_epsilon ) {
// converged.
break;
}
}
}
this->setIterations( 0, num_iterations, 0 );
}
double getErrorvDSP_f() {
for ( int row = 0; row < this->m_dim; row++ ) {
vDSP_dotpr ( &(this->m_M[ row * this->m_dim ]), 1, this->m_z, 1, &(this->m_Mz[row]), this->m_dim );
}
vDSP_vadd( this->m_Mz, 1, this->m_q, 1, this->m_w, 1, this->m_dim );
T violation = 0.0;
for ( int i = 0; i < this->m_dim; i++ ) {
const T z = this->m_z[i];
const T w = this->m_w[i];
const T L = m_z_lo[i];
const T H = m_z_hi[i];
if ( fabs(L - 0.0) < this->m_epsilon ) {
// normal constraint
violation += ( fabs(z*w) - std::min(z, (T)0.0) - std::min(w, (T)0.0) );
}
else if (z > (L + H ) / 2.0 ) {
violation += ( fabs( ( z - H ) * w ) + std::max( z - H, (T)0.0) );
}
else {
violation += ( fabs( ( L - z ) * w ) + std::max( L - z, (T)0.0) );
}
}
//cerr << "violation: " << violation << "\n";
return violation;
}
double getErrorvDSP_d() {
for ( int row = 0; row < this->m_dim; row++ ) {
vDSP_dotprD ( &(this->m_M[ row * this->m_dim ]), 1, this->m_z, 1, &(this->m_Mz[row]), this->m_dim );
}
vDSP_vaddD( this->m_Mz, 1, this->m_q, 1, this->m_w, 1, this->m_dim );
T violation = 0.0;
for ( int i = 0; i < this->m_dim; i++ ) {
const T z = this->m_z[i];
const T w = this->m_w[i];
const T L = m_z_lo[i];
const T H = m_z_hi[i];
if ( fabs(L - 0.0) < this->m_epsilon ) {
// normal constraint
violation += ( fabs(z*w) - std::min(z, (T)0.0) - std::min(w, (T)0.0) );
}
else if (z > (L + H ) / 2.0 ) {
violation += ( fabs( ( z - H ) * w ) + std::max( z - H, (T)0.0) );
}
else {
violation += ( fabs( ( L - z ) * w ) + std::max( L - z, (T)0.0) );
}
}
//cerr << "violation: " << violation << "\n";
return violation;
}
inline void calcZ_f() {
for ( int row = 0; row < this->m_dim; row++ ) {
// calc z^{r+1} = - (q + L z^{r+1} + U z^r) / D
T diag = this->m_M[ row * this->m_dim + row ];
T dot;
vDSP_dotpr ( &(this->m_M[ row * this->m_dim ]), 1, this->m_z, 1, &dot, this->m_dim );
this->m_z[row] = min ( max ( (diag * this->m_z[row] - dot - this->m_q[row]) / diag, m_z_lo[row] ), m_z_hi[row] );
}
}
inline void calcZ_d() {
for ( int row = 0; row < this->m_dim; row++ ) {
// calc z^{r+1} = - (q + L z^{r+1} + U z^r) / D
T diag = this->m_M[ row * this->m_dim + row ];
T dot;
vDSP_dotprD ( &(this->m_M[ row * this->m_dim ]), 1, this->m_z, 1, &dot, this->m_dim );
this->m_z[row] = min ( max ( (diag * this->m_z[row] - dot - this->m_q[row]) / diag, m_z_lo[row] ), m_z_hi[row] );
}
}
};
#endif /*__TEST_CASE_LCP_PGS_H__*/