Skip to content

feat: Capture solver log in SolverMetrics#23

Open
FBumann wants to merge 2 commits intofeature/solving-metricsfrom
feat/solver-log-in-metrics
Open

feat: Capture solver log in SolverMetrics#23
FBumann wants to merge 2 commits intofeature/solving-metricsfrom
feat/solver-log-in-metrics

Conversation

@FBumann
Copy link
Owner

@FBumann FBumann commented Mar 17, 2026

Summary

  • Add solver_log: str | None field to SolverMetrics for programmatic access to solver output after solving
  • In Model.solve(), capture the solver log from a temp file (or user-provided log_fn) and attach it to metrics
  • Handles edge cases: file creation failure (graceful fallback), encoding (UTF-8 with replace), cleanup (Path.unlink(missing_ok=True))
  • Log is captured in finally so it's available even when the solver fails

Blocked by

Changes proposed in this Pull Request

  • linopy/constants.py: Add solver_log field to SolverMetrics, with truncated display in __repr__
  • linopy/model.py: Create temp log file when log_fn is not provided, read log into solver_log on metrics after solving
  • test/test_optimization.py: Add test_solver_log_captured and test_solver_log_with_log_fn tests

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Test plan

  • test_solver_log_captured — verifies solver_metrics.solver_log is non-empty after solve (all solver/io_api combos)
  • test_solver_log_with_log_fn — verifies log capture when log_fn is explicitly provided
  • Verified across all 17 solver/io_api combinations (gurobi, highs, cbc, scip, cplex, xpress, mosek × lp/mps/direct)
  • Existing test_solver_metrics.py tests still pass

🤖 Generated with Claude Code

Store the solver log output as a string in Result.solver_log and
Model.solver_log, making it programmatically accessible after solving.
When no log_fn is provided, a temporary file is used to capture the log
and cleaned up afterwards. When log_fn is provided, the file contents
are read into solver_log as well.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 17, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b654d371-5855-4607-8204-94d12103f607

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/solver-log-in-metrics
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@FBumann FBumann changed the title feat: Capture solver log in Result and expose on Model feat: Capture solver log in SolverMetrics Mar 17, 2026
Prevents potential NameError if NamedTemporaryFile creation fails
or if the solver raises an exception.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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