19 if (chol.rcond()>1e-8) x=chol.solve(b);
66 vnl_solve_symmetric_le(AHA,b1,lambda);
95 double b1=-1.0*(H_inv*g).sum();
100 if (std::fabs(H_inv_sum)<1e-8)
102 std::cerr<<
"Uh-oh. H_inv.sum()="<<H_inv_sum<<std::endl
104 <<
"H_inv="<<H_inv<<std::endl;
108 double lambda = b1/H_inv_sum;
123 std::vector<bool>& valid,
129 double min_alpha=1.0;
130 for (
unsigned i=0;i<n_valid;++i)
134 double alpha = -1.0*x1[i]/dx[i];
137 min_alpha=alpha; worst_i=i;
144 for (
unsigned i=0;i<n;++i)
148 x[i]+=min_alpha*dx[i1];
149 if (i1==(
unsigned int)worst_i)
171 std::vector<bool>& valid,
179 unsigned nc=A.
rows();
184 for (
unsigned j=0;j<n;++j)
191 for (
unsigned i=0;i<n;++i)
193 if (valid[i]) { H1(i1,j1)=H(i,j); ++i1; }
197 for (
unsigned i=0;i<nc;++i,++i1) A1(i,j1)=A(i,j);
206 for (
unsigned i=0;i<n;++i)
208 if (valid[i]) { g1[i1]=g[i]; x1[i1]=x[i]; ++i1; }
220 return vnl_solve_qp_update_x(x,x1,dx,valid,n_valid);
229 std::vector<bool>& valid,
240 for (
unsigned j=0;j<n;++j)
247 for (
unsigned i=0;i<n;++i)
249 if (valid[i]) { H1(i1,j1)=H(i,j); ++i1; }
258 for (
unsigned i=0;i<n;++i)
260 if (valid[i]) { g1[i1]=g[i]; x1[i1]=x[i]; ++i1; }
269 return vnl_solve_qp_update_x(x,x1,dx,valid,n_valid);
304 std::cerr<<
"Supplied x does not satisfy equality constraints\n";
307 for (
unsigned i=0;i<n;++i)
312 std::cerr<<
"Element "<<i<<
" of x is negative. Must be >=0 on input.\n";
318 std::vector<bool> valid(n,
true);
326 std::cerr<<
"Oops: Final x does not satisfy equality constraints\n";
354 if (std::fabs(x.
sum()-1.0)>1e-8)
357 std::cerr<<
"Supplied x does not sum to unity.\n";
360 for (
unsigned i=0;i<n;++i)
365 std::cerr<<
"Element "<<i<<
" of x is negative. Must be >=0 on input.\n";
371 std::vector<bool> valid(n,
true);
376 if (std::fabs(x.
sum()-1.0)>1e-8)
379 std::cerr<<
"Oops. Final x does not sum to unity.\n";
unsigned int cols() const
Return the number of columns.
T vnl_vector_ssd(vnl_vector< T > const &v1, vnl_vector< T > const &v2)
Euclidean Distance between two vectors.
bool vnl_solve_qp_non_neg_step(const vnl_matrix< double > &H, const vnl_vector< double > &g, const vnl_matrix< double > &A, const vnl_vector< double > &b, vnl_vector< double > &x, std::vector< bool > &valid, unsigned &n_valid)
Solve unconstrained problem and apply one extra constraint if necessary.
bool vnl_solve_qp_with_equality_constraints(const vnl_matrix< double > &H, const vnl_vector< double > &g, const vnl_matrix< double > &A, const vnl_vector< double > &b, vnl_vector< double > &x)
Solve quadratic programming problem with linear constraints.
vnl_matrix< T > transpose() const
Return transpose.
Functions to solve various forms of constrained quadratic programming.
vnl_matrix< double > inverse() const
static T sum(T const *v, unsigned n)
Decomposition of symmetric matrix.
size_t size() const
Return the length, number of elements, dimension of this vector.
Decomposition of symmetric matrix.
T sum() const
Sum of values in a vector.
vnl_matrix< T > inverse() const
bool vnl_solve_qp_non_neg_sum_one(const vnl_matrix< double > &H, const vnl_vector< double > &g, vnl_vector< double > &x, bool verbose)
Find non-negative solution to a constrained quadratic programming problem.
double rcond() const
Return reciprocal condition number (smallest/largest singular values).
unsigned int rows() const
Return the number of rows.
Holds the singular value decomposition of a vnl_matrix.
bool vnl_solve_qp_with_non_neg_constraints(const vnl_matrix< double > &H, const vnl_vector< double > &g, const vnl_matrix< double > &A, const vnl_vector< double > &b, vnl_vector< double > &x, double con_tol, bool verbose)
Find non-negative solution to a constrained quadratic programming problem.
Holds the singular value decomposition of a vnl_matrix.
bool vnl_solve_qp_zero_sum(const vnl_matrix< double > &H, const vnl_vector< double > &g, vnl_vector< double > &x)
Solve quadratic programming problem with constraint sum(x)=0.
bool vnl_solve_qp_non_neg_sum_one_step(const vnl_matrix< double > &H, const vnl_vector< double > &g, vnl_vector< double > &x, std::vector< bool > &valid, unsigned &n_valid)
Solve unconstrained problem and apply one extra constraint if necessary.