Skip to content

Commit 1e664ce

Browse files
authored
New breaking release (#71)
* Better test * Improve performances for complete search * Perf regressino debug * Update for regression * Update compat * Format
1 parent e6d28fc commit 1e664ce

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
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"
@@ -11,7 +11,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1111
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"
1212

1313
[compat]
14-
ConstraintCommons = "0.2"
14+
ConstraintCommons = "0.3"
1515
Intervals = "1"
1616
PatternFolds = "0.2"
1717
StatsBase = "0.34"

src/common.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Convert various arguments into valid domains format.
6464
to_domains(domain_sizes::Vector{Int}) = map(ds -> domain(0:ds), domain_sizes)
6565

6666
function to_domains(X, ds::Int = δ_extrema(X) + 1)
67-
d = domain(0:ds-1)
67+
d = domain(0:(ds-1))
6868
return fill(d, length(first(X)))
6969
end
7070

src/explore.jl

Lines changed: 51 additions & 24 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,52 @@ 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
48+
best::Vector{T}
49+
solutions::Vector{NTuple{N,T}}
50+
non_solutions::Vector{NTuple{N,T}}
51+
52+
CompleteExplorerState{N,T}() where {N,T} =
53+
new{N,T}(Vector{T}(), Vector{NTuple{N,T}}(), Vector{NTuple{N,T}}())
54+
end
55+
56+
function explorer_state(domains, ::Val{:complete})
57+
return CompleteExplorerState{length(domains),Union{map(eltype, domains)...}}()
58+
end
59+
60+
struct PartialExplorerState{T} <: AbstractExplorerState
4661
best::Vector{T}
4762
solutions::Set{Vector{T}}
4863
non_solutions::Set{Vector{T}}
4964

50-
ExplorerState{T}() where {T} = new{T}([], Set{Vector{T}}(), Set{Vector{T}}())
65+
PartialExplorerState{T}() where {T} =
66+
new{T}(Vector{T}(), Set{Vector{T}}(), Set{Vector{T}}())
67+
end
68+
function explorer_state(domains, ::Val{:partial})
69+
return PartialExplorerState{Union{map(eltype, domains)...}}()
5170
end
5271

53-
ExplorerState(domains) = ExplorerState{Union{map(eltype, domains)...}}()
54-
55-
mutable struct Explorer{F1<:Function,D<:AbstractDomain,F2<:Union{Function,Nothing},T}
72+
mutable struct Explorer{
73+
F1<:Function,
74+
D<:AbstractDomain,
75+
F2<:Union{Function,Nothing},
76+
S<:AbstractExplorerState,
77+
}
5678
concepts::Dict{Int,Tuple{F1,Vector{Int}}}
5779
domains::Dict{Int,D}
5880
objective::F2
5981
settings::ExploreSettings
60-
state::ExplorerState{T}
82+
state::S
83+
84+
function Explorer(concepts, domains, objective, settings, state)
85+
F1 = isempty(concepts) ? Function : typeof(concepts).parameters[2].parameters[1]
86+
D = isempty(domains) ? AbstractDomain : typeof(domains).parameters[2]
87+
F2 = typeof(objective)
88+
S = typeof(state)
89+
return new{F1,D,F2,S}(concepts, domains, objective, settings, state)
90+
end
6191
end
6292

6393
"""
@@ -88,13 +118,14 @@ function Explorer(
88118
objective = nothing;
89119
settings = ExploreSettings(domains),
90120
)
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)...}
121+
if settings.search == :flexible
122+
settings.search =
123+
settings.max_samplings < settings.complete_search_limit ? :complete : :partial
124+
end
125+
state = explorer_state(domains, Val(settings.search))
95126
d_c = Dict(enumerate(concepts))
96127
d_d = Dict(enumerate(domains))
97-
return Explorer{F1,D,F2,T}(d_c, d_d, objective, settings, ExplorerState{T}())
128+
return Explorer(d_c, d_d, objective, settings, state)
98129
end
99130

100131
function Explorer()
@@ -225,15 +256,14 @@ function update_exploration!(explorer, f, c, search = explorer.settings.search)
225256
obj = explorer.objective
226257
sl = search == :complete ? Inf : explorer.settings.solutions_limit
227258

228-
cv = collect(c)
229-
if f(cv)
259+
if f(c)
230260
if length(solutions) < sl
231-
push!(solutions, cv)
261+
push!(solutions, c)
232262
obj !== nothing && (explorer.state.best = argmin(obj, solutions))
233263
end
234264
else
235265
if length(non_sltns) < sl
236-
push!(non_sltns, cv)
266+
push!(non_sltns, c)
237267
end
238268
end
239269
return nothing
@@ -261,7 +291,9 @@ function _explore!(explorer, f, ::Val{:partial};)
261291
end
262292

263293
function _explore!(explorer, f, ::Val{:complete})
264-
C = Base.Iterators.product(map(d -> get_domain(d), explorer.domains |> values)...)
294+
C = Base.Iterators.product(
295+
Iterators.map(d -> get_domain(d), explorer.domains |> values)...,
296+
)
265297
foreach(c -> update_exploration!(explorer, f, c, :complete), C)
266298
return nothing
267299
end
@@ -293,12 +325,7 @@ function explore!(explorer::Explorer)
293325
f(isempty(vars) ? x : @view x[vars]) for
294326
(f, vars) in explorer.concepts |> values
295327
])
296-
s = explorer.settings
297-
search = s.search
298-
if search == :flexible
299-
search = s.max_samplings < s.complete_search_limit ? :complete : :partial
300-
end
301-
return _explore!(explorer, c, Val(search))
328+
return _explore!(explorer, c, Val(explorer.settings.search))
302329
end
303330

304331

@@ -346,5 +373,5 @@ end
346373
@test length(X) == factorial(4)
347374
@test length(X̅) == 4^4 - factorial(4)
348375

349-
explorer = ConstraintDomains.Explorer()
376+
explorer = ConstraintDomains.Explorer([(allunique, 1:4)], domains)
350377
end

0 commit comments

Comments
 (0)