WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Vereinfachung Code

 

peda

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 16.01.2019, 18:04     Titel: Vereinfachung Code
  Antworten mit Zitat      
Hallo ich bin Matlabanfänger,

bei meiner Aufgabe handelt es sich um einen Supermarkt:

derzeit 2 Kassen für alle Kunden
• Geschäftsführung am überlegen, eine Expresskasse einzuführen
• Expresskasse für Kunden mit weniger als 5 Artikel
• Ankunftsrate
für normale Kunden exponentialverteilt mit Erwartungswert 3 Minuten
von ‚Expresskunden‘ ist exponentialverteilt mit Erwartungswert 5 Minuten
• Bedienzeit:
von normalen Kunden exponentialverteilt mit Erwartungswert 5 Minuten
von ‚Expresskunden‘ ist exponentialverteilt mit Erwartungswert 3 Minuten
• wie verändern sich die mittleren Schlangenlängen der einzelnen Kassen
bei den unterschiedlichen Strategien?

Ich als Anfänger hab leider einen einen sehr umfangreichen Code geschrieben und würde diesen irgendwie gerne vereinfachen und kürzen.

Vielleicht kann mir ja jemand helfen und vll sieht auch jemand einen Fehler, den ich übersehen habe.

Danke für eure Hilfe.

LG

Code:
% definition of the variables
openTime = 7:19;                                  % open from 7:00 - 19:00 clock
simulationsTime = (openTime(end)-openTime(1))*60; % in minutes

expressCustomers_cum = 0;                         % cumulated arriving time of express customers
normalCustomers_cum = 0;                          % cumulated arriving time of normal customers

finalEndTimeCustomer_Cash1 = [];                  % end time of customers from cash 1
finalEndTimeCustomer_Cash2 = [];                  % end time of customers from cash 2
tempQueueEndTime_Cash1 = [];                      % end time of customers in the queue in front of cash 1
tempQueueEndTime_Cash2 = [];                      % end time of customers in the queue in front of cash 2
queue_Cash1 = 0;                                  % queue cash 1, strategy without express cash
queue_Cash2 = 0;                                  % queue cash 2, strategy without express cash

finalEndTimeCustomer_Cash1_Way2 = [];             % end time of customers from cash 1, strategy 2, with express cash
finalEndTimeCustomer_Cash2_Way2 = [];             % end time of customers from cash 2, strategy 2
finalEndTimeCustomer_CashExpress_Way2 = [];       % end time of customers from express cash, strategy 2
tempQueueEndTime_Cash1_Way2 = [];                 % end time of customers in the queue in front of cash 1, strategy 2
tempQueueEndTime_Cash2_Way2 = [];                 % end time of customers in the queue in front of cash 2, strategy 2
tempQueueEndTime_CashExpress_Way2 = [];           % end time of customers in the queue in front of express cash, strategy 2
queue_Cash1_Way2 = 0;                             % queue cash 1, strategy with express cash
queue_Cash2_Way2 = 0;                             % queue cash 2, strategy with express cash
queue_CashExpress_Way2 = 0;                       % queue express cash

ii = 0;
jj= 0;

isNormalCustomerTimeReady = 0;                   % variable if normal customer is ready
isExpressCustomerTimeReady = 0;                  % variable if express customer is ready

while (isNormalCustomerTimeReady == 0) || (isExpressCustomerTimeReady == 0)
    % calculation of the arrival times of the customers
    if isNormalCustomerTimeReady == 0
        if (normalCustomers_cum(end) < simulationsTime)         % while arrival time of normal customers is under simulation time
            ii=ii+1;                                            % index + 1
            normalCustomers = (-3)*log(rand(1,1));              % arrival time of normal customers
            if ii == 1                                          % if index = 1
                normalCustomers_cum(ii,1) = normalCustomers;    % cumulated arrival time of normal customer is the first arrival time
            else
                normalCustomers_cum(ii,1) = (normalCustomers_cum(ii-1) + normalCustomers);     % else we sum up the normal arrival times
            end
        else
            isNormalCustomerTimeReady = 1;                      % normal customers have achieved the simulation time
        end
    end
    if isExpressCustomerTimeReady == 0                          
        if (expressCustomers_cum(end) < simulationsTime)        % while arrival time of express customers is under simulation time
            jj=jj+1;                                            % index + 1
            expressCustomers = (-5)*log(rand(1,1));             % arrival time of express customers
            if jj == 1                                          % if index = 1
                expressCustomers_cum(jj,1) = expressCustomers;  % cumulated arrival time of express customer is the first arrival time
            else
                expressCustomers_cum(jj,1) = (expressCustomers_cum(jj-1) + expressCustomers);  % else we sum up the express arrival times
            end
        else
            isExpressCustomerTimeReady = 1;                     % express customers have achieved the simulation time
        end
    end
end

% in the first coluum are the arrival times, in the second coluum are
% the values 0 or 1
normalCustomers_cum(:,2) = zeros(length(normalCustomers_cum),1);                % 0 for normal customers
expressCustomers_cum(:,2) = ones(length(expressCustomers_cum),1);               % 1 for express customers

customersMatrix = [normalCustomers_cum;expressCustomers_cum]; % arrival time of both types of customers
customersMatrix = Function_Sort(customersMatrix);             % sorting of the arrival times, with a function for sorting


% simulation
for ii = 1:length(customersMatrix(:,1))              % for 1 to length of custumers (all rows, so all arrival times)
    if customersMatrix(ii,2) == 1                    % if the incomming customer is an express customer
        durationCash = (-3)*log(rand());             % calculation of the duration of the service time
    else                                             % if the incomming customer is a normal customer
        durationCash = (-5)*log(rand());             % calculation of the duration of the service time
    end
    % --------------------------------------------------------------------------------------------
    % strategy without express cash
    % --------------------------------------------------------------------------------------------
    % if new customer arrives, how many customers have now been serviced
    [queue_Cash1,tempQueueEndTime_Cash1] = Function4FlushQueue(customersMatrix(1:ii,1),queue_Cash1,tempQueueEndTime_Cash1);
    [queue_Cash2,tempQueueEndTime_Cash2] = Function4FlushQueue(customersMatrix(1:ii,1),queue_Cash2,tempQueueEndTime_Cash2);
   
    % choose the cash
    if queue_Cash1 <= queue_Cash2          % the customer choose the cash with the shorter queue, when both queues have the same length the customer choose cash 1
        % cash 1
        [finalEndTimeCustomer_Cash1,queue_Cash1,tempQueueEndTime_Cash1]  = Function4CashWaitTime(customersMatrix(1:ii,1),finalEndTimeCustomer_Cash1,durationCash,queue_Cash1,tempQueueEndTime_Cash1);
    else                                   % queue at cash 2 is shorter
        % cash 2
        [finalEndTimeCustomer_Cash2,queue_Cash2,tempQueueEndTime_Cash2]  = Function4CashWaitTime(customersMatrix(1:ii,1),finalEndTimeCustomer_Cash2,durationCash,queue_Cash2,tempQueueEndTime_Cash2);
    end
    numbersQueue_Cash1(ii) = queue_Cash1; % length of queue cash 1 by customer ii
    numbersQueue_Cash2(ii) = queue_Cash2; % length of queue cash 2 by customer ii
   
    % --------------------------------------------------------------------------------------------
    % strategy with express cash
    % --------------------------------------------------------------------------------------------
    % if new customer arrives, how many customers have now been serviced
    [queue_Cash1_Way2,tempQueueEndTime_Cash1_Way2] = Function4FlushQueue(customersMatrix(1:ii,1),queue_Cash1_Way2,tempQueueEndTime_Cash1_Way2);
    [queue_Cash2_Way2,tempQueueEndTime_Cash2_Way2] = Function4FlushQueue(customersMatrix(1:ii,1),queue_Cash2_Way2,tempQueueEndTime_Cash2_Way2);
    [queue_CashExpress_Way2,tempQueueEndTime_CashExpress_Way2] = Function4FlushQueue(customersMatrix(1:ii,1),queue_CashExpress_Way2,tempQueueEndTime_CashExpress_Way2);
   
    % choose the cash
    if customersMatrix(ii,2) == 0                   % normal customer
        if queue_Cash1_Way2 <= queue_Cash2_Way2     % the normal customer choose the cash with the shorter queue, when both queues have the same length the normal customer choose cash 1
            chosenCash = 1;                         % cash 1
        else                                        % queue at cash 2 is shorter
            chosenCash = 2;                         % cash 2
        end
    else                                            % express customer
        if (queue_CashExpress_Way2 <= queue_Cash1_Way2) && (queue_CashExpress_Way2 <= queue_Cash2_Way2) % is the shortest queue at the express cash, the express customer choose the expres cash
            chosenCash = 3;                         % express cash
        elseif queue_Cash1_Way2 <= queue_Cash2_Way2 % is the queue at cash 1 shorter the express customer choose cash 1
            chosenCash = 1;                         % cash 1
        else                                        % queue at cash 2 is shorter
            chosenCash = 2;                         % cash 2
        end
    end
   
    if chosenCash == 1                             % if the choosen cash is cash 1
        [finalEndTimeCustomer_Cash1_Way2,queue_Cash1_Way2,tempQueueEndTime_Cash1_Way2] = Function4CashWaitTime(customersMatrix(1:ii,1),finalEndTimeCustomer_Cash1_Way2,durationCash,queue_Cash1_Way2,tempQueueEndTime_Cash1_Way2);
    elseif chosenCash == 2                         % if the choosen cash is cash 2
        [finalEndTimeCustomer_Cash2_Way2,queue_Cash2_Way2,tempQueueEndTime_Cash2_Way2] = Function4CashWaitTime(customersMatrix(1:ii,1),finalEndTimeCustomer_Cash2_Way2,durationCash,queue_Cash2_Way2,tempQueueEndTime_Cash2_Way2);
    else                                           % if the choosen cash is the express cash
        [finalEndTimeCustomer_CashExpress_Way2,queue_CashExpress_Way2,tempQueueEndTime_CashExpress_Way2] = Function4CashWaitTime(customersMatrix(1:ii,1),finalEndTimeCustomer_CashExpress_Way2,durationCash,queue_CashExpress_Way2,tempQueueEndTime_CashExpress_Way2);
    end
    numbersQueue_Cash1_Way2(ii) = queue_Cash1_Way2;  % length of queue cash 1 by customer ii
    numbersQueue_Cash2_Way2(ii) = queue_Cash2_Way2;  % length of queue cash 2 by customer ii
    numbersQueue_CashExpress_Way2(ii) = queue_CashExpress_Way2; % length of queue express by customer ii
end

fprintf('\n ------------------------------------------------------------------------------------------------------')
fprintf('\nDie Simulationszeit beträgt %g Stunden, in dieser Zeit wurden %g Kunden bedient',simulationsTime/60,length(customersMatrix));

fprintf('\n\n1. Strategie: Zwei Kassen');
fprintf('\nDie durchschnittliche Länge der Warteschlange der ersten  Kassa beträgt %.2g Personen',sum(numbersQueue_Cash1)/length(numbersQueue_Cash1));
fprintf('\nDie durchschnittliche Länge der Warteschlange der zweiten Kassa beträgt %.2g Personen',sum(numbersQueue_Cash2)/length(numbersQueue_Cash2));

fprintf('\n\n2. Strategie: Drei Kassen, eine nur für Expresskunden' );
fprintf('\nDie durchschnittliche Länge der Warteschlange der ersten  Kassa beträgt %.2g Personen',sum(numbersQueue_Cash1_Way2)/length(numbersQueue_Cash1_Way2));
fprintf('\nDie durchschnittliche Länge der Warteschlange der zweiten Kassa beträgt %.2g Personen',sum(numbersQueue_Cash2_Way2)/length(numbersQueue_Cash2_Way2));
fprintf('\nDie durchschnittliche Länge der Warteschlange der express Kassa beträgt %.2g Personen\n',sum(numbersQueue_CashExpress_Way2)/length(numbersQueue_CashExpress_Way2));
end

function  [finalEndTimeCustomer_Cash,queue_Cash,tempQueueEndTime_Cash] = Function4CashWaitTime(customersArrivalTime,finalEndTimeCustomer_Cash,durationCash,queue_Cash,tempQueueEndTime_Cash)
% calculates the end time of the customer and increases the queue
% input parameters:  
% customersArrivalTime          -->   vector of customer arrival times, only the first column
% finalEndTimeCustomer_Cash     -->   vector of end times
% durationCash                  -->   duration of the cash
% queue_Cash                    -->   queue of the cash
% tempQueueEndTime_Cash         -->   end time of customers in the queue in front of cash

% output parameters:
% finalEndTimeCustomer_Cash     -->   vector of end times
% queue_Cash                    -->   queue of the cash
% tempQueueEndTime_Cash         -->   end time of customers in the queue in front of cash
indexCash = length(finalEndTimeCustomer_Cash) + 1;        % calculation of index
if indexCash == 1                                         % first customer
    finalEndTimeCustomer_Cash(indexCash) = customersArrivalTime(end) + durationCash;  % end time of customer = incomming time of customer + duration time of customer
    waitTime_Customer_Cash = 0;                           % waiting time is 0
else                                                      % else (further customers)
    finalEndTimeCustomer_Cash(indexCash) = max(finalEndTimeCustomer_Cash(indexCash-1),customersArrivalTime(end)) + durationCash; % maximum of end time(previous customer) or incomming time customer + duration time
    waitTime_Customer_Cash = max((finalEndTimeCustomer_Cash(indexCash-1)-customersArrivalTime(end)),0);  % waiting time is maximum of end time (previous customer) - incomming time customer or 0
end
if waitTime_Customer_Cash > 0                             % is there a waiting time
    queue_Cash = queue_Cash + 1;                          % queue + 1
    tempQueueEndTime_Cash(queue_Cash) = finalEndTimeCustomer_Cash(indexCash-1);  % remember the end time of the previous customer to know when the next customer starts
end
end

function [queue_Cash,tempQueueEndTime_Cash] = Function4FlushQueue(customersArrivalTime,queue_Cash,tempQueueEndTime_Cash)
% deletes customers from the queue using the end times of the customers on the waiting list and the arrivals of new customers
% input parameters:  
% customersArrivalTime          -->   vector of customer arrival times, only the first column
% queue_Cash                    -->   queue of the cash
% tempQueueEndTime_Cash         -->   end time of customers in the queue in front of cash

% output parameters:
% queue_Cash                    -->   queue of the cash
% tempQueueEndTime_Cash         -->   end time of customers in the queue in front of cash

if queue_Cash > 0                                                                       % someone is in the queue
    idxCustomerIsReadyInQueue = (customersArrivalTime(end) >= tempQueueEndTime_Cash);   % what customers in queue are ready when the next customer arrives
    queue_Cash = queue_Cash - sum(idxCustomerIsReadyInQueue);                           % all ready customers leave the queue
    tempQueueEndTime_Cash(idxCustomerIsReadyInQueue) = [];                              % we set the time of the customer on the queue empty
end
end

function [Matrix2Sort] = Function_Sort(Matrix2Sort)
% Sorting, sort by values in the first column
% input parameter:
% Matrix2Sort                   -->   matrix with n rows and 2 columns (nx2)

% output parameter:
% Matrix2Sort                   -->   sorted matrix with n rows and 2 columns (nx2)

for hh = 1:length(Matrix2Sort)-1
    for jj = length(Matrix2Sort):-1:hh+1          % from the end of length of matrix sort, in one steps to hh + 1
        if Matrix2Sort(jj,1) < Matrix2Sort(hh,1)  % compare the arrival times of the last customer with the arrival times of the first customer
            helpvar = Matrix2Sort(hh,:);          % if the arrival time of the first customer is higher than the arrival time of the last customer put the first customer on the helpvariable that we do not lose the first customer
            Matrix2Sort(hh,:) = Matrix2Sort(jj,:);% and the last customer put on the first position
            Matrix2Sort(jj,:) = helpvar;          % now take the customer which was the first customer before from the helpvariable and put it on the last position
        end
    end
end
end
 


Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 17.01.2019, 15:44     Titel: Re: Vereinfachung Code
  Antworten mit Zitat      
Hallo peda,

Ich würde kürzere Namen für die Variablen verwenden. Bei "tempQueueEndTime_CashExpress_Way2" gruselt es mich. Ich würde "isNormalCustomerTimeReady" durch "readyNormal" ersetzen, denn es unterscheidet bereits deutlich "readyExpress".
In
Code:
function [Matrix2Sort] = Function_Sort(Matrix2Sort)
% Sorting, sort by values in the first column
% input parameter:
% Matrix2Sort                   -->   matrix with n rows and 2 columns (nx2)

% output parameter:
% Matrix2Sort                   -->   sorted matrix with n rows and 2 columns (nx2)

for hh = 1:length(Matrix2Sort)-1
    for jj = length(Matrix2Sort):-1:hh+1          % from the end of length of matrix sort, in one steps to hh + 1
        if Matrix2Sort(jj,1) < Matrix2Sort(hh,1)  % compare the arrival times of the last customer with the arrival times of the first customer
            helpvar = Matrix2Sort(hh,:);          % if the arrival time of the first customer is higher than the arrival time of the last customer put the first customer on the helpvariable that we do not lose the first customer
            Matrix2Sort(hh,:) = Matrix2Sort(jj,:);% and the last customer put on the first position
            Matrix2Sort(jj,:) = helpvar;          % now take the customer which was the first customer before from the helpvariable and put it on the last position
        end
    end
end
end

wäre "M" genauso hilfreich wie "Matrix2Sort", aber es würde den Text viel einfacher lesbar machen. Man kann allerdings auch gleich Matlabs sort Funktion verwenden:
Code:
function M = SortColumn1(M)
[~, index] = sort(M(:,1));
M = M(index, :);
end

"SortColumn1" sagt andeutungsweise, was die Funktion auch macht, während das bei "Function_Sort" nicht ganz klar ist.

Die ausgiebige Kommentierung des Codes ist sehr gut!

Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
grandmasta

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 18.01.2019, 08:28     Titel: Vereinfachung Code
  Antworten mit Zitat      
Danke Jan. LG
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.