Skip to content

Commit

Permalink
Make parameters a keyargs list (#33)
Browse files Browse the repository at this point in the history
* Temp save

* Make parameters a list of keywords

* Update CI. Tag new version
  • Loading branch information
Azzaare authored Jan 19, 2023
1 parent 1a1db32 commit 3e65c16
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
name: CI
on:
- push
- pull_request
push:
branches:
- main
tags: '*'
pull_request:
concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand All @@ -11,8 +19,7 @@ jobs:
matrix:
version:
- '1.6'
- '1.7'
- "^1.8.0-0"
- '1.8'
- 'nightly'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstraintDomains"
uuid = "5800fd60-8556-4464-8d61-84ebf7a0bedb"
authors = ["Jean-François Baffier"]
version = "0.3.4"
version = "0.3.5"

[deps]
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"
Expand Down
14 changes: 10 additions & 4 deletions src/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ abstract type ContinuousDomain{T<:Real} <: AbstractDomain end
Intervals{T <: Real} <: ContinuousDomain{T}
An encapsuler to store a vector of `PatternFolds.Interval`. Dynamic changes to `Intervals` are not handled yet.
"""
struct Intervals{T<:Real,L,R} <: ContinuousDomain{T}
domain::Vector{Interval{T,L,R}}
struct Intervals{T<:Real,I<:Interval{T}} <: ContinuousDomain{T}
domain::Vector{I}
end

"""
Expand Down Expand Up @@ -76,8 +76,14 @@ function merge_domains(d1::D, d2::D) where {D<:ContinuousDomain}
end

function intersect_domains(i₁::I1, i₂::I2) where {I1<:Interval,I2<:Interval}
a = a_ismore(i₁, i₂) ? i₁.a : i₂.a
b = b_isless(i₁, i₂) ? i₁.b : i₂.b
if i₁.first > i₂.first
return intersect_domains(i₂, i₁)
end
if i₁.last < i₂.first
return domain()
end
a = i₂.first
b = min(i₁.last, i₂.last)
return Interval(a, b)
end

Expand Down
21 changes: 9 additions & 12 deletions src/explore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,36 @@ function ExploreSettings(
return ExploreSettings(complete_search_limit, max_samplings, search, solutions_limit)
end

function _explore(domains, concept, param, s, ::Val{:partial})
function _explore(domains, f, s, ::Val{:partial})
solutions = Set{Vector{Int}}()
non_sltns = Set{Vector{Int}}()

sl = s.solutions_limit

f = isnothing(param) ? ((x; param = p) -> concept(x)) : concept

for _ in 1:s.max_samplings
length(solutions) sl && length(non_sltns) sl && break
config = map(rand, domains)
c = f(config; param) ? solutions : non_sltns
c = f(config) ? solutions : non_sltns
length(c) < sl && push!(c, config)
end
return solutions, non_sltns
end

function _explore(domains, concept, param, ::ExploreSettings, ::Val{:complete})
function _explore(domains, f, ::ExploreSettings, ::Val{:complete})
solutions = Set{Vector{Int}}()
non_sltns = Set{Vector{Int}}()

f = isnothing(param) ? ((x; param = p) -> concept(x)) : concept

configurations = Base.Iterators.product(map(d -> get_domain(d), domains)...)
foreach(
c -> (cv = collect(c); push!(f(cv; param) ? solutions : non_sltns, cv)),
c -> (cv = collect(c); push!(f(cv) ? solutions : non_sltns, cv)),
configurations,
)
return solutions, non_sltns
end

function _explore(domains, concept, param, s, ::Val{:flexible})
function _explore(domains, f, s, ::Val{:flexible})
search = s.max_samplings < s.complete_search_limit ? :complete : :partial
return _explore(domains, concept, param, s, Val(search))
return _explore(domains, f, s, Val(search))
end

"""
Expand All @@ -67,8 +63,9 @@ Beware that if the density of the solutions in the search space is low, `solutio
function explore(
domains,
concept;
param=nothing,
settings = ExploreSettings(domains),
parameters...
)
return _explore(domains, concept, param, settings, Val(settings.search))
f = x -> concept(x; parameters...)
return _explore(domains, f, settings, Val(settings.search))
end
6 changes: 3 additions & 3 deletions src/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ Base.eltype(::D) where {T, D <: Union{DiscreteDomain{T}, ContinuousDomain{T}}} =

function Base.convert(::Type{Intervals}, d::RangeDomain{T}) where {T <: Real}
a, b = extrema(get_domain(d))
return domain((Float64(a), true), (Float64(b), true))
return domain(Float64(a)..Float64(b))
end

function Base.convert(::Type{RangeDomain}, d::Intervals{T}) where {T <: Real}
i = get_domain(d)[1]
a = Int(value(i, :a) + (closed(i, :a) ? 0 : 1))
b = Int(value(i, :b) - (closed(i, :b) ? 0 : 1))
a = Int(i.first)
b = Int(i.last)
return domain(a:b)
end

0 comments on commit 3e65c16

Please sign in to comment.