Skip to content

Commit

Permalink
parametric list of states; void types; fix test bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tmigot committed Dec 25, 2020
1 parent a73b369 commit 4c2e70a
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 89 deletions.
4 changes: 0 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version = "0.2.5"

[deps]
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand All @@ -19,6 +18,3 @@ julia = "^1.0.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
8 changes: 4 additions & 4 deletions src/State/ListOfStates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ the returned DataFrame still contains all the columns.
see also: add\\_to\\_list!, length, ListStates
"""
function print(list :: AbstractListStates; verbose :: Bool = true, print_sym :: Union{Nothing,Array{Symbol,1}} = nothing)

df = DataFrame()


tab = zeros(0, length(list.list))#Array{Any,2}(undef, length(fieldnames(typeof(list.list[1,1]))))
for k in fieldnames(typeof(list.list[1,1]))
df[!,k] = [getfield(i[1], k) for i in list.list]
tab = vcat(tab, [getfield(i[1], k) for i in list.list]');
end
df = DataFrame(tab)

if print_sym == nothing
verbose && print(df)
Expand Down
19 changes: 14 additions & 5 deletions src/Stopping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ end
export @instate

include("Stopping/StopRemoteControl.jl")
export StopRemoteControl, cheap_stop_remote_control
export AbstractStopRemoteControl, StopRemoteControl, cheap_stop_remote_control

"""
AbstractStoppingMeta
Expand All @@ -131,14 +131,23 @@ AbstractStopping
Abstract type, if specialized stopping were to be implemented they would need to
be subtypes of AbstractStopping
"""
abstract type AbstractStopping{T <: AbstractState,
Pb <: Any,
abstract type AbstractStopping{Pb <: Any,
M <: AbstractStoppingMeta,
SRC <: AbstractStopRemoteControl} end
SRC <: AbstractStopRemoteControl,
T <: AbstractState,
LoS <: AbstractListStates} end

export AbstractStopping
struct VoidStopping{Pb, M, SRC, T, LoS} <: AbstractStopping{Pb, M, SRC, T, LoS} end
function VoidStopping()
return VoidStopping{Any, StoppingMeta, StopRemoteControl, GenericState, VoidListStates}()
end

export AbstractStopping, VoidStopping

import Base.show
function show(io :: IO, stp :: VoidStopping)
println(io, typeof(stp))
end
function show(io :: IO, stp :: AbstractStopping)
println(io, typeof(stp))
println(io, "with the current state $(typeof(stp.current_state)) and metadata $(typeof(stp.meta)).")
Expand Down
64 changes: 32 additions & 32 deletions src/Stopping/GenericStoppingmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Examples:
GenericStopping(pb, x0, rtol = 1e-1)
"""
mutable struct GenericStopping{T, Pb, M, SRC} <: AbstractStopping{T, Pb, M, SRC}
mutable struct GenericStopping{Pb, M, SRC, T, LoS} <: AbstractStopping{Pb, M, SRC, T, LoS}

# Problem
pb :: Pb
Expand All @@ -58,10 +58,10 @@ mutable struct GenericStopping{T, Pb, M, SRC} <: AbstractStopping{T, Pb, M, SRC}
current_state :: T

# Stopping of the main problem, or nothing
main_stp :: Union{AbstractStopping, Nothing}
main_stp :: AbstractStopping

# History of states
listofstates :: Union{ListStates, Nothing}
listofstates :: LoS

# User-specific structure
stopping_user_struct :: Any #this type should be parametric
Expand All @@ -72,14 +72,14 @@ function GenericStopping(pb :: Pb,
meta :: M,
stop_remote :: SRC,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
kwargs...
) where {T <: AbstractState,
Pb <: Any,
) where {Pb <: Any,
M <: AbstractStoppingMeta,
SRC <: AbstractStopRemoteControl}
SRC <: AbstractStopRemoteControl,
T <: AbstractState}

return GenericStopping(pb, meta, stop_remote, current_state,
main_stp, list, stopping_user_struct)
Expand All @@ -88,13 +88,13 @@ end
function GenericStopping(pb :: Pb,
meta :: M,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
kwargs...
) where {T <: AbstractState,
Pb <: Any,
M <: AbstractStoppingMeta}
) where {Pb <: Any,
M <: AbstractStoppingMeta,
T <: AbstractState}

stop_remote = StopRemoteControl() #main_stp == nothing ? StopRemoteControl() : cheap_stop_remote_control()

Expand All @@ -104,11 +104,11 @@ end

function GenericStopping(pb :: Pb,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
kwargs...
) where {T <: AbstractState, Pb <: Any}
) where {Pb <: Any, T <: AbstractState}

meta = StoppingMeta(; kwargs...)
stop_remote = StopRemoteControl() #main_stp == nothing ? StopRemoteControl() : cheap_stop_remote_control()
Expand Down Expand Up @@ -145,9 +145,9 @@ function update_and_start!(stp :: AbstractStopping;
kwargs...)

if stp.stop_remote.cheap_check
update!(stp.current_state; kwargs...)
_smart_update!(stp.current_state; kwargs...)
else
_smart_update!(stp.current_state; kwargs...)
update!(stp.current_state; kwargs...)
end
OK = start!(stp, no_start_opt_check = no_start_opt_check)

Expand Down Expand Up @@ -262,8 +262,8 @@ function reinit!(stp :: AbstractStopping;
stp.meta.nb_of_stop = 0

#reinitialize the list of states
if rlist && (stp.listofstates != nothing)
list = rstate ? nothing : ListStates(stp.current_state)
if rlist && (typeof(stp.listofstates) != VoidListStates)
list = rstate ? VoidListStates() : ListStates(stp.current_state)
end

#reinitialize the state
Expand All @@ -285,11 +285,11 @@ Note: Kwargs are forwarded to the *update!* call.
function update_and_stop!(stp :: AbstractStopping; kwargs...)

if stp.stop_remote.cheap_check
update!(stp.current_state; kwargs...)
OK = stop!(stp)
else
_smart_update!(stp.current_state; kwargs...)
OK = cheap_stop!(stp)
else
update!(stp.current_state; kwargs...)
OK = stop!(stp)
end

return OK
Expand Down Expand Up @@ -326,7 +326,7 @@ The function *stop!* successively calls: *\\_domain\\_check*, *\\_optimality\\_c
Note:
- Kwargs are sent to the *\\_optimality\\_check!* call.
- If listofstates != nothing, call add\\_to\\_list! to update the list of State.
- If listofstates != VoidListStates, call add\\_to\\_list! to update the list of State.
"""
function stop!(stp :: AbstractStopping; kwargs...)

Expand Down Expand Up @@ -357,7 +357,7 @@ function stop!(stp :: AbstractStopping; kwargs...)
src.stalled_check && _stalled_check!(stp, x)
src.iteration_check && _iteration_check!(stp, x)

if src.main_pb_check && stp.main_stp != nothing
if src.main_pb_check && !(typeof(stp.main_stp) <: VoidStopping)
_main_pb_check!(stp, x)
end

Expand All @@ -368,7 +368,7 @@ function stop!(stp :: AbstractStopping; kwargs...)

_add_stop!(stp)

if stp.listofstates != nothing
if typeof(stp.listofstates) != VoidListStates
add_to_list!(stp.listofstates, stp.current_state)
end

Expand All @@ -391,7 +391,7 @@ The function *cheap_stop!* successively calls:
Note:
- Kwargs are sent to the *\\_optimality\\_check!* call.
- If listofstates != nothing, call add\\_to\\_list! to update the list of State.
- If listofstates != VoidListStates, call add\\_to\\_list! to update the list of State.
"""
function cheap_stop!(stp :: AbstractStopping; kwargs...)

Expand All @@ -415,7 +415,7 @@ function cheap_stop!(stp :: AbstractStopping; kwargs...)

_add_stop!(stp)

if stp.listofstates != nothing
if typeof(stp.listofstates) != VoidListStates
add_to_list!(stp.listofstates, stp.current_state)
end

Expand Down Expand Up @@ -508,7 +508,7 @@ function _resources_check!(stp :: AbstractStopping,
end

"""
\\_main\\_pb\\_check!: check the resources and the time of the upper problem (if main_stp != nothing).
\\_main\\_pb\\_check!: check the resources and the time of the upper problem (if main_stp != VoidStopping.
`_main_pb_check!(:: AbstractStopping, :: Union{Number, AbstractVector})`
Expand All @@ -526,7 +526,7 @@ function _main_pb_check!(stp :: AbstractStopping,
_resources_check!(stp.main_stp, x)
resources = stp.main_stp.meta.resources

if stp.main_stp.main_stp != nothing
if !(typeof(stp.main_stp.main_stp) <: VoidStopping)
_main_pb_check!(stp.main_stp, x)
main_main_pb = stp.main_stp.meta.main_pb
else
Expand Down Expand Up @@ -603,8 +603,8 @@ end
`_optimality_check(:: AbstractStopping; kwargs...)`
"""
function _optimality_check(stp :: AbstractStopping{T, Pb, M, SRC};
kwargs...) where {T, Pb, M, SRC}
function _optimality_check(stp :: AbstractStopping{Pb, M, SRC, T, LoS};
kwargs...) where {Pb, M, SRC, T, LoS}

setfield!(stp.current_state, :current_score,
stp.meta.optimality_check(stp.pb, stp.current_state; kwargs...))
Expand Down
18 changes: 9 additions & 9 deletions src/Stopping/LineSearchStoppingmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Input :
See also GenericStopping, NLPStopping, LSAtT
"""
mutable struct LS_Stopping{Pb, M, SRC} <: AbstractStopping{LSAtT, Pb, M, SRC}
mutable struct LS_Stopping{Pb, M, SRC, LoS} <: AbstractStopping{Pb, M, SRC, LSAtT, LoS}
# problem
pb :: Pb

Expand All @@ -46,10 +46,10 @@ mutable struct LS_Stopping{Pb, M, SRC} <: AbstractStopping{LSAtT, Pb, M, SRC}
current_state :: LSAtT

# Stopping of the main problem, or nothing
main_stp :: Union{AbstractStopping, Nothing}
main_stp :: AbstractStopping

# History of states
listofstates :: Union{ListStates, Nothing}
listofstates :: LoS

# User-specific structure
stopping_user_struct :: Any
Expand All @@ -60,8 +60,8 @@ function LS_Stopping(pb :: Pb,
meta :: M,
stop_remote :: SRC,
current_state :: LSAtT;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
) where {Pb <: Any,
M <: AbstractStoppingMeta,
Expand All @@ -74,8 +74,8 @@ end
function LS_Stopping(pb :: Pb,
meta :: M,
current_state :: LSAtT;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
) where {Pb <: Any, M <: AbstractStoppingMeta}

Expand All @@ -87,8 +87,8 @@ end

function LS_Stopping(pb :: Pb,
current_state :: LSAtT;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
kwargs...) where {Pb <: Any}

Expand Down
31 changes: 17 additions & 14 deletions src/Stopping/LinearAlgebraStopping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ There is additional constructors:
See also GenericStopping, NLPStopping, LS\\_Stopping, linear\\_system\\_check, normal\\_equation\\_check
"""
mutable struct LAStopping{T, Pb, M, SRC} <: AbstractStopping{T, Pb, M, SRC}
mutable struct LAStopping{Pb, M, SRC, T, LoS} <: AbstractStopping{Pb, M, SRC, T, LoS}

# problem
pb :: Pb
Expand All @@ -52,9 +52,9 @@ See also GenericStopping, NLPStopping, LS\\_Stopping, linear\\_system\\_check, n
# current state of the problem
current_state :: T
# Stopping of the main problem, or nothing
main_stp :: Union{AbstractStopping, Nothing}
main_stp :: AbstractStopping
# History of states
listofstates :: Union{ListStates, Nothing}
listofstates :: LoS
# User-specific structure
stopping_user_struct :: Any

Expand All @@ -67,14 +67,14 @@ See also GenericStopping, NLPStopping, LS\\_Stopping, linear\\_system\\_check, n
meta :: M,
stop_remote :: SRC,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
zero_start :: Bool = false
) where {T <: AbstractState,
Pb <: Any,
) where {Pb <: Any,
M <: AbstractStoppingMeta,
SRC <: AbstractStopRemoteControl}
SRC <: AbstractStopRemoteControl,
T <: AbstractState}

return LAStopping(pb, meta, stop_remote, current_state,
main_stp, list, stopping_user_struct, zero_start)
Expand All @@ -83,10 +83,13 @@ See also GenericStopping, NLPStopping, LS\\_Stopping, linear\\_system\\_check, n
function LAStopping(pb :: Pb,
meta :: M,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
zero_start :: Bool = false) where {T <: AbstractState, Pb <: Any, M <: AbstractStoppingMeta}
zero_start :: Bool = false
) where {Pb <: Any,
M <: AbstractStoppingMeta,
T <: AbstractState}

stop_remote = StopRemoteControl() #main_stp == nothing ? StopRemoteControl() : cheap_stop_remote_control()

Expand All @@ -96,11 +99,11 @@ See also GenericStopping, NLPStopping, LS\\_Stopping, linear\\_system\\_check, n

function LAStopping(pb :: Pb,
current_state :: T;
main_stp :: Union{AbstractStopping, Nothing} = nothing,
list :: Union{ListStates, Nothing} = nothing,
main_stp :: AbstractStopping = VoidStopping(),
list :: AbstractListStates = VoidListStates(),
stopping_user_struct :: Any = nothing,
zero_start :: Bool = false,
kwargs...) where {T <: AbstractState, Pb <: Any}
kwargs...) where {Pb <: Any, T <: AbstractState}

if :max_cntrs in keys(kwargs)
mcntrs = kwargs[:max_cntrs]
Expand Down
Loading

0 comments on commit 4c2e70a

Please sign in to comment.