Browse Source

feat: basic path optimization

master
mco 4 months ago
parent
commit
a21b1dea4f
4 changed files with 18 additions and 9 deletions
  1. +2
    -2
      ants.yml
  2. +5
    -3
      objects/ant.py
  3. +10
    -3
      objects/utils.py
  4. +1
    -1
      setup.cfg

+ 2
- 2
ants.yml View File

@ -55,12 +55,12 @@ farms:
sources:
width: 50
height: 50
max_sources: 2
max_sources: 1
food: 100
ants:
width: 3
height: 3
max_memory: 100
max_memory: 200
max_food: 10
hp: 300
velocity:


+ 5
- 3
objects/ant.py View File

@ -1,3 +1,4 @@
import random
import logging
from objects import BaseObject, Velocity, Direction, History, Life, Size, Food, Status
@ -60,7 +61,8 @@ class Ant(BaseObject):
def set_velocity(self, vx, vy, wait=0):
self.velocity = Velocity(vx, vy, wait)
def wait(self, timeout):
def wait(self):
timeout = (round(random.random() * 4) * 4) + 4
logging.debug("Ant {} will wait {}".format(self.uuid, timeout))
self.velocity.wait = timeout
@ -79,14 +81,14 @@ class Ant(BaseObject):
self.history.path = list(other.history.path)
self.history.back_path = list(other.history.back_path)
self.status.lost = '0'
self.wait(4)
self.wait()
elif (self.get_status() == 1 and other.get_status() == 6) or \
(self.get_status() == 7 and other.get_status() == 0):
self.history.path = list(other.history.path)
self.history.back_path = list(other.history.back_path)
self.history.reverse()
self.status.lost = '0'
self.wait(4)
self.wait()
return True
return False


+ 10
- 3
objects/utils.py View File

@ -9,12 +9,19 @@ class History:
self.max_len = max_len
def add(self, position):
self.path.append(position)
if len(self.path) > self.max_len:
self.path.pop(0)
best_path = []
for point in self.path:
if point == position:
break
best_path.append(point)
best_path.append(position)
if len(best_path) > self.max_len:
best_path.pop(0)
self.path = best_path
def reverse(self):
self.back_path = list(reversed(self.path))
self.path = []
def __str__(self):
return "Path({}) / Back path = {}".format(self.path, self.back_path)


+ 1
- 1
setup.cfg View File

@ -1,5 +1,5 @@
[metadata]
version = 2.0.0
version = 2.0.1
license = WTFPL
classifiers =
Programming Language :: Python :: 3


Loading…
Cancel
Save