Skip to content

Commit 3994f15

Browse files
authored
Update doc for JuliaConstraints website (#62)
* Update doc * Fix format
1 parent cf4fe62 commit 3994f15

File tree

1 file changed

+181
-33
lines changed

1 file changed

+181
-33
lines changed

src/explore.jl

Lines changed: 181 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ struct ExploreSettings
66
end
77

88
"""
9-
ExploreSettings(domains;
10-
complete_search_limit = 10^6,
11-
max_samplings = sum(domain_size, domains),
12-
search = :flexible,
13-
solutions_limit = floor(Int, sqrt(max_samplings)))
9+
ExploreSettings(
10+
domains;
11+
complete_search_limit = 10^6,
12+
max_samplings = sum(domain_size, domains),
13+
search = :flexible,
14+
solutions_limit = floor(Int, sqrt(max_samplings)),
15+
)
1416
15-
Create an `ExploreSettings` object to configure the exploration of a search space composed of a collection of domains.
17+
Create settings for the exploration of a search space composed by a collection of domains.
1618
1719
# Arguments
18-
- `domains`: A collection of domains to be explored.
19-
- `complete_search_limit`: An integer specifying the maximum limit for complete search iterations. Default is 10^6.
20-
- `max_samplings`: An integer specifying the maximum number of samplings. Default is the sum of domain sizes.
21-
- `search`: A symbol indicating the type of search to perform. Default is `:flexible`.
22-
- `solutions_limit`: An integer specifying the limit on the number of solutions. Default is the floor of the square root of `max_samplings`.
20+
- `domains`: A collection of domains representing the search space.
21+
- `complete_search_limit`: Maximum size of the search space for complete search.
22+
- `max_samplings`: Maximum number of samples to take during partial search.
23+
- `search`: Search strategy (`:flexible`, `:complete`, or `:partial`).
24+
- `solutions_limit`: Maximum number of solutions to store.
2325
2426
# Returns
25-
- `ExploreSettings` object with the specified settings.
27+
An `ExploreSettings` object.
28+
29+
# Example
30+
```julia
31+
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
32+
settings = ExploreSettings(domains, search = :complete)
33+
```
2634
"""
2735
function ExploreSettings(
2836
domains;
@@ -50,21 +58,43 @@ mutable struct Explorer{F1<:Function,D<:AbstractDomain,F2<:Union{Function,Nothin
5058
objective::F2
5159
settings::ExploreSettings
5260
state::ExplorerState{T}
61+
end
5362

54-
function Explorer(
55-
concepts,
56-
domains,
57-
objective = nothing;
58-
settings = ExploreSettings(domains),
59-
)
60-
F1 = isempty(concepts) ? Function : Union{map(c -> typeof(c[1]), concepts)...}
61-
D = isempty(domains) ? AbstractDomain : Union{map(typeof, domains)...}
62-
F2 = typeof(objective)
63-
T = isempty(domains) ? Real : Union{map(eltype, domains)...}
64-
d_c = Dict(enumerate(concepts))
65-
d_d = Dict(enumerate(domains))
66-
return new{F1,D,F2,T}(d_c, d_d, objective, settings, ExplorerState{T}())
67-
end
63+
"""
64+
Explorer(concepts, domains, objective = nothing; settings = ExploreSettings(domains))
65+
66+
Create an Explorer object for searching a constraint satisfaction problem space.
67+
68+
# Arguments
69+
- `concepts`: A collection of tuples, each containing a concept function and its associated variable indices.
70+
- `domains`: A collection of domains representing the search space.
71+
- `objective`: An optional objective function for optimization problems.
72+
- `settings`: An `ExploreSettings` object to configure the exploration process.
73+
74+
# Returns
75+
An `Explorer` object ready for exploration.
76+
77+
# Example
78+
```julia
79+
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
80+
concepts = [(sum, [1, 2])]
81+
objective = x -> x[1] + x[2]
82+
explorer = Explorer(concepts, domains, objective)
83+
```
84+
"""
85+
function Explorer(
86+
concepts,
87+
domains,
88+
objective = nothing;
89+
settings = ExploreSettings(domains),
90+
)
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)...}
95+
d_c = Dict(enumerate(concepts))
96+
d_d = Dict(enumerate(domains))
97+
return Explorer{F1,D,F2,T}(d_c, d_d, objective, settings, ExplorerState{T}())
6898
end
6999

70100
function Explorer()
@@ -75,28 +105,118 @@ function Explorer()
75105
return Explorer(concepts, domains, objective; settings)
76106
end
77107

108+
"""
109+
push!(explorer::Explorer, concept::Tuple{Function,Vector{Int}})
110+
111+
Add a new concept to the `Explorer` object.
112+
113+
# Arguments
114+
- `explorer`: The `Explorer` object to modify.
115+
- `concept`: A tuple containing a concept function and its associated variable indices.
116+
117+
# Returns
118+
The key (index) of the newly added concept.
119+
120+
# Example
121+
```julia
122+
explorer = Explorer()
123+
key = push!(explorer, (sum, [1, 2]))
124+
```
125+
"""
78126
function Base.push!(explorer::Explorer, concept::Tuple{Function,Vector{Int}})
79127
max_key = maximum(keys(explorer.concepts); init = 0)
80128
explorer.concepts[max_key+1] = concept
81129
return max_key + 1
82130
end
83131

132+
"""
133+
delete_concept!(explorer::Explorer, key::Int)
134+
135+
Remove a concept from the `Explorer` object by its key.
136+
137+
# Arguments
138+
- `explorer`: The `Explorer` object to modify.
139+
- `key`: The key (index) of the concept to remove.
140+
141+
# Returns
142+
Nothing. The `Explorer` object is modified in-place.
143+
144+
# Example
145+
```julia
146+
explorer = Explorer([(sum, [1, 2])], [domain([1, 2, 3])])
147+
delete_concept!(explorer, 1)
148+
```
149+
"""
84150
function delete_concept!(explorer::Explorer, key::Int)
85151
delete!(explorer.concepts, key)
86152
return nothing
87153
end
88154

155+
"""
156+
push!(explorer::Explorer, domain::AbstractDomain)
157+
158+
Add a new domain to the `Explorer` object.
159+
160+
# Arguments
161+
- `explorer`: The `Explorer` object to modify.
162+
- `domain`: An `AbstractDomain` object to add to the search space.
163+
164+
# Returns
165+
The key (index) of the newly added domain.
166+
167+
# Example
168+
```julia
169+
explorer = Explorer()
170+
key = push!(explorer, domain([1, 2, 3]))
171+
```
172+
"""
89173
function Base.push!(explorer::Explorer, domain::AbstractDomain)
90174
max_key = maximum(keys(explorer.domains); init = 0)
91175
explorer.domains[max_key+1] = domain
92176
return max_key + 1
93177
end
94178

179+
"""
180+
delete_domain!(explorer::Explorer, key::Int)
181+
182+
Remove a domain from the `Explorer` object by its key.
183+
184+
# Arguments
185+
- `explorer`: The `Explorer` object to modify.
186+
- `key`: The key (index) of the domain to remove.
187+
188+
# Returns
189+
Nothing. The `Explorer` object is modified in-place.
190+
191+
# Example
192+
```julia
193+
explorer = Explorer([], [domain([1, 2, 3])])
194+
delete_domain!(explorer, 1)
195+
```
196+
"""
95197
function delete_domain!(explorer::Explorer, key::Int)
96198
delete!(explorer.domains, key)
97199
return nothing
98200
end
99201

202+
"""
203+
set!(explorer::Explorer, objective::Function)
204+
205+
Set the objective function for the `Explorer` object.
206+
207+
# Arguments
208+
- `explorer`: The `Explorer` object to modify.
209+
- `objective`: A function to use as the objective for optimization.
210+
211+
# Returns
212+
Nothing. The `Explorer` object is modified in-place.
213+
214+
# Example
215+
```julia
216+
explorer = Explorer()
217+
set!(explorer, x -> sum(x))
218+
```
219+
"""
100220
set!(explorer::Explorer, objective::Function) = explorer.objective = objective
101221

102222
function update_exploration!(explorer, f, c, search = explorer.settings.search)
@@ -122,7 +242,7 @@ end
122242
"""
123243
_explore(args...)
124244
125-
Internals of the `explore` function. Behavior is automatically adjusted on the kind of exploration: `:flexible`, `:complete`, `:partial`.
245+
Internals of the `explore!` function. Behavior is automatically adjusted on the kind of exploration: `:flexible`, `:complete`, `:partial`.
126246
"""
127247
function _explore!(explorer, f, ::Val{:partial})
128248
sl = explorer.settings.solutions_limit
@@ -146,6 +266,27 @@ function _explore!(explorer, f, ::Val{:complete})
146266
return nothing
147267
end
148268

269+
"""
270+
explore!(explorer::Explorer)
271+
272+
Perform exploration on the search space defined by the `Explorer` object.
273+
274+
This function explores the search space according to the settings specified in the `Explorer` object.
275+
It updates the `Explorer`'s state with found solutions and non-solutions.
276+
277+
# Arguments
278+
- `explorer`: An `Explorer` object containing the problem definition and exploration settings.
279+
280+
# Returns
281+
Nothing. The `Explorer`'s state is updated in-place.
282+
283+
# Example
284+
```julia
285+
explorer = Explorer(concepts, domains)
286+
explore!(explorer)
287+
println("Solutions found: ", length(explorer.state.solutions))
288+
```
289+
"""
149290
function explore!(explorer::Explorer)
150291
c =
151292
x -> all([
@@ -160,19 +301,26 @@ function explore!(explorer::Explorer)
160301
return _explore!(explorer, c, Val(search))
161302
end
162303

304+
163305
"""
164306
explore(domains, concept; settings = ExploreSettings(domains), parameters...)
165307
166-
Search (a part of) a search space and return a pair of vectors of configurations: `(solutions, non_solutions)`. The exploration behavior is determined based on the `settings`.
308+
Explore a search space defined by domains and a concept.
167309
168310
# Arguments
169-
- `domains`: A collection of domains to be explored.
170-
- `concept`: The concept representing the constraint to be targeted.
171-
- `settings`: An optional `ExploreSettings` object to configure the exploration. Default is `ExploreSettings(domains)`.
172-
- `parameters...`: Additional parameters for the `concept`.
311+
- `domains`: A collection of domains representing the search space.
312+
- `concept`: The concept function defining the constraint.
313+
- `settings`: An `ExploreSettings` object to configure the exploration process.
314+
- `parameters`: Additional parameters to pass to the concept function.
173315
174316
# Returns
175-
- A tuple of sets: `(solutions, non_solutions)`.
317+
A tuple containing two sets: (solutions, non_solutions).
318+
319+
# Example
320+
```julia
321+
domains = [domain([1, 2, 3]), domain([4, 5, 6])]
322+
solutions, non_solutions = explore(domains, allunique)
323+
```
176324
"""
177325
function explore(domains, concept; settings = ExploreSettings(domains), parameters...)
178326
f = x -> concept(x; parameters...)

0 commit comments

Comments
 (0)