Commit 566bb84e3ac629a863cbede51a400552f6d03b5d
1 parent
5bcfee4d
Exists in
master
Done: asp_program
Showing
4 changed files
with
189 additions
and
67 deletions
Show diff stats
| @@ -0,0 +1,106 @@ | @@ -0,0 +1,106 @@ | ||
| 1 | +module PASPProc | ||
| 2 | + | ||
| 3 | +include("../asplang/ASPLang.jl") | ||
| 4 | +using ASPLang | ||
| 5 | + | ||
| 6 | +export tree_string, | ||
| 7 | + filter_tree, | ||
| 8 | + filter_children, | ||
| 9 | + has_child, | ||
| 10 | + node_findreplace, | ||
| 11 | + has_annotation_P, | ||
| 12 | + repl_annotation_disjunction, | ||
| 13 | + derived | ||
| 14 | + | ||
| 15 | +tree_string(node::AnonymousVariable; level=0, indent="\t") = "$(indent^level)Anonymous: $(node.args |> string)" | ||
| 16 | +tree_string(node::Functor; level=0, indent="\t") = "$(indent^level)Functor: $(node.args |> string)" | ||
| 17 | +tree_string(node::Annotation; level=0, indent="\t") = "$(indent^level)Annotation: $(node.args |> string)" | ||
| 18 | +tree_string(node::Variable; level=0, indent="\t") = "$(indent^level)Variable: $(node.args |> string)" | ||
| 19 | +tree_string(node::StringConstant; level=0, indent="\t") = "$(indent^level)String: $(node.args |> string)" | ||
| 20 | +tree_string(node::SymbolicConstant; level=0, indent="\t") = "$(indent^level)Symbol: $(node.args |> string)" | ||
| 21 | +tree_string(node::NumberConstant; level=0, indent="\t") = "$(indent^level)Number: $(node.args |> string)" | ||
| 22 | +#tree_print(node::Terminal; level=0, indent="\t") = node |> typeof |> string | ||
| 23 | +function tree_string(node::ASPLang.NonTerminal; level=0, indent="\t") | ||
| 24 | + node_head = node |> typeof |> string | ||
| 25 | + sub_results = [ "$(indent^level)$node_head" ] | ||
| 26 | + if isa(node.args, AbstractArray) && length(node.args) > 0 | ||
| 27 | + for child in node.args | ||
| 28 | + child_tree = if isa(child, ASPLang.Node) | ||
| 29 | + tree_string(child, level = level + 1, indent=indent) | ||
| 30 | + else | ||
| 31 | + "$(indent^(level+1))$(child |> string)" | ||
| 32 | + end | ||
| 33 | + push!(sub_results, child_tree) | ||
| 34 | + end | ||
| 35 | + else | ||
| 36 | + push!(sub_results, tree_string(node.args, level=level+1, indent=indent)) | ||
| 37 | + end | ||
| 38 | + join(sub_results, "\n") | ||
| 39 | +end | ||
| 40 | +tree_string(node; level=0, indent="\t") = "$(indent^level)$(node |> typeof |> string):$(node |> string)" | ||
| 41 | + | ||
| 42 | + | ||
| 43 | +function filter_tree(node::NonTerminal, prop) | ||
| 44 | + results = if prop(node) | ||
| 45 | + [node] | ||
| 46 | + else | ||
| 47 | + [] | ||
| 48 | + end | ||
| 49 | + if isa(node.args, AbstractArray) | ||
| 50 | + for d in node.args | ||
| 51 | + append!(results, filter_tree(d, prop)) | ||
| 52 | + end | ||
| 53 | + else | ||
| 54 | + if prop(node.args) | ||
| 55 | + push!(results, node.args) | ||
| 56 | + end | ||
| 57 | + end | ||
| 58 | + results | ||
| 59 | +end | ||
| 60 | + | ||
| 61 | +function filter_children(node::NonTerminal, prop) | ||
| 62 | + if isa(node.args, AbstractArray) | ||
| 63 | + filter(prop, node.args) | ||
| 64 | + else | ||
| 65 | + if prop(node.args) | ||
| 66 | + node.args | ||
| 67 | + else | ||
| 68 | + nothing | ||
| 69 | + end | ||
| 70 | + end | ||
| 71 | +end | ||
| 72 | + | ||
| 73 | +function has_child(prop) | ||
| 74 | + node::NonTerminal -> any(prop, node.args) | ||
| 75 | +end | ||
| 76 | + | ||
| 77 | +function node_findreplace(node::NonTerminal, prop, action) | ||
| 78 | + if prop(node) | ||
| 79 | + action(node) | ||
| 80 | + elseif isa(node.args, AbstractArray) | ||
| 81 | + new_args = [] | ||
| 82 | + for arg in node.args | ||
| 83 | + push!(new_args, node_findreplace(arg, prop, action)) | ||
| 84 | + end | ||
| 85 | + typeof(node)(new_args) | ||
| 86 | + else | ||
| 87 | + node | ||
| 88 | + end | ||
| 89 | +end | ||
| 90 | + | ||
| 91 | +has_annotation_P = n -> has_child(isa(n, ASPLang.Annotation)) | ||
| 92 | +function repl_annotation_disjunction(n::NonTerminal) | ||
| 93 | + a = [xi for xi in n.args if !isa(xi, Annotation)][1] | ||
| 94 | + Head( | ||
| 95 | + Disjunction( | ||
| 96 | + [a, Negated(a)] | ||
| 97 | + )) | ||
| 98 | +end | ||
| 99 | + | ||
| 100 | +function derived(p::ASPLang.Program) | ||
| 101 | + node_findreplace(p, | ||
| 102 | + has_annotation_P, | ||
| 103 | + repl_annotation_disjunction) | ||
| 104 | +end | ||
| 105 | + | ||
| 106 | +end # PASPproc | ||
| 0 | \ No newline at end of file | 107 | \ No newline at end of file |
drafts.jl
| @@ -1,67 +0,0 @@ | @@ -1,67 +0,0 @@ | ||
| 1 | -using Symbolics | ||
| 2 | -using Latexify | ||
| 3 | - | ||
| 4 | -# # γ = 0.5 | ||
| 5 | -# no_s = [ | ||
| 6 | -# 0 , | ||
| 7 | -# 23 , | ||
| 8 | -# 614 , | ||
| 9 | -# 165 , | ||
| 10 | -# 169 , | ||
| 11 | -# 0 , | ||
| 12 | -# 0 , | ||
| 13 | -# 4 , | ||
| 14 | -# 25 , | ||
| 15 | -# ] | ||
| 16 | - | ||
| 17 | -# γ = 0.8 | ||
| 18 | -no_s = [ | ||
| 19 | - 0 , | ||
| 20 | - 28 , | ||
| 21 | - 632 , | ||
| 22 | - 246 , | ||
| 23 | - 59 , | ||
| 24 | - 0 , | ||
| 25 | - 0 , | ||
| 26 | - 5 , | ||
| 27 | - 27 , | ||
| 28 | -] | ||
| 29 | -pr_s = (x -> x // 1000).(no_s) | ||
| 30 | - | ||
| 31 | -@variables θ | ||
| 32 | -num_e = [ | ||
| 33 | - 0 , | ||
| 34 | - 0 , | ||
| 35 | - 7 , | ||
| 36 | - 3 * θ , | ||
| 37 | - 3 * (1 - θ) , | ||
| 38 | - 0 , | ||
| 39 | - 0 , | ||
| 40 | - 3 , | ||
| 41 | - 10 , | ||
| 42 | -] | ||
| 43 | -pr_e = (x -> x // 23).(num_e) | ||
| 44 | - | ||
| 45 | -target = expand(sum( (x -> x^2).(pr_s - pr_e) )) | ||
| 46 | -println(latexify(target)) | ||
| 47 | - | ||
| 48 | -# using Plots | ||
| 49 | -# g(t) = (20869963/66125000) + (477/52900)*t + (18/529)*(t^2) | ||
| 50 | -# t = 0:0.1:1 | ||
| 51 | -# plot(t, g.(t)) | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | -function solve2(a, b, c) | ||
| 56 | - delta = sqrt(Complex(b^2 - 4 * a * c)) | ||
| 57 | - return ( (-b - delta)/(2*a), (-b + delta)/(2*a) ) | ||
| 58 | -end | ||
| 59 | - | ||
| 60 | -# # g = target ~ 0 | ||
| 61 | - | ||
| 62 | -a = float(18//529) | ||
| 63 | -b = -float(21903//264500) | ||
| 64 | -c = float(188207311//529000000) | ||
| 65 | - | ||
| 66 | -println("a: $a b: $b c: $c") | ||
| 67 | -println("̂θ = $(-b/a)") |
| @@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
| 1 | +include("../asplang/ASPLang.jl") | ||
| 2 | +include("PASPProc.jl") | ||
| 3 | + | ||
| 4 | +using .ASPLang | ||
| 5 | +using .PASPProc | ||
| 6 | + | ||
| 7 | + | ||
| 8 | +root_path = "/home/fc/sci/projetos/zugzwang/code/asplang" | ||
| 9 | +asp_file = "sbf.lp" | ||
| 10 | +src = read("$(root_path)/$(asp_file)", String) | ||
| 11 | + | ||
| 12 | +println(methods(tree_string)) | ||
| 13 | +# e = parse_asp(src, asp_grammar) | ||
| 14 | +# println(isa(e, NonTerminal)) | ||
| 15 | +# t = tree_string(e) | ||
| 16 | +# println(t) | ||
| 0 | \ No newline at end of file | 17 | \ No newline at end of file |
| @@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
| 1 | +using Symbolics | ||
| 2 | +using Latexify | ||
| 3 | + | ||
| 4 | +# # γ = 0.5 | ||
| 5 | +# no_s = [ | ||
| 6 | +# 0 , | ||
| 7 | +# 23 , | ||
| 8 | +# 614 , | ||
| 9 | +# 165 , | ||
| 10 | +# 169 , | ||
| 11 | +# 0 , | ||
| 12 | +# 0 , | ||
| 13 | +# 4 , | ||
| 14 | +# 25 , | ||
| 15 | +# ] | ||
| 16 | + | ||
| 17 | +# γ = 0.8 | ||
| 18 | +no_s = [ | ||
| 19 | + 0 , | ||
| 20 | + 28 , | ||
| 21 | + 632 , | ||
| 22 | + 246 , | ||
| 23 | + 59 , | ||
| 24 | + 0 , | ||
| 25 | + 0 , | ||
| 26 | + 5 , | ||
| 27 | + 27 , | ||
| 28 | +] | ||
| 29 | +pr_s = (x -> x // 1000).(no_s) | ||
| 30 | + | ||
| 31 | +@variables θ | ||
| 32 | +num_e = [ | ||
| 33 | + 0 , | ||
| 34 | + 0 , | ||
| 35 | + 7 , | ||
| 36 | + 3 * θ , | ||
| 37 | + 3 * (1 - θ) , | ||
| 38 | + 0 , | ||
| 39 | + 0 , | ||
| 40 | + 3 , | ||
| 41 | + 10 , | ||
| 42 | +] | ||
| 43 | +pr_e = (x -> x // 23).(num_e) | ||
| 44 | + | ||
| 45 | +target = expand(sum( (x -> x^2).(pr_s - pr_e) )) | ||
| 46 | +println(latexify(target)) | ||
| 47 | + | ||
| 48 | +# using Plots | ||
| 49 | +# g(t) = (20869963/66125000) + (477/52900)*t + (18/529)*(t^2) | ||
| 50 | +# t = 0:0.1:1 | ||
| 51 | +# plot(t, g.(t)) | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +function solve2(a, b, c) | ||
| 56 | + delta = sqrt(Complex(b^2 - 4 * a * c)) | ||
| 57 | + return ( (-b - delta)/(2*a), (-b + delta)/(2*a) ) | ||
| 58 | +end | ||
| 59 | + | ||
| 60 | +# # g = target ~ 0 | ||
| 61 | + | ||
| 62 | +a = float(18//529) | ||
| 63 | +b = -float(21903//264500) | ||
| 64 | +c = float(188207311//529000000) | ||
| 65 | + | ||
| 66 | +println("a: $a b: $b c: $c") | ||
| 67 | +println("̂θ = $(-b/a)") |