37 #ifndef SHARK_ALGORITHMS_QP_QPMCBOXDECOMP_H 38 #define SHARK_ALGORITHMS_QP_QPMCBOXDECOMP_H 42 #include <shark/Algorithms/QP/Impl/AnalyticProblems.h> 49 template <
class Matrix>
59 template<
class Problem>
60 double operator()(Problem& problem, std::size_t& i, std::size_t& j){
62 return problem.selectWorkingSet(i,j);
78 RealMatrix
const& linearMat,
99 SHARK_RUNTIME_CHECK(kernel.size() == linearMat.size1(),
"Size of kernel matrix and linear factor to not agree.");
106 unsigned int y = target.
element(i);
113 for (
unsigned int p=0; p<
m_cardP; p++, v++)
118 double Q =
m_M(
m_classes * (y * m_cardP + p) + y, p) * k;
141 return solutionMatrix;
144 double alpha(std::size_t i, std::size_t p)
const{
154 return solutionGradientMatrix;
180 double maxViolation = 0.0;
187 maxViolation = std::max(maxViolation,g);
191 maxViolation = std::max(maxViolation,-g);
223 std::size_t r =
m_cardP * y + p;
247 std::size_t rv =
m_cardP*yv+pv;
248 std::size_t rw =
m_cardP*yw+pw;
278 double largest = 0.0;
290 if (largest < 10.0 * epsilon)
305 if ((v == 0.0 && g <= 0.0) || (v ==
m_C && g >= 0.0))
322 for (
int a = (
int)
m_activeEx - 1; a >= 0; a--)
341 if (mu == 0.0)
continue;
346 std::size_t r =
m_cardP * yv + pv;
356 for (std::size_t b=0; b<row.
size; b++)
364 double upd = mu * def * k;
367 std::size_t f = ex.
avar[b];
393 double maxViolation = 0.0;
400 if (ga >maxViolation && aa <
m_C)
405 else if (-ga > maxViolation && aa > 0.0)
411 if (maxViolation == 0.0)
return maxViolation;
415 std::size_t ii = vari.
i;
417 unsigned int pi = vari.
p;
424 double bestgain = gi * gi / di;
429 unsigned int ya = exa.
y;
433 for (std::size_t pf=0, b=0; pf <
m_cardP; pf++)
435 std::size_t f = exa.
var[pf];
436 double qif = def * k[a];
442 if(f >= m_activeVar || f == i)
450 if (!(af > 0.0 && gf < 0.0) && !(af < m_C && gf > 0.0))
453 double gain = detail::maximumGainQuadratic2D(di,df,qif,di,gi,gf);
454 if( gain > bestgain){
474 for (std::size_t b=0; b<row.
size; b++){
479 double upd = mu* def * k;
480 for (std::size_t b=0; b<ex.
active; b++)
497 std::size_t ih = exv->
active - 1;
498 std::size_t
h = exv->
avar[ih];
539 for (std::size_t v = 0; v <
m_cardP; v++)
639 template<
class Matrix>
652 std::size_t classes = bias.size();
653 std::size_t numExamples = m_problem->getNumExamples();
654 std::size_t
cardP = m_problem->cardP();
655 RealVector stepsize(classes, 0.01);
656 RealVector prev(classes,0);
657 RealVector step(classes);
660 unsigned long long iterations = 0;
665 solver.
solve(stop, &propInner);
671 RealMatrix dualGradient = m_problem->solutionGradient();
673 RealVector grad(classes,0);
675 for (std::size_t i=0; i<numExamples; i++){
676 for (std::size_t p=0; p<
cardP; p++){
677 double g = dualGradient(i,p);
680 unsigned int y = m_problem->label(i);
682 for (std::size_t b=0; b<row.
size; b++)
702 grad -= sum(grad) / classes;
706 for (std::size_t c=0; c<classes; c++)
710 step(c) = -stepsize(c);
712 step(c) = stepsize(c);
714 double gg = prev(c) * grad(c);
725 step -= sum(step) / classes;
730 performBiasUpdate(step,nu);
732 if (max(stepsize) < 0.01 * stop.
minAccuracy)
break;
740 prop->accuracy = m_problem->checkKKT();
741 prop->value = m_problem->functionValue();
742 prop->iterations = iterations;
743 prop->seconds = finish_time - start_time;
747 void performBiasUpdate(
750 std::size_t numExamples = m_problem->getNumExamples();
751 std::size_t
cardP = m_problem->cardP();
752 RealMatrix deltaLinear(numExamples,cardP,0.0);
753 for (std::size_t i=0; i<numExamples; i++){
754 for (std::size_t p=0; p<
cardP; p++){
755 unsigned int y = m_problem->label(i);
757 for (std::size_t b=0; b<row.
size; b++)
763 m_problem->addDeltaLinear(deltaLinear);