【问题标题】:Model road queue effects with simpy用 simpy 模拟道路队列效果
【发布时间】:2021-10-08 12:57:02
【问题描述】:

我正在使用 simpy 来模拟交通模拟,我想在其中模拟汽车跟随并禁止超车。举个例子,考虑两辆车:A 正在高速行驶,并且正在接近前面的 B 车。 A 车无法超越 B 车,必须减速。

我的想法是将道路分成小块,并让这些块成为资源。这样,当一辆车正在使用道路块时,其他汽车必须等待道路块被释放才能使用它。如果我们订购大块,那么超车就不会发生。

缺点是块的数量可能很大。

在继续使用这种方法之前,我想问一下是否有更好的方法在 simpy 中对此进行建模?

【问题讨论】:

    标签: simulation simpy traffic-simulation


    【解决方案1】:

    您是否考虑过将其建模为代理基础模型

    我将每辆车建模为可以看到前面的汽车并对其做出反应的代理。这种行为并不完美,因为可能会发生崩溃。我还有一个计时器,当它应该更新其状态时,它会向每辆车广播

    """
    Simple agent base model of cars following each other
    Each car adjust their speed to pervent crashing into
    the preceeding car, or getting too far behind
    
    Programmer: Michael R. Gibbs
    """
    
    import simpy
    import random
    
    class Car():
        """
        Models a cars movements and position
        Each car reacts to the car in front of it
        """
    
        car_cnt = 0
    
        def __init__(self, env, start_pos, start_speed, lead_car=None):
            """
            init starting state
            """
    
            Car.car_cnt += 1
            self.car_id = Car.car_cnt
    
            self.env = env
            self.pos = start_pos
            self.speed = start_speed
            self.lead_car = lead_car
    
        def move(self):
            """
            updates the car state
            position and speed
            """
    
            if self.lead_car is None:
                # no lead car, must be the first car
                # make random adjustment to speed
                self.speed += random.triangular(-5,5,0)
    
                dist = 0
            else:
                # addjust to lead car to be maintain a distance of 50
                dist = self.lead_car.pos - self.pos
                speed_diff = self.lead_car.speed - self.speed
    
                x_from_goal_dist = dist - 50
    
                if self.lead_car.pos <= self.pos: # tool close CRASHED!!!
                    print(f'{env.now}  car {self.car_id} crashed')
                    self.speed = self.lead_car.speed - 5
    
                if x_from_goal_dist <= -40: # getting way too close
                    self.speed -= 10
                elif x_from_goal_dist >= 50: # getting way too far behind
                    self.speed += 5
                elif x_from_goal_dist < 0: # creeping up, slow down
                    self.speed -= random.triangular(0,5,0)
                else:
                    self.speed += random.triangular(0,5,0)
    
                dist -= self.speed
    
            self.pos += self.speed
    
            print(f'{self.env.now}  car {self.car_id} is {dist} from prev car at pos {self.pos} at speed {self.speed}')
    
    def run_cars(env, car_list):
        """
        Moves all the cars every time unit
        """
        while True:
            yield env.timeout(1)
    
            for car in car_list:
                car.move()
    
    env = simpy.Environment()
    
    car_list = []
    
    car = Car(env,100,20)
    car_list.append(car)
    
    car = Car(env,50,25,car)
    car_list.append(car)
    
    car = Car(env,0,26,car)
    car_list.append(car)
    
    env.process(run_cars(env,car_list))
    
    env.run(200)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2011-05-03
      • 2012-08-24
      • 1970-01-01
      相关资源
      最近更新 更多