Skip to content

Add option to skip model statistics collection before solve (context.solver.collect_model_statistics) - #42

Open
a-safari wants to merge 2 commits into
IBMDecisionOptimization:masterfrom
a-safari:disabling-model-statistics
Open

a-safari wants to merge 2 commits into
IBMDecisionOptimization:masterfrom
a-safari:disabling-model-statistics

Conversation

@a-safari

@a-safari a-safari commented Sep 2, 2026

Copy link
Copy Markdown

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 every
local 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 an
unconditional block that:

  1. Computes mdl.statistics (iterating all variables and constraints to count
    binary/integer/continuous/semi-continuous/semi-integer variables and all
    constraint types).
  2. Builds a KPI metadata dictionary (make_new_kpis_dict(...)) from those
    statistics.
  3. Publishes that metadata via 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
Context API — context.solver.auto_publish.solve_details only controls
whether the already computed metadata is published via
update_solve_details(...); it does not prevent mdl.statistics from being
computed in the first place.

Changes

  • docplex/mp/context.py

    • Added a new SolverContext attribute, collect_model_statistics, defaulting
      to True to preserve existing behavior for all current users.
    • Documented the new attribute alongside the other solver.* settings.
  • docplex/mp/solve_env.py

    • Wrapped the statistics/KPI construction block inside
      CplexLocalSolveEnv.before_solve() in an
      if context.solver.collect_model_statistics: check.
    • When the flag is False, the method now calls
      the_env.notify_start_solve({}) instead, preserving the solve-start
      notification lifecycle without computing statistics or KPI metadata.
    • All other behavior in before_solve() is unchanged.
    • after_solve() is unmodified.

Backward compatibility

  • Default value is True, so behavior is unchanged unless a caller explicitly
    sets context.solver.collect_model_statistics = False.
  • No public API is removed or renamed.

How to use

from docplex.mp.model import Model

model = Model(name="my_model")
model.context.solver.collect_model_statistics = False

@a-safari a-safari closed this Sep 3, 2026
@a-safari a-safari reopened this Sep 3, 2026
@a-safari
a-safari force-pushed the disabling-model-statistics branch from f01e19b to a554d60 Compare September 3, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant