36 #ifndef SHARK_ALGORITHMS_QP_QPSOLVER_H 37 #define SHARK_ALGORITHMS_QP_QPSOLVER_H 51 template<
class MatrixT>
59 : quadratic(quadratic)
60 ,
linear(quadratic.size(),0)
61 ,
alpha(quadratic.size(),0)
64 ,
boxMin(quadratic.size(),0)
65 ,
boxMax(quadratic.size(),0)
78 RealVector
const& regularizers
79 ): quadratic(quadratic)
81 ,
alpha(quadratic.size(),0)
94 double Cn = regularizers[0];
95 double Cp = regularizers[0];
96 if(regularizers.size() == 2)
100 unsigned int label = labels.
element(i);
101 double weight = weights.
element(i);
103 diagonal(i) = quadratic.entry(i, i);
104 linear(i) = label? 1.0:-1.0;
105 boxMin(i) = label? 0.0:-Cn*weight;
106 boxMax(i) = label? Cp*weight : 0.0;
162 template<
class MatrixT>
170 : quadratic(quadratic)
172 ,
alpha(quadratic.size(),1.0/quadratic.size())
181 for(std::size_t i = 0; i!=
dimensions(); ++i){
183 diagonal(i) = quadratic.entry(i, i);
217 quadratic.flipColumnsAndRows(i, j);
245 template<
class MatrixT>
253 : quadratic(quadratic)
254 ,
linear(quadratic.size())
255 ,
alpha(quadratic.size(),0)
258 , positive(quadratic.size())
266 for(std::size_t i = 0; i!=
dimensions(); ++i){
268 diagonal(i) = quadratic.entry(i, i);
270 positive[i] =
linear(i) > 0;
275 : quadratic(quadratic)
276 ,
linear(quadratic.size())
277 ,
alpha(quadratic.size(),0)
280 , positive(quadratic.size())
288 m_Cp = m_Cn = regularizers[0];
289 if(regularizers.size() == 2)
290 m_Cp = regularizers[1];
293 for(std::size_t i = 0; i!=
dimensions(); ++i){
295 diagonal(i) = quadratic.entry(i, i);
297 positive[i] =
linear(i) > 0;
303 : quadratic(quadratic)
305 ,
alpha(quadratic.size(),0)
308 , positive(quadratic.size())
316 for(std::size_t i = 0; i!=
dimensions(); ++i){
318 diagonal(i) = quadratic.entry(i, i);
319 positive[i] = labels.
element(i) ? 1: 0;
328 return positive[i] ? 0.0 : -m_Cn;
331 return positive[i] ? m_Cp : 0.0;
356 quadratic.flipColumnsAndRows(i, j);
360 std::swap( permutation[i], permutation[j]);
366 bool sameFactor = factor == variableScalingFactor;
367 double newCp = m_Cp*factor;
368 double newCn = m_Cn*factor;
369 for(std::size_t i = 0; i !=
dimensions(); ++i){
370 if(sameFactor &&
alpha(i)== m_Cp)
372 else if(sameFactor &&
alpha(i) == -m_Cn)
375 alpha(i) *= variableScalingFactor;
383 std::vector<char> positive;
402 template<
class Problem,
class SelectionStrategy =
typename Problem::PreferedSelectionStrategy >
408 ):m_problem(problem){}
420 unsigned long long iter = 0;
421 unsigned long long shrinkCounter = 0;
423 SelectionStrategy workingSet;
433 if (stop.
maxSeconds < 1e100 && (iter+1) % 1000 == 0 ){
435 if (current_time - start_time > stop.
maxSeconds){
436 if (prop != NULL) prop->type =
QpTimeout;
442 std::size_t i = 0, j = 0;
443 double acc = workingSet(m_problem,i, j);
445 m_problem.unshrink();
451 workingSet(m_problem,i,j);
456 m_problem.updateSMO(i,j);
460 if(shrinkCounter == 0 && m_problem.shrink(stop.
minAccuracy)){
461 shrinkCounter = std::max<std::size_t>(1000,m_problem.dimensions());
472 std::size_t i = 0, j = 0;
473 prop->accuracy = workingSet(m_problem,i, j);
474 prop->value = m_problem.functionValue();
475 prop->iterations = iter;
476 prop->seconds = finish_time - start_time;