Skip to content

Commit 6ba371c

Browse files
author
Issa Hanou
committed
wrote tests for ASP functionality.
1 parent 9d907a0 commit 6ba371c

File tree

11 files changed

+318
-343
lines changed

11 files changed

+318
-343
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ docs/site/
2323
# environment.
2424
Manifest.toml
2525

26+
# Files generated by ASPSolver
27+
*.lp

src/HerbConstraints.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ include("solver/uniform_solver/uniform_solver.jl")
5959
include("solver/uniform_solver/uniform_treemanipulations.jl")
6060
include("solver/domainutils.jl")
6161

62+
include("solver/uniform_solver/asp/asp_tree_transformations.jl")
63+
include("solver/uniform_solver/asp/asp_constraint_transformations.jl")
64+
include("solver/uniform_solver/asp/asp_uniform_tree_solver.jl")
65+
6266
include("patternmatch.jl")
6367
include("lessthanorequal.jl")
6468
include("makeequal.jl")
@@ -148,4 +152,10 @@ export
148152
StateHole,
149153
freeze_state
150154

155+
#asp uniform solver
156+
tree_to_ASP,
157+
constraint_tree_to_ASP,
158+
to_ASP,
159+
ASPSolver
160+
151161
end # module HerbConstraints

src/solver/uniform_solver/asp/asp_constraint_transformations.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Transforms the contains constraint into ASP format.
4545
Contains(4) -> :- not node(_,4).
4646
"""
4747
function to_ASP(grammar::AbstractGrammar, constraint::Contains, constraint_index::Int64)
48-
return ":- not node(_, $(constraint.rule)).\n"
48+
return ":- not node(_,$(constraint.rule)).\n"
4949
end
5050

5151
"""
@@ -56,7 +56,7 @@ Transforms the unique constraint into ASP format.
5656
Unique(4) -> { node(X,4) : node(X,4) } 1.
5757
"""
5858
function to_ASP(grammar::AbstractGrammar, constraint::Unique, constraint_index::Int64)
59-
return "{ node(X, $(constraint.rule)) : node(X, $(constraint.rule)) } 1.\n"
59+
return "{ node(X,$(constraint.rule)) : node(X,$(constraint.rule)) } 1.\n"
6060
end
6161

6262
"""
@@ -95,16 +95,16 @@ is_smaller(X,Y) :- node(X,XV), node(Y,YV), XV = YV, S = #sum {Z: child(X,Z,XC),
9595
:- node(X1,5),child(X1,1,X),child(X1,2,Y),child(X1,3,Z) not is_smaller(Y,Z).
9696
"""
9797
function to_ASP(grammar::AbstractGrammar, constraint::Ordered, constraint_index::Int64)
98-
output = "is_smaller(X,Y) :- node(X,XV), node(Y,YV), XV < YV.\n"
99-
output *= "is_smaller(X,Y) :- node(X,XV), node(Y,YV), XV = YV, S = #sum {Z: child(X,Z,XC), child(Y,Z,YC), is_smaller(XC, YC)}, M = #max {Z: child(X,Z,XC)}, S = M.\n"
98+
output = "is_smaller(X,Y) :- node(X,XV),node(Y,YV),XV < YV.\n"
99+
output *= "is_smaller(X,Y) :- node(X,XV),node(Y,YV),XV = YV,S = #sum { Z : child(X,Z,XC),child(Y,Z,YC),is_smaller(XC,YC) }, M = #max { Z : child(X,Z,XC) }, S = M.\n"
100100

101101
tree, domains, _ = constraint_tree_to_ASP(grammar, constraint.tree, 1, constraint_index)
102102

103103
output *= domains
104104

105105
# create ordered constraints, for each consecutive pair of ordered vars
106106
for i in 1:length(constraint.order)-1
107-
output *= ":- $(tree), not is_smaller($(constraint.order[i]), $(constraint.order[i+1])).\n"
107+
output *= ":- $(tree),not is_smaller($(constraint.order[i]),$(constraint.order[i+1])).\n"
108108
end
109109

110110
return output

src/solver/uniform_solver/asp/asp_test.jl

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

src/solver/uniform_solver/asp/asp_tree_transformations.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ end
7171
node_to_ASP(tree::StateHole, grammar::AbstractGrammar, node_index::Int64)
7272
7373
Transform a [StateHole] into an ASP representation in the form
74-
`1 { node(node_index, rule_id_1); node(node_index, rule_id_2);...}1.`
74+
`1 { node(node_index, rule_id_1); node(node_index, rule_id_2);...} 1.`
7575
"""
7676
function node_to_ASP(tree::StateHole, grammar::AbstractGrammar, node_index::Int64)
7777
options = join(["node($(node_index),$(ind))" for ind in Base.findall(tree.domain)], ";")
@@ -90,7 +90,7 @@ Transforms a template tree to an ASP form suitable for constraints.
9090
function constraint_tree_to_ASP(grammar::AbstractGrammar, tree::AbstractRuleNode, node_index::Int64, constraint_index::Int64)
9191
tree_facts, additional_facts = "", ""
9292
tmp_facts, tmp_additional = constraint_node_to_ASP(grammar, tree, node_index, constraint_index::Int64)
93-
tree_facts *= tmp_facts
93+
tree_facts *= "$(tmp_facts)"
9494
additional_facts *= join(tmp_additional, "")
9595
parent_index = node_index
9696
node_index += 1
@@ -128,7 +128,7 @@ Transforms a [UniformHole] or [DomainRuleNode] into an ASP representation in the
128128
and the allowed domains of this constraint node.
129129
"""
130130
function constraint_node_to_ASP(grammar::AbstractGrammar, node::Union{UniformHole,DomainRuleNode}, node_index::Int64, constraint_index::Int64)
131-
return "node(X$(node_index),D$(node_index)),allowed(c$(constraint_index)x$(node_index),D$(node_index))", map(x -> "allowed(c$(constraint_index)x$(node_index), $x).\n", collect(filter(x -> node.domain[x], 1:length(grammar.rules))))
131+
return "node(X$(node_index),D$(node_index)),allowed(c$(constraint_index)x$(node_index),D$(node_index))", map(x -> "allowed(c$(constraint_index)x$(node_index),$x).\n", collect(filter(x -> node.domain[x], 1:length(grammar.rules))))
132132
end
133133

134134
"""
@@ -139,5 +139,5 @@ Transforms a [StateHole] into an ASP representation in the form
139139
and the allowed domains of this constraint node.
140140
"""
141141
function constraint_node_to_ASP(grammar::AbstractGrammar, node::StateHole, node_index::Int64, constraint_index::Int64)
142-
return "node(X$(node_index),D$(node_index)),allowed(c$(constraint_index)x$(node_index),D$(node_index))", map(x -> "allowed(c$(constraint_index)x$(node_index), $x).\n", Base.findall(node.domain))
142+
return "node(X$(node_index),D$(node_index)),allowed(c$(constraint_index)x$(node_index),D$(node_index))", map(x -> "allowed(c$(constraint_index)x$(node_index),$x).\n", Base.findall(node.domain))
143143
end

src/solver/uniform_solver/asp/asp_uniform_tree_solver.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,13 @@ end
5555
function extract_solutions(solver::ASPSolver, output_lines)
5656
current_solution = Dict{Int64,Int64}()
5757
for line in output_lines
58-
if startswith(line, "Answer")
59-
if !isempty(current_solution)
60-
push!(solver.solutions, current_solution)
61-
end
62-
current_solution = Dict{Int64,Int64}()
63-
elseif startswith(line, "node")
58+
if startswith(line, "node")
6459
node_assignments = split(line, " ")
6560
for node in node_assignments
6661
m = match(r"node\((\d+),(\d+)\)", node)
6762
current_solution[parse(Int, m.captures[1])] = parse(Int, m.captures[2])
6863
end
64+
push!(solver.solutions, current_solution)
6965
elseif startswith(line, "SATISFIABLE")
7066
break
7167
end

src/solver/uniform_solver/asp/example.lp

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

0 commit comments

Comments
 (0)