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 Nov 24, 2023
1 parent 85de3c5 commit e2fb635
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Hype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def compile(domain, problem, type = 'rb')
#-----------------------------------------------
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}")
elsif not File.exist?(problem = ARGV.shift)
abort("Problem not found: #{problem}")
else
type = ARGV.first
type = ARGV[0]
t = Time.now.to_f
Hype.parse(domain, problem)
Hype.compile(domain, problem)
Expand Down
8 changes: 4 additions & 4 deletions Hypertension_U.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def planning(tasks, level = 0, plan = [1,0])
@plans << plan
end
else
case decomposition = @domain[(current_task = tasks.shift).first]
case decomposition = @domain[(current_task = tasks.shift)[0]]
# Operator with single outcome
when Numeric
execute(current_task, decomposition, tasks, level, plan)
# Operator with multiple outcomes
when Hash
task_name = current_task.first
task_name = current_task[0]
begin
decomposition.each {|task_prob,probability|
current_task[0] = task_prob
Expand Down Expand Up @@ -68,7 +68,7 @@ def planning(tasks, level = 0, plan = [1,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
end
end
Expand All @@ -88,7 +88,7 @@ def state_valuation(old_state)

def execute(current_task, probability, tasks, level, plan)
old_state = @state
puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug
puts "#{' ' * level}#{current_task[0]}(#{current_task.drop(1).join(' ')})" if @debug
begin
# Minimum probability and applied
if (new_prob = plan[PROBABILITY] * probability) >= @min_prob and __send__(*current_task)
Expand Down
38 changes: 19 additions & 19 deletions UHyper_Compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module UHyper_Compiler
#-----------------------------------------------

def expression_to_hyper(precond_expression, axioms)
case precond_expression.first
case precond_expression[0]
when 'and', 'or'
if precond_expression.size == 2 then expression_to_hyper(precond_expression[1], axioms)
else '(' << precond_expression.drop(1).map! {|exp| expression_to_hyper(exp, axioms)}.join(" #{precond_expression.first} ") << ')'
else '(' << precond_expression.drop(1).map! {|exp| expression_to_hyper(exp, axioms)}.join(" #{precond_expression[0]} ") << ')'
end
when 'not' then (term = expression_to_hyper(precond_expression[1], axioms)).delete_prefix!('not ') or 'not ' << term
when 'call' then call(precond_expression)
when 'assign' then '(_' << precond_expression[1].delete_prefix('?') << ' = ' << evaluate(precond_expression[2]) << ')'
when nil then 'false' # Empty list is false
else
terms = precond_expression.drop(1).map! {|i| evaluate(i)}.join(', ')
if axioms.assoc(precond_expression.first) then "#{precond_expression.first}(#{terms})"
else "@state[#{evaluate(precond_expression.first)}].include?([#{terms}])"
if axioms.assoc(precond_expression[0]) then "#{precond_expression[0]}(#{terms})"
else "@state[#{evaluate(precond_expression[0])}].include?([#{terms}])"
end
end
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def evaluate(term, namespace = '', quotes = true)
elsif term.match?(/^-?\d/) then quotes ? "'#{term.to_f}'" : term.to_f.to_s
else term
end
elsif term.first == 'call'
elsif term[0] == 'call'
term = call(term, namespace)
quotes && term.match?(/^-?\d/) ? "'#{term}'" : term
else "[#{term.map {|i| evaluate(i, namespace, quotes)}.join(', ')}]"
Expand Down Expand Up @@ -143,7 +143,7 @@ def operator_to_hyper(name, param, precond_expression, effect_add, effect_del, d
define_operators << "\n return unless #{precond_expression}" if precond_expression
# Effective
effect_calls = []
effect_add.reject! {|pre| effect_calls << call(pre) if pre.first == 'call'}
effect_add.reject! {|pre| effect_calls << call(pre) if pre[0] == 'call'}
unless effect_add.empty? and effect_del.empty?
define_operators << "\n @state = @state.dup"
apply('delete', effect_del, define_operators, duplicated = {})
Expand Down Expand Up @@ -182,7 +182,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
methods.each_with_index {|(name,param,*decompositions),mi|
paramstr = "(#{param.join(', ').tr!('?','_')})" unless param.empty?
decompositions.map! {|dec|
define_methods << "\n def #{name}_#{dec.first}#{paramstr}"
define_methods << "\n def #{name}_#{dec[0]}#{paramstr}"
# Obtain free variables
# TODO refactor this block to work with complex expressions
free_variables = []
Expand All @@ -194,15 +194,15 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
dependent_attachments = []
unless (precond_expression = dec[1]).empty?
ground_variables = param.dup
precond_expression = precond_expression.first == 'and' ? precond_expression.drop(1) : [precond_expression]
precond_expression = precond_expression[0] == 'and' ? precond_expression.drop(1) : [precond_expression]
precond_expression.reject! {|pre|
pre = pre.last unless positive = pre.first != 'not'
if attachments.assoc(pre.first)
pre = pre[1] unless positive = pre[0] != 'not'
if attachments.assoc(pre[0])
precond_attachments << pre.unshift(positive)
elsif pre.first == 'assign'
elsif pre[0] == 'assign'
ground_variables << pre[1] if pre[2].flatten.all? {|j| not j.start_with?('?') or ground_variables.include?(j)}
false
elsif positive and pre.first != 'call' and not axioms.assoc(pre.first)
elsif positive and pre[0] != 'call' and not axioms.assoc(pre[0])
free_variables.concat(pre.select {|j| j.instance_of?(String) and j.start_with?('?') and not ground_variables.include?(j)})
false
end
Expand All @@ -211,19 +211,19 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
ground_free_variables = ground_variables + free_variables
# Filter elements from precondition
precond_expression.each {|pre|
if pre.first != 'not'
if pre[0] != 'not'
pre_flat = pre.flatten
precond = precond_pos
else
pre_flat = pre.last.flatten
pre_flat = pre[1].flatten
precond = precond_not
end
call_axiom = (assign = pre_flat.first == 'assign') || pre_flat.first == 'call' || axioms.assoc(pre_flat.first)
call_axiom = (assign = pre_flat[0] == 'assign') || pre_flat[0] == 'call' || axioms.assoc(pre_flat[0])
pre_flat.select! {|t| t.start_with?('?') and not ground_variables.include?(t)}
if pre_flat.empty? then ground_axioms_calls << pre
elsif not pre_flat.all? {|t| free_variables.include?(t)}
dependent_attachments << pre
ground_free_variables << pre_flat.first if assign
ground_free_variables << pre_flat[0] if assign
elsif call_axiom then lifted_axioms_calls << pre
else precond << pre
end
Expand Down Expand Up @@ -319,12 +319,12 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st
end
}
unless dependent_attachments.empty?
raise "Call with free variable in #{name} #{dec.first}" if dependent_attachments.flatten.any? {|t| t.start_with?('?') and not ground_free_variables.include?(t)}
raise "Call with free variable in #{name} #{dec[0]}" if dependent_attachments.flatten.any? {|t| t.start_with?('?') and not ground_free_variables.include?(t)}
define_methods << "#{indentation}next unless " << expression_to_hyper(dependent_attachments.unshift('and'), axioms)
end
# Subtasks
define_methods << indentation << (dec[2].empty? ? 'yield []' : "yield [#{indentation} [" << dec[2].map {|g| g.map {|i| evaluate(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 @@ -389,7 +389,7 @@ def compile_problem(domain_name, problem_name, operators, methods, predicates, s
# Tasks
problem_str << "\n },\n # Tasks\n [" <<
tasks.map! {|task,*terms| "\n ['#{task}'#{terms.map! {|o| o.instance_of?(String) ? o.match?(/^-?\d/) ? ", '#{o.to_f}'" : ', _' << o : ', ' << evaluate(o, namespace)}.join}]"}.join(',') <<
"\n ],\n # Debug\n ARGV.first == 'debug',\n # Maximum plans found\n ARGV[1] ? ARGV[1].to_i : -1,\n # Minimum probability for plans\n ARGV[2] ? ARGV[2].to_f : 0#{",\n # Ordered\n false" if ordered == false}"
"\n ],\n # Debug\n ARGV[0] == 'debug',\n # Maximum plans found\n ARGV[1] ? ARGV[1].to_i : -1,\n # Minimum probability for plans\n ARGV[2] ? ARGV[2].to_f : 0#{",\n # Ordered\n false" if ordered == false}"
tasks.unshift(ordered) unless tasks.empty?
problem_str.gsub!(/\b-\b/,'_')
domain_filename ? "# Generated by Hype\nrequire_relative '#{domain_filename}'\n\n#{problem_str}\n)" : "#{problem_str}\n)"
Expand Down
20 changes: 10 additions & 10 deletions UJSHOP_Parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ module UJSHOP_Parser

def define_expression(name, group)
raise "Error with #{name}" unless group.instance_of?(Array)
return unless first = group.first
return unless first = group[0]
# Add implicit conjunction to expression
group.unshift(first = AND) if first.instance_of?(Array)
if first == AND or first == OR
if group.size > 2 then group.drop(1).each {|g| define_expression(name, g)}
elsif group.size == 2 then define_expression(name, group.replace(group.last))
elsif group.size == 2 then define_expression(name, group.replace(group[1]))
else raise "Unexpected zero arguments for #{first} in #{name}"
end
elsif first == NOT
raise "Expected single argument for not in #{name}" if group.size != 2
define_expression(name, group.last)
define_expression(name, group[1])
elsif first == 'call'
raise "Expected function name in #{name} call" unless group[1].instance_of?(String)
group.drop(2).each {|g| define_expression(name, g) if g.instance_of?(Array) and g.first == first}
group.drop(2).each {|g| define_expression(name, g) if g.instance_of?(Array) and g[0] == first}
elsif first == 'assign'
raise "Expected 2 arguments for assign in #{name}" if group.size != 3
raise "Unexpected #{group[1]} as variable to assign in #{name}" unless group[1].start_with?('?')
define_expression(name, group.last) if group.last.instance_of?(Array) and group.last.first == 'call'
define_expression(name, group[2]) if group[2].instance_of?(Array) and group[2][0] == 'call'
elsif a = @axioms.assoc(first)
raise "Axiom #{first} defined with arity #{a[1].size}, unexpected arity #{group.size.pred} in #{name}" if a[1].size != group.size.pred
elsif a = @attachments.assoc(first)
Expand All @@ -56,7 +56,7 @@ def define_effects(name, group)

def parse_operator(op)
op.shift
raise 'Operator without name definition' unless (name = op.first.shift).instance_of?(String)
raise 'Operator without name definition' unless (name = op[0].shift).instance_of?(String)
name.sub!(/^!!/,'invisible_') or name.delete_prefix!('!')
raise "#{name} redefined" if @operators.assoc(name)
raise "#{name} have size #{op.size} instead of 4 or more" if op.size < 4
Expand All @@ -71,7 +71,7 @@ def parse_operator(op)
else
i = 0
until op.empty?
operator << (op.first.instance_of?(String) ? op.shift : "#{name}_#{i}")
operator << (op[0].instance_of?(String) ? op.shift : "#{name}_#{i}")
define_effects(name, del = op.shift)
define_effects(name, add = op.shift)
operator.push(add, del, op.shift.to_f)
Expand All @@ -93,7 +93,7 @@ def parse_method(met)
end
until met.empty?
# Optional label, add index for the unlabeled decompositions
if met.first.instance_of?(String)
if met[0].instance_of?(String)
label = met.shift
raise "#{name} redefined #{label} decomposition" if method.drop(2).assoc(label)
else label = "case_#{method.size - 2}"
Expand Down Expand Up @@ -153,13 +153,13 @@ def parse_domain(domain_filename)
@attachments = []
tokens = tokens[2]
while group = tokens.shift
case group.first
case group[0]
when ':operator' then parse_operator(group)
when ':method' then parse_method(group)
when ':-' then parse_axiom(group)
when ':rewards' then (@rewards = group).shift
when ':attachments' then (@attachments = group).shift
else raise "#{group.first} is not recognized in domain"
else raise "#{group[0]} is not recognized in domain"
end
end
else raise "File #{domain_filename} does not match domain pattern"
Expand Down
2 changes: 1 addition & 1 deletion examples/cookie/pb1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
['get_cookie', 'bob', 'home', 'cookie_store']
],
# Debug
ARGV.first == 'debug'
ARGV[0] == 'debug'
)

abort('Problem failed to generate expected plan') if plan != [
Expand Down
2 changes: 1 addition & 1 deletion examples/healthcare/batch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jshop_output = ARGV.first == 'jshop'
jshop_output = ARGV[0] == 'jshop'

def plan(file, max_plans, min_prob, patients, n)
File.binwrite(file, `ruby pbgenerator.rb -max_plans #{max_plans} -min_prob #{min_prob} -patients #{patients} -physicians #{n} -radiologists #{n} -pathologists #{n} -registrars #{n} -hospitals #{n} -cancers #{n}`)
Expand Down
2 changes: 1 addition & 1 deletion examples/healthcare/pbhealthcare.ujshop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'healthcare'

debug = ARGV.first == 'debug'
debug = ARGV[0] == 'debug'
max_plans = ARGV[1] ? ARGV[1].to_i : -1
min_prob = ARGV[2] ? ARGV[2].to_f : 0

Expand Down
2 changes: 1 addition & 1 deletion examples/healthcare/pbhealthcare_test_steps.ujshop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'healthcare'

debug = ARGV.first == 'debug'
debug = ARGV[0] == 'debug'
max_plans = ARGV[1] ? ARGV[1].to_i : -1
min_prob = ARGV[2] ? ARGV[2].to_f : 0

Expand Down
4 changes: 2 additions & 2 deletions examples/maze/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def adjacent(from, to, goal)
end
}
candidates.sort!.each {|k|
to.replace(k.last)
to.replace(k[1])
yield
}
end
Expand All @@ -39,7 +39,7 @@ def adjacent_not_visited(agent, from, to, goal)
end
}
candidates.sort!.each {|k|
to.replace(k.last)
to.replace(k[1])
yield
}
end
Expand Down
2 changes: 1 addition & 1 deletion examples/maze/pbgenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
front = [0, 0, 0, 0].pack('C4')
end

width = height = (ARGV.first || 7).to_i # 2 * N + 1
width = height = (ARGV[0] || 7).to_i # 2 * N + 1
w = width << 1 | 1
h = height << 1 | 1
room_size = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/biscuit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def get_cookie_goto_and_buy_cookie(_agent, _from, _to)
['get_cookie', _bob, _home, _cookie_store]
],
# Debug
ARGV.first == 'debug',
ARGV[0] == 'debug',
# Maximum plans found
ARGV[1] ? ARGV[1].to_i : -1,
# Minimum probability for plans
Expand Down
2 changes: 1 addition & 1 deletion tests/gardening.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def move_to_load_before_move_to_pour_case_0(_p, _l)
['move_to_load_before_move_to_pour', _plant0, '4.0']
],
# Debug
ARGV.first == 'debug',
ARGV[0] == 'debug',
# Maximum plans found
ARGV[1] ? ARGV[1].to_i : -1,
# Minimum probability for plans
Expand Down
2 changes: 1 addition & 1 deletion tests/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def forward_recursion(_agent, _goal)
['forward', _robot, _goal]
],
# Debug
ARGV.first == 'debug',
ARGV[0] == 'debug',
# Maximum plans found
ARGV[1] ? ARGV[1].to_i : -1,
# Minimum probability for plans
Expand Down
2 changes: 1 addition & 1 deletion tests/postulate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def add_one(_current)
['add_one', '2.0']
],
# Debug
ARGV.first == 'debug',
ARGV[0] == 'debug',
# Maximum plans found
ARGV[1] ? ARGV[1].to_i : -1,
# Minimum probability for plans
Expand Down

0 comments on commit e2fb635

Please sign in to comment.