Exemplo n.º 1
0
    def test_get_candidate_neighbours3(self):
        ant = Ant((2, 2), (0, 0))
        ant.updateDirection(math.pi / 4)

        field = PheromoneField((10, 10, 5), G, True, 0.01, 0.1, 0.1, 2)
        neighbours = field.getCandidateNeighbours(ant)

        self.assertEqual(set(neighbours), {(1, 3), (3, 1)})
Exemplo n.º 2
0
    def test_get_right_antenna(self):
        ant = Ant((0, 0), (-2, -2))

        self.assertEqual(ant.getRightAntenna(), (1, -1))

        ant.updateDirection(math.pi / 2)

        self.assertEqual(ant.getRightAntenna(), (1, 1))
Exemplo n.º 3
0
    def ant_walk_loop(self, ant: Ant):
        while not ant.reachedGoal() and not self.stopped:
            self.antWalk(ant)

        # update progress
        if self.progress_callback:
            self.progress_lock.acquire()
            self.completed_ants += 1
            self.progress_subtask = self.completed_ants / len(self.g.edges)
            # field progress equals ant walk + update field over all runs
            previous_run_progress = self.run / self.runs
            self.total_progress = previous_run_progress + (self.progress_subtask / 2) / self.runs
            self.progress_callback.progress(self.total_progress, self.progress_subtask, self.subtask)
            self.progress_lock.release()
Exemplo n.º 4
0
    def antWalk(self, ant: Ant):
        neighbours = self.getCandidateNeighbours(ant)
        # If only one neighbour is valid, walk to that pixel
        if len(neighbours) == 1:
            newDirec = ant.calcAngle(neighbours[0]) - ant.direction
        elif ant.goal in neighbours:
            newDirec = ant.calcAngle(ant.goal) - ant.direction
        else:
            newDirec = None
            while newDirec is None or not self.is_valid_location(*ant.calcPixel(ant.direction + newDirec)):
                # With chance p we either get a random directional change, or a pheromone based directional change
                rand = np.random.uniform(0, 1)
                if rand < self.p:
                    newDirec = self.randomDirectionalChange()
                else:
                    newDirec = self.pheromoneBasedDirection(neighbours, ant)

        # Update the ant's new direction
        ant.updateDirection(newDirec)
        # Let the ant take a step
        ant.takeStep()
Exemplo n.º 5
0
 def test_pheromone_based_direction(self):
     test = PheromoneField((10, 10, 5), G, True, 0.01, 0.1, 0.1, 2, 8)
     ant = Ant((1, 1), (8, 8), 0, 0)
Exemplo n.º 6
0
    def test_get_candidate_neighbours2(self):
        ant = Ant((2, 2), (0, 0))
        field = PheromoneField((10, 10, 5), G, True, 0.01, 0.1, 0.1, 2)
        neighbours = field.getCandidateNeighbours(ant)

        self.assertEqual(set(neighbours), {(3, 1), (2, 1)})
Exemplo n.º 7
0
 def initializeEdge(self, edge) -> Ant:
     return Ant((self.g.nodes[edge[0]]['x'], self.g.nodes[edge[0]]['y']),
                (self.g.nodes[edge[1]]['x'], self.g.nodes[edge[1]]['y']), edge[0], edge[1])