Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/APIreference/APItypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,14 @@ This is the data structure holding information about one solver iteration. ``mjD
of mjSolverStat data structures, one for each iteration of the solver, up to a maximum of mjNSOLVER. The actual number
of solver iterations is given by ``mjData.solver_niter``.

For recorded CG and Newton iterations, ``lsresult`` gives the line search result:
0 (success), 1 (search vector too small), 2 (no improvement, initial convergence),
3 (could not bracket), 4 (improvement without convergence), 5 (no improvement),
6 (no second bracket point update), and 7 (no improvement, could not bracket).
The value is -1 for PGS and NoSlip iterations, which have no primal line search.
As with the other per-iteration statistics, a terminal search that takes no step
does not create an additional entry in ``mjData.solver``.

.. mujoco-include:: mjSolverStat


Expand Down
1 change: 1 addition & 0 deletions doc/includes/references.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef struct mjSolverStat_ { // per-iteration solver statistics
int nchange; // number of constraint state changes
int neval; // number of cost evaluations in line search
int nupdate; // number of Cholesky updates in line search
int lsresult; // line search result (CG/Newton), -1 if not applicable
} mjSolverStat;
typedef struct mjData_ {
// constant sizes
Expand Down
1 change: 1 addition & 0 deletions include/mujoco/mjdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ typedef struct mjSolverStat_ { // per-iteration solver statistics
int nchange; // number of constraint state changes
int neval; // number of cost evaluations in line search
int nupdate; // number of Cholesky updates in line search
int lsresult; // line search result (CG/Newton), -1 if not applicable
} mjSolverStat;


Expand Down
9 changes: 9 additions & 0 deletions python/mujoco/bindings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,15 @@ def assert_unallocated_are_filled(data):
# allocated arena arrays are views of mjData
self.assertFalse(data.tendon_efcadr.flags.owndata)

def test_solver_line_search_result_list(self):
self.data.solver[0].lsresult = 3
self.data.solver[1].lsresult = -1
self.assertEqual(self.data.solver.lsresult[0], 3)
self.assertEqual(self.data.solver.lsresult[1], -1)
np.testing.assert_array_equal(self.data.solver[:2].lsresult, [3, -1])
self.data.solver.lsresult[0] = 4
self.assertEqual(self.data.solver[0].lsresult, 4)

def test_mj_struct_list_equality(self):
model2 = mujoco.MjModel.from_xml_string(TEST_XML)
data2 = mujoco.MjData(model2)
Expand Down
5 changes: 5 additions & 0 deletions python/mujoco/introspect/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5650,6 +5650,11 @@
type=ValueType(name='int'),
doc='number of Cholesky updates in line search',
),
StructFieldDecl(
name='lsresult',
type=ValueType(name='int'),
doc='line search result (CG/Newton), -1 if not applicable',
),
),
)),
('mjData',
Expand Down
2 changes: 2 additions & 0 deletions python/mujoco/structs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ This is useful for example when the MJB is not available as a file on disk.)"));
X(nchange);
X(neval);
X(nupdate);
X(lsresult);
#undef X

py::class_<MjSolverStatList> mjSolverStatList(m, "_MjSolverStatList");
Expand All @@ -846,6 +847,7 @@ This is useful for example when the MJB is not available as a file on disk.)"));
X(int, nchange);
X(int, neval);
X(int, nupdate);
X(int, lsresult);
#undef X

// ==================== MJPRECONTACT =========================================
Expand Down
1 change: 1 addition & 0 deletions python/mujoco/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ class MjStructList<raw::MjSolverStat>
X(int, nchange);
X(int, neval);
X(int, nupdate);
X(int, lsresult);
#undef X

protected:
Expand Down
6 changes: 4 additions & 2 deletions python/mujoco/structs_wrappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,8 @@ MjSolverStatList::MjStructList(raw::MjSolverStat* ptr, int num,
X(int, nactive),
X(int, nchange),
X(int, neval),
X(int, nupdate) {}
X(int, nupdate),
X(int, lsresult) {}
#undef X
#undef XN

Expand All @@ -1099,7 +1100,8 @@ MjSolverStatList::MjStructList(MjSolverStatList& other, py::slice slice)
X(int, nactive),
X(int, nchange),
X(int, neval),
X(int, nupdate) {}
X(int, nupdate),
X(int, lsresult) {}
#undef X

// ==================== MJVPERTURB =============================================
Expand Down
5 changes: 3 additions & 2 deletions src/engine/engine_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,10 @@ void mj_printFormattedData(const mjModel* m, const mjData* d, const char* filena
fprintf(fp, " lineslope = ");
fprintf(fp, float_format, stat->lineslope);
fprintf(fp, "\n");
fprintf(fp, " nactive = %d nchange = %d neval = %d nupdate = %d\n",
fprintf(fp, " nactive = %d nchange = %d neval = %d nupdate = %d"
" lsresult = %d\n",
stat->nactive, stat->nchange,
stat->neval, stat->nupdate);
stat->neval, stat->nupdate, stat->lsresult);
}
fprintf(fp, "\n");
}
Expand Down
9 changes: 5 additions & 4 deletions src/engine/engine_solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static inline mjtNum dot2(const mjtNum vec1[2], const mjtNum vec2[2]) {
// save solver statistics
static void saveStats(const mjModel* m, mjData* d, int island, int iter,
mjtNum improvement, mjtNum gradient, mjtNum lineslope,
int nactive, int nchange, int neval, int nupdate) {
int nactive, int nchange, int neval, int nupdate, int lsresult) {
// if island out of range, return
if (island >= mjNISLAND) {
return;
Expand All @@ -87,6 +87,7 @@ static void saveStats(const mjModel* m, mjData* d, int island, int iter,
stat->nchange = nchange;
stat->neval = neval;
stat->nupdate = nupdate;
stat->lsresult = lsresult;
}


Expand Down Expand Up @@ -787,7 +788,7 @@ static void solPGS(const mjModel* m, mjData* d, int island,

// scale improvement, save stats
improvement *= scale;
saveStats(m, d, island_stat, iter, improvement, 0, 0, nactive, nchange, 0, 0);
saveStats(m, d, island_stat, iter, improvement, 0, 0, nactive, nchange, 0, 0, -1);

// Nesterov gradient restart (O'Donoghue-Candès): reset when correction opposes extrapolation
if (nesterov) {
Expand Down Expand Up @@ -1045,7 +1046,7 @@ static void solNoSlip(const mjModel* m, mjData* d, int island,
// save noslip stats after all the entries from regular solver
if (island_stat < mjNISLAND) {
int stats_iter = iter + d->solver_niter[island_stat];
saveStats(m, d, island_stat, stats_iter, improvement, 0, 0, nactive, nchange, 0, 0);
saveStats(m, d, island_stat, stats_iter, improvement, 0, 0, nactive, nchange, 0, 0, -1);
}

// increment iteration count
Expand Down Expand Up @@ -4247,7 +4248,7 @@ static void mj_solPrimal(const mjModel* m, mjData* d, int island, int maxiter, i
mjtNum improvement = scale * ls_improvement;
mjtNum gradient = scale * mju_norm(ctx.grad, nv);
saveStats(m, d, island, iter, improvement, gradient, ctx.LSslope,
ctx.nactive, nchange, ctx.LSiter, ctx.nupdate);
ctx.nactive, nchange, ctx.LSiter, ctx.nupdate, ctx.LSresult);

// Newton decrement: 0.5*grad'*H^-1*grad, the model's predicted improvement of the
// next step; clamp to 0 so that tolerance == 0 keeps early termination disabled
Expand Down
60 changes: 48 additions & 12 deletions test/engine/engine_solver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,21 @@ TEST_F(SolverTest, SolversEquivalent) {
SolverTolerances tolerances;
} kConfigs[] = {
{.path = kModelPath,
.tolerances = {
.newton = MjTol(1e-13, 1e-5),
.cg = MjTol(1e-13, 1e-5),
.pgs_pyramidal = MjTol(1e-13, 1e-5),
.pgs_elliptic = MjTol(1e-3, 1e-3),
}},
.tolerances =
{
.newton = MjTol(1e-13, 1e-5),
.cg = MjTol(1e-13, 1e-5),
.pgs_pyramidal = MjTol(1e-13, 1e-5),
.pgs_elliptic = MjTol(1e-3, 1e-3),
}},
{.path = kHumanoidPath,
.tolerances = {
.newton = MjTol(1e-13, 1e-5),
.cg = MjTol(1e-12, 1e-5),
.pgs_pyramidal = MjTol(1e-12, 1e-5),
.pgs_elliptic = MjTol(1e-8, 1e-4),
}},
.tolerances =
{
.newton = MjTol(1e-13, 1e-5),
.cg = MjTol(1e-12, 1e-5),
.pgs_pyramidal = MjTol(1e-12, 1e-5),
.pgs_elliptic = MjTol(1e-8, 1e-4),
}},
};

for (const auto& config : kConfigs) {
Expand Down Expand Up @@ -393,6 +395,40 @@ TEST_F(SolverTest, EllipticLineSearchPrecisionDiagnostics) {
}
}

TEST_F(SolverTest, LineSearchResultStatistics) {
static constexpr char xml[] = R"(
<mujoco>
<option tolerance="0" iterations="1" ls_iterations="1"/>
<worldbody>
<geom type="plane" size="1 1 1"/>
<body pos="0 0 0.09">
<freejoint/>
<geom type="sphere" size="0.1" mass="1"/>
</body>
</worldbody>
</mujoco>
)";
char error[1024];
MjModelPtr model = LoadModelFromString(xml, error, sizeof(error));
ASSERT_THAT(model, NotNull()) << error;
MjDataPtr data = MakeData(model);
model->opt.disableflags |= mjDSBL_ISLAND | mjDSBL_WARMSTART;

for (mjtSolver solver : {mjSOL_CG, mjSOL_NEWTON, mjSOL_PGS}) {
model->opt.solver = solver;
mj_resetData(model.get(), data.get());
mj_forward(model.get(), data.get());
ASSERT_EQ(data->solver_niter[0], 1) << solver;
if (solver == mjSOL_PGS) {
EXPECT_EQ(data->solver[0].lsresult, -1);
} else {
// The initial two evaluations exhaust the line-search budget.
EXPECT_EQ(data->solver[0].lsresult, 3);
EXPECT_GE(data->solver[0].neval, model->opt.ls_iterations);
}
}
}

TEST_F(SolverTest, EllipticLineSearchCancellation) {
std::string xml = R"(
<mujoco>
Expand Down
1 change: 1 addition & 0 deletions unity/Runtime/Bindings/MjBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,7 @@ public unsafe struct mjSolverStat_ {
public int nchange;
public int neval;
public int nupdate;
public int lsresult;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
1 change: 1 addition & 0 deletions wasm/codegen/generated/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5562,6 +5562,7 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("gradient", &MjSolverStat::gradient, &MjSolverStat::set_gradient)
.property("improvement", &MjSolverStat::improvement, &MjSolverStat::set_improvement)
.property("lineslope", &MjSolverStat::lineslope, &MjSolverStat::set_lineslope)
.property("lsresult", &MjSolverStat::lsresult, &MjSolverStat::set_lsresult)
.property("nactive", &MjSolverStat::nactive, &MjSolverStat::set_nactive)
.property("nchange", &MjSolverStat::nchange, &MjSolverStat::set_nchange)
.property("neval", &MjSolverStat::neval, &MjSolverStat::set_neval)
Expand Down
6 changes: 6 additions & 0 deletions wasm/codegen/generated/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ struct MjSolverStat {
void set_nupdate(int value) {
ptr_->nupdate = value;
}
int lsresult() const {
return ptr_->lsresult;
}
void set_lsresult(int value) {
ptr_->lsresult = value;
}

private:
mjSolverStat* ptr_;
Expand Down
Loading