forked from SolverStoppingJulia/Stopping.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLPAtXmod.jl
139 lines (112 loc) · 4.88 KB
/
NLPAtXmod.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Type: NLPAtX
Methods: update!, reinit!
NLPAtX contains the information concerning a nonlinear problem at
the iteration x.
min_{x ∈ ℜⁿ} f(x) subject to lcon <= c(x) <= ucon, lvar <= x <= uvar.
Basic information is:
- x : the current candidate for solution to our original problem
- fx : which is the funciton evaluation at x
- gx : which is the gradient evaluation at x
- Hx : which is the hessian representation at x
- mu : Lagrange multiplier of the bounds constraints
- cx : evaluation of the constraint function at x
- Jx : jacobian matrix of the constraint function at x
- lambda : Lagrange multiplier of the constraints
- current_time : time
- current_score : score
- evals : number of evaluations of the function (import the type NLPModels.Counters)
Note: * by default, unknown entries are set to nothing (except evals).
* All these information (except for x and lambda) are optionnal and need to be update when
required. The update is done trhough the update! function.
* x and lambda are mandatory entries. If no constraints lambda = [].
* The constructor check the size of the entries.
"""
mutable struct NLPAtX <: AbstractState
#Unconstrained State
x :: AbstractVector # current point
fx :: FloatVoid # objective function
gx :: Iterate # gradient size: x
Hx :: MatrixType # hessian size: |x| x |x|
#Bounds State
mu :: Iterate # Lagrange multipliers with bounds size of |x|
#Constrained State
cx :: Iterate # vector of constraints lc <= c(x) <= uc
Jx :: MatrixType # jacobian matrix, size: |lambda| x |x|
lambda :: AbstractVector # Lagrange multipliers
#Resources State
current_time :: FloatVoid
current_score :: FloatVoid
evals :: Counters
function NLPAtX(x :: AbstractVector,
lambda :: AbstractVector;
fx :: FloatVoid = nothing,
gx :: Iterate = nothing,
Hx :: MatrixType = nothing,
mu :: Iterate = nothing,
cx :: Iterate = nothing,
Jx :: MatrixType = nothing,
current_time :: FloatVoid = nothing,
current_score :: FloatVoid = nothing,
evals :: Counters = Counters())
_size_check(x, lambda, fx, gx, Hx, mu, cx, Jx)
return new(x, fx, gx, Hx, mu, cx, Jx, lambda, current_time, current_score, evals)
end
end
"""
An additional constructor for unconstrained problems
"""
function NLPAtX(x :: AbstractVector;
fx :: FloatVoid = nothing,
gx :: Iterate = nothing,
Hx :: MatrixType = nothing,
mu :: Iterate = nothing,
current_time :: FloatVoid = nothing,
current_score :: FloatVoid = nothing,
evals :: Counters = Counters())
_size_check(x, zeros(0), fx, gx, Hx, mu, nothing, nothing)
return NLPAtX(x, zeros(0), fx = fx, gx = gx,
Hx = Hx, mu = mu, current_time = current_time,
current_score = current_score, evals = evals)
end
"""
reinit!: function that set all the entries at void except the mandatory x
Warning: if x, lambda or evals are given as a keyword argument they will be
prioritized over the existing x, lambda and the default Counters.
"""
function reinit!(stateatx :: NLPAtX, x :: AbstractVector, l :: AbstractVector; kwargs...)
for k ∈ fieldnames(typeof(stateatx))
if !(k ∈ [:x,:lambda,:evals]) setfield!(stateatx, k, nothing) end
end
return update!(stateatx; x=x, lambda = l, evals = Counters(), kwargs...)
end
"""
reinit!: short version of reinit! reusing the x in the state
Warning: if x, lambda or evals are given as a keyword argument they will be
prioritized over the existing x, lambda and the default Counters.
"""
function reinit!(stateatx :: NLPAtX; kwargs...)
return reinit!(stateatx, stateatx.x, stateatx.lambda; kwargs...)
end
"""
_size_check!: check the size of the entries in the State
"""
function _size_check(x, lambda, fx, gx, Hx, mu, cx, Jx)
if gx != nothing && length(gx) != length(x)
throw(error("Wrong size of gx in the NLPAtX."))
end
if Hx != nothing && size(Hx) != (length(x), length(x))
throw(error("Wrong size of Hx in the NLPAtX."))
end
if mu != nothing && length(mu) != length(x)
throw(error("Wrong size of mu in the NLPAtX."))
end
if lambda != zeros(0)
if cx != nothing && length(cx) != length(lambda)
throw(error("Wrong size of cx in the NLPAtX."))
end
if Jx != nothing && size(Jx) != (length(lambda), length(x))
throw(error("Wrong size of Jx in the NLPAtX."))
end
end
end