Add option to skip model statistics collection before solve (context.solver.collect_model_statistics) - #42
Open
a-safari wants to merge 2 commits into
Conversation
a-safari
force-pushed
the
disabling-model-statistics
branch
from
September 3, 2026 07:46
f01e19b to
a554d60
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an opt-in configuration flag,
context.solver.collect_model_statistics,that allows callers to skip the model statistics / KPI metadata collection that
CplexLocalSolveEnv.before_solve()currently performs unconditionally before everylocal CPLEX solve.
Motivation
Profiling a MIP-heavy application that calls
Model.solve()many times per run(hundreds of solves) showed that
Model.statistics(and the_make_new_stats()call it triggers) was one of the largest contributors to wall-clock time outside
of CPLEX itself.
Looking at
CplexLocalSolveEnv.before_solve(), this cost comes from anunconditional block that:
mdl.statistics(iterating all variables and constraints to countbinary/integer/continuous/semi-continuous/semi-integer variables and all
constraint types).
make_new_kpis_dict(...)) from thosestatistics.
the_env.notify_start_solve(kpis)and,optionally,
the_env.update_solve_details(...).Applications that do not consume this solve-start metadata pay this
cost on every single solve with no benefit.
There is currently no way to opt out of this calculation through the public
ContextAPI —context.solver.auto_publish.solve_detailsonly controlswhether the already computed metadata is published via
update_solve_details(...); it does not preventmdl.statisticsfrom beingcomputed in the first place.
Changes
docplex/mp/context.pySolverContextattribute,collect_model_statistics, defaultingto
Trueto preserve existing behavior for all current users.solver.*settings.docplex/mp/solve_env.pyCplexLocalSolveEnv.before_solve()in anif context.solver.collect_model_statistics:check.False, the method now callsthe_env.notify_start_solve({})instead, preserving the solve-startnotification lifecycle without computing statistics or KPI metadata.
before_solve()is unchanged.after_solve()is unmodified.Backward compatibility
True, so behavior is unchanged unless a caller explicitlysets
context.solver.collect_model_statistics = False.How to use