Skip to content

Commit e577c32

Browse files
committed
adjust tests
1 parent 25d3bea commit e577c32

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

src/stats.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ It contains the following fields:
6363
- `elapsed_time`: The elapsed time computed by the solver (default: `Inf`);
6464
- `solver_specific::Dict{Symbol,Any}`: A solver specific dictionary.
6565
66-
The constructor tries to preallocate storage for the fields above.
66+
The constructor preallocates storage for the fields above.
67+
Special storage may be used for `multipliers_L` and `multipliers_U` by passing them to the constructor.
68+
For instance, if a problem has few bound constraints, those multipliers could be held in sparse vectors.
69+
6770
The following fields indicate whether the information above has been updated and is reliable:
6871
6972
- `solution_reliable`
@@ -84,7 +87,7 @@ All other variables can be input as keyword arguments.
8487
8588
Notice that `GenericExecutionStats` does not compute anything, it simply stores.
8689
"""
87-
mutable struct GenericExecutionStats{T, S, V} <: AbstractExecutionStats
90+
mutable struct GenericExecutionStats{T, S, V, Tsp} <: AbstractExecutionStats
8891
status::Symbol
8992
solution_reliable::Bool
9093
solution::S # x
@@ -102,7 +105,7 @@ mutable struct GenericExecutionStats{T, S, V} <: AbstractExecutionStats
102105
time_reliable::Bool
103106
elapsed_time::Float64
104107
solver_specific_reliable::Bool
105-
solver_specific::Dict{Symbol, Any}
108+
solver_specific::Dict{Symbol, Tsp}
106109
end
107110

108111
function GenericExecutionStats(
@@ -111,10 +114,10 @@ function GenericExecutionStats(
111114
solution::S = similar(nlp.meta.x0),
112115
objective::T = T(Inf),
113116
dual_feas::T = T(Inf),
114-
primal_feas::T = unconstrained(nlp) || bound_constrained(nlp) ? zero(T) : T(Inf),
117+
primal_feas::T = unconstrained(nlp) ? zero(T) : T(Inf),
115118
multipliers::S = similar(nlp.meta.y0),
116-
multipliers_L::V = similar(nlp.meta.y0, bound_constrained(nlp) ? nlp.meta.nvar : 0),
117-
multipliers_U::V = similar(nlp.meta.y0, bound_constrained(nlp) ? nlp.meta.nvar : 0),
119+
multipliers_L::V = similar(nlp.meta.y0, has_bounds(nlp) ? nlp.meta.nvar : 0),
120+
multipliers_U::V = similar(nlp.meta.y0, has_bounds(nlp) ? nlp.meta.nvar : 0),
118121
iter::Int = -1,
119122
elapsed_time::Real = Inf,
120123
solver_specific::Dict{Symbol, Tsp} = Dict{Symbol, Any}(),
@@ -126,7 +129,7 @@ function GenericExecutionStats(
126129
)
127130
throw(KeyError(status))
128131
end
129-
return GenericExecutionStats{T, S, V}(
132+
return GenericExecutionStats{T, S, V, Tsp}(
130133
status,
131134
false,
132135
solution,

test/dummy_solver.jl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ function dummy_solver(
6161
:max_eval
6262
end
6363

64-
return GenericExecutionStats(
65-
:unknown,
66-
nlp,
67-
objective = fx,
68-
dual_feas = norm(dual),
69-
primal_feas = norm(cx),
70-
multipliers = y,
71-
multipliers_L = zeros(T, nvar),
72-
multipliers_U = zeros(T, nvar),
73-
elapsed_time = elapsed_time,
74-
solution = x,
75-
iter = iter,
76-
)
64+
stats = GenericExecutionStats(status, nlp)
65+
set_objective!(stats, fx)
66+
set_residuals!(stats, norm(cx), norm(dual))
67+
z = has_bounds(nlp) ? zeros(T, nvar) : zeros(T, 0)
68+
set_multipliers!(stats, y, z, z)
69+
set_time!(stats, elapsed_time)
70+
set_solution!(stats, x)
71+
set_iter!(stats, iter)
72+
return stats
7773
end

test/test_stats.jl

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
function test_stats()
22
show_statuses()
33
nlp = ADNLPModel(x -> dot(x, x), zeros(2))
4-
stats = GenericExecutionStats(
5-
:first_order,
6-
nlp,
7-
objective = 1.0,
8-
dual_feas = 1e-12,
9-
solution = ones(100),
10-
iter = 10,
11-
solver_specific = Dict(
12-
:matvec => 10,
13-
:dot => 25,
14-
:empty_vec => [],
15-
:small_vec => [2.0; 3.0],
16-
:axpy => 20,
17-
:ray => -1 ./ (1:100),
18-
),
19-
)
4+
stats = GenericExecutionStats(:first_order, nlp)
5+
set_objective!(stats, 1.0)
6+
set_residuals!(stats, 0.0, 1e-12)
7+
set_solution!(stats, ones(2))
8+
set_iter!(stats, 10)
9+
set_solver_specific!(stats, :matvec, 10)
10+
set_solver_specific!(stats, :dot, 25)
11+
set_solver_specific!(stats, :empty_vec, [])
12+
set_solver_specific!(stats, :axpy, 20)
13+
set_solver_specific!(stats, :ray, -1 ./ (1:100))
2014

2115
show(stats)
2216
print(stats)

0 commit comments

Comments
 (0)