Skip to content

Commit fe64c64

Browse files
committed
better handling list of problems
1 parent 6b7d923 commit fe64c64

File tree

5 files changed

+61
-67
lines changed

5 files changed

+61
-67
lines changed

docs/src/assets/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ version = "1.1.2"
10671067
deps = ["ADNLPModels", "CTBase", "CTDirect", "CTModels", "DocStringExtensions", "ExaModels", "OrderedCollections", "SolverCore"]
10681068
path = "/Users/ocots/Research/logiciels/dev/control-toolbox/OptimalControlProblems"
10691069
uuid = "59046045-fb9c-4c23-964f-ff0a25704f96"
1070-
version = "0.2.2"
1070+
version = "1.0.0"
10711071
weakdeps = ["JuMP", "OptimalControl"]
10721072

10731073
[deps.OptimalControlProblems.extensions]

ext/JuMPModels.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ using OrderedCollections: OrderedDict
2222
# include problems files
2323
rel_path_problems = "JuMPModels"
2424
path = joinpath(dirname(@__FILE__), rel_path_problems)
25-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
26-
for file in files
27-
if file "JuMPModels.jl"
28-
include(joinpath(rel_path_problems, file))
29-
end
25+
list_of_problems = OptimalControlProblems.problems()
26+
for problem in list_of_problems
27+
include(joinpath(rel_path_problems, "$(problem).jl"))
3028
end
3129

3230
"""

ext/OptimalControlModels.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ using OptimalControl
55
using DocStringExtensions
66
using OrderedCollections: OrderedDict
77

8+
# list of problems
9+
list_of_problems = OptimalControlProblems.problems()
10+
811
# include problems files
912
rel_path_problems = "OptimalControlModels"
1013
path = joinpath(dirname(@__FILE__), rel_path_problems)
11-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
12-
for file in files
13-
include(joinpath(rel_path_problems, file))
14+
for problem in list_of_problems
15+
include(joinpath(rel_path_problems, "$(problem).jl"))
1416
end
1517

1618
# include problems files (_s versions)
1719
rel_path_problems = "OptimalControlModels_s"
1820
path = joinpath(dirname(@__FILE__), rel_path_problems)
19-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
20-
for file in files
21-
include(joinpath(rel_path_problems, file))
21+
for problem in list_of_problems
22+
include(joinpath(rel_path_problems, "$(problem)_s.jl"))
2223
end
2324

2425
end

src/OptimalControlProblems.jl

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import ADNLPModels: ADNLPModels, ADNLPModel
2323

2424
# -----------------
2525
# SHOULD NO BE HERE ("triiiiiiiiiiiiiiit !!!" - bruit de sifflet)
26-
#nlp_model(docp::CTDirect.DOCP) = docp.nlp
27-
#ocp_model(docp::CTDirect.DOCP) = docp.ocp
2826
function build_ocp_solution(
2927
docp::CTDirect.DOCP, nlp_solution::SolverCore.AbstractExecutionStats
3028
)
@@ -65,13 +63,38 @@ struct OptimalControlBackend <: AbstractModelBackend end
6563
# weak dependencies
6664
weakdeps = Dict(OptimalControlBackend => :OptimalControl, JuMPBackend => :JuMP)
6765

68-
# path to problems
69-
path = joinpath(dirname(@__FILE__), "..", "ext", "MetaData")
66+
# Create the list of problems
67+
function make_list_of_problems()
7068

71-
# ------- Problem Definitions -------
72-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
73-
for file in files
74-
problem = Symbol(file[1:(end - 3)])
69+
# path to problems
70+
path = joinpath(dirname(@__FILE__), "..", "ext", "MetaData")
71+
72+
# ------- Problem Definitions -------
73+
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
74+
75+
# collect all the problems
76+
list_of_problems = Symbol[]
77+
for file in files
78+
problem = Symbol(file[1:(end - 3)])
79+
push!(list_of_problems, problem)
80+
end
81+
82+
# exclude the following problems
83+
problems_to_exclude = [
84+
:bioreactor,
85+
:cart_pendulum,
86+
:dielectrophoretic_particle,
87+
:moonlander,
88+
]
89+
list_of_problems = setdiff(list_of_problems, problems_to_exclude)
90+
91+
return tuple(list_of_problems...), path
92+
93+
end
94+
95+
const LIST_OF_PROBLEMS, METADATA_PATH = make_list_of_problems()
96+
97+
for problem in LIST_OF_PROBLEMS
7598
problem_s = Symbol(problem, :_s)
7699

77100
# Build the docstring string explicitly here
@@ -113,32 +136,30 @@ for file in files
113136
end
114137

115138
# ------- Problem Metadata -------
116-
for file in files
117-
include(joinpath(path, file))
139+
for problem in LIST_OF_PROBLEMS
140+
include(joinpath(METADATA_PATH, "$problem.jl"))
118141
end
119-
const number_of_problems = length(files)
120142

121-
const infos = [
143+
const METADATA_INFOS = [
122144
:grid_size
123145
:parameters
124146
]
125147

126-
const types = [
148+
const METADATA_TYPES = [
127149
Int,
128150
Union{Nothing,NamedTuple},
129151
]
130152

131-
const metadata_storage = OrderedDict()
153+
const METADATA_STORAGE = OrderedDict()
132154

133-
for i in 1:number_of_problems
134-
file_key = Symbol(split(files[i], ".")[1])
135-
metadata_storage[file_key] = OrderedDict()
136-
for (data, T) in zip(infos, types)
137-
value = eval(Meta.parse("$(file_key)_meta"))[data]
155+
for problem in LIST_OF_PROBLEMS
156+
METADATA_STORAGE[problem] = OrderedDict()
157+
for (data, T) in zip(METADATA_INFOS, METADATA_TYPES)
158+
value = eval(Meta.parse("$(problem)_meta"))[data]
138159
if !(value isa T)
139160
error("Type mismatch: Expected $(T) for $(data), but got $(typeof(value))")
140161
end
141-
metadata_storage[file_key][data] = value
162+
METADATA_STORAGE[problem][data] = value
142163
end
143164
end
144165

@@ -153,7 +174,7 @@ Return the dictionary containing the metadata of all available optimal control p
153174
julia> metadata()
154175
```
155176
"""
156-
metadata() = metadata_storage
177+
metadata() = METADATA_STORAGE
157178

158179
"""
159180
$(TYPEDSIGNATURES)
@@ -189,8 +210,8 @@ julia> data[:grid_size]
189210
```
190211
"""
191212
function metadata(problem::Symbol)
192-
!(problem keys(metadata_storage)) && throw(CTBase.IncorrectArgument("There is no problem named $problem in metadata. To get the list of available problems, make julia> metadata()"))
193-
return metadata_storage[problem]
213+
!(problem keys(METADATA_STORAGE)) && throw(CTBase.IncorrectArgument("There is no problem named $problem in metadata. To get the list of available problems, make julia> metadata()"))
214+
return METADATA_STORAGE[problem]
194215
end
195216

196217
# ------- Available Problems Function -------
@@ -211,27 +232,7 @@ julia> OptimalControlProblems.problems()
211232
```
212233
"""
213234
function problems()::Vector{Symbol}
214-
215-
#
216-
list_of_problems = Symbol[]
217-
218-
# collect all the problems
219-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
220-
for file in files
221-
problem = Symbol(file[1:(end - 3)])
222-
push!(list_of_problems, problem)
223-
end
224-
225-
# # exclude the following problems
226-
problems_to_exclude = [
227-
:bioreactor,
228-
:cart_pendulum,
229-
:dielectrophoretic_particle,
230-
:moonlander,
231-
]
232-
list_of_problems = setdiff(list_of_problems, problems_to_exclude)
233-
234-
return list_of_problems
235+
return Symbol[LIST_OF_PROBLEMS...]
235236
end
236237

237238
"""

test/runtests.jl

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,14 @@ const MAX_ITER = 1000
2323
const MAX_WALL_TIME = 500.0
2424

2525
# Collect all the problems from OptimalControlProblems
26-
path = joinpath(dirname(@__FILE__), "..", "ext", "MetaData")
27-
list_of_problems = []
28-
files = filter(x -> x[(end - 2):end] == ".jl", readdir(path))
29-
for file in files
30-
problem = Symbol(file[1:(end - 3)])
31-
push!(list_of_problems, problem)
32-
end
26+
list_of_problems = OptimalControlProblems.problems()
3327

3428
# Remove from the tests the following problems
3529
# problems_to_exclude = [
36-
# :bioreactor,
37-
# :cart_pendulum,
38-
# :dielectrophoretic_particle,
39-
# :moonlander,
30+
# :bioreactor, # no need to remove here since already removed in OptimalControlProblems.jl
31+
# :cart_pendulum, # no need to remove here since already removed in OptimalControlProblems.jl
32+
# :dielectrophoretic_particle, # no need to remove here since already removed in OptimalControlProblems.jl
33+
# :moonlander, # no need to remove here since already removed in OptimalControlProblems.jl
4034
# ]
4135
# list_of_problems = setdiff(list_of_problems, problems_to_exclude)
4236

0 commit comments

Comments
 (0)