Ура, третя лабораторна робота, як на мене то вона виявилась найцікавішою і найскладнішою (помимо п'ятої).
Ось її реалізаця:
Код:
%#> Lab#3_Sheremeta
function Lab3_Sheremeta
%-> input data
amountOfServiceUnits = 70;
N = 100; % amount of abonents
lambda = 12.5; % call admission intensity
mu = 180; % average call duration, s
Tmod = 4; % duration of the simulation, h
%-> generating traffic
[admissionMoments, endMoments] = trafficGenerator(N, lambda, mu, Tmod);
%-> modeling with fixed step
tic;
disp('-> Traffic modeling with fixed step');
[steps, amountOfCallsInSystem, losses] = fixedStepModelling(admissionMoments, endMoments, amountOfServiceUnits);
fprintf('\tLosses = %3.3f percent\n', losses/(length(admissionMoments)/100));
% charting results for modeling with fixed step
figure(1);
plot(steps, amountOfCallsInSystem, 'r');
grid on;
% plot setting
title('Traffic modeling with fixed step');
xlabel('Time, s');
ylabel('Amount of calls in system');
ylim([min(amountOfCallsInSystem), max(amountOfCallsInSystem)*1.1]);
elapsedTime = toc;
fprintf('\tElapsed time for modeling with fixed step equals %f\n\n', elapsedTime);
%-> modeling with special condition step
tic;
disp('-> Traffic modeling with special condition step');
[steps, amountOfCallsInSystem, losses] = specialConditionStepModeling(admissionMoments, endMoments, amountOfServiceUnits);
fprintf('\tLosses = %3.3f percent\n', losses/(length(admissionMoments)/100));
% charting results for modeling with special condition step
figure(2);
plot(steps, amountOfCallsInSystem, 'b');
grid on;
% plot setting
title('Traffic modeling with special condition step');
xlabel('Time, s');
ylabel('Amount of calls in system');
ylim([min(amountOfCallsInSystem), max(amountOfCallsInSystem)*1.1]);
elapsedTime = toc;
fprintf('\tElapsed time for modeling with special condition step equals %f\n\n', elapsedTime);
givenLosses = 0.2;
% -> finding amount of service untits to satisfy given losses using modeling with fixed step
for amountOfServiceUnits = 1:ceil(N*lambda*mu/3600)
[steps, amountOfCallsInSystem, losses] = fixedStepModelling(admissionMoments, endMoments, amountOfServiceUnits);
if losses/length(admissionMoments) <= givenLosses
fprintf('-> Amount of service untits to satisfy given losses evaluated by using modeling with fixed step equals %d\n', amountOfServiceUnits);
break;
end
end
% -> finding amount of service untits to satisfy given losses using modeling with special condition step
for amountOfServiceUnits = 1:ceil(N*lambda*mu/3600)
[steps, amountOfCallsInSystem, losses] = specialConditionStepModeling(admissionMoments, endMoments, amountOfServiceUnits);
if losses/length(admissionMoments) <= givenLosses
fprintf('-> Amount of service untits to satisfy given losses evaluated by using modeling with special condition step equals %d\n', amountOfServiceUnits);
break;
end
end
% -> finding amount of service untits to satisfy given losses using first Erlang formula
losses = firstErlang(N*lambda*mu/3600, ceil(N*lambda*mu/3600));
for i = 1:length(losses)
if losses(i) <= givenLosses
fprintf('-> Amount of service untits to satisfy given losses evaluated by using first Erlang formula equals %d\n', i - 1);
break;
end
end
end
function [steps, amountOfCallsInSystem, losses] = fixedStepModelling(admissionMoments, endMoments, amountOfServiceUnits)
step = 1.5; % awesome hardcode=)
steps = step:step:ceil(max(endMoments)/step)*step;
amountOfCallsInSystem = zeros(1, length(steps)); % array with amountOfCallsInSystem per step
losses = 0;
currentCalls = []; % [endOfCall1, ..., endOfCallN]
callIndex = 1;
iteration = 0;
for step = steps
iteration = iteration + 1;
currentCalls(currentCalls <= step) = [];
while(callIndex <= length(admissionMoments) && admissionMoments(callIndex) <= step)
if length(currentCalls) < amountOfServiceUnits
currentCalls(end + 1) = endMoments(callIndex);
else
losses = losses + 1;
end
callIndex = callIndex + 1;
end
amountOfCallsInSystem(iteration) = length(currentCalls);
end
end
function [steps, amountOfCallsInSystem, losses] = specialConditionStepModeling(admissionMoments, endMoments, amountOfServiceUnits)
steps = sort([admissionMoments, endMoments]);
amountOfCallsInSystem = zeros(1, length(steps)); % array with amountOfCallsInSystem per step
losses = 0;
currentCalls = []; % [endOfCall1, ..., endOfCallN]
callIndex = 1;
iteration = 0;
for step = steps
iteration = iteration + 1;
currentCalls(currentCalls == step) = [];
if callIndex <= length(admissionMoments) && admissionMoments(callIndex) == step
if length(currentCalls) < amountOfServiceUnits
currentCalls(end + 1) = endMoments(callIndex);
else
losses = losses + 1;
end
callIndex = callIndex + 1;
end
amountOfCallsInSystem(iteration) = length(currentCalls);
end
end
function losses = firstErlang(Y, V) % returns array of losses for V = 0:V accordingly
losses = zeros(1, V + 1);
for v = 0:V
temp = zeros(1, v + 1);
for k = 0:v
if k == 0
erlang = (Y^k)/factorial(k);
else
erlang = (erlang*Y)/k;
end
temp(k + 1) = erlang;
end
losses(v + 1) = temp(v + 1)/sum(temp);
end
end
function [admissionMoments, endMoments] = trafficGenerator(N, lambda, mu, Tmod)
% N - amount of abonents
% lambda - call admission intensity in hour of greatest load
% mu - average call duration, s
% Tmod - duration of the simulation, h
admissionMoments = zeros(1, ceil(Tmod*lambda*N));
% R = exprnd(mu) generates random numbers from the exponential distribution with mean parameter mu
temp = exprnd(1/(lambda/3600*N), 1, ceil(Tmod*lambda*N));
temp = temp*((Tmod*3600)/sum(temp));
for i = 1:length(temp)
admissionMoments(i) = sum(temp(1:i));
end
% R = poissrnd(lambda) generates random numbers from the Poisson distribution with mean parameter lambda
endMoments = admissionMoments + poissrnd(mu, 1, ceil(Tmod*lambda*N));
end
P.S. Якшо чесно дуже крута лабораторна робота - в процесі її написання стикався зі всякими пріколами які на перший погляд є не джуе очевидними.
P.S.S. Бажаю всім успіхів в здачі цієї лабораторної роботи)