I am designing an AI for mouse and cat. So they have HP, and cat will chase and eat mouse, mouse will eat cheese. This eating action will help them to gain HP. If they can't eat food, they will die if they use up all the HP.

So I searched through books and I have a basic algorithm for this.

def chooseAction(actions, goals):
  # Go through each action, and calculate the
  # discontentment.
  bestAction =  actions[0]
  bestValue = calculateDiscontentment(actions[0], goals)
  for action in actions:
    thisValue = calculateDiscontentment(action, goals)
    if thisValue < bestValue:
      bestValue = thisValue
      bestAction = action
  # return the best action
  return bestAction

def calculateDiscontentment(action, goals):
  # Keep a running total
  discontentment = 0
  # Loop through each goal
  for goal in action:
    # Calculate the new value after the action
    newValue = goal.value + action.getGoalChange(goal)
    # Get the discontentment of this value
    discontentment += goal.getDiscontentment(value)

struct Goal:
  def getDiscontentment(newValue):
    return newValue * newValue

This algorithm is quite easy to understand, and quite easy to implement.

So I have to determine Goal and goal value for each action they take.

Let say a mouse, He may want to move, eat.

So I have to come up with a value(wiliness) for these values.

What is a good way to determine these values?

My approach is here.

Ley say my mouse have a view range of 3 cells and it can only walk in four directions up down left and right.

The goal eat value may determine by its MAX_ENERGY and NOW_ENERGY and I come out a formula eatValue = MAX_ENERGY - NOW_ENERGY. This make sense because, it NOW_ENERGY is equal to MAX_ENERGY, my mouse has a wiliness 0 to eat.

What is a good way of come out this simple formulations? What will be the good heriustic for my mouse to move?

  • \$\begingroup\$ You may want to edit the question to be more generic. Also explain more about what you've tried and why it isn't working. \$\endgroup\$
    – House
    Commented Sep 18, 2013 at 13:30

2 Answers 2


You may want to add some more formulas and weight them. e.g.: Mouse want to escape cat, mouse want to eat and mouse want to roam around randomly. You an now weight these three formulas and then calculate the actual values for each goal. If you want to make it a bit more random you can choose to select the action from these goals with a semi random algorithm, wheel-of-fortune-selection for example:

foreach goal
  calculate goalPower=goalValue*weight;

So lets say the mouse want to:

  • escape the cat in every case (weight=10)
  • likes eating a lot (w=5)
  • roam around if there is nothing to do (w=1).

Lets imagine a situation where the cat is 3 cells away and the mouse has 6 energy left (from 10):


Now you pick a random number between 0 and 50 and then choose the corresponding action (0-19 eat,20-49 escape,50 roam)

I hope this idea get you started


Note: To optimize your values, you can simply use the "survival of the fittest" method. Some basic approach of the evolutionary algorithm. To do this, simulate some mice and some cats (e.g. 10 mice, 2 cats). To optimize values for the mice, simulate until 50% of the mice died or some maximum time has passed. Choose the fittest of them (e.g. look at their Health Points) and duplicate them. Slightly modify the values of the duplicates (=mutation) and put them all again randomly onto the map. And repeat this for some hundred times ("episodes"). Do the same for the cats...

Your parameters will then automatically adapt to the optimum. You'll see, that there are different optimal values for a different environment (e.g. less/more cheese, less/more cats, bigger map size)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .