Beispiel #1
0
    def append(self, value):
        """
        Appends an event to the histogram
        """
        # Shift data and replace the first element
        self.data = lib.shift_list(self.data)
        self.data[0] = value
        self.timer += 1

        # Keep track of the extremes
        if value > self.max: self.max = value
        if value < self.min: self.min = value
Beispiel #2
0
 def append(self, value):
     """
     Appends an event to the histogram
     """
     # Shift data and replace the first element
     self.data = lib.shift_list(self.data)
     self.data[0] = value
     self.timer += 1
     
     # Keep track of the extremes
     if value > self.max: self.max = value
     if value < self.min: self.min = value
Beispiel #3
0
    def update(self):
        """
        Updates the roundabout : rotate the cars, dispatch them...
        """
        #self.incoming_roads = sorted(self.incoming_roads, compa)
        #   Make the cars rotate
        if lib.clock() - self.last_shift > ROUNDABOUT_ROTATION_RATE:
            self.last_shift = lib.clock()
            self.slots_roads = lib.shift_list(self.slots_roads)

        #   Spawning mode
        if self.spawning and len(self.leaving_roads) and (
                lib.clock() - self.spawn_timer > self.spawn_time):
            self.spawn_timer = lib.clock()
            num_possible_roads = len(self.leaving_roads)
            # Possible ways out. NB : the "1000/ " thing ensures *integer* probabilities.
            possible_roads_events = [(self.leaving_roads[i],
                                      1000 / num_possible_roads)
                                     for i in range(num_possible_roads)]

            chosen_road = lib.proba_poll(possible_roads_events)
            if chosen_road.is_free:
                car_type_events = [(STANDARD_CAR, 80), (TRUCK, 15),
                                   (SPEED_CAR, 5)]

                new_car = __car__.Car(chosen_road.get_free_lane(),
                                      lib.proba_poll(car_type_events))

        #   Update traffic lights
        self._update_traffic_lights()

        #   Update cars
        for car in self.cars:
            self.update_car(car)

        #   Kill cars that have reached their destination
        for car in self.to_kill:
            car_slot = lib.find_key(self.slots_cars, car)
            self.slots_cars[car_slot] = None
            car.die()

        self.to_kill = []
Beispiel #4
0
    def update(self):
        """
        Updates the roundabout : rotate the cars, dispatch them...
        """
        #self.incoming_roads = sorted(self.incoming_roads, compa)
        #   Make the cars rotate
        if lib.clock() - self.last_shift > ROUNDABOUT_ROTATION_RATE:
            self.last_shift = lib.clock()
            self.slots_roads = lib.shift_list(self.slots_roads)

        #   Spawning mode
        if self.spawning and len(self.leaving_roads) and (lib.clock() - self.spawn_timer > self.spawn_time):
            self.spawn_timer = lib.clock() 
            num_possible_roads    = len(self.leaving_roads)
            # Possible ways out. NB : the "1000/ " thing ensures *integer* probabilities.
            possible_roads_events = [(self.leaving_roads[i], 1000/num_possible_roads) for i in range(num_possible_roads)]
        
            chosen_road = lib.proba_poll(possible_roads_events)
            if chosen_road.is_free:
                car_type_events = [(STANDARD_CAR, 80), 
                                   (TRUCK       , 15), 
                                   (SPEED_CAR   ,  5)]
                                   
                new_car = __car__.Car(chosen_road.get_free_lane(), lib.proba_poll(car_type_events))

        #   Update traffic lights
        self._update_traffic_lights()
                
        #   Update cars
        for car in self.cars:
            self.update_car(car)
        
        #   Kill cars that have reached their destination
        for car in self.to_kill:
            car_slot = lib.find_key(self.slots_cars, car)
            self.slots_cars[car_slot] = None
            car.die()

        self.to_kill = []