% This file uses the output of program lambda
% Open workspace of lambda before running this program


% Packet size in bits
packet_size = 1000;

queue_capacity_pkts = 5000;
queue_capacity_bits = queue_capacity_pkts * packet_size;

mean_arrival_rate_pps = mean(pkts);
mean_arrival_rate_bitsps = (mean_arrival_rate_pps * packet_size);

MMPP_mean_arrival_rate_pps = mean(MMPP_pkts);
MMPP_mean_arrival_rate_bitsps = (MMPP_mean_arrival_rate_pps * packet_size);

j = 1;
mean_queue_delay_array = 0;
MMPP_mean_queue_delay_array = 0;

mean_loss_rate_array = 0;
MMPP_mean_loss_rate_array = 0;

mean_throughput_array = 0;
MMPP_mean_throughput_array = 0;

load_array = 0;
load_array = 0.39:0.03:0.99;
for loadj = 0.39:0.03:0.99
    pload = loadj;
    mean_service_rate_bps = mean_arrival_rate_bitsps / pload;
    MMPP_mean_service_rate_bps = MMPP_mean_arrival_rate_bitsps / pload;
    mean_queue_size = 0;
    MMPP_mean_queue_size = 0;
    queue_size = 0;
    MMPP_queue_size = 0;
    loss_rate = 0; throughput = 0;
    MMPP_loss_rate = 0; MMPP_throughput = 0;
    mean_loss_rate = 0; mean_throughput = 0;
    MMPP_mean_loss_rate = 0; MMPP_mean_throughput = 0;
    for i = 1:datasize
        if ((queue_size == queue_capacity_bits) && ((pkts(i) * packet_size)>mean_service_rate_bps))
            loss_rate = ((pkts(i) * packet_size)-mean_service_rate_bps);
            throughput = ((pkts(i) * packet_size) - ((pkts(i) * packet_size)-mean_service_rate_bps))/(pkts(i) * packet_size);
        else
            loss_rate = 0;
            throughput = 1;
        end
        mean_loss_rate = (((mean_loss_rate * (i-1)) + loss_rate)/i);
        mean_throughput = (((mean_throughput * (i-1)) + throughput)/i);
        
        queue_size = queue_size + ((pkts(i) * packet_size) -  mean_service_rate_bps);
        if (queue_size < 0)
            queue_size = 0;
        end
        if (queue_size > queue_capacity_bits)
            queue_size = queue_capacity_bits;
        end
        mean_queue_size = (((mean_queue_size * (i-1)) + queue_size)/i);
        
        if ((MMPP_queue_size == queue_capacity_bits) && ((MMPP_pkts(i) * packet_size)>MMPP_mean_service_rate_bps))
            MMPP_loss_rate = ((MMPP_pkts(i) * packet_size)-MMPP_mean_service_rate_bps);
            MMPP_throughput = ((MMPP_pkts(i) * packet_size) - ((MMPP_pkts(i) * packet_size)-MMPP_mean_service_rate_bps))/(MMPP_pkts(i) * packet_size);
        else
            MMPP_loss_rate = 0;
            MMPP_throughput = 1;
        end
        MMPP_mean_loss_rate = (((MMPP_mean_loss_rate * (i-1)) + MMPP_loss_rate)/i);
        MMPP_mean_throughput = (((MMPP_mean_throughput * (i-1)) + MMPP_throughput)/i);
        
        MMPP_queue_size = MMPP_queue_size + ((MMPP_pkts(i) * packet_size) -  MMPP_mean_service_rate_bps);
        if (MMPP_queue_size < 0)
            MMPP_queue_size = 0;
        end
        if (MMPP_queue_size > queue_capacity_bits)
            MMPP_queue_size = queue_capacity_bits;
        end
        MMPP_mean_queue_size = (((MMPP_mean_queue_size * (i-1)) + MMPP_queue_size)/i);
    end
    
    mean_loss_rate_array(j) = mean_loss_rate;
    mean_throughput_array(j) = mean_throughput;
    MMPP_mean_loss_rate_array(j) = MMPP_mean_loss_rate;
    MMPP_mean_throughput_array(j) = MMPP_mean_throughput;
    mean_queue_delay_array (j) = (mean_queue_size / mean_service_rate_bps);
    MMPP_mean_queue_delay_array (j) = (MMPP_mean_queue_size / MMPP_mean_service_rate_bps);
    j = j + 1;
end

% mean_delay_combiner_array = 0;
% mean_delay_combiner_array = [mean_queue_delay_array' MMPP_mean_queue_delay_array'];
% figure (1);
% plot (load_array, mean_delay_combiner_array, '-*');
% grid on;
% hold on;


% 
% plot (load_array, MMPP_mean_queue_delay_array, '-o');


mean_lossrate_combiner_array = 0;
mean_lossrate_combiner_array = [mean_loss_rate_array' MMPP_mean_loss_rate_array'];
figure (1);
plot (load_array, mean_lossrate_combiner_array, '-*');
grid on;
hold on;
% % 
% plot (load_array, MMPP_mean_loss_rate_array, '-o');
% legend('Data Trace','New Algorithm','Original Method');

% mean_lossrate_combiner_array = 0;
% mean_lossrate_combiner_array = [mean_throughput_array' MMPP_mean_throughput_array'];
% figure (1);
% plot (load_array, mean_lossrate_combiner_array, '-*');
% grid on;
% hold on;
% 
% plot (load_array, MMPP_mean_throughput_array, '-o');
% legend('Data Trace','New Algorithm','Original Method');