Skip to content

Commit d4d4c02

Browse files
committed
Update for regression
1 parent ca1db69 commit d4d4c02

File tree

3 files changed

+41
-116
lines changed

3 files changed

+41
-116
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstraintDomains"
22
uuid = "5800fd60-8556-4464-8d61-84ebf7a0bedb"
33
authors = ["Jean-François Baffier"]
4-
version = "0.3.15"
4+
version = "0.4.0"
55

66
[deps]
77
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"

src/explore.jl

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct ExploreSettings
1+
mutable struct ExploreSettings
22
complete_search_limit::Int
33
max_samplings::Int
44
search::Symbol
@@ -42,22 +42,45 @@ function ExploreSettings(
4242
return ExploreSettings(complete_search_limit, max_samplings, search, solutions_limit)
4343
end
4444

45-
struct ExplorerState{T}
45+
abstract type AbstractExplorerState end
46+
47+
struct CompleteExplorerState{N,T} <: AbstractExplorerState
4648
best::Vector{T}
47-
solutions::Vector{Vector{T}}
48-
non_solutions::Vector{Vector{T}}
49+
solutions::Vector{NTuple{N,T}}
50+
non_solutions::Vector{NTuple{N,T}}
51+
52+
CompleteExplorerState{N,T}() where {N,T} = new{N,T}(Vector{T}(), Vector{NTuple{N,T}}(), Vector{NTuple{N,T}}())
53+
end
4954

50-
ExplorerState{T}() where {T} = new{T}(Vector{T}(), Vector{Vector{T}}(), Vector{Vector{T}}())
55+
function explorer_state(domains, ::Val{:complete})
56+
return CompleteExplorerState{length(domains),Union{map(eltype, domains)...}}()
5157
end
5258

53-
ExplorerState(domains) = ExplorerState{Union{map(eltype, domains)...}}()
59+
struct PartialExplorerState{T} <: AbstractExplorerState
60+
best::Vector{T}
61+
solutions::Set{Vector{T}}
62+
non_solutions::Set{Vector{T}}
63+
64+
PartialExplorerState{T}() where {T} = new{T}(Vector{T}(), Set{Vector{T}}(), Set{Vector{T}}())
65+
end
66+
function explorer_state(domains, ::Val{:partial})
67+
return PartialExplorerState{Union{map(eltype, domains)...}}()
68+
end
5469

55-
mutable struct Explorer{F1<:Function,D<:AbstractDomain,F2<:Union{Function,Nothing},T}
70+
mutable struct Explorer{F1<:Function,D<:AbstractDomain,F2<:Union{Function,Nothing},S<:AbstractExplorerState}
5671
concepts::Dict{Int,Tuple{F1,Vector{Int}}}
5772
domains::Dict{Int,D}
5873
objective::F2
5974
settings::ExploreSettings
60-
state::ExplorerState{T}
75+
state::S
76+
77+
function Explorer(concepts, domains, objective, settings, state)
78+
F1 = isempty(concepts) ? Function : typeof(concepts).parameters[2].parameters[1]
79+
D = isempty(domains) ? AbstractDomain : typeof(domains).parameters[2]
80+
F2 = typeof(objective)
81+
S = typeof(state)
82+
return new{F1,D,F2,S}(concepts, domains, objective, settings, state)
83+
end
6184
end
6285

6386
"""
@@ -88,13 +111,13 @@ function Explorer(
88111
objective=nothing;
89112
settings=ExploreSettings(domains),
90113
)
91-
F1 = isempty(concepts) ? Function : Union{map(c -> typeof(c[1]), concepts)...}
92-
D = isempty(domains) ? AbstractDomain : Union{map(typeof, domains)...}
93-
F2 = typeof(objective)
94-
T = isempty(domains) ? Real : Union{map(eltype, domains)...}
114+
if settings.search == :flexible
115+
settings.search = settings.max_samplings < settings.complete_search_limit ? :complete : :partial
116+
end
117+
state = explorer_state(domains, Val(settings.search))
95118
d_c = Dict(enumerate(concepts))
96119
d_d = Dict(enumerate(domains))
97-
return Explorer{F1,D,F2,T}(d_c, d_d, objective, settings, ExplorerState{T}())
120+
return Explorer(d_c, d_d, objective, settings, state)
98121
end
99122

100123
function Explorer()
@@ -225,15 +248,14 @@ function update_exploration!(explorer, f, c, search=explorer.settings.search)
225248
obj = explorer.objective
226249
sl = search == :complete ? Inf : explorer.settings.solutions_limit
227250

228-
cv = collect(c)
229-
if f(cv)
251+
if f(c)
230252
if length(solutions) < sl
231-
push!(solutions, cv)
253+
push!(solutions, c)
232254
obj !== nothing && (explorer.state.best = argmin(obj, solutions))
233255
end
234256
else
235257
if length(non_sltns) < sl
236-
push!(non_sltns, cv)
258+
push!(non_sltns, c)
237259
end
238260
end
239261
return nothing
@@ -257,8 +279,6 @@ function _explore!(explorer, f, ::Val{:partial};)
257279
config = map(rand, domains)
258280
update_exploration!(explorer, f, config)
259281
end
260-
unique!(explorer.state.solutions)
261-
unique!(explorer.state.non_solutions)
262282
return nothing
263283
end
264284

@@ -295,12 +315,7 @@ function explore!(explorer::Explorer)
295315
f(isempty(vars) ? x : @view x[vars]) for
296316
(f, vars) in explorer.concepts |> values
297317
])
298-
s = explorer.settings
299-
search = s.search
300-
if search == :flexible
301-
search = s.max_samplings < s.complete_search_limit ? :complete : :partial
302-
end
303-
return _explore!(explorer, c, Val(search))
318+
return _explore!(explorer, c, Val(explorer.settings.search))
304319
end
305320

306321

src/old_explore.jl

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)