You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/src/guidelines.md
+22-4Lines changed: 22 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
These are guidelines for the creation of models using NLPModels to help keeping the models uniform, and for future reference in the creation of solvers.
4
4
5
5
Table of contents:
6
+
6
7
-[Bare minimum](@ref bare-minimum)
7
8
-[Expected behaviour](@ref expected-behaviour)
8
9
-[Advanced counters](@ref advanced-counters)
@@ -14,23 +15,28 @@ Your model should derive from `AbstractNLPModel` or some other abstract class de
14
15
It is mandatory that it have a `meta :: NLPModelMeta` field, storing all the relevant problem information.
15
16
The model also needs to provide `Counters` information. The easiest way is to define `counters :: Counters`.
For alternatives to storing `Counters` in the model, check [advanced counters](@ref advanced-counters).
24
27
The minimum information that should be set for your model through `NLPModelMeta` is `nvar`, the number of variables.
25
28
The following is a valid constructor for `MyModel`:
29
+
26
30
```julia
27
31
functionMyModel()
28
32
returnMyModel(NLPModelMeta(5), Counters())
29
33
end
30
34
```
35
+
31
36
More information can be passed to `NLPModelMeta`.
32
37
See the full list [here](https://github.com/JuliaSmoothOptimizers/NLPModels.jl/blob/main/src/nlp/meta.jl#L32).
33
38
The essential fields are
39
+
34
40
-`x0`: Starting point (defaults to `zeros`)
35
41
-`lvar`, `uvar`: Bounds on the variables (default to `(-∞,∞)`)
36
42
-`ncon`: Number of constraints (defaults to `0`)
@@ -43,6 +49,7 @@ Luckily, many have a default implementation.
43
49
We collect here the list of functions that should be implemented for a complete API.
44
50
45
51
Here, the following notation applies:
52
+
46
53
-`nlp` is your instance of `MyModel <: AbstractNLPModel`
47
54
-`x` is the point where the function is evaluated
48
55
-`y` is the vector of Lagrange multipliers (for constrained problems only)
@@ -112,25 +119,32 @@ The following is a non-exhaustive list of expected behaviour for methods.
112
119
113
120
To further specialize your model, you can also define `show_header` and possibly `show`.
114
121
The default `show_header` simply prints the `typeof` the NLPModel, so it should be specialized with the specific information that you prefer. For instance, `SlackModel` defines
122
+
115
123
```julia
116
124
show_header(io ::IO, nlp ::SlackModel) =println(io, "SlackModel - Model with slack variables")
117
125
```
118
-
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`:
126
+
127
+
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 `get_counters` for them as well. Alternatively, you may desire to change the behaviour of show. The default behaviour is
128
+
119
129
```julia
120
-
functionshow(io ::IO, nlp ::SlackModel)
130
+
functionshow(io ::IO, nlp ::AbstractNLPModel)
121
131
show_header(io, nlp)
122
132
show(io, nlp.meta)
123
-
show(io, nlp.model.counters)
133
+
show(io, get_counters(nlp.model))
124
134
end
125
135
```
126
136
127
137
## [Advanced counters](@id advanced-counters)
128
138
129
139
If a model does not implement `counters`, then it needs to define
140
+
141
+
-`get_counters(nlp)` - get the `Counters` object associated with `nlp`
130
142
-`neval_xxx(nlp)` - get field `xxx` of `Counters`
131
143
-`reset!(nlp)` - resetting all counters
132
144
-`increment!(nlp, s)` - increment counter `s`
145
+
133
146
For instance
147
+
134
148
```julia
135
149
for counter infieldnames(Counters)
136
150
@evalbegin
@@ -144,19 +158,23 @@ function increment!(nlp :: MyModel, s :: Symbol)
144
158
INCREMENT COUNTER s
145
159
end
146
160
```
161
+
147
162
One example of such model is the `SlackModel`, which stores an internal `model :: AbstractNLPModel`, thus defining
163
+
148
164
```julia
149
165
$counter(nlp ::SlackModel) =$counter(nlp.model)
150
166
reset!(nlp ::SlackModel) =reset!(nlp.model)
151
167
increment!(nlp ::SlackModel, s ::Symbol) =increment!(nlp.model, s)
152
168
```
169
+
153
170
This construction can be replicated calling the macro `@default_counters Model inner`.
154
171
In the case of SlackModel, the equivalent call is
172
+
155
173
```julia
156
174
@default_counters SlackModel model
157
175
```
158
176
159
-
Furthermore, the `show` method has to be updated with the correct direction of `counter`. See [show](@ref show) for more information.
177
+
Furthermore, the `show` method may have to be updated with the correct direction of `counter`. See [show](@ref show) for more information.
0 commit comments