内格尔Schreckenberg模型模拟源码程序 - matlab算法设计 - 谷速源码
下载频道> 资源分类> matlab源码> 算法设计> 内格尔Schreckenberg模型模拟源码程序

标题:内格尔Schreckenberg模型模拟源码程序
分享到:

所属分类: 算法设计 资源类型: 文件大小: 1.67 KB 上传时间: 2016-01-24 22:51:03 下载次数: 6 资源积分:1分 提 供 者: matlab源码 20160124105048134
内容:
内格尔Schreckenberg模型模拟源码程序,程序员在编程的过程中可以参考学习使用,希望对IT程序员有用,此源码程序简单易懂、方便阅读,有很好的学习价值!
部分代码如下:
% Nagel Schreckenberg model simulation
%Written by Alexander Farley 
%Feb 6 2012
%alexander.s.farley at gmail.com
%This script implements the Nagel Schreckenberg cellular automata based
%traffic model. Flow vs density curves are displayed. 
%
%
%Update June 5 2012: Fixed problem in vehicle velocity updates
%Update Jan 13 2013: Fixed problem with vmax=1 showing no flow
%Update June 12 2013: Corrected calculation of flow rate for FD plot
%Update Feb 9 2015: Replace mean2() and add pause() at end for compatibility with Octave. Reduce default number of simulation runs to speed up calculation. 
%
clc;
clear;
close all;
 
%Parameters
vmax = 6;
p = 0.6;
road_length = 40;
simulation_steps = 400;
render_on = 0;
pause_on = 0;
delay_on = 0;
delay_length = 0.05; %10 FPS
 
road = zeros(1,road_length);       %Contains occupation state
road_next = road;
velocities = zeros(1,road_length); %Contains velocity state
velocities_next = velocities;
 
%Sampling
num_samples = 200;
samples = zeros(2,num_samples); %Contains density and flow rate
density_step = 1/num_samples;
 
history = zeros(simulation_steps, road_length);
velocity_history = zeros(simulation_steps, road_length);
 
figure
 
for g=1:num_samples
    
    %Generate traffic
    road = zeros(1,road_length);       %Contains occupation state
    road_next = road;
    density = g/num_samples;
    
    %Generate traffic
    
for i=1:road_length
    if rand < density 
        road(i) = 1;
    end
end
 
    if render_on
    imshow(road);
    drawnow
    end
    
    %Run simulation
for i=1:simulation_steps
    history(i, :) = road;
    velocity_history(i,:) = velocities;
           %--------------------Velocity update ------------------------%
    for j=1:road_length
       if road(j) == 1
           distance = 0;
           %Seek vmax ahead
           bf = 0;
           for k=1:vmax
               distance = k;
              
               if j+k <= road_length %The index is the "cell under consideration" - is it safe to land here?
                   index = j+k;
               else
                   index = j+k-road_length; %Deal with wrapping
               end
               
               if road(index) == 1 
                   bf = 1;
               end
               
               if bf == 1, break, end
           end
            
           if velocities(j) < vmax %Acceleration
               velocities(j) = velocities(j) + 1;
           end
           
           if (velocities(j) > distance - 1) && bf == 1 %Collision avoidance
               velocities(j) = distance - 1;
           end
           
           if rand < p && velocities(j) > 0 %Random braking
               velocities(j) = velocities(j) - 1;
           end
           
           
       end
    end
    
    %--------------------Movement -------------------------------%
    for j=1:road_length
        if road(j) ==1
            if j+velocities(j) <= road_length
                index = j+velocities(j);
            else
                index = j+velocities(j) - road_length; %Deal with wrapping
            end
            %Collision detection
            if road_next(index) == 1
                disp('Collision detected')
            end
            road_next(index) = 1;
            velocities_next(index) = velocities(j);
        end
    end
    
    velocities = velocities_next;
    
    road = road_next;
    road_next = zeros(1,road_length);
    
    if render_on
    imshow(road);
    drawnow
    end
    
    if pause_on
        pause
    end
    
    if delay_on
        pause(delay_length)
    end
    
end
 
    %Record density and flow rate
    velocity_history = velocity_history.*history;
    samples(:,g) = [mean(history(:)) (sum(velocity_history(:))/sum(history(:)))*mean(history(:))];
    
    disp('Sample step:')
    g
end
    
scatter(samples(1,:), samples(2,:));
axis([0 1 0 1]);
xlabel('Density')
ylabel('Flow (normalized)')
title('Nagel Schreckenberg Flow-density Curve')
 
%imshow(history)
 
pause
 

文件列表(点击上边下载按钮,如果是垃圾文件请在下面评价差评或者投诉):

内格尔Schreckenberg模型模拟源码程序/
内格尔Schreckenberg模型模拟源码程序/NaSchr.m

关键词: 内格尔 源码 模型

Top_arrow
回到顶部
联系方式| 版权声明| 招聘信息| 广告服务| 银行汇款| 法律顾问| 兼职技术| 付款方式| 关于我们|
网站客服网站客服 程序员兼职招聘 程序员兼职招聘
沪ICP备19040327号-3
公安备案号:沪公网安备 31011802003874号
库纳格流体控制系统(上海)有限公司 版权所有
Copyright © 1999-2014, GUSUCODE.COM, All Rights Reserved