Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Created November 22, 2015 12:35
Show Gist options
  • Save kartikkukreja/f514d1f9b4d585db4506 to your computer and use it in GitHub Desktop.
Save kartikkukreja/f514d1f9b4d585db4506 to your computer and use it in GitHub Desktop.
Linear Combination Heuristic
class LinearCombinationHeuristic:
def __init__(self, game, role, weights, MAX_POSSIBLE_ACTIONS, MAX_POSSIBLE_STATES):
self.game = game
self.role = role
self.weights = weights
self.MAX_POSSIBLE_ACTIONS = MAX_POSSIBLE_ACTIONS
self.MAX_POSSIBLE_STATES = MAX_POSSIBLE_STATES
self.heuristics = [actionMobility, stateMobility, actionFocus, stateFocus, goalValue]
assert(len(self.heuristics) == len(self.weights))
def evaluate(self, state):
return sum(self.weights[i] * self.heuristics[i](state) for i in xrange(len(self.heuristics)))
def actionMobility(self, state):
return (self.game.getLegalMoves(state, self.role) * 100.0) / self.MAX_POSSIBLE_ACTIONS
def stateMobility(self, state):
return (len({self.game.getNextState(state, jointMove) for jointMove in self.game.getLegalJointMoves(state)}) * 100.0) / self.MAX_POSSIBLE_STATES
def actionFocus(self, state):
# There's no need to recompute action mobility. This code is just for illustration.
return 100.0 - actionMobility(state)
def stateFocus(self, state):
# There's no need to recompute state mobility. This code is just for illustration.
return 100.0 - stateMobility(state)
def goalValue(self, state):
return self.game.getGoalValues(state)[self.role]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment