Example #1
0
    def do_step(self):
        """
        Runs one simulation step and returns True, unless the simulation is
        finished (params.max_days has been reached).
        """
        w, g = self._world, self._world.get_grid()
        cars = w.get_agents()
        t = TimeLord()
        w.update_grid()

        start = dt.now()
        if t.get_day_time() == 0:
            duration = dt.now() - start
            print("===== Day: %i (%.2f secs) =====" %
                  (t.get_day(), duration.seconds +
                   (duration.microseconds / 1000000)))
            #self.print_grid(self._world.print_grid())

            ct, cnt = 0, 0
            for car in cars:
                if not car.is_travelling():

                    car.restart_route(
                    )  # or car.reverse_route() to have them travel home
                    ct += 1
                else:
                    cnt += 1
            print("** Restarted %i cars, %i still travelling" % (ct, cnt))

            start = dt.now()

        t.next_time_step()
        self._jam_progression.append(g.get_jam_amount())

        if self._parameters.max_days > -1 and t.get_day(
        ) >= self._parameters.max_days:
            return False
        else:
            return True
    def _transfer_car(self, direction):
        """
        Moves the next car from the street stack to the mainstack of node in the given direction.
        """
        from Grid import MovementEvent, GRID_EVENT as GE
        t = TimeLord()
        time = t.get_day_time()
        street_stack = self._streets[direction]
        if street_stack and len(street_stack) > 0:
            car = street_stack[0]
            to_node = self._neighbours[direction]
            if to_node.add_car(car):
                street_stack.pop(0)
                car.handle_movement_event(MovementEvent(GE.JUNCTION_ARRIVED, time, self))
                # print('car_transfered')
                return True
            else:
                car.handle_movement_event(MovementEvent(GE.JUNCTION_REJECTED, time, self))
                self._add_street_jam(direction,time)
                return False

        return True
Example #3
0
    def _transfer_car(self, direction):
        """
        Moves the next car from the street stack to the mainstack of node in the given direction.
        """
        from Grid import MovementEvent, GRID_EVENT as GE
        t = TimeLord()
        time = t.get_day_time()
        street_stack = self._streets[direction]
        if street_stack and len(street_stack) > 0:
            car = street_stack[0]
            to_node = self._neighbours[direction]
            if to_node.add_car(car):
                street_stack.pop(0)
                car.handle_movement_event(
                    MovementEvent(GE.JUNCTION_ARRIVED, time, self))
                # print('car_transfered')
                return True
            else:
                car.handle_movement_event(
                    MovementEvent(GE.JUNCTION_REJECTED, time, self))
                self._add_street_jam(direction, time)
                return False

        return True