1 / 14

Algoritmai ir duomenų struktūros ( AD S)

Algoritmai ir duomenų struktūros ( AD S). 6 paskaita Saulius Ragaišis , VU MIF saulius.ragaisis@mif.vu.lt 20 12 - 03 - 12. Modeliavimo programos pavyzdys. Sąlyga : savitarnos parduotuvė dirba T laiko vienetų;

cohman
Download Presentation

Algoritmai ir duomenų struktūros ( AD S)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algoritmai ir duomenų struktūros(ADS) 6 paskaita Saulius Ragaišis, VU MIF saulius.ragaisis@mif.vu.lt 2012-03-12

  2. Modeliavimo programos pavyzdys Sąlyga:savitarnos parduotuvė dirba T laiko vienetų; joje yra N kasų;kiekvienu laiko momentu su tikimybe M (%) ateina klientas;jo apsipirkimo laikas yra atsitiktinis, bet ne didesnis nei A;baigęs pirkti, jis stoja į eilę prie atsitiktinės kasos;jo aptarnavimo prie kasos laikas yra atsitiktinis, bet ne didesnis nei B.

  3. Modeliavimo programos pavyzdys Rasti:kiek reikia turėti parduotuvės vežimėlių, kad jų nepritrūktų (daroma prielaida, kad aptarnavus klientą prie kasos, jo vežimėliu iš karto gali naudotis naujas klientas).

  4. Programos parametrų failas 500 modeliavimo laikas 5 kasu skaičius 50 kliento atėjimo tikimybė (%) 40 maksimalus kliento apsipirkimo laikas 10 maksimalus kliento aptarnavimo prie kasos laikas

  5. Eilėje saugomų duomenų tipas unit QueueData; interface type QueueDataType = ...; implementation end.

  6. ADT Eilė unit Queue; interface uses QueueData; type QueueType = ...; procedure createQueue (var queue : QueueType); function isEmptyQueue (const queue : QueueType) : boolean; function isFullQueue (const queue : QueueType) : boolean; procedure enQueue (var queue : QueueType; data : QueueDataType; var err : boolean); procedure deQueue (var queue : QueueType; var data : QueueDataType; var err : boolean); procedure firstInQueue (const queue : QueueType; var data : QueueDataType; var err : boolean); function lengthOfQueue(const queue : QueueType) : integer; procedure destroyQueue (var queue : QueueType); implementation

  7. Programa program Shop; uses QueueData, Queue; const PARAMS_FILE_NAME = 'params.txt'; MAX_CASH_DESKS = 10; MAX_ACTIVE_CLIENTS = 20; { Global variables } var errors : boolean; simulationTime,noOfCashDesks,newClientProbability, maxBuyingTime, maxCashingTime : integer; procedure loadParams; ... { Program variables } var time,maxNoOfShoppingCarts,usedShoppingCarts,noOfActiveClients, i, j, k : integer; cashingTime : QueueDataType; activeClients : array[1..MAX_ACTIVE_CLIENTS] of integer; cashDesks : array[1..MAX_CASH_DESKS] of QueueType; cashingLeft : array[1..MAX_CASH_DESKS] of integer; begin loadParams;if errors then exit; ...

  8. Programa (2) while not errors and (time < simulationTime) do{ Main simulation loop } begin inc(time); { Get new client } if random(100) <= newClientProbability then begin { New client should be added to active clients } inc(noOfActiveClients); if noOfActiveClients > MAX_ACTIVE_CLIENTS then begin writeln('Number of active clients become too big ( >', MAX_ACTIVE_CLIENTS, ' ).'); errors := true; break; end; activeClients[noOfActiveClients] := time + random(maxBuyingTime) + 1; { New client gets a shopping cart } inc(usedShoppingCarts); if usedShoppingCarts > maxNoOfShoppingCarts then maxNoOfShoppingCarts := usedShoppingCarts; end; { Check active clients who finished buying } ... { Check queues at cash desks } ... end; { Main simulation loop }

  9. Programa (3) { Check active clients who finished buying } j := 0; for i := 1 to noOfActiveClients do begin if activeClients[i] > time then begin inc(j); activeClients[j] := activeClients[i]; end else begin k := random(noOfCashDesks) + 1; cashingTime := random(maxCashingTime) + 1; enQueue(cashDesks[k], cashingTime, errors); if errors then begin writeln('The queue of cash desk becomes too long.'); break; end; if lengthOfQueue(cashDesks[k]) = 1 then cashingLeft[k] := cashingTime + 1; dec(noOfActiveClients); end end;

  10. Programa (4) { Check queues at cash desks } for k := 1 to noOfCashDesks do if lengthOfQueue(cashDesks[k]) > 0 then begin dec(cashingLeft[k]); if cashingLeft[k] = 0 then begin deQueue(cashDesks[k], cashingTime, errors); if errors then begin writeln('Program error: trying to dequeue.'); break; end; if lengthOfQueue(cashDesks[k]) > 0 then begin firstInQueue(cashDesks[k], cashingTime, errors); cashingLeft[k] := cashingTime; end; end; end;

  11. Programa (5) { Output simulation results } writeln('Max number of shopping carts is ', maxNoOfShoppingCarts); { Clearing data structues } for i := 1 to MAX_CASH_DESKS do destroyQueue(cashDesks[i]); end. Pilnas programos tekstas bus pateiktas internete.

  12. Programos vykdymas D:\MIF\A&DS\_Pvz>shop_a Max number of shopping carts is 244 D:\MIF\A&DS\_Pvz>shop_a Max number of shopping carts is 244 D:\MIF\A&DS\_Pvz>shop_a Max number of shopping carts is 244 Pagrindinės programos pradžioje pridėjus: Randomize; D:\MIF\A&DS\_Pvz>shop_r Max number of shopping carts is 257 D:\MIF\A&DS\_Pvz>shop_r Max number of shopping carts is 256 D:\MIF\A&DS\_Pvz>shop_r Max number of shopping carts is 267 D:\MIF\A&DS\_Pvz>shop_r Max number of shopping carts is 251Ar programos rezultatai teisingi?

  13. Apibendrinimas Visada vertėtų pavertinti, ar programos rezultatai prasmingi. Modeliavome 500 laiko vienetų, o kliento atėjimo tikimybė yra 50%. Taigi per visą laiką turėtų ateiti apie 250 klientų. O programa sako, kad tiek vežimėlių ir reikia. Kyla įtarimas, kad vežimėliai neatlaisvinami... Patikrinus, pasirodo, kad programoje praleistas dec(usedShoppingCarts);

  14. Klausimai ?

More Related