'.format(self.name)
+
E0 = Grammar('E0',
Rules( # Grammar for E_0 [Figure 22.4]
S='NP VP | S Conjunction S',
@@ -196,15 +197,15 @@ def CYK_parse(words, grammar):
P = defaultdict(float)
# Insert lexical rules for each word.
for (i, word) in enumerate(words):
- for (X, p) in grammar.categories[word]: # XXX grammar.categories needs changing, above
+ for (X, p) in grammar.categories[word]: # XXX grammar.categories needs changing, above
P[X, i, 1] = p
# Combine first and second parts of right-hand sides of rules,
# from short to long.
for length in range(2, N+1):
for start in range(N-length+1):
- for len1 in range(1, length): # N.B. the book incorrectly has N instead of length
+ for len1 in range(1, length): # N.B. the book incorrectly has N instead of length
len2 = length - len1
- for (X, Y, Z, p) in grammar.cnf_rules(): # XXX grammar needs this method
+ for (X, Y, Z, p) in grammar.cnf_rules(): # XXX grammar needs this method
P[X, start, length] = max(P[X, start, length],
P[Y, start, len1] * P[Z, start+len1, len2] * p)
return P
@@ -215,17 +216,18 @@ def CYK_parse(words, grammar):
# First entry in list is the base URL, and then following are relative URL pages
examplePagesSet = ["https://en.wikipedia.org/wiki/", "Aesthetics", "Analytic_philosophy",
- "Ancient_Greek", "Aristotle", "Astrology","Atheism", "Baruch_Spinoza",
+ "Ancient_Greek", "Aristotle", "Astrology", "Atheism", "Baruch_Spinoza",
"Belief", "Betrand Russell", "Confucius", "Consciousness",
"Continental Philosophy", "Dialectic", "Eastern_Philosophy",
"Epistemology", "Ethics", "Existentialism", "Friedrich_Nietzsche",
"Idealism", "Immanuel_Kant", "List_of_political_philosophers", "Logic",
"Metaphysics", "Philosophers", "Philosophy", "Philosophy_of_mind", "Physics",
- "Plato", "Political_philosophy", "Pythagoras", "Rationalism","Social_philosophy",
- "Socrates", "Subjectivity", "Theology", "Truth", "Western_philosophy"]
+ "Plato", "Political_philosophy", "Pythagoras", "Rationalism",
+ "Social_philosophy", "Socrates", "Subjectivity", "Theology",
+ "Truth", "Western_philosophy"]
-def loadPageHTML( addressList ):
+def loadPageHTML(addressList):
"""Download HTML page content for every URL address passed as argument"""
contentDict = {}
for addr in addressList:
@@ -236,20 +238,23 @@ def loadPageHTML( addressList ):
contentDict[addr] = html
return contentDict
-def initPages( addressList ):
+
+def initPages(addressList):
"""Create a dictionary of pages from a list of URL addresses"""
pages = {}
for addr in addressList:
pages[addr] = Page(addr)
return pages
-def stripRawHTML( raw_html ):
+
+def stripRawHTML(raw_html):
"""Remove the section of the HTML which contains links to stylesheets etc.,
and remove all other unnessecary HTML"""
# TODO: Strip more out of the raw html
- return re.sub(".*?", "", raw_html, flags=re.DOTALL) # remove section
+ return re.sub(".*?", "", raw_html, flags=re.DOTALL) # remove section
-def determineInlinks( page ):
+
+def determineInlinks(page):
"""Given a set of pages that have their outlinks determined, we can fill
out a page's inlinks by looking through all other page's outlinks"""
inlinks = []
@@ -260,14 +265,16 @@ def determineInlinks( page ):
inlinks.append(addr)
return inlinks
-def findOutlinks( page, handleURLs=None ):
+
+def findOutlinks(page, handleURLs=None):
"""Search a page's HTML content for URL links to other pages"""
urls = re.findall(r'href=[\'"]?([^\'" >]+)', pagesContent[page.address])
if handleURLs:
urls = handleURLs(urls)
return urls
-def onlyWikipediaURLS( urls ):
+
+def onlyWikipediaURLS(urls):
"""Some example HTML page data is from wikipedia. This function converts
relative wikipedia links to full wikipedia URLs"""
wikiURLs = [url for url in urls if url.startswith('/wiki/')]
@@ -277,11 +284,11 @@ def onlyWikipediaURLS( urls ):
# ______________________________________________________________________________
# HITS Helper Functions
-def expand_pages( pages ):
+def expand_pages(pages):
"""From Textbook: adds in every page that links to or is linked from one of
the relevant pages."""
expanded = {}
- for addr,page in pages.items():
+ for addr, page in pages.items():
if addr not in expanded:
expanded[addr] = page
for inlink in page.inlinks:
@@ -292,6 +299,7 @@ def expand_pages( pages ):
expanded[outlink] = pagesIndex[outlink]
return expanded
+
def relevant_pages(query):
"""Relevant pages are pages that contain the query in its entireity.
If a page's content contains the query it is returned by the function."""
@@ -302,16 +310,18 @@ def relevant_pages(query):
relevant[addr] = page
return relevant
-def normalize( pages ):
+
+def normalize(pages):
"""From the pseudocode: Normalize divides each page's score by the sum of
the squares of all pages' scores (separately for both the authority and hubs scores).
"""
- summed_hub = sum(page.hub**2 for _,page in pages.items())
- summed_auth = sum(page.authority**2 for _,page in pages.items())
+ summed_hub = sum(page.hub**2 for _, page in pages.items())
+ summed_auth = sum(page.authority**2 for _, page in pages.items())
for _, page in pages.items():
page.hub /= summed_hub
page.authority /= summed_auth
+
class ConvergenceDetector(object):
"""If the hub and authority values of the pages are no longer changing, we have
reached a convergence and further iterations will have no effect. This detects convergence
@@ -326,16 +336,16 @@ def __call__(self):
def detect(self):
curr_hubs = [page.hub for addr, page in pagesIndex.items()]
curr_auths = [page.authority for addr, page in pagesIndex.items()]
- if self.hub_history == None:
- self.hub_history, self.auth_history = [],[]
+ if self.hub_history is None:
+ self.hub_history, self.auth_history = [], []
else:
- diffsHub = [abs(x-y) for x, y in zip(curr_hubs,self.hub_history[-1])]
- diffsAuth = [abs(x-y) for x, y in zip(curr_auths,self.auth_history[-1])]
+ diffsHub = [abs(x-y) for x, y in zip(curr_hubs, self.hub_history[-1])]
+ diffsAuth = [abs(x-y) for x, y in zip(curr_auths, self.auth_history[-1])]
aveDeltaHub = sum(diffsHub)/float(len(pagesIndex))
aveDeltaAuth = sum(diffsAuth)/float(len(pagesIndex))
- if aveDeltaHub < 0.01 and aveDeltaAuth < 0.01: # may need tweaking
+ if aveDeltaHub < 0.01 and aveDeltaAuth < 0.01: # may need tweaking
return True
- if len(self.hub_history) > 2: # prevent list from getting long
+ if len(self.hub_history) > 2: # prevent list from getting long
del self.hub_history[0]
del self.auth_history[0]
self.hub_history.append([x for x in curr_hubs])
@@ -343,12 +353,13 @@ def detect(self):
return False
-def getInlinks( page ):
+def getInlinks(page):
if not page.inlinks:
page.inlinks = determineInlinks(page)
- return [p for addr, p in pagesIndex.items() if addr in page.inlinks ]
+ return [p for addr, p in pagesIndex.items() if addr in page.inlinks]
-def getOutlinks( page ):
+
+def getOutlinks(page):
if not page.outlinks:
page.outlinks = findOutlinks(page)
return [p for addr, p in pagesIndex.items() if addr in page.outlinks]
@@ -365,20 +376,22 @@ def __init__(self, address, hub=0, authority=0, inlinks=None, outlinks=None):
self.inlinks = inlinks
self.outlinks = outlinks
-pagesContent = {} # maps Page relative or absolute URL/location to page's HTML content
+
+pagesContent = {} # maps Page relative or absolute URL/location to page's HTML content
pagesIndex = {}
-convergence = ConvergenceDetector() # assign function to variable to mimic pseudocode's syntax
+convergence = ConvergenceDetector() # assign function to variable to mimic pseudocode's syntax
+
def HITS(query):
"""The HITS algorithm for computing hubs and authorities with respect to a query."""
- pages = expand_pages(relevant_pages(query)) # in order to 'map' faithfully to pseudocode we
- for p in pages: # won't pass the list of pages as an argument
+ pages = expand_pages(relevant_pages(query)) # in order to 'map' faithfully to pseudocode we
+ for p in pages: # won't pass the list of pages as an argument
p.authority = 1
p.hub = 1
- while True: # repeat until... convergence
+ while True: # repeat until... convergence
for p in pages:
p.authority = sum(x.hub for x in getInlinks(p)) # p.authority ← ∑i Inlinki(p).Hub
- p.hub = sum(x.authority for x in getOutlinks(p)) # p.hub ← ∑i Outlinki(p).Authority
+ p.hub = sum(x.authority for x in getOutlinks(p)) # p.hub ← ∑i Outlinki(p).Authority
normalize(pages)
if convergence():
break
diff --git a/planning.py b/planning.py
index 47eae77da..b92cb6eaa 100644
--- a/planning.py
+++ b/planning.py
@@ -5,6 +5,7 @@
from utils import Expr, expr, first
from logic import FolKB
+
class PDLL:
"""
PDLL used to define a search problem.
@@ -34,6 +35,7 @@ def act(self, action):
raise Exception("Action '{}' pre-conditions not satisfied".format(action))
list_action(self.kb, args)
+
class Action:
"""
Defines an action schema using preconditions and effects.
@@ -112,16 +114,19 @@ def goal_test(kb):
return False
return True
- ## Actions
+ # Actions
+
# Load
- precond_pos = [expr("At(c, a)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"), expr("Airport(a)")]
+ precond_pos = [expr("At(c, a)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"),
+ expr("Airport(a)")]
precond_neg = []
effect_add = [expr("In(c, p)")]
effect_rem = [expr("At(c, a)")]
load = Action(expr("Load(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem])
# Unload
- precond_pos = [expr("In(c, p)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"), expr("Airport(a)")]
+ precond_pos = [expr("In(c, p)"), expr("At(p, a)"), expr("Cargo(c)"), expr("Plane(p)"),
+ expr("Airport(a)")]
precond_neg = []
effect_add = [expr("At(c, a)")]
effect_rem = [expr("In(c, p)")]
@@ -151,31 +156,34 @@ def goal_test(kb):
return False
return True
- ##Actions
- #Remove
+ # Actions
+
+ # Remove
precond_pos = [expr("At(obj, loc)")]
precond_neg = []
effect_add = [expr("At(obj, Ground)")]
effect_rem = [expr("At(obj, loc)")]
remove = Action(expr("Remove(obj, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem])
- #PutOn
+ # PutOn
precond_pos = [expr("Tire(t)"), expr("At(t, Ground)")]
precond_neg = [expr("At(Flat, Axle)")]
effect_add = [expr("At(t, Axle)")]
effect_rem = [expr("At(t, Ground)")]
put_on = Action(expr("PutOn(t, Axle)"), [precond_pos, precond_neg], [effect_add, effect_rem])
- #LeaveOvernight
+ # LeaveOvernight
precond_pos = []
precond_neg = []
effect_add = []
effect_rem = [expr("At(Spare, Ground)"), expr("At(Spare, Axle)"), expr("At(Spare, Trunk)"),
expr("At(Flat, Ground)"), expr("At(Flat, Axle)"), expr("At(Flat, Trunk)")]
- leave_overnight = Action(expr("LeaveOvernight"), [precond_pos, precond_neg], [effect_add, effect_rem])
+ leave_overnight = Action(expr("LeaveOvernight"), [precond_pos, precond_neg],
+ [effect_add, effect_rem])
return PDLL(init, [remove, put_on, leave_overnight], goal_test)
+
def three_block_tower():
init = [expr('On(A, Table)'),
expr('On(B, Table)'),
@@ -193,23 +201,27 @@ def goal_test(kb):
return False
return True
- ## Actions
+ # Actions
+
# Move
- precond_pos = [expr('On(b, x)'), expr('Clear(b)'), expr('Clear(y)'), expr('Block(b)'), expr('Block(y)')]
+ precond_pos = [expr('On(b, x)'), expr('Clear(b)'), expr('Clear(y)'), expr('Block(b)'),
+ expr('Block(y)')]
precond_neg = []
effect_add = [expr('On(b, y)'), expr('Clear(x)')]
effect_rem = [expr('On(b, x)'), expr('Clear(y)')]
move = Action(expr('Move(b, x, y)'), [precond_pos, precond_neg], [effect_add, effect_rem])
-
+
# MoveToTable
precond_pos = [expr('On(b, x)'), expr('Clear(b)'), expr('Block(b)')]
precond_neg = []
effect_add = [expr('On(b, Table)'), expr('Clear(x)')]
effect_rem = [expr('On(b, x)')]
- moveToTable = Action(expr('MoveToTable(b, x)'), [precond_pos, precond_neg], [effect_add, effect_rem])
+ moveToTable = Action(expr('MoveToTable(b, x)'), [precond_pos, precond_neg],
+ [effect_add, effect_rem])
return PDLL(init, [move, moveToTable], goal_test)
+
def have_cake_and_eat_cake_too():
init = [expr('Have(Cake)')]
@@ -220,7 +232,8 @@ def goal_test(kb):
return False
return True
- ##Actions
+ # Actions
+
# Eat cake
precond_pos = [expr('Have(Cake)')]
precond_neg = []
@@ -228,7 +241,7 @@ def goal_test(kb):
effect_rem = [expr('Have(Cake)')]
eat_cake = Action(expr('Eat(Cake)'), [precond_pos, precond_neg], [effect_add, effect_rem])
- #Bake Cake
+ # Bake Cake
precond_pos = []
precond_neg = [expr('Have(Cake)')]
effect_add = [expr('Have(Cake)')]
@@ -247,69 +260,63 @@ class Level():
def __init__(self, poskb, negkb):
self.poskb = poskb
- #Current state
+ # Current state
self.current_state_pos = poskb.clauses
self.current_state_neg = negkb.clauses
- #Current action to current state link
+ # Current action to current state link
self.current_action_links_pos = {}
self.current_action_links_neg = {}
- #Current state to action link
+ # Current state to action link
self.current_state_links_pos = {}
self.current_state_links_neg = {}
- #Current action to next state link
+ # Current action to next state link
self.next_action_links = {}
- #Next state to current action link
+ # Next state to current action link
self.next_state_links_pos = {}
self.next_state_links_neg = {}
self.mutex = []
-
def __call__(self, actions, objects):
self.build(actions, objects)
self.find_mutex()
-
def find_mutex(self):
- #Inconsistent effects
+ # Inconsistent effects
for poseff in self.next_state_links_pos:
- #negeff = Expr('not'+poseff.op, poseff.args)
negeff = poseff
if negeff in self.next_state_links_neg:
for a in self.next_state_links_pos[poseff]:
for b in self.next_state_links_neg[negeff]:
- if set([a,b]) not in self.mutex:
- self.mutex.append(set([a,b]))
+ if set([a, b]) not in self.mutex:
+ self.mutex.append(set([a, b]))
- #Interference
+ # Interference
for posprecond in self.current_state_links_pos:
- #negeff = Expr('not'+posprecond.op, posprecond.args)
negeff = posprecond
if negeff in self.next_state_links_neg:
for a in self.current_state_links_pos[posprecond]:
for b in self.next_state_links_neg[negeff]:
- if set([a,b]) not in self.mutex:
- self.mutex.append(set([a,b]))
+ if set([a, b]) not in self.mutex:
+ self.mutex.append(set([a, b]))
for negprecond in self.current_state_links_neg:
- #poseff = Expr(negprecond.op[3:], negprecond.args)
poseff = negprecond
if poseff in self.next_state_links_pos:
for a in self.next_state_links_pos[poseff]:
for b in self.current_state_links_neg[negprecond]:
- if set([a,b]) not in self.mutex:
- self.mutex.append(set([a,b]))
+ if set([a, b]) not in self.mutex:
+ self.mutex.append(set([a, b]))
- #Competing needs
+ # Competing needs
for posprecond in self.current_state_links_pos:
- #negprecond = Expr('not'+posprecond.op, posprecond.args)
negprecond = posprecond
if negprecond in self.current_state_links_neg:
for a in self.current_state_links_pos[posprecond]:
for b in self.current_state_links_neg[negprecond]:
- if set([a,b]) not in self.mutex:
- self.mutex.append(set([a,b]))
+ if set([a, b]) not in self.mutex:
+ self.mutex.append(set([a, b]))
- #Inconsistent support
+ # Inconsistent support
state_mutex = []
for pair in self.mutex:
next_state_0 = self.next_action_links[list(pair)[0]]
@@ -322,22 +329,22 @@ def find_mutex(self):
self.mutex = self.mutex+state_mutex
-
def build(self, actions, objects):
- #Add persistence actions for positive states
+ # Add persistence actions for positive states
for clause in self.current_state_pos:
self.current_action_links_pos[Expr('Persistence', clause)] = [clause]
self.next_action_links[Expr('Persistence', clause)] = [clause]
self.current_state_links_pos[clause] = [Expr('Persistence', clause)]
self.next_state_links_pos[clause] = [Expr('Persistence', clause)]
- #Add persistence actions for negative states
+ # Add persistence actions for negative states
for clause in self.current_state_neg:
- self.current_action_links_neg[Expr('Persistence', Expr('not'+clause.op, clause.args))] = [clause]
- self.next_action_links[Expr('Persistence', Expr('not'+clause.op, clause.args))] = [clause]
- self.current_state_links_neg[clause] = [Expr('Persistence', Expr('not'+clause.op, clause.args))]
- self.next_state_links_neg[clause] = [Expr('Persistence', Expr('not'+clause.op, clause.args))]
+ not_expr = Expr('not'+clause.op, clause.args)
+ self.current_action_links_neg[Expr('Persistence', not_expr)] = [clause]
+ self.next_action_links[Expr('Persistence', not_expr)] = [clause]
+ self.current_state_links_neg[clause] = [Expr('Persistence', not_expr)]
+ self.next_state_links_neg[clause] = [Expr('Persistence', not_expr)]
for a in actions:
num_args = len(a.args)
@@ -365,7 +372,6 @@ def build(self, actions, objects):
for clause in a.precond_neg:
new_clause = a.substitute(clause, arg)
- #new_clause = Expr('not'+new_clause.op, new_clause.arg)
self.current_action_links_neg[new_action].append(new_clause)
if new_clause in self.current_state_links_neg:
self.current_state_links_neg[new_clause].append(new_action)
@@ -389,9 +395,10 @@ def build(self, actions, objects):
else:
self.next_state_links_neg[new_clause] = [new_action]
-
def perform_actions(self):
- new_kb_pos, new_kb_neg = FolKB(list(set(self.next_state_links_pos.keys()))), FolKB(list(set(self.next_state_links_neg.keys())))
+ new_kb_pos = FolKB(list(set(self.next_state_links_pos.keys())))
+ new_kb_neg = FolKB(list(set(self.next_state_links_neg.keys())))
+
return Level(new_kb_pos, new_kb_neg)
@@ -435,7 +442,12 @@ def __init__(self, pdll, negkb):
self.solution = []
def check_leveloff(self):
- if (set(self.graph.levels[-1].current_state_pos) == set(self.graph.levels[-2].current_state_pos)) and (set(lf.graph.levels[-1].current_state_neg) == set(self.graph.levels[-2].current_state_neg)):
+ first_check = (set(self.graph.levels[-1].current_state_pos) ==
+ set(self.graph.levels[-2].current_state_pos))
+ second_check = (set(self.graph.levels[-1].current_state_neg) ==
+ set(self.graph.levels[-2].current_state_neg))
+
+ if first_check and second_check:
return True
def extract_solution(self, goals_pos, goals_neg, index):
@@ -446,7 +458,7 @@ def extract_solution(self, goals_pos, goals_neg, index):
level = self.graph.levels[index-1]
- #Create all combinations of actions that satisfy the goal
+ # Create all combinations of actions that satisfy the goal
actions = []
for goal in goals_pos:
actions.append(level.next_state_links_pos[goal])
@@ -456,7 +468,7 @@ def extract_solution(self, goals_pos, goals_neg, index):
all_actions = list(itertools.product(*actions))
- #Filter out the action combinations which contain mutexes
+ # Filter out the action combinations which contain mutexes
non_mutex_actions = []
for action_tuple in all_actions:
action_pairs = itertools.combinations(list(set(action_tuple)), 2)
@@ -466,7 +478,7 @@ def extract_solution(self, goals_pos, goals_neg, index):
non_mutex_actions.pop(-1)
break
- #Recursion
+ # Recursion
for action_list in non_mutex_actions:
if [action_list, index] not in self.solution:
self.solution.append([action_list, index])
@@ -488,7 +500,7 @@ def extract_solution(self, goals_pos, goals_neg, index):
else:
self.extract_solution(new_goals_pos, new_goals_neg, index-1)
- #Level-Order multiple solutions
+ # Level-Order multiple solutions
solution = []
for item in self.solution:
if item[1] == -1:
@@ -515,12 +527,14 @@ def spare_tire_graphplan():
pdll = spare_tire()
negkb = FolKB([expr('At(Flat, Trunk)')])
graphplan = GraphPlan(pdll, negkb)
- ##Not sure
+
+ # Not sure
goals_pos = [expr('At(Spare, Axle)'), expr('At(Flat, Ground)')]
goals_neg = []
while True:
- if goal_test(graphplan.graph.levels[-1].poskb, goals_pos) and graphplan.graph.non_mutex_goals(goals_pos+goals_neg, -1):
+ if (goal_test(graphplan.graph.levels[-1].poskb, goals_pos) and
+ graphplan.graph.non_mutex_goals(goals_pos+goals_neg, -1)):
solution = graphplan.extract_solution(goals_pos, goals_neg, -1)
if solution:
return solution
@@ -528,6 +542,7 @@ def spare_tire_graphplan():
if len(graphplan.graph.levels)>=2 and graphplan.check_leveloff():
return None
+
def double_tennis_problem():
init = [expr('At(A, LeftBaseLine)'),
expr('At(B, RightNet)'),
@@ -542,15 +557,16 @@ def goal_test(kb):
return False
return True
- ##actions
- #hit
- precond_pos=[expr("Approaching(Ball, loc)"), expr("At(actor, loc)")]
- precond_neg=[]
- effect_add=[expr("Returned(Ball)")]
+ # Actions
+
+ # Hit
+ precond_pos = [expr("Approaching(Ball,loc)"), expr("At(actor,loc)")]
+ precond_neg = []
+ effect_add = [expr("Returned(Ball)")]
effect_rem = []
hit = Action(expr("Hit(actor, Ball)"), [precond_pos, precond_neg], [effect_add, effect_rem])
- #go
+ # Go
precond_pos = [expr("At(actor, loc)")]
precond_neg = []
effect_add = [expr("At(actor, to)")]
diff --git a/probability.py b/probability.py
index a5699b7f4..e102e4dd8 100644
--- a/probability.py
+++ b/probability.py
@@ -272,6 +272,7 @@ def sample(self, event):
def __repr__(self):
return repr((self.variable, ' '.join(self.parents)))
+
# Burglary example [Figure 14.2]
T, F = True, False
@@ -409,6 +410,7 @@ def all_events(variables, bn, e):
# [Figure 14.12a]: sprinkler network
+
sprinkler = BayesNet([
('Cloudy', '', 0.5),
('Sprinkler', 'Cloudy', {T: 0.10, F: 0.50}),
diff --git a/rl.py b/rl.py
index 77a04f98a..43d860935 100644
--- a/rl.py
+++ b/rl.py
@@ -29,7 +29,7 @@ def T(self, s, a):
def __init__(self, pi, mdp):
self.pi = pi
- self.mdp = PassiveADPAgent.ModelMDP(mdp.init, mdp.actlist,
+ self.mdp = PassiveADPAgent.ModelMDP(mdp.init, mdp.actlist,
mdp.terminals, mdp.gamma, mdp.states)
self.U = {}
self.Nsa = defaultdict(int)
@@ -91,7 +91,7 @@ def __init__(self, pi, mdp, alpha=None):
def __call__(self, percept):
s1, r1 = self.update_state(percept)
- pi, U, Ns, s, a, r = self.pi, self.U, self.Ns, self.s, self.a, self.r
+ pi, U, Ns, s, r = self.pi, self.U, self.Ns, self.s, self.r
alpha, gamma, terminals = self.alpha, self.gamma, self.terminals
if not Ns[s1]:
U[s1] = r1
@@ -153,13 +153,15 @@ def actions_in_state(self, state):
def __call__(self, percept):
s1, r1 = self.update_state(percept)
Q, Nsa, s, a, r = self.Q, self.Nsa, self.s, self.a, self.r
- alpha, gamma, terminals, actions_in_state = self.alpha, self.gamma, self.terminals, self.actions_in_state
+ alpha, gamma, terminals = self.alpha, self.gamma, self.terminals,
+ actions_in_state = self.actions_in_state
+
if s in terminals:
Q[s, None] = r1
if s is not None:
Nsa[s, a] += 1
- Q[s, a] += alpha(Nsa[s, a]) * (r + gamma * max(Q[s1, a1] for a1 in actions_in_state(s1))
- - Q[s, a])
+ Q[s, a] += alpha(Nsa[s, a]) * (r + gamma * max(Q[s1, a1]
+ for a1 in actions_in_state(s1)) - Q[s, a])
if s in terminals:
self.s = self.a = self.r = None
else:
diff --git a/text.py b/text.py
index 65eef28f6..991c764d9 100644
--- a/text.py
+++ b/text.py
@@ -362,7 +362,10 @@ def decode(self, ciphertext):
def score(self, code):
"""Score is product of word scores, unigram scores, and bigram scores.
This can get very small, so we use logs and exp."""
- text = permutation_decode(self.ciphertext, code)
+
+ # TODO: Implement the permutation_decode function
+ text = permutation_decode(self.ciphertext, code) # noqa
+
logP = (sum([log(self.Pwords[word]) for word in words(text)]) +
sum([log(self.P1[c]) for c in text]) +
sum([log(self.P2[b]) for b in bigrams(text)]))
diff --git a/utils.py b/utils.py
index 7a547c67c..ed44f1e9e 100644
--- a/utils.py
+++ b/utils.py
@@ -3,7 +3,6 @@
import bisect
import collections
import collections.abc
-import functools
import operator
import os.path
import random
@@ -59,7 +58,8 @@ def is_in(elt, seq):
"""Similar to (elt in seq), but compares with 'is', not '=='."""
return any(x is elt for x in seq)
-def mode(data):
+
+def mode(data):
"""Return the most common data item. If there are ties, return any one of them."""
[(item, count)] = collections.Counter(data).most_common(1)
return item
@@ -67,6 +67,7 @@ def mode(data):
# ______________________________________________________________________________
# argmin and argmax
+
identity = lambda x: x
argmin = min
@@ -90,7 +91,6 @@ def shuffled(iterable):
return items
-
# ______________________________________________________________________________
# Statistical and mathematical functions
@@ -167,7 +167,6 @@ def vector_add(a, b):
return tuple(map(operator.add, a, b))
-
def scalar_vector_product(X, Y):
"""Return vector as a product of a scalar and a vector"""
return [X * y for y in Y]
@@ -259,6 +258,7 @@ def step(x):
"""Return activation value of x with sign function"""
return 1 if x >= 0 else 0
+
try: # math.isclose was added in Python 3.5; but we might be in 3.4
from math import isclose
except ImportError:
@@ -361,21 +361,50 @@ def __init__(self, op, *args):
self.args = args
# Operator overloads
- def __neg__(self): return Expr('-', self)
- def __pos__(self): return Expr('+', self)
- def __invert__(self): return Expr('~', self)
- def __add__(self, rhs): return Expr('+', self, rhs)
- def __sub__(self, rhs): return Expr('-', self, rhs)
- def __mul__(self, rhs): return Expr('*', self, rhs)
- def __pow__(self, rhs): return Expr('**',self, rhs)
- def __mod__(self, rhs): return Expr('%', self, rhs)
- def __and__(self, rhs): return Expr('&', self, rhs)
- def __xor__(self, rhs): return Expr('^', self, rhs)
- def __rshift__(self, rhs): return Expr('>>', self, rhs)
- def __lshift__(self, rhs): return Expr('<<', self, rhs)
- def __truediv__(self, rhs): return Expr('/', self, rhs)
- def __floordiv__(self, rhs): return Expr('//', self, rhs)
- def __matmul__(self, rhs): return Expr('@', self, rhs)
+ def __neg__(self):
+ return Expr('-', self)
+
+ def __pos__(self):
+ return Expr('+', self)
+
+ def __invert__(self):
+ return Expr('~', self)
+
+ def __add__(self, rhs):
+ return Expr('+', self, rhs)
+
+ def __sub__(self, rhs):
+ return Expr('-', self, rhs)
+
+ def __mul__(self, rhs):
+ return Expr('*', self, rhs)
+
+ def __pow__(self, rhs):
+ return Expr('**', self, rhs)
+
+ def __mod__(self, rhs):
+ return Expr('%', self, rhs)
+
+ def __and__(self, rhs):
+ return Expr('&', self, rhs)
+
+ def __xor__(self, rhs):
+ return Expr('^', self, rhs)
+
+ def __rshift__(self, rhs):
+ return Expr('>>', self, rhs)
+
+ def __lshift__(self, rhs):
+ return Expr('<<', self, rhs)
+
+ def __truediv__(self, rhs):
+ return Expr('/', self, rhs)
+
+ def __floordiv__(self, rhs):
+ return Expr('//', self, rhs)
+
+ def __matmul__(self, rhs):
+ return Expr('@', self, rhs)
def __or__(self, rhs):
"""Allow both P | Q, and P |'==>'| Q."""
@@ -385,20 +414,47 @@ def __or__(self, rhs):
return PartialExpr(rhs, self)
# Reverse operator overloads
- def __radd__(self, lhs): return Expr('+', lhs, self)
- def __rsub__(self, lhs): return Expr('-', lhs, self)
- def __rmul__(self, lhs): return Expr('*', lhs, self)
- def __rdiv__(self, lhs): return Expr('/', lhs, self)
- def __rpow__(self, lhs): return Expr('**', lhs, self)
- def __rmod__(self, lhs): return Expr('%', lhs, self)
- def __rand__(self, lhs): return Expr('&', lhs, self)
- def __rxor__(self, lhs): return Expr('^', lhs, self)
- def __ror__(self, lhs): return Expr('|', lhs, self)
- def __rrshift__(self, lhs): return Expr('>>', lhs, self)
- def __rlshift__(self, lhs): return Expr('<<', lhs, self)
- def __rtruediv__(self, lhs): return Expr('/', lhs, self)
- def __rfloordiv__(self, lhs): return Expr('//', lhs, self)
- def __rmatmul__(self, lhs): return Expr('@', lhs, self)
+ def __radd__(self, lhs):
+ return Expr('+', lhs, self)
+
+ def __rsub__(self, lhs):
+ return Expr('-', lhs, self)
+
+ def __rmul__(self, lhs):
+ return Expr('*', lhs, self)
+
+ def __rdiv__(self, lhs):
+ return Expr('/', lhs, self)
+
+ def __rpow__(self, lhs):
+ return Expr('**', lhs, self)
+
+ def __rmod__(self, lhs):
+ return Expr('%', lhs, self)
+
+ def __rand__(self, lhs):
+ return Expr('&', lhs, self)
+
+ def __rxor__(self, lhs):
+ return Expr('^', lhs, self)
+
+ def __ror__(self, lhs):
+ return Expr('|', lhs, self)
+
+ def __rrshift__(self, lhs):
+ return Expr('>>', lhs, self)
+
+ def __rlshift__(self, lhs):
+ return Expr('<<', lhs, self)
+
+ def __rtruediv__(self, lhs):
+ return Expr('/', lhs, self)
+
+ def __rfloordiv__(self, lhs):
+ return Expr('//', lhs, self)
+
+ def __rmatmul__(self, lhs):
+ return Expr('@', lhs, self)
def __call__(self, *args):
"Call: if 'f' is a Symbol, then f(0) == Expr('f', 0)."
@@ -430,6 +486,7 @@ def __repr__(self):
# An 'Expression' is either an Expr or a Number.
# Symbol is not an explicit type; it is any Expr with 0 args.
+
Number = (int, float, complex)
Expression = (Expr, Number)
@@ -464,9 +521,14 @@ def arity(expression):
class PartialExpr:
"""Given 'P |'==>'| Q, first form PartialExpr('==>', P), then combine with Q."""
- def __init__(self, op, lhs): self.op, self.lhs = op, lhs
- def __or__(self, rhs): return Expr(self.op, self.lhs, rhs)
- def __repr__(self): return "PartialExpr('{}', {})".format(self.op, self.lhs)
+ def __init__(self, op, lhs):
+ self.op, self.lhs = op, lhs
+
+ def __or__(self, rhs):
+ return Expr(self.op, self.lhs, rhs)
+
+ def __repr__(self):
+ return "PartialExpr('{}', {})".format(self.op, self.lhs)
def expr(x):
@@ -482,6 +544,7 @@ def expr(x):
else:
return x
+
infix_ops = '==> <== <=>'.split()
@@ -614,5 +677,6 @@ class Bool(int):
"""Just like `bool`, except values display as 'T' and 'F' instead of 'True' and 'False'"""
__str__ = __repr__ = lambda self: 'T' if self else 'F'
+
T = Bool(True)
F = Bool(False)
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy