Anda di halaman 1dari 8

Zulham Julio

120402067

Tugas Pemodelan dan


Simulasi
M/M/1 Simulation

M/M/1 Simulation

Teori Singkat
Bagian ini menampilkan program matlab untuk simulasi M/M/1. Pendefinisian diberikan oleh
gambar 1.28. File header rand.h dipakai untuk mendeklarasikan fungsi dari generator
bilangan acak.Konstanta Q_LIMIT di set pada nilai 100 dan server_status variable ada 2
yaitu BUSY dan IDLE. Penunjuk file *infile dan *outfile dipakai untuk membolehkan kita
untuk membuka input dan output file yg ada dalam kode.
Parameter input yg dibaca dari program mm1 secara berurutan yaitu terdiri dari 1.0 , 0.5 dan
1000, dimana nilai mean interarrival sama dengan 1, mean service time = 0.5 dan jumlah
delay=1000.

Source Code
function varargout = MM1_2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MM1_2_OpeningFcn, ...
'gui_OutputFcn', @MM1_2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before MM1_2 is made visible.


function MM1_2_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for MM1_2


handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = MM1_2_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;

function interarrival_Callback(hObject, eventdata, handles)


% hObject
handle to interarrival (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function interarrival_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function service_Callback(hObject, eventdata, handles)
% hObject
handle to service (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function service_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function delay_Callback(hObject, eventdata, handles)


% hObject
handle to delay (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function delay_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function Average_delay_Callback(hObject, eventdata, handles)


% hObject
handle to Average_delay (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function Average_delay_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function average_number_Callback(hObject, eventdata, handles)


% hObject
handle to average_number (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function average_number_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function server_utilization_Callback(hObject, eventdata, handles)


% hObject
handle to server_utilization (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function server_utilization_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit11_Callback(hObject, eventdata, handles)


% hObject
handle to edit11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function time_Callback(hObject, eventdata, handles)
% hObject
handle to time (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function time_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function status_Callback(hObject, eventdata, handles)
% hObject
handle to status (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function status_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

%% --- Executes on button press in Collect.


function Collect_Callback(hObject, eventdata, handles)
global
global
global
global
global

queue_lim service_mean_time arrival_mean_time num_events


sim_time avg_num_in_queue avg_delay P server_status util
num_delays_required queue_size time_last_event num_pack_insys
total_delays time_in_queue time_in_server delay time_next_event
min_time_next_event arr_time type_of_event time_since_last_event

arrival_mean_time(1:20) = str2num(get(handles.interarrival,'string'));
num_delays_required = str2num(get(handles.delay,'string'));
service_mean_time = str2num(get(handles.service,'string'));
queue_lim=100;
util(1:20) = 0;
avg_num_in_queue(1:20) = 0;
avg_delay(1:20) = 0;
P(1:20) = 1;

% system limit

for j=1:20 %loop for increasing the mean arrrival time


arrival_mean_time(j+1) = arrival_mean_time(j) + 0.001;
num_events=2;
% initialization
sim_time = 0.0;
server_status=0;
queue_size=0;
time_last_event=0.0;
num_pack_insys=0;
total_delays=0.0;
time_in_queue=0.0;
time_in_server=0.0;
delay = 0.0;
time_next_event(1) = sim_time + exponential(arrival_mean_time(j+1));
time_next_event(2) = exp(30);
%set(handles.status,'String', 'Launching Simulation..',num2str(j));
while(num_pack_insys < num_delays_required)
min_time_next_event = exp(29);
type_of_event=0;
for i=1:num_events

if(time_next_event(i)<min_time_next_event)
min_time_next_event = time_next_event(i);
type_of_event = i;

end
end
if(type_of_event == 0)
%set(handles.status,'String', 'no event in
time',num2str(sim_time));
set(handles.status,'String', 'no event in time');
end
sim_time = min_time_next_event;
time_since_last_event = sim_time - time_last_event;
time_last_event = sim_time;
time_in_queue = time_in_queue + queue_size *
time_since_last_event ;
time_in_server = time_in_server + server_status *
time_since_last_event;

if (type_of_event==1)
%disp(['packet arrived']);
% -------------------------larrival------------------------time_next_event(1) = sim_time +
exponential(arrival_mean_time(j+1));
% epomenos xronos afiksis
if(server_status == 1)
num_pack_insys = num_pack_insys + 1;
queue_size = queue_size + 1 ;
if(queue_size > queue_lim)
set(handles.status,'String', 'queue size = ');
set(handles.status,'String', 'Launching Simulation..');
%set(handles.status,'String', 'queue size =
',num2str(queue_size));
%set(handles.status,'String', 'Launching
Simulation..',num2str(sim_time));
pause(1);
end
arr_time(queue_size) = sim_time;
else
server_status = 1;
time_next_event(2) = sim_time + exponential(service_mean_time);
end

elseif (type_of_event==2)
% ---------------service and departure--------------if(queue_size == 0)
server_status = 0;

time_next_event(2) = exp(30);

else

queue_size = queue_size - 1;
delay = sim_time - arr_time(1);
total_delays = total_delays + delay;
time_next_event(2) = sim_time +
exponential(service_mean_time);

for i = 1:queue_size
arr_time(i)= arr_time(i+1);
end
end
end
end
%results output
util(j+1) = time_in_server/sim_time;
avg_num_in_queue(j+1) = time_in_queue/sim_time;
avg_delay(j+1) = total_delays/num_pack_insys;
P(j+1) = service_mean_time./arrival_mean_time(j+1);
set(handles.Average_delay,'String',avg_delay(j+1));
set(handles.average_number,'String',avg_num_in_queue(j+1));
set(handles.server_utilization,'String',util(j+1));
set(handles.time,'String',sim_time);
end

% --- Executes on button press in exit.


function exit_Callback(hObject, eventdata, handles)
delete(gcbf);

Hasil Simulasi di MATLAB