Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:程序在“之前”的任意时间停止迭代;至于;循环完成_Matlab_For Loop_Random_Queue_Simulation - Fatal编程技术网

Matlab:程序在“之前”的任意时间停止迭代;至于;循环完成

Matlab:程序在“之前”的任意时间停止迭代;至于;循环完成,matlab,for-loop,random,queue,simulation,Matlab,For Loop,Random,Queue,Simulation,在MATLAB中编写一个程序,模拟队列中的等待时间。目前,它应该为所有855个作业分配一个队列进入和退出时间(以秒为单位)。但是,每次我运行它时,它都会选择一个随机的时间点,并停止为在此时间之后到达的作业分配队列进入时间。也许我的随机数发生器有问题?这是我的密码: function waitTimes = mcQueue2(arrivals, services) % inputs the array of absolute arrival times and service times for

在MATLAB中编写一个程序,模拟队列中的等待时间。目前,它应该为所有855个作业分配一个队列进入和退出时间(以秒为单位)。但是,每次我运行它时,它都会选择一个随机的时间点,并停止为在此时间之后到达的作业分配队列进入时间。也许我的随机数发生器有问题?这是我的密码:

function waitTimes = mcQueue2(arrivals, services)
% inputs the array of absolute arrival times and service times for each
% voter, places them into queue and service, 
% RECORDS WAIT TIME

secsOpen = 46800; 
arrivalArray = arrivalTimes(arrivals); % uses xlsread to make an array from an excel 
% file of times, in seconds, from 1 to secsOpen when jobs arrive
serviceArray = generateServiceTimes(services); % generateServiceTimes reads an input 
% array from an excel sheet, counts its elements, creates an array of that number of 
% elements, and populates each element with a random number using logninv()

arrivalArray(1) = 1; % set the first arrival to arrive at the first second
serversBusy = [0 0 0 0]; % each time a job exits queue and begins service, the service 
% time associated with that job is added to the serversBusy time of whichever server is 
% handling 
numMachines = numel(serversBusy);
arrivalIndex = 1;
queue = []; 
numInQueue = zeros(1, secsOpen); % tracks how many people are in queue each second
waitTimes = cat(2, (1:numel(arrivalArray))', zeros(numel(arrivalArray), 2));

for sec = 1:secsOpen
    % every second, check if someone is arriving that second
    if arrivalArray(arrivalIndex) == sec
        % if they arrive, put them into the queue
        queue = cat(2, queue, arrivalIndex);
        % record the time they entered queue
        waitTimes(arrivalIndex, 2) = sec;
        % increment arrivalIndex to wait for the next arrival
        arrivalIndex = arrivalIndex + 1;
    end
    %check if any of the servers are becoming available
    for ii = 1:numMachines
        if serversBusy(ii) <= sec && numel(queue) > 0
        % if so, send the first voter in queue to the server
            % record the current time as the time they exited queue
            waitTimes(queue(1), 3) = sec; 
            % add this job's service time to the time the server is busy until
            serversBusy(ii) = sec + serviceArray(queue(1));
            % remove the job from the queue
            queue(1) = [];
        end
    end
    numInQueue(sec) = length(queue);
end
plot(1:sec, numInQueue, '*')
end
(零继续超过30到855)1到27的输出正是我想要的。为什么它不继续对列表中的其余元素执行此操作

以下是到达时间和生成服务时间的代码:

function arrivals = arrivalTimes(excelFile)
% inputs an excel file of the template 'SamplePrecincts.xlsx' outputs the
% array of arrival times
% the excelFile contains the interarrival times between jobs, so arrivalTimes sums them     
% to get the absolute time when the job arrives.
interarrivals = (xlsread(excelFile, 'Sheet2', 'D1:ALN5'))';
interarrivals(~any(interarrivals,2), : ) = []; % get rid of any zeros
arrivals = zeros(size(interarrivals, 1), size(interarrivals, 2));
arrivals(1) = 0;
for k = 2:numel(interarrivals)
    arrivals(k) = arrivals(k-1) + interarrivals(k);
end
end
excelFile Sheet2 D1:ALN5中的每个单元格具有以下公式:

 FLOOR.MATH(GAMMA.INV(RAND(), 1, $C$2))
其中C2当前为53.9731

function serviceTimes = generateServiceTimes(excelFile)
% takes an input of the template 'SamplePrecincts.xlsx' and generates
% lognormal vote times based on the YKA paper. returns in seconds
interarrivals = (xlsread(excelFile, 'Sheet2', 'D1:ALN5'))';
interarrivals(~any(interarrivals,2), : ) = []; % get rid of any zeros
[r, c] = size(interarrivals);
voteTimes = zeros(r, c);
for k = 1:numel(voteTimes)
    voteTimes(k) = ceil(60*logninv(rand(), 1.7042, 0.4406));
end
end

首先:范围
'D1:ALN5'
对应于5*999=4995个条目(=jobs),因此我不确定这是否符合855个jobs的值

我调查了你的问题,我想我知道你的问题是什么: 当您加载
到达时间时
,只有当存在一个只由零组成的整体时,才可以去掉零

我的建议是让
arrivals
成为一个向量而不是一个矩阵,例如通过添加以下行
interarrivals=interarrivals(:)就在行
间隔=(xlsread(excelFile,'Sheet2','D1:ALN5')之后到达时间
-功能中选择code>

请注意,这不会阻止
waitTimes
变量最终有大量的零:第二个column中的零表示作业永远无法启动(在
secsOpen
限制之前)。第三个COLLMN中的零表示作业永远无法完成(在
secsOpen
限制之前)


还请注意,如果在时间达到
secsOpen
限制之前启动最后一个作业,则会出现错误(如果作业较少(如您首先编写的855),或者作业的
secsOpen
值较高,则可能会发生这种情况),不过,如果arrivalArray(arrivalIndex)未达到
secsOpen
限制,则可以通过更改行
来轻松解决此问题==sec
to
if(arrivalIndex)我猜上述输出来自
waitTimes
变量。第一列来自从Excel文件读取的
Arrivalray
使用
arrivalTimes
函数。是否确定
arrivalArray
的每个元素都是一个整数,可以与
arrivalArray(arrivalIndex)进行相同的比较==sec
?到达时间的代码是什么样子的,一个样本输入
到达时间也会是什么样子?谢谢你的输入,结果我自己修复了它!我去掉了
到达时间
服务
参数,用一个用作
arrivalTimes()
generateServiceTimes()
WAIT的参数没有帮助,我只是随机让它迭代600次而不是855次。Sean-你需要打印出
ArrivalRay
数据。它看起来像什么?
function serviceTimes = generateServiceTimes(excelFile)
% takes an input of the template 'SamplePrecincts.xlsx' and generates
% lognormal vote times based on the YKA paper. returns in seconds
interarrivals = (xlsread(excelFile, 'Sheet2', 'D1:ALN5'))';
interarrivals(~any(interarrivals,2), : ) = []; % get rid of any zeros
[r, c] = size(interarrivals);
voteTimes = zeros(r, c);
for k = 1:numel(voteTimes)
    voteTimes(k) = ceil(60*logninv(rand(), 1.7042, 0.4406));
end
end