Skip to content

Commit

Permalink
Prefer Array [index] instead of first/last
Browse files Browse the repository at this point in the history
  • Loading branch information
Maumagnaguagno committed Dec 9, 2023
1 parent bd235a6 commit 07d9257
Show file tree
Hide file tree
Showing 33 changed files with 250 additions and 251 deletions.
10 changes: 5 additions & 5 deletions Hype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def subtasks_to_s(tasks, indent, ordered = true)
else
operators = @parser.operators
output = "#{indent}#{'un' unless ordered}ordered"
tasks.each {|t| output << indent << (operators.assoc(t.first) ? 'operator' : 'method ') << " (#{t.join(' ')})"}
tasks.each {|t| output << indent << (operators.assoc(t[0]) ? 'operator' : 'method ') << " (#{t.join(' ')})"}
output
end
end
Expand All @@ -69,7 +69,7 @@ def operators_to_s
output = ''
indent = "\n "
@parser.operators.each {|op|
output << "\n #{op.first}(#{op[1].join(' ')})"
output << "\n #{op[0]}(#{op[1].join(' ')})"
output << "\n Precond positive:#{predicates_to_s(op[2], indent)}" unless op[2].empty?
output << "\n Precond negative:#{predicates_to_s(op[3], indent)}" unless op[3].empty?
output << "\n Effect positive:#{predicates_to_s(op[4], indent)}" unless op[4].empty?
Expand All @@ -89,7 +89,7 @@ def methods_to_s
@parser.methods.each {|name,param,*decompositions|
output << "\n #{name}(#{param.join(' ')})"
decompositions.each {|dec|
output << "\n Label: #{dec.first}"
output << "\n Label: #{dec[0]}"
output << "\n Free variables:\n #{dec[1].join(indent)}" unless dec[1].empty?
output << "\n Precond positive:#{predicates_to_s(dec[2], indent)}" unless dec[2].empty?
output << "\n Precond negative:#{predicates_to_s(dec[3], indent)}" unless dec[3].empty?
Expand All @@ -111,7 +111,7 @@ def to_s
Problem #{@parser.problem_name}
State:#{predicates_to_s(@parser.state.flat_map {|k,v| [k].product(v)}, "\n ")}\n
Goal:
Tasks:#{subtasks_to_s(@parser.tasks.drop(1), "\n ", @parser.tasks.first)}
Tasks:#{subtasks_to_s(@parser.tasks.drop(1), "\n ", @parser.tasks[0])}
Positive:#{@parser.goal_pos.empty? ? "\n empty" : predicates_to_s(@parser.goal_pos, "\n ")}
Negative:#{@parser.goal_not.empty? ? "\n empty" : predicates_to_s(@parser.goal_not, "\n ")}"
end
Expand Down Expand Up @@ -217,7 +217,7 @@ def execute
#-----------------------------------------------
if $0 == __FILE__
begin
if ARGV.size < 2 or ARGV.first == '-h'
if ARGV.size < 2 or ARGV[0] == '-h'
puts Hype::HELP
elsif not File.exist?(domain = ARGV.shift)
abort("Domain not found: #{domain}")
Expand Down
20 changes: 10 additions & 10 deletions Hypertension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ module Hypertension

def planning(tasks, level = 0)
return tasks if tasks.empty?
case decomposition = @domain[(current_task = tasks.shift).first]
case decomposition = @domain[(current_task = tasks.shift)[0]]
# Operator (true: visible, false: invisible)
when true, false
puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug
puts "#{' ' * level}#{current_task[0]}(#{current_task.drop(1).join(' ')})" if @debug
old_state = @state
begin
# Keep decomposing the hierarchy if operator applied
Expand All @@ -48,7 +48,7 @@ def planning(tasks, level = 0)
end
current_task.unshift(task_name)
# Error
else raise "Domain defines no decomposition for #{current_task.first}"
else raise "Domain defines no decomposition for #{current_task[0]}"
end
nil
end
Expand All @@ -58,10 +58,10 @@ def planning(tasks, level = 0)
def planning(tasks, level = 0)
return tasks if tasks.empty?
index, current_task = tasks.shift
case decomposition = @domain[current_task.first]
case decomposition = @domain[current_task[0]]
# Operator (true: visible, false: invisible)
when true, false
puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug
puts "#{' ' * level}#{current_task[0]}(#{current_task.drop(1).join(' ')})" if @debug
old_state = @state
begin
# Keep decomposing the hierarchy if operator applied
Expand All @@ -83,7 +83,7 @@ def planning(tasks, level = 0)
puts "#{' ' * level.pred}#{method}(#{current_task.join(' ')})" if @debug
# Every unification is tested
__send__(method, *current_task) {|subtasks|
subtasks.map! {|t| [(@index += 1 if @domain[t.first]), t]}
subtasks.each {|t| [(@index += 1 if @domain[t[0]]), t]}
new_index = @index
if plan = planning(subtasks.concat(tasks), level)
@decomposition.unshift("#{index} #{task_name} #{current_task.join(' ')} -> #{method[task_name.size+1..-1]} #{(old_index+1..new_index).to_a.join(' ')}")
Expand All @@ -98,7 +98,7 @@ def planning(tasks, level = 0)
end
current_task.unshift(task_name)
# Error
else raise "Domain defines no decomposition for #{current_task.first}"
else raise "Domain defines no decomposition for #{current_task[0]}"
end
nil
end
Expand Down Expand Up @@ -154,7 +154,7 @@ def generate(precond_pos, precond_not, *free)
# Free variable
if t.instance_of?(Array)
# Not unified
if t.first.empty?
if t[0].empty?
match_objects.push(t, o)
# No match with previous unification
elsif not t.include?(o)
Expand All @@ -171,7 +171,7 @@ def generate(precond_pos, precond_not, *free)
match_objects.shift << match_objects.shift until match_objects.empty?
}
# Unification closed
terms.each {|i| i.first << 0 if i.instance_of?(Array) and i.first.empty?}
terms.each {|i| i[0] << 0 if i.instance_of?(Array) and i[0].empty?}
}
# Remove pointer and duplicates
objects.each {|i|
Expand Down Expand Up @@ -224,7 +224,7 @@ def problem(state, tasks, debug = false, ordered = true)
print_data(tasks)
puts 'Planning'.center(50,'-')
t = Time.now.to_f
plan = ordered ? planning(tasks) : task_permutations(state, tasks, (tasks.pop if tasks[-1]&.first == :invisible_goal))
plan = ordered ? planning(tasks) : task_permutations(state, tasks, (tasks.pop if tasks[-1]&.[](0) == :invisible_goal))
puts "Time: #{Time.now.to_f - t}s", 'Plan'.center(50,'-')
if plan
if plan.empty? then puts 'Empty plan'
Expand Down
12 changes: 6 additions & 6 deletions compilers/Cyber_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def apply(modifier, effects, define_operators, duplicated, arity)

def tasks_to_hyper(output, tasks, indentation, next_task = 'task->next')
tasks.each_with_index {|s,i|
output << "#{indentation}subtask#{i}->value = #{term(s.first)};"
output << "#{indentation}tindex(subtask#{i});" unless s.first.start_with?('invisible_')
output << "#{indentation}subtask#{i}->value = #{term(s[0])};"
output << "#{indentation}tindex(subtask#{i});" unless s[0].start_with?('invisible_')
output << "#{indentation}subtask#{i}->parameters[0] = #{s.size - 1};"
1.upto(s.size - 1) {|j| output << "#{indentation}subtask#{i}->parameters[#{j}] = #{term(s[j])};"}
output << "#{indentation}subtask#{i}->next = #{i != tasks.size - 1 ? "subtask#{i + 1}" : next_task};"
Expand Down Expand Up @@ -147,7 +147,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
visit_param = nil
unless dec[4].empty?
dec[4].each {|s|
if s.size > 1 and s.first.start_with?('invisible_visit_')
if s.size > 1 and s[0].start_with?('invisible_visit_')
if ((visit_param = s.drop(1)) & f).empty?
define_methods << "\n if(applicable_const(visit#{visit_param.size}, #{terms_to_hyper(visit_param)})) return false;"
visit_param = nil
Expand Down Expand Up @@ -202,7 +202,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
# close_method_str.prepend('}') and no indentation change for compact output
close_method_str.prepend("#{indentation}}")
indentation << ' '
if terms2.size == 1 then define_methods << "#{indentation}VALUE #{terms2.first} = *it#{counter};"
if terms2.size == 1 then define_methods << "#{indentation}VALUE #{terms2[0]} = *it#{counter};"
else terms2.each_with_index {|term,i| define_methods << "#{indentation}VALUE #{term} = std::get<#{i}>(*it#{counter});"}
end
elsif pre == '=' then equality << "#{terms2[0]} != #{terms2[1]}"
Expand Down Expand Up @@ -289,9 +289,9 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
end
comparison << pre
elsif k
if k.first.empty? then define_state_const << "\nstatic VALUE0 #{pre}_ = true;"
if k[0].empty? then define_state_const << "\nstatic VALUE0 #{pre}_ = true;"
else
define_state_const << "\nstatic VALUE#{arity[pre] ||= k.first.size} #{pre == '=' ? 'equal' : pre}_\n{\n #{k.map {|terms| terms_to_hyper(terms)}.join(",\n ")}\n};"
define_state_const << "\nstatic VALUE#{arity[pre] ||= k[0].size} #{pre == '=' ? 'equal' : pre}_\n{\n #{k.map {|terms| terms_to_hyper(terms)}.join(",\n ")}\n};"
tokens.concat(k.flatten(1))
end
end
Expand Down
12 changes: 6 additions & 6 deletions compilers/Dot_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
# Operators
operators.each {|op|
# Header
domain_str << " \"#{op.first}\" [\n shape=record\n label=\"{{#{op.first}|#{op[1].join(' ')}}|{"
domain_str << " \"#{op[0]}\" [\n shape=record\n label=\"{{#{op[0]}|#{op[1].join(' ')}}|{"
# Preconditions
predicates_to_dot(domain_str, op[2], op[3])
# Effects
Expand All @@ -32,21 +32,21 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
method_str = ''
decompositions = []
met.drop(2).each_with_index {|dec,i|
decompositions << "<n#{i}>#{met.first}_#{dec.first}"
decompositions << "<n#{i}>#{met[0]}_#{dec[0]}"
# Label
method_str << " \"label_#{met.first}_#{dec.first}\" [\n shape=Mrecord\n label=\"{{#{met.first}_#{dec.first}|#{dec[1].join(' ')}}|"
method_str << " \"label_#{met[0]}_#{dec[0]}\" [\n shape=Mrecord\n label=\"{{#{met[0]}_#{dec[0]}|#{dec[1].join(' ')}}|"
# Preconditions
predicates_to_dot(method_str, dec[2], dec[3])
# Subtasks
connections = ''
dec[4].each_with_index {|subtask,j|
method_str << "|<n#{j}>#{subtask.join(' ')}"
connections << " \"label_#{met.first}_#{dec.first}\":n#{j} -> \"#{subtask.first}\"\n" if all_connections or operators.assoc(subtask.first)
connections << " \"label_#{met[0]}_#{dec[0]}\":n#{j} -> \"#{subtask[0]}\"\n" if all_connections or operators.assoc(subtask[0])
}
# Connections
method_str << "}\"\n ]\n \"#{met.first}\":n#{i} -> \"label_#{met.first}_#{dec.first}\" [style=dotted]\n#{connections}"
method_str << "}\"\n ]\n \"#{met[0]}\":n#{i} -> \"label_#{met[0]}_#{dec[0]}\" [style=dotted]\n#{connections}"
}
domain_str << " \"#{met.first}\" [\n shape=Mrecord\n style=bold\n label=\"{{#{met.first}|#{met[1].join(' ')}}|{#{decompositions.join('|')}}}\"\n ]\n#{method_str}"
domain_str << " \"#{met[0]}\" [\n shape=Mrecord\n style=bold\n label=\"{{#{met[0]}|#{met[1].join(' ')}}|{#{decompositions.join('|')}}}\"\n ]\n#{method_str}"
}
domain_str << '}'
end
Expand Down
8 changes: 4 additions & 4 deletions compilers/Hyper_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
methods.each_with_index {|(name,param,*decompositions),mi|
variables = "(#{param.join(', ').tr!('?','_')})" unless param.empty?
decompositions.map! {|dec|
define_methods << "\n def #{name}_#{dec.first}#{variables}"
define_methods << "\n def #{name}_#{dec[0]}#{variables}"
equality = []
define_methods_comparison = ''
f = dec[1]
Expand Down Expand Up @@ -143,7 +143,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
define_methods << define_methods_comparison
visit_param = nil
dec[4].each {|s|
if s.size > 1 and s.first.start_with?('invisible_visit_')
if s.size > 1 and s[0].start_with?('invisible_visit_')
if ((visit_param = s.drop(1)) & f).empty?
define_methods << "\n return if @visit.include?(#{terms_to_hyper(visit_param)})"
visit_param = nil
Expand Down Expand Up @@ -236,7 +236,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
define_methods << define_methods_comparison
end
define_methods << indentation << (dec[4].empty? ? 'yield []' : "yield [#{indentation} [" << dec[4].map {|g| g.map {|i| term(i)}.join(', ')}.join("],#{indentation} [") << "]#{indentation}]") << close_method_str
"\n :#{name}_#{dec.first}"
"\n :#{name}_#{dec[0]}"
}
domain_str << "\n :#{name} => [" << decompositions.join(',') << (methods.size.pred == mi ? "\n ]" : "\n ],")
}
Expand Down Expand Up @@ -287,7 +287,7 @@ def compile_problem(domain_name, problem_name, operators, methods, predicates, s
}
# Tasks
ordered = tasks.shift
problem_str << start_str << " ],\n # Tasks\n [" << tasks.map {|g| "\n [:#{g.join(', :')}]"}.join(',') << "\n ],\n # Debug\n ARGV.first == 'debug'#{",\n # Ordered\n false" if ordered == false}\n)"
problem_str << start_str << " ],\n # Tasks\n [" << tasks.map {|g| "\n [:#{g.join(', :')}]"}.join(',') << "\n ],\n # Debug\n ARGV[0] == 'debug'#{",\n # Ordered\n false" if ordered == false}\n)"
unless tasks.empty?
tasks.unshift(ordered)
tasks.pop if tasks[-1][0] == 'invisible_goal'
Expand Down
10 changes: 5 additions & 5 deletions compilers/JSHOP_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def subtasks_to_jshop(output, tasks, operators, indentation, order = true)
else
output << "#{indentation}(#{':unordered' unless order}\n"
tasks.each {|t|
name = t.first
name = t[0]
t[0] = "!#{name.sub(/^invisible_/,'!')}" if operators.assoc(name)
output << "#{indentation} (#{t.join(' ')})\n"
t[0] = name
Expand All @@ -46,7 +46,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
# Operators
operators.each {|op|
# Header
domain_str << "\n\n (:operator (!#{op.first.sub(/^invisible_/,'!')} #{op[1].join(' ')})\n"
domain_str << "\n\n (:operator (!#{op[0].sub(/^invisible_/,'!')} #{op[1].join(' ')})\n"
# Preconditions
predicates_to_jshop(domain_str, op[2], op[3])
# Delete effects
Expand All @@ -64,10 +64,10 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
# Methods
domain_str << "\n\n ;#{SPACER}\n ; Methods\n ;#{SPACER}"
methods.each {|met|
header = "\n\n (:method (#{met.first} #{met[1].join(' ')})\n "
header = "\n\n (:method (#{met[0]} #{met[1].join(' ')})\n "
met.drop(2).each {|dec|
# Header and label
domain_str << header << "#{dec.first}\n"
domain_str << header << "#{dec[0]}\n"
# Preconditions
predicates_to_jshop(domain_str, dec[2], dec[3])
# Subtasks
Expand Down Expand Up @@ -96,7 +96,7 @@ def compile_problem(domain_name, problem_name, operators, methods, predicates, s
end
# Tasks
problem_str << ")\n\n ;#{SPACER}\n ; Tasks\n ;#{SPACER}\n\n"
subtasks_to_jshop(problem_str, goal_pos.empty? && goal_not.empty? ? tasks.drop(1) : tasks.drop(1) << ['!!goal'], operators, ' ', tasks.first)
subtasks_to_jshop(problem_str, goal_pos.empty? && goal_not.empty? ? tasks.drop(1) : tasks.drop(1) << ['!!goal'], operators, ' ', tasks[0])
problem_str << ')'
end
end
14 changes: 7 additions & 7 deletions compilers/PDDL_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
# Operators
operators.each {|op|
# Header
action_str << "\n (:action #{op.first}\n :parameters (#{op[1].join(' ')})\n :precondition (and\n"
action_str << "\n (:action #{op[0]}\n :parameters (#{op[1].join(' ')})\n :precondition (and\n"
# Preconditions
op[2].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre.first] ||= pre}
op[3].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre.first] ||= pre}
op[2].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre[0]] ||= pre}
op[3].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre[0]] ||= pre}
negative_preconditions = true unless op[3].empty?
# Effects
action_str << " )\n :effect (and\n"
op[4].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre.first] ||= pre}
op[5].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre.first] ||= pre}
op[4].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre[0]] ||= pre}
op[5].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre[0]] ||= pre}
action_str << " )\n )\n"
}
goal_pos.each {|pre| declared[pre.first] ||= pre}
goal_not.each {|pre| declared[pre.first] ||= pre}
goal_pos.each {|pre| declared[pre[0]] ||= pre}
goal_not.each {|pre| declared[pre[0]] ||= pre}
domain_str = "; Generated by Hype\n(define (domain #{domain_name})
(:requirements :strips#{' :negative-preconditions' if negative_preconditions}#{' :equality' if declared.delete('=')})\n\n (:predicates\n"
declared.each_value {|pre|
Expand Down
8 changes: 4 additions & 4 deletions examples/experiments/Debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def input
end

def assert(pre)
if pre.first == 'not'
pre = pre.last
raise "Unexpected (#{pre.join(' ')}) in state" if @state[pre.first].include?(pre.drop(1))
elsif not @state[pre.first].include?(pre.drop(1))
if pre[0] == 'not'
pre = pre[1]
raise "Unexpected (#{pre.join(' ')}) in state" if @state[pre[0]].include?(pre.drop(1))
elsif not @state[pre[0]].include?(pre.drop(1))
raise "Expected (#{pre.join(' ')}) in state"
end
end
Expand Down
2 changes: 1 addition & 1 deletion examples/experiments/Expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def evaluate(expression, &block)

def call(expression)
f = expression.shift
expression.map! {|i| i.instance_of?(Array) && i.first == :call ? (i.shift; call(i)) : i}.inject(f)
expression.map! {|i| i.instance_of?(Array) && i[0] == :call ? (i.shift; call(i)) : i}.inject(f)
end

#-----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/experiments/Stochastic.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Stochastic

def planning(tasks, level = 0)
if not tasks.empty? and (d = @domain[tasks.first.first]).instance_of?(Array)
if not tasks.empty? and (d = @domain[tasks[0][0]]).instance_of?(Array)
d.shuffle!
end
super
Expand Down
2 changes: 1 addition & 1 deletion examples/goldminer/pb1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
[:get_gold]
],
# Debug
ARGV.first == 'debug'
ARGV[0] == 'debug'
)

# Test
Expand Down
2 changes: 1 addition & 1 deletion examples/hanoi/hanoi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def generate(discs)
end
end

puts Hanoi.generate(ARGV.first.to_i) if $0 == __FILE__
puts Hanoi.generate(ARGV[0].to_i) if $0 == __FILE__
4 changes: 2 additions & 2 deletions examples/n_queens/N_Queens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def try_next(y)
#-----------------------------------------------
if $0 == __FILE__
# Size input
size = ARGV.first ? ARGV.first.to_i : 8
N_Queens.solve(size, ARGV.last == 'debug', true)
size = ARGV[0] ? ARGV[0].to_i : 8
N_Queens.solve(size, ARGV[1] == 'debug', true)
# Draw from row size - 1 to 0
N_Queens.state[:queen].reverse_each {|i,|
row = '[ ]' * size
Expand Down
2 changes: 1 addition & 1 deletion examples/robby/pb1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
[:swap_at, robby, right]
],
# Debug
ARGV.first == 'debug'
ARGV[0] == 'debug'
)

# Test
Expand Down
4 changes: 2 additions & 2 deletions examples/sudoku/Sudoku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def try_next(counter)
if symbols.empty?
return
elsif symbols.size == 1
singles << [:put_symbol, x, y, b, s = symbols.first]
singles << [:put_symbol, x, y, b, s = symbols[0]]
col.delete(s)
row.delete(s)
box.delete(s)
Expand All @@ -117,7 +117,7 @@ def try_next(counter)
# Main
#-----------------------------------------------
if $0 == __FILE__
debug = ARGV.first == 'debug'
debug = ARGV[0] == 'debug'
# Easy
board = '
3 4 | 1 2
Expand Down
Loading

0 comments on commit 07d9257

Please sign in to comment.