示例#1
0
int cQuadProg::Solve(const tProb& prob, tSoln& out_soln)
{
	assert(CheckProb(prob));

	int m = prob.GetNumCons(); // number of constraints
	int me = prob.mNumEquality; // number of equality constraints
	int mmax = m; // number of linear constraints
	int n = prob.GetDim(); // number of optimization variables
	int nmax = n; // number of bounded variables
	int mnn = m + n + n;

	out_soln.mX = prob.mInitX;
	Eigen::VectorXd u_buffer(mnn);

	// arg const cast....but c subroutine should still be safe....i think....
	double* c = const_cast<double*>(prob.mC.data());
	double* d = const_cast<double*>(prob.md.data());
	double* a = const_cast<double*>(prob.mA.data());
	double* b = const_cast<double*>(prob.mb.data());
	double* xl = const_cast<double*>(prob.mLower.data());
	double* xu = const_cast<double*>(prob.mUpper.data());
	double* x = out_soln.mX.data();
	double* u = u_buffer.data();
	double eps = prob.mEps;
	int mode = prob.mMode;
	int iout = 1; // where to rite output messages
	int ifail = 0; // output failure code.
	int iprint = 0; // output verbosity level (0 for nothing).

	int lwar = 3 * n*n + 10 * n + mmax + m + 1;
	Eigen::VectorXd war_buffer(lwar);
	double *war = war_buffer.data();

	int liwar = n;
	std::vector<int> iwar_buffer(liwar);
	int *iwar = iwar_buffer.data();

	// solve QP // an integer maps to an int
	ql_(&m, &me, &mmax, &n, &nmax, &mnn, c, d, a, b, xl, xu, x, u, &eps, &mode, &iout,
		&ifail, &iprint, war, &lwar, iwar, &liwar);

	bool fail = CheckFail(ifail);
	assert(!fail);
	return ifail;
}
示例#2
0
static int solveQP( const scalar& tol, MatrixXXsc& C, VectorXs& dvec, VectorXs& alpha )
{
  static_assert( std::is_same<scalar,double>::value, "QL only supports doubles." );

  assert( dvec.size() == alpha.size() );

  // All constraints are bound constraints.
  int m = 0;
  int me = 0;
  int mmax = 0;

  // C should be symmetric
  assert( ( C - C.transpose() ).lpNorm<Eigen::Infinity>() < 1.0e-14 );
  // Number of degrees of freedom.
  assert( C.rows() == C.cols() );
  int n{ int( C.rows() ) };
  int nmax = n;
  int mnn = m + n + n;
  assert( dvec.size() == nmax );

  // Impose non-negativity constraints on all variables
  Eigen::VectorXd xl = Eigen::VectorXd::Zero( nmax );
  Eigen::VectorXd xu = Eigen::VectorXd::Constant( nmax, std::numeric_limits<double>::infinity() );

  // u will contain the constraint multipliers
  Eigen::VectorXd u( mnn );

  // Status of the solve
  int ifail = -1;
  // Use the built-in cholesky decomposition
  int mode = 1;
  // Some fortran output stuff
  int iout = 0;
  // 1 => print output, 0 => silent
  int iprint = 1;

  // Working space
  assert( m == 0 && me == 0 && mmax == 0 );
  int lwar = 3 * ( nmax * nmax ) / 2 + 10 * nmax + 2;
  Eigen::VectorXd war( lwar );
  // Additional working space
  int liwar = n;
  Eigen::VectorXi iwar( liwar );

  {
    scalar tol_local = tol;
    ql_( &m,
        &me,
        &mmax,
        &n,
        &nmax,
        &mnn,
        C.data(),
        dvec.data(),
        nullptr,
        nullptr,
        xl.data(),
        xu.data(),
        alpha.data(),
        u.data(),
        &tol_local,
        &mode,
        &iout,
        &ifail,
        &iprint,
        war.data(),
        &lwar,
        iwar.data(),
        &liwar );
  }

  return ifail;
}