Code covered by the BSD License  

Highlights from
CTMSIM - an interactive freeway traffic macrosimulator

image thumbnail
from CTMSIM - an interactive freeway traffic macrosimulator by Alex Kurzhanskiy
Freeway traffic simulation based on Asymmetric Cell Transmission Model

auto_control(densities, ordata, counts, periods, celldata, ts, q_on)
function [new_orflows, new_counts] = auto_control(densities, ordata, counts, periods, celldata, ts, q_on)
% AUTO_CONTROL - invoke on-ramp flow controllers.
%
% Call:   [new_orflows, new_counts] = auto_control(densities, ordata, counts, periods, celldata, ts, q_on)
%
% Parameters:
%             densities - vector of densities;
%             ordata    - matrix of current on-ramp data: demands, flows, queues,
%                         number of rows is the same as size of 'densities';
%             counts    - vector of controller counters, same size as 'densities';
%             periods   - vector of controller periods, same size as 'densities';
%             celldata  - array of freeway cell data structures, whose length
%                         must be the same as size of 'densities';
%             ts        - sampling period;
%             q_on      - queue controller on/off flag.
%
% Returns:
%          new_orflows - vector of on-ramp flows generated by controllers;
%          new_counts  - updated controller counters.
%
% Last modified:   11/06/2006.

%
% Alex Kurzhanskiy   <akurzhan@eecs.berkeley.edu>
%

J = size(densities, 1);
K = size(ordata, 1);
L = size(counts, 1);
M = size(periods, 1);
N = size(celldata, 2);

if J ~= N
  error('AUTO_CONTROL: number of densities does not match number of cells.');
end

if K ~= N
  error('AUTO_CONTROL: number of on-ramp flows does not match number of cells.');
end

if L ~= N
  error('AUTO_CONTROL: number of controller counts does not match number of cells.');
end

if M ~= N
  error('AUTO_CONTROL: number of controller periods does not match number of cells.');
end

K = size(ordata, 2);

if K ~= 3
  error('AUTO_CONTROL: on-ramp data matrix has wrong size.');
end

new_orflows = ordata(:, 2);
new_counts  = [];

for i = N:-1:1  % going from downstream up
  if isempty(celldata(i).ORname)  % cell has no on-ramp
    new_counts  = [new_counts; 0];
    continue;
  end

  if counts(i, 1) == 0  % time to invoke controller
    if isempty(celldata(i).ORmlcontroller)  % if no controller assigned
      new_orflows(i) = celldata(i).ORfmax;  % allow maximum flow
    else  % otherwise call a controller
      fnm         = ['controller_' lower(celldata(i).ORmlcontroller.name)];
      new_orflows = feval(fnm, densities, new_orflows, celldata, ts, i);
    end

    if q_on & ~isempty(celldata(i).ORqcontroller)
      fnm     = ['controller_' lower(celldata(i).ORqcontroller.name)];
      q_flows = feval(fnm, ordata(:, 1), new_orflows, ordata(:, 3), celldata, ts, i);
    else
      q_flows = new_orflows;
    end

    new_orflows = (max([new_orflows'; q_flows']))';  % choose less restrictive values
  end

  if (counts(i, 1) + 1) == periods(i, 1)
    c = 0;  % next time step controller should be invoked
  else
    c = counts(i, 1) + 1;  % increment counter
  end
  new_counts = [new_counts; c];
end

global CTMSIM_ORC;

if ~isstruct(CTMSIM_ORC) | ~isfield(CTMSIM_ORC, 'nodelete')
  clear global CTMSIM_ORC;
end

return;

Contact us at files@mathworks.com