Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving to python3 and small fix for an FD translator corner case. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 54 additions & 54 deletions prp-scripts/fault-tolerant-conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@
def get_choice(query, options):
not_valid = True
while not_valid:
print
print query
print()
print(query)
i = 1
for opt in options:
print " %d. %s" % (i, opt)
print(" %d. %s" % (i, opt))
i += 1
answer = raw_input("? ")
answer = input("? ")
try:
choice = int(answer)
assert 0 < choice < i
not_valid = False
except:
print "\nError: You must select a number between 1 and %d" % (i-1)
print("\nError: You must select a number between 1 and %d" % (i-1))
return choice - 1


def print_domain_stats(p):
print
print "Actions:\t %d" % len(p.actions)
print "Max faults:\t %d" % p.max_faults
print "1-Normative:\t %s" % str(is_normative(p))
print()
print("Actions:\t %d" % len(p.actions))
print("Max faults:\t %d" % p.max_faults)
print("1-Normative:\t %s" % str(is_normative(p)))


def load_problem(dom_name):
print "Parsing..."
print("Parsing...")
p = parser.Problem(dom_name)
p.max_faults = -1

print "Normalizing..."
print("Normalizing...")
for a in p.actions:
normalize(a)

print "Ready!"
print("Ready!")

return p

Expand Down Expand Up @@ -70,36 +70,36 @@ def add_fault_limit(p, max_faults):
[],
parser.Not([parser.Primitive(started)]),
None,
parser.And(map(parser.Primitive, [started, p.faults[0]]))))
parser.And(list(map(parser.Primitive, [started, p.faults[0]])))))


def pick_faulty_outcome(p, action):
effs = filter(lambda eff: not eff.faulty, action.effect.args)
effs = [eff for eff in action.effect.args if not eff.faulty]
if len(effs) < 1:
print "Error: No more normal effects to become faulty."
print("Error: No more normal effects to become faulty.")
return False
elif len(effs) == 1:
print "\nWarning: You are about to make the final effect faulty."
print("\nWarning: You are about to make the final effect faulty.")
if 1 == get_choice('Continue?', ['Yes', 'No']):
return False

print "\nYou have the following choice of outcomes:"
print("\nYou have the following choice of outcomes:")
for i in range(len(effs)):
print "\n\t--- %d ---" % (i+1)
print str(effs[i])
print
print("\n\t--- %d ---" % (i+1))
print(str(effs[i]))
print()

outcome_choice = get_choice('Which outcome?', ['' for i in range(len(effs))] + ['Cancel'])

if outcome_choice == len(effs):
print "Cancelling..."
print("Cancelling...")
return False

eff = effs[outcome_choice]

make_outcome_faulty(p, eff)

print "Done..."
print("Done...")
return


Expand Down Expand Up @@ -133,7 +133,7 @@ def is_normative(p):

for a in p.actions:
if isinstance(a.effect, parser.Oneof):
return 1 == len(filter(lambda eff: not eff.faulty, a.effect.args))
return 1 == len([eff for eff in a.effect.args if not eff.faulty])


def convert(dom_name):
Expand All @@ -154,65 +154,65 @@ def convert(dom_name):
'Quit']) # 8

if 0 == next_action:
print
print "Actions:"
print()
print("Actions:")
for a in p.actions:
print " - %s" % a.name
print
print(" - %s" % a.name)
print()
elif 1 == next_action:
print
print()
if -1 != p.max_faults:
print "Error: Max faults already set (reload the domain first)"
print("Error: Max faults already set (reload the domain first)")
else:
answer = raw_input("Maximum number of faults? ")
answer = input("Maximum number of faults? ")
try:
choice = int(answer)
if choice < 1:
print "\nError: You must select a positive number for max faults."
print("\nError: You must select a positive number for max faults.")
else:
add_fault_limit(p, choice)
except ValueError:
print "\nError: You must select a number for max faults (%s)" % str(answer)
print("\nError: You must select a number for max faults (%s)" % str(answer))
elif 2 == next_action:
print
print()
if -1 == p.max_faults:
print "Error: You must first set the maximum number of faults."
print("Error: You must first set the maximum number of faults.")
continue

suitable_actions = filter(action_valid, p.actions)
suitable_actions = list(filter(action_valid, p.actions))

if not suitable_actions:
print "Error: No actions available to make faulty."
print("Error: No actions available to make faulty.")
continue

action_choice = get_choice('Which action?', [a.name for a in suitable_actions])
action = p.actions[action_choice]

if not isinstance(action.effect, parser.Oneof):
print "Error: You can only make non-deterministic effects faulty."
print("Error: You can only make non-deterministic effects faulty.")
else:
pick_faulty_outcome(p, action)
elif 3 == next_action:
print
print()
if -1 == p.max_faults:
print "Error: You must first set the maximum number of faults."
print("Error: You must first set the maximum number of faults.")
continue

suitable_actions = filter(action_valid, p.actions)
suitable_actions = list(filter(action_valid, p.actions))

if not suitable_actions:
print "Error: No actions available to make faulty."
print("Error: No actions available to make faulty.")
continue

for act in suitable_actions:
effs = filter(lambda eff: not eff.faulty, act.effect.args)
effs = [eff for eff in act.effect.args if not eff.faulty]
if len(effs) > 1:
print "\nMaking action %s faulty." % act.name
print "You have the following choice of outcomes:"
print("\nMaking action %s faulty." % act.name)
print("You have the following choice of outcomes:")
for i in range(len(effs)):
print "\n\t--- %d ---" % (i+1)
print str(effs[i])
print
print("\n\t--- %d ---" % (i+1))
print(str(effs[i]))
print()

outcome_choice = get_choice('Which is the normal (i.e., non-faulty) outcome?', ['' for i in range(len(effs))])

Expand All @@ -223,24 +223,24 @@ def convert(dom_name):
elif 4 == next_action:
print_domain_stats(p)
elif 5 == next_action:
print "\n\n-----------------\n"
print("\n\n-----------------\n")
p._export_domain(sys.stdout)
print "\n\n-----------------\n"
print("\n\n-----------------\n")
elif 6 == next_action:
print
answer = raw_input("What filename would you like to use? ")
print()
answer = input("What filename would you like to use? ")
p.export(answer, '')
elif 7 == next_action:
p = load_problem(dom_name)
elif 8 == next_action:
print
print()
return


if __name__ == '__main__':
print
print()
if len(sys.argv) != 2:
print " Usage: python %s <domain>\n" % sys.argv[0]
print(" Usage: python %s <domain>\n" % sys.argv[0])
sys.exit(1)

convert(sys.argv[1])
14 changes: 7 additions & 7 deletions prp-scripts/fondparser/action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from formula import Formula
from predicate import Predicate
from .formula import Formula
from .predicate import Predicate

class Action (object):
"""
Expand Down Expand Up @@ -124,11 +124,11 @@ def dump (self, lvl=0):
lvl: Tab level
"""

print "\t" * lvl + "Action %s" % self.name
print "\t" * (lvl + 1) + "Parameters: " + ", ".join([v_type + " " + v_name for v_name, v_type in self.parameters])
print(("\t" * lvl + "Action %s" % self.name))
print(("\t" * (lvl + 1) + "Parameters: " + ", ".join([v_type + " " + v_name for v_name, v_type in self.parameters])))

print (lvl + 1) * "\t" + "Precondition: " + str (self.precondition)
print(((lvl + 1) * "\t" + "Precondition: " + str (self.precondition)))

print (lvl + 1) * "\t" + "Effect: " + str (self.effect)
print(((lvl + 1) * "\t" + "Effect: " + str (self.effect)))

print (lvl + 1) * "\t" + "Observe: " + str(self.observe)
print(((lvl + 1) * "\t" + "Observe: " + str(self.observe)))
10 changes: 5 additions & 5 deletions prp-scripts/fondparser/formula.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from predicate import Predicate
from .predicate import Predicate


class Formula(object):
Expand Down Expand Up @@ -231,8 +231,8 @@ def __init__(self, args):
Inputs:
args: list of formula objects
"""
args = filter(lambda x: not isinstance(x, And), args) + \
[item for andarg in filter(lambda x: isinstance(x, And), args) for item in andarg.args]
args = [x for x in args if not isinstance(x, And)] + \
[item for andarg in [x for x in args if isinstance(x, And)] for item in andarg.args]

super(And, self).__init__("and", args)

Expand Down Expand Up @@ -432,8 +432,8 @@ def to_ground (self, fluent_dict):
self.predicate.args = None
if hash (self.predicate) not in fluent_dict:
for p in sorted (fluent_dict.values()):
print p
print "Did not find %s" % str(self.predicate)
print(p)
print(("Did not find %s" % str(self.predicate)))
self.predicate = fluent_dict[hash(self.predicate)]

def __eq__ (self, f):
Expand Down
44 changes: 22 additions & 22 deletions prp-scripts/fondparser/grounder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from parser import Problem
from action import Action
from formula import Primitive, Forall, When, And
from predicate import Predicate
from .parser import Problem
from .action import Action
from .formula import Primitive, Forall, When, And
from .predicate import Predicate
import itertools


Expand Down Expand Up @@ -98,34 +98,34 @@ def is_equal (self, p):
assert isinstance (p, GroundProblem), "Must compare two ground problems"

if self.objects != p.objects:
print "objects"
print("objects")
return False

if self.init != p.init:
return False

if self.goal != p.goal:
print "goal"
print("goal")
return False

if not all ([sa == pa for sa, pa in \
zip (sorted (list (self.operators)), \
sorted (list (p.operators)))]):
print "operators"
print("operators")
return False

if not all ([sp == pp for sp, pp in \
zip (sorted (list (self.fluents)), \
sorted (list (p.fluents)))]):
print "fluents"
print "*self*"
print sorted( list( self.fluents))
print "*p*"
print sorted (list( p.fluents))
print("fluents")
print("*self*")
print((sorted( list( self.fluents))))
print("*p*")
print((sorted (list( p.fluents))))
return False

if self.types != p.types or self.parent_types != p.parent_types:
print "types"
print("types")
return False

return True
Expand All @@ -148,7 +148,7 @@ def _export_domain (self, fp, sp=" "):

# types
#TODO likely wrong, doesn't capture the type hierarchy
s = " ".join (filter(lambda t: t!= Predicate.OBJECT, self.types))
s = " ".join ([t for t in self.types if t!= Predicate.OBJECT])
fp.write (sp + "(:types %s)%s" %(s, "\n"))

# fluents (ground predicates)
Expand Down Expand Up @@ -403,16 +403,16 @@ def dump(self):
"Fluents": self.fluents
}

for k, v in d.iteritems():
print "*** %s ***" % k
for k, v in list(d.items()):
print(("*** %s ***" % k))
if k == "Operators":
for op in self.operators:
op.dump(lvl=1) # inherited from superclass Action
elif hasattr(v, "__iter__"):
for item in v:
print "\t" + str(item)
print(("\t" + str(item)))
else:
print "\t" + str(v)
print(("\t" + str(v)))


class Operator(Action):
Expand Down Expand Up @@ -465,12 +465,12 @@ def dump(self, lvl=0):
"""

# for operators, sufficient just to print the name, because pretty self-explanatory
print "\t" * lvl + "Operator %s" % self.name
print(("\t" * lvl + "Operator %s" % self.name))
if len(self.parameters) > 0:
print "\t" * (lvl + 1) + "Parameters: " + \
", ".join([v_type + " " + v_name for v_name, v_type in self.parameters])
print(("\t" * (lvl + 1) + "Parameters: " + \
", ".join([v_type + " " + v_name for v_name, v_type in self.parameters])))
else:
print "\t" * (lvl + 1) + "Parameters: <none>"
print(("\t" * (lvl + 1) + "Parameters: <none>"))
#print(lvl + 1) * "\t" + "Precondition: " + str(self.precondition)
#print(lvl + 1) * "\t" + "Effect: " + str(self.effect)
#print(lvl + 1) * "\t" + "Observe: " + str(self.observe)
Loading