Optimize some set operations

This commit is contained in:
Petr Viktorin 2011-04-27 04:34:29 +03:00
parent b2e54ca1ea
commit 0de03e864f

View file

@ -1040,10 +1040,9 @@ class PokemonNode(Node, Facade, namedtuple('PokemonNode',
def expand_forget(self): def expand_forget(self):
cost = self.search.costs['forget'] cost = self.search.costs['forget']
for move in self.moves_: for move in self.moves_.difference(self.search.goal_moves):
if move not in self.search.goal_moves: yield cost, ForgetAction(self.search, move), self._replace(
yield cost, ForgetAction(self.search, move), self._replace( moves_=self.moves_.difference([move]), new_level=False)
moves_=self.moves_.difference([move]), new_level=False)
def expand_trade(self): def expand_trade(self):
search = self.search search = self.search
@ -1135,20 +1134,20 @@ class PokemonNode(Node, Facade, namedtuple('PokemonNode',
def expand_sketch(self): def expand_sketch(self):
moves = self.moves_ moves = self.moves_
for sketch in moves: sketch = self.search.sketch
if sketch == self.search.sketch: if sketch in moves:
for sketched in sorted(self.search.goal_moves): for sketched in sorted(self.search.goal_moves):
if sketched in self.search.unsketchable: if sketched in self.search.unsketchable:
continue continue
if sketched not in moves: if sketched not in moves:
moves = set(moves) moves = set(moves)
moves.remove(sketch) moves.remove(sketch)
moves.add(sketched) moves.add(sketched)
action = SketchAction(self.search, sketched) action = SketchAction(self.search, sketched)
cost = self.search.costs['sketch'] cost = self.search.costs['sketch']
yield cost, action, self._replace( yield cost, action, self._replace(
new_level=False, moves_=frozenset(moves)) new_level=False, moves_=frozenset(moves))
return return
def estimate(self, g): def estimate(self, g):
# Given good estimates, A* finds solutions much faster. # Given good estimates, A* finds solutions much faster.
@ -1163,12 +1162,6 @@ class PokemonNode(Node, Facade, namedtuple('PokemonNode',
if trade_cost is None: if trade_cost is None:
trade_cost = search.costs['trade'] * 2 trade_cost = search.costs['trade'] * 2
return trade_cost return trade_cost
evo_chain = search.evolution_chains[self.pokemon_]
if evo_chain == search.goal_evolution_chain:
breed_cost = 0
else:
breed_cost = search.costs['breed']
return trade_cost + breed_cost
class BaseBreedNode(Node): class BaseBreedNode(Node):
"""Breed node """Breed node