Skip to content

Introduce get_counters accessor - #562

Merged
dpo merged 1 commit into
mainfrom
counters-getter
Mar 13, 2026
Merged

Introduce get_counters accessor#562
dpo merged 1 commit into
mainfrom
counters-getter

Conversation

@dpo

@dpo dpo commented Mar 12, 2026

Copy link
Copy Markdown
Member

An accessor is more robust than accessing to an attribute explicitly, and allows concrete models to have Counters attributes that are not named counters.

For backward compatibility, get_counters(model) = model.counters, though that may change in the future.

Copilot AI review requested due to automatic review settings March 12, 2026 20:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a get_counters accessor and updates the NLP/NLS counter plumbing (including display and the @default_counters macro) to use it, reducing direct coupling to a counters field name and enabling alternative counter storage layouts in concrete models.

Changes:

  • Add and export get_counters(::AbstractNLPModel) (defaulting to nlp.counters) and route counter access through it.
  • Update NLP/NLS show, sum_counters, and counter getter/increment/reset logic to use get_counters.
  • Update tests and guidelines to reference the new accessor.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/nlp/utils.jl Updates wrapper-model counter test to compare via get_counters instead of direct field access.
src/nls/show.jl Uses get_counters(nls) when displaying NLS counters.
src/nls/counters.jl Routes NLS counter access/increment/reset/sum through get_counters.
src/nlp/utils.jl Updates @default_counters forwarding of .counters to use get_counters(inner).
src/nlp/show.jl Uses get_counters(nlp) when displaying NLP counters.
src/nlp/counters.jl Exports/defines get_counters and updates counter APIs to call it.
docs/src/guidelines.md Updates show example to use get_counters, plus formatting tweaks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/src/guidelines.md Outdated
Comment on lines +127 to +128
Furthermore, we define a general `show` that calls `show_header` and specific `show` functions for the `meta` and the `counters`. If your model does not have `counters` in the default location, you must define `show` for them as well. Alternatively, you may desire to change the behaviour of show. Here is an example, again from `SlackModel`:

Copilot AI Mar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section says models without counters in the default location "must define show for them", but the codebase now uses get_counters(nlp) in the default show. With this PR, overriding get_counters should generally be sufficient for show to work without custom show methods; the guideline text should be updated accordingly.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, I need to implement get_counters for SlackModels.

Comment thread docs/src/guidelines.md
Comment on lines 139 to 143
If a model does not implement `counters`, then it needs to define

- `neval_xxx(nlp)` - get field `xxx` of `Counters`
- `reset!(nlp)` - resetting all counters
- `increment!(nlp, s)` - increment counter `s`

Copilot AI Mar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Advanced counters" guidance is now misleading: a model can store counters under a different field name and just implement get_counters(nlp) to reuse the existing neval_*, increment!, reset!, etc. implementations. Consider updating this section to recommend overriding get_counters first, and only implementing all neval_xxx/reset!/increment! manually when the counters are not representable as a Counters object.

Copilot uses AI. Check for mistakes.
Comment thread src/nlp/counters.jl Outdated
Comment thread src/nlp/counters.jl
@github-actions

github-actions Bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor
Package name latest stable
ADNLPModels
AdaptiveRegularization
AmplNLReader
BundleAdjustmentModels
CUTEst
CaNNOLeS
DCISolver
FletcherPenaltySolver
FluxNLPModels
JSOSolvers
JSOSuite
LLSModels
ManualNLPModels
NLPModelsIpopt
NLPModelsJuMP
NLPModelsKnitro
NLPModelsModifiers
NLPModelsTest
NLSProblems
PDENLPModels
PartiallySeparableNLPModels
PartiallySeparableSolvers
Percival
QuadraticModels
RegularizedOptimization
RegularizedProblems
SolverBenchmark
SolverTest
SolverTools

@dpo
dpo force-pushed the counters-getter branch 2 times, most recently from 6d1c37b to a4b6102 Compare March 12, 2026 21:38
An accessor is more robust than accessing to an attribute explicitly,
and allows concrete models to have Counters attributes that are not
named `counters`.

For backward compatibility, get_counters(model) = model.counters,
though that may change in the future.
@dpo
dpo force-pushed the counters-getter branch from a4b6102 to d26bd52 Compare March 12, 2026 23:25
@dpo
dpo merged commit 808b347 into main Mar 13, 2026
74 checks passed
@dpo
dpo deleted the counters-getter branch March 13, 2026 15:42
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.

2 participants