5 #ifndef SPIDAR_QUADRATIC_PROGRAMMING_HPP 6 #define SPIDAR_QUADRATIC_PROGRAMMING_HPP 19 template <
class Traits>
23 typedef typename Traits::ValueType ValueType;
24 typedef typename Traits::MatrixType MatrixType;
25 typedef typename Traits::VectorType VectorType;
26 typedef typename Traits::ActiveSetType ActiveSetType;
28 static const int size(
void) {
return Traits::SIZE; }
43 int solve(
const MatrixType& q,
const VectorType& c, VectorType& x,
const VectorType& min,
const VectorType& max)
46 initialize(x, min, max);
49 for (
int i=0; i<64; ++i)
59 if(calcLambda())
return i;
72 void initialize(VectorType& x,
const VectorType& min,
const VectorType& max)
74 minX_ = VectorType() - min;
77 for (
int i=0; i<size(); ++i)
85 void updateMatrix(
const MatrixType& q,
const VectorType& c)
87 for (
int i=0; i<size(); ++i)
91 for (
int j=0; j<size(); ++j)
93 if (activeSet_[j] > 0)
95 vecL_[i] -= q[i][j] * maxX_[i];
96 matR_[i][j] = identity_[i][j];
98 else if (activeSet_[j] < 0)
100 vecL_[i] += q[i][j] * minX_[i];
101 matR_[i][j] = -identity_[i][j];
105 matR_[i][j] = q[i][j];
111 void checkResult(
void)
113 for (
int i=0; i<size(); ++i)
115 if (activeSet_[i] > 0)
120 else if (activeSet_[i] < 0)
123 vecX_[i] = -minX_[i];
127 vecY_[i] = ValueType(0);
133 bool calcLambda(
void)
137 for (
int i=0; i<size(); ++i)
139 if (activeSet_[i]!=0)
152 void calcAlpha(VectorType& x)
154 int minIndex = -1, bval;
155 ValueType alpha, minAlpha = 1;
157 for (
int i=0; i<size(); ++i)
159 if (activeSet_[i]==0)
161 vecD_[i] = vecX_[i] - x[i];
165 alpha = -(minX_[i] + x[i]) / vecD_[i];
167 if (alpha > 0 && minAlpha > alpha)
178 else if (vecD_[i] > 0)
180 alpha = (maxX_[i] - x[i]) / vecD_[i];
182 if (alpha > 0 && minAlpha > alpha)
198 activeSet_[minIndex] = bval;
200 for (
int i=0; i<size(); ++i)
204 x[i] += minAlpha * vecD_[i];
210 for (
int i=0; i<size(); ++i)
217 MatrixType identity_;
227 ActiveSetType activeSet_;
234 #endif // SPIDAR_QUADRATIC_PROGRAMMING_HPP
static void solve(MatrixType &a, VectorType &b, VectorType &x)
ガウスの消去法を用いて連立一次方程式を解きます.