def get_target(self): """ Figure out where to move towards. We're trying to move towards the mid-point of two other robots, while also keeping them at a maximal distance. """ (a, b) = self.get_target_bots() mid = midpoint(a.location, b.location) dist_mid = self.dist_from(mid) dist_a = self.dist_from_bot(a) dist_b = self.dist_from_bot(b) # Attempt to maximise distances, while reducing distance to mid best = mid score = self.score(mid) def in_line(): for i in xrange(-50, 50, 5): i /= 10 v = length_towards(i, mid) there = self.location + v yield there def other(): """Perpedicular to the mid point""" v = mid - self.location ref = Vector(v.y, v.x) + self.location for i in xrange(-50, 50, 5): i /= 10 v = length_towards(i, ref) there = self.location + v yield there def rand(): """Consider some random places""" for i in xrange(100): x = randint(0, self._arena.width) y = randint(0, self._arena.height) v = Vector(x, y) there = self.location + v yield there def possibles(): for alg in [in_line, other, rand]: for pos in alg(): yield pos for pos in possibles(): if not self._arena.position_valid(pos): continue s = self.score(pos) if s > score: best = pos return best
def score(self, position): (a, b) = self.get_target_bots() mid = midpoint(a.location, b.location) dist_mid = abs(position - mid) dist_a = abs(position - a.location) dist_b = abs(position - b.location) # Minimise dist_mid, Maximise dist_{a,b} score = dist_a * 1.3 + dist_b - ( 1.8 * dist_mid ) return score
def test_midpoint2(self): exp = Point(2, 2) mid = midpoint(self._a, self._b) util.assertEqual(exp, mid)
def test_midpoint(self): exp = Point(0, 0) mid = midpoint(self._a, self._c) util.assertEqual(exp, mid)