FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Changed max to argmax by Chipe1 · Pull Request #455 · aimacode/aima-python · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 2 additions & 4 deletions search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,9 @@ def __call__(self, s1): # as of now s1 is a state rather than a percept
self.H[self.s] = min(self.LRTA_cost(self.s, b, self.problem.output(self.s, b),
self.H) for b in self.problem.actions(self.s))

# costs for action b in problem.actions(s1)
costs = [self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H)
for b in self.problem.actions(s1)]
# an action b in problem.actions(s1) that minimizes costs
self.a = list(self.problem.actions(s1))[costs.index(min(costs))]
self.a = argmin(self.problem.actions(s1),
key=lambda b:self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H))

self.s = s1
return self.a
Expand Down
11 changes: 5 additions & 6 deletions text.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def add_sequence(self, words):
n = self.n
words = self.add_empty(words, n)

for i in range(len(words) - n):
for i in range(len(words) - n + 1):
self.add(tuple(words[i:i + n]))

def samples(self, nwords):
Expand Down Expand Up @@ -318,9 +318,7 @@ def score(self, plaintext):
def decode(self, ciphertext):
"""Return the shift decoding of text with the best score."""

list_ = [(self.score(shift), shift)
for shift in all_shifts(ciphertext)]
return max(list_, key=lambda elm: elm[0])[1]
return argmax(all_shifts(ciphertext), key=lambda shift: self.score(shift))


def all_shifts(text):
Expand Down Expand Up @@ -380,8 +378,9 @@ def __init__(self, initial=None, goal=None, decoder=None):

def actions(self, state):
# Find the best
p, plainchar = max([(self.decoder.P1[c], c)
for c in alphabet if c not in state])

search_list = [c for c in alphabet if c not in state]
plainchar = argmax(search_list, key=lambda c: self.decoder.P1[c])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is fine, but I wonder which is clearer:
lambda c: self.decoder.P1[c]
or
self.decoder.P1.get

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Did you mean self.decoder.P1.getitem
I got an error when I tried self.decoder.P1.get AttributeError: 'UnigramTextModel' object has no attribute 'get'

succs = [extend(state, plainchar, cipherchar)] # ???? # noqa

def goal_test(self, state):
Expand Down

Back | FazBrowse Home | New Git URL