2015-08-30 35 views
0

我正在創建一個汽車模擬項目,並且遇到了問題。當我運行我的代碼時,汽車有時會相互超車。具有最佳速度功能的車輛跟隨模型 - 超車發生

我花了幾天的時間試圖弄清楚爲什麼會發生這種情況,我仍然不知道。最佳速度函數應設置(反)加速度,以使超車不會發生,但由於某種原因,它仍然允許汽車有時相互超速,而不是很快減速。

你能幫助我,還是隻是推我的方向,我應該看?

這裏是我的最佳速度函數:

def optimal_velocity_function(dx, d_safe, v_max): 
vx_opt = v_max * (np.tanh(dx - d_safe) + np.tanh(d_safe)) 
return vx_opt  

現在我使用它歐拉法中解決ODE:

def euler_method(x, v, n_cars, h, tau, d_safe, v_max): 
# Euler method used to solve ODE 
# returns new position of car and its new velocity 
dv = np.zeros(n_cars) 

for j in range(n_cars - 1): 
     dv[j] = tau ** (-1) * (optimal_velocity_function(x[j+1] - x[j], d_safe, v_max) - v[j]) 

dv[n_cars - 1] = tau ** (-1) * (v_max - v[n_cars - 1]) # Speed of first car 

v_new = v + h * dv 
x_new = x + h * v_new 

return [x_new, v_new]  

這裏是模型的其餘部分,基本上只產生起始值然後使用上面的函數進行迭代。

def optimal_velocity_model(n, n_cars, d_0, v_0, h, tau, d_safe, v_max): 
global x_limit, canvas, xx, vv 

car_positions = np.linspace(0, n_cars, n_cars) 
x = np.array(sorted(np.random.random(n_cars) + car_positions)) # Generation of cars with minimal distance 
x = x * d_0 
v = np.random.random(n_cars) + v_0 # Generating initial speeds around v_0 
xx = np.zeros([n_cars, n]) # Matrix of locations 
vv = np.zeros([n_cars, n]) # Matrix of velocities 

for i in range(n): 
    xx[:, i] = x 
    vv[:, i] = v 
    [x, v] = euler_method(x, v, n_cars, h, tau, d_safe, v_max) 

x_limit = xx.max() # Interval in which will cars be 
return  

非常感謝。 J.

+0

爲什麼添加'+ np.tanh(d_safe)'?爲什麼你使用'tanh'呢?有沒有原因,或者你只是因爲功能看起來不錯才使用它?如果減少時間步長,問題是否也會發生? –

回答

0

我認爲你在這裏陷入了數學而不是關注問題的物理。通過使用delta X上的tanhh以及安全距離,您可以生成一個軟SPEED轉換,可能無法保證超車被禁止。

我的思路是這樣的:

  • 首先評估兩臺車接近安全距離的相對速度

  • 定義的最小減速對車輛性能,可以阻止它在距離內

  • 使用tanhh或任何其他函數來建模積極平滑制動或反之間的轉換 。

  • 通過集成該加速來更新您的速度。

要計算我能想到的一個簡單的方法減速,以保持使用的tanh:

Deceleration=Max_Dec(relative_speed)*[1+tanh(d_safe-dx)] 

希望這可以進一步引導你!