Skip to main content
Adding supplementary optimization suggestions
Source Link
Graham
  • 176
  • 6

It looks like you're looping through the ants twice, once inside checkdistancebetweenantsandfood() and once when you call it. That seems like it's probably wrong.

Method:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)

Call:

    # ants find food
    for name in ants.keys():
        globals()[name].checkdistancebetweenantsandfood()

Other optimizations

Another possible optimization is to exit the loop if a food source is found. Presumably the ants can only eat from one food source at a time.

Example:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)
                    break

If there are many food sources, you might try putting the food sources into a grid based on their position. That way the ants only need to check the for food sources in adjacent cells instead of having to loop through all of the food sources.

It looks like you're looping through the ants twice, once inside checkdistancebetweenantsandfood() and once when you call it. That seems like it's probably wrong.

Method:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)

Call:

    # ants find food
    for name in ants.keys():
        globals()[name].checkdistancebetweenantsandfood()

It looks like you're looping through the ants twice, once inside checkdistancebetweenantsandfood() and once when you call it. That seems like it's probably wrong.

Method:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)

Call:

    # ants find food
    for name in ants.keys():
        globals()[name].checkdistancebetweenantsandfood()

Other optimizations

Another possible optimization is to exit the loop if a food source is found. Presumably the ants can only eat from one food source at a time.

Example:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)
                    break

If there are many food sources, you might try putting the food sources into a grid based on their position. That way the ants only need to check the for food sources in adjacent cells instead of having to loop through all of the food sources.

Source Link
Graham
  • 176
  • 6

It looks like you're looping through the ants twice, once inside checkdistancebetweenantsandfood() and once when you call it. That seems like it's probably wrong.

Method:

    def checkdistancebetweenantsandfood(self):
    for name in ants.keys():
        for i in range(len(foodposx)-1):
            # measure distance between ant and food
            if -1 < globals()[name].posx - foodposx[i] < 1:
                if -1 < globals()[name].posy - foodposy[i] < 1:
                    globals()[name].eat(name, i, foodposx, foodposy)

Call:

    # ants find food
    for name in ants.keys():
        globals()[name].checkdistancebetweenantsandfood()