Skip to content

Commit 8e91aa4

Browse files
committed
constraint: accept null findex in boxed LCP solvers
1 parent 42099a9 commit 8e91aa4

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

dart/constraint/DantzigBoxedLcpSolver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool DantzigBoxedLcpSolver::solve(
7575
int* findex,
7676
bool earlyTermination)
7777
{
78-
if (n <= 0 || !A || !x || !b || !lo || !hi || !findex)
78+
if (n <= 0 || !A || !x || !b || !lo || !hi)
7979
return false;
8080

8181
const int nSkip = math::padding(n);
@@ -89,13 +89,14 @@ bool DantzigBoxedLcpSolver::solve(
8989
Eigen::VectorXd bVec(n);
9090
Eigen::VectorXd loVec(n);
9191
Eigen::VectorXd hiVec(n);
92-
Eigen::VectorXi findexVec(n);
92+
Eigen::VectorXi findexVec = Eigen::VectorXi::Constant(n, -1);
9393
Eigen::VectorXd xVec(n);
9494
for (int i = 0; i < n; ++i) {
9595
bVec[i] = b[i];
9696
loVec[i] = lo[i];
9797
hiVec[i] = hi[i];
98-
findexVec[i] = findex[i];
98+
if (findex)
99+
findexVec[i] = findex[i];
99100
xVec[i] = x[i];
100101
}
101102

dart/constraint/PgsBoxedLcpSolver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool PgsBoxedLcpSolver::solve(
9191
int* findex,
9292
bool earlyTermination)
9393
{
94-
if (n <= 0 || !A || !x || !b || !lo || !hi || !findex)
94+
if (n <= 0 || !A || !x || !b || !lo || !hi)
9595
return false;
9696

9797
const int nSkip = math::padding(n);
@@ -105,13 +105,14 @@ bool PgsBoxedLcpSolver::solve(
105105
Eigen::VectorXd bVec(n);
106106
Eigen::VectorXd loVec(n);
107107
Eigen::VectorXd hiVec(n);
108-
Eigen::VectorXi findexVec(n);
108+
Eigen::VectorXi findexVec = Eigen::VectorXi::Constant(n, -1);
109109
Eigen::VectorXd xVec(n);
110110
for (int i = 0; i < n; ++i) {
111111
bVec[i] = b[i];
112112
loVec[i] = lo[i];
113113
hiVec[i] = hi[i];
114-
findexVec[i] = findex[i];
114+
if (findex)
115+
findexVec[i] = findex[i];
115116
xVec[i] = x[i];
116117
}
117118

tests/unit/constraint/test_BoxedLcpConstraintSolver.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,41 @@ TEST(BoxedLcpConstraintSolver, SecondaryCanBeDisabled)
163163
EXPECT_FALSE(dantzig->getDefaultOptions().earlyTermination);
164164
EXPECT_EQ(solver.getSecondaryLcpSolver(), nullptr);
165165
}
166+
167+
//==============================================================================
168+
TEST(DantzigBoxedLcpSolver, AcceptsNullFindex)
169+
{
170+
constraint::DantzigBoxedLcpSolver solver;
171+
172+
constexpr int kSize = 1;
173+
double A[kSize] = {1.0};
174+
double x[kSize] = {0.0};
175+
const double target = 0.5;
176+
double b[kSize] = {A[0] * target};
177+
double lo[kSize] = {0.0};
178+
double hi[kSize] = {std::numeric_limits<double>::infinity()};
179+
180+
const bool success = solver.solve(kSize, A, x, b, 0, lo, hi, nullptr, false);
181+
182+
EXPECT_TRUE(success);
183+
EXPECT_NEAR(x[0], target, 1e-8);
184+
}
185+
186+
//==============================================================================
187+
TEST(PgsBoxedLcpSolver, AcceptsNullFindex)
188+
{
189+
constraint::PgsBoxedLcpSolver solver;
190+
191+
constexpr int kSize = 1;
192+
double A[kSize] = {1.0};
193+
double x[kSize] = {0.0};
194+
const double target = 0.5;
195+
double b[kSize] = {A[0] * target};
196+
double lo[kSize] = {0.0};
197+
double hi[kSize] = {std::numeric_limits<double>::infinity()};
198+
199+
const bool success = solver.solve(kSize, A, x, b, 0, lo, hi, nullptr, false);
200+
201+
EXPECT_TRUE(success);
202+
EXPECT_NEAR(x[0], target, 1e-6);
203+
}

0 commit comments

Comments
 (0)