From e2fb635875bd8a0158d75638f185706535e07b82 Mon Sep 17 00:00:00 2001 From: Mau Magnaguagno Date: Fri, 24 Nov 2023 07:58:26 -0300 Subject: [PATCH] Prefer Array [index] instead of first/last --- Hype.rb | 4 +- Hypertension_U.rb | 8 ++-- UHyper_Compiler.rb | 38 +++++++++---------- UJSHOP_Parser.rb | 20 +++++----- examples/cookie/pb1.rb | 2 +- examples/healthcare/batch.rb | 2 +- examples/healthcare/pbhealthcare.ujshop.rb | 2 +- .../pbhealthcare_test_steps.ujshop.rb | 2 +- examples/maze/external.rb | 4 +- examples/maze/pbgenerator.rb | 2 +- tests/biscuit.rb | 2 +- tests/gardening.rb | 2 +- tests/path.rb | 2 +- tests/postulate.rb | 2 +- 14 files changed, 46 insertions(+), 46 deletions(-) diff --git a/Hype.rb b/Hype.rb index 38960a2..56f9b94 100755 --- a/Hype.rb +++ b/Hype.rb @@ -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) diff --git a/Hypertension_U.rb b/Hypertension_U.rb index 272bf63..8490674 100644 --- a/Hypertension_U.rb +++ b/Hypertension_U.rb @@ -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 @@ -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 @@ -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) diff --git a/UHyper_Compiler.rb b/UHyper_Compiler.rb index c7d03b6..7d17311 100644 --- a/UHyper_Compiler.rb +++ b/UHyper_Compiler.rb @@ -8,10 +8,10 @@ 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) @@ -19,8 +19,8 @@ def expression_to_hyper(precond_expression, axioms) 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 @@ -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(', ')}]" @@ -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 = {}) @@ -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 = [] @@ -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 @@ -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 @@ -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 ],") } @@ -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)" diff --git a/UJSHOP_Parser.rb b/UJSHOP_Parser.rb index 3a59193..85dc73c 100644 --- a/UJSHOP_Parser.rb +++ b/UJSHOP_Parser.rb @@ -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) @@ -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 @@ -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) @@ -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}" @@ -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" diff --git a/examples/cookie/pb1.rb b/examples/cookie/pb1.rb index 26ec3e3..ec40df6 100644 --- a/examples/cookie/pb1.rb +++ b/examples/cookie/pb1.rb @@ -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 != [ diff --git a/examples/healthcare/batch.rb b/examples/healthcare/batch.rb index 966802c..aff0ddd 100644 --- a/examples/healthcare/batch.rb +++ b/examples/healthcare/batch.rb @@ -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}`) diff --git a/examples/healthcare/pbhealthcare.ujshop.rb b/examples/healthcare/pbhealthcare.ujshop.rb index cb8423e..2ed780f 100644 --- a/examples/healthcare/pbhealthcare.ujshop.rb +++ b/examples/healthcare/pbhealthcare.ujshop.rb @@ -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 diff --git a/examples/healthcare/pbhealthcare_test_steps.ujshop.rb b/examples/healthcare/pbhealthcare_test_steps.ujshop.rb index bb81577..679fe33 100644 --- a/examples/healthcare/pbhealthcare_test_steps.ujshop.rb +++ b/examples/healthcare/pbhealthcare_test_steps.ujshop.rb @@ -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 diff --git a/examples/maze/external.rb b/examples/maze/external.rb index ac4780b..b828a4c 100644 --- a/examples/maze/external.rb +++ b/examples/maze/external.rb @@ -19,7 +19,7 @@ def adjacent(from, to, goal) end } candidates.sort!.each {|k| - to.replace(k.last) + to.replace(k[1]) yield } end @@ -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 diff --git a/examples/maze/pbgenerator.rb b/examples/maze/pbgenerator.rb index 5729821..1c01fcf 100644 --- a/examples/maze/pbgenerator.rb +++ b/examples/maze/pbgenerator.rb @@ -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 diff --git a/tests/biscuit.rb b/tests/biscuit.rb index 319c8c4..bc38e5e 100644 --- a/tests/biscuit.rb +++ b/tests/biscuit.rb @@ -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 diff --git a/tests/gardening.rb b/tests/gardening.rb index 805a39b..9a6195e 100644 --- a/tests/gardening.rb +++ b/tests/gardening.rb @@ -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 diff --git a/tests/path.rb b/tests/path.rb index 7ced278..feb4064 100644 --- a/tests/path.rb +++ b/tests/path.rb @@ -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 diff --git a/tests/postulate.rb b/tests/postulate.rb index aec3057..0d0cefe 100644 --- a/tests/postulate.rb +++ b/tests/postulate.rb @@ -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