Project 2 飛機場模擬_第1頁
Project 2 飛機場模擬_第2頁
Project 2 飛機場模擬_第3頁
Project 2 飛機場模擬_第4頁
Project 2 飛機場模擬_第5頁
已閱讀5頁,還剩49頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Project 2飛機場模擬班級:2012級 自動化系 自動化 智能科學(xué)姓名:蔡 冰 學(xué)號:12350001姓名:李東特 學(xué)號:12350029姓名:盧秀芳 學(xué)號:12350042【題目要求】 圖1 飛機場P1、將所有用于飛機場模擬的函數(shù)和方法組合成一個完整的程序。用飛機場模擬程序做若干次試運行實驗,調(diào)整準備著陸和起飛的飛機數(shù)的期望值,并找出在飛機不會被拒絕服務(wù)的條件下這些數(shù)字盡可能大的近似值。如果隊列的長度增加或減少,那么這些值將會有什么變化?P2、修改模擬程序,使飛機場有兩條飛機跑道,其中一條總是用來著陸,另一條總是用來起飛。比較雙跑道機場能服務(wù)的總飛機數(shù)和單條飛機跑道的飛機場的相應(yīng)數(shù)字,

2、前者是否為后者的兩倍?P3、修改模擬程序,使飛機場有兩條飛機跑道,其中一條總是用來著陸,另一條總是用來起飛。如果某個隊列是空的,那么兩條跑道都能用于其他的隊列。如果著陸隊列總是滿的,并且另一架飛機要到達著陸,那么將停止起飛,并將兩條跑道都用于清理擱置的著陸飛機。P4、修改模擬程序,使飛機場有3條飛機跑道,其中各保留一條總是用于著陸和起飛,第三條用于著陸,但在著陸隊列為空的情況下,第三條亦可用于起飛。P5、修改最初的模擬程序(單條跑道),使得當(dāng)每架飛機到達著陸時,它將有(作為它的數(shù)據(jù)成員的)一個(隨機產(chǎn)生的)油位,以剩余的時間單元度量。如果飛機沒有足夠的油位在隊列中等待,則允許它立即著陸。因此著

3、陸隊列里的飛機可能需要再等待附加的單元,因此可能用完自身的燃料。作為著陸函數(shù)的一部分要檢查這一點,并查明在飛機由于燃料耗盡而開始墜毀前機場有多忙。P6、寫一個占位程序來代替隨機函數(shù),這個占位程序既能用于調(diào)試程序又允許用戶正確地控制每一個時間單元內(nèi)每個隊列到達的飛機數(shù)。【數(shù)據(jù)結(jié)構(gòu)與算法】P1:這里采用了兩種輸入輸出方式:開始是使用cin、cout。用于處理數(shù)據(jù)較多,后來采用文件輸入輸出。但題P2P6仍然使用cin、cout。(文件輸入輸出)流程圖如下: P2:P3:P4:P5:P6:【測試數(shù)據(jù)、結(jié)果及分析】【分工、貢獻%、自我評分】【項目總結(jié)】1、本次實驗加深了我們對于隊列的理解,以及更深刻地理

4、解了他的實際應(yīng)用,可以仿真機場的調(diào)度,由此推廣開,也可以仿真任何可以用于排隊的場合,只要修改相應(yīng)的類對象就可以實現(xiàn)。2、本次實驗的難度在于實驗所涉及的項目非常之多,各種對象,函數(shù)非常之多,導(dǎo)致調(diào)試問題較多。3、本次試驗出現(xiàn)的技術(shù)性問題Plane類Runway類、隊列以及Random類分別放在不同的頭文件中,但相互之間有交集,存在著相互引用的現(xiàn)象,導(dǎo)致重復(fù)定義的問題。解決方法為每個類及自定義類型加上宏定義“#ifndef #define #endif”就可以避免重定義問題。輸入到文件中時,由于該語句是寫在Runway類中的,每次測試數(shù)據(jù)都會調(diào)用到該函數(shù) ,重復(fù)打開該輸出文件,要加上iso:app

5、語句,以便將后面測試數(shù)據(jù)的結(jié)果加到前面的結(jié)果中,否則,輸出文件中只有最后的數(shù)據(jù)的結(jié)果。由于文件較多,要注意文件的鏈接問題。4、本次項目在實現(xiàn)過程中依照課本中給的類定義及主函數(shù)實現(xiàn)一條跑道的問題,優(yōu)先處理降落的飛機。而兩條跑道的時候,則可以同時處理起飛和降落,可以用兩個switch函數(shù)來判斷,一個判斷是否有降落,一個判斷是否有起飛。同樣是兩條軌道的情況,但是當(dāng)一條軌道為空時可用于其他用途。小組最開始討論的結(jié)果是再加上一條隊列,但是考慮到實際情況,以及該種方法可能不是最優(yōu)的做法,小組放棄了這種思路。最后采用的方法的關(guān)鍵是給Runway函數(shù)添加兩個size函數(shù),通過size函數(shù)判斷跑道現(xiàn)在的狀態(tài),在

6、作出相應(yīng)的響應(yīng)。三條軌道的情況同樣也沒有太大的改變,只要判斷出landing 軌道為空時就可以改變其用途用于起飛。將飛機燃油的情況加入考慮其實是隊列優(yōu)先級的一種應(yīng)用。考慮飛機的燃油情況,將飛機的燃油情況加入飛機的標志中,在原來的機場調(diào)度的情況下加入了飛機優(yōu)先級的考慮,通過計算判斷其是否處于緊急狀況,是的話則將隊列中應(yīng)降落的飛機設(shè)為當(dāng)前的緊急飛機,改變其入隊順序?!境绦蚯鍐巍縋1:用文件輸入輸出見附錄B,用cin、cout輸入輸出見附錄CP2P6:見附錄C附錄說明:附錄A 類及函數(shù)實現(xiàn)文件附錄B P1用文件輸入輸出附錄C P1P6程序清單(用cin、cout輸入輸出)附錄D P1運行后在屏幕上輸

7、出的結(jié)果,僅含一次輸入后的結(jié)果 附錄A說明:類及函數(shù)實現(xiàn)文件“p.h”#include<iostream>using namespace std;#ifndef PLANE_STATUS#define PLANE_STATUSenum Plane_status null, arriving, departing;#endif#ifndef PLANE#define PLANEclass Planepublic:Plane();Plane(int flt, int time, Plane_status status);void refuse() const;void land(int

8、 time) const;void fly(int time) const;int started() const;private:int flt_num;int clock_start;Plane_status state;#endif“q.h”#include <iostream>#include"p.h"using namespace std;#ifndef ERROR_CODE#define ERROR_CODEenum Error_code success,overflow ,underflow,fail;#endiftypedef Plane Que

9、ue_entry;#ifndef QUEUE#define QUEUEclass Queuepublic:Queue();bool empty()const;Error_code serve();Error_code append(const Queue_entry &item);Error_code retrieve(Queue_entry &item)const;protected:int count;int front,rear;Queue_entry entry1000;#endif#ifndef EXTENDED_QUEUE#define EXTENDED_QUEUE

10、class Extended_Queue:public Queuepublic:int size()const;#endif“r.h”#include<iostream>#include "q.h"using namespace std;#ifndef RUNWAY_ACTIVITY#define RUNWAY_ACTIVITY#endif#ifndef RUNWAY#define RUNWAY#endifenum Runway_activity idle, land, take_off;class Runway public:Runway(int limit)

11、;Error_code can_land(const Plane &current);Error_code can_depart(const Plane &current);Runway_activity activity(int time, Plane &moving);void shut_down(int time) const;private:Extended_Queue landing;Extended_Queue takeoff;int queue_limit;int num_land_requests; / 請求降落的飛機數(shù)int num_takeoff_r

12、equests; / 請求起飛的飛機數(shù)int num_landings; / 已經(jīng)降落的飛機數(shù)int num_takeoffs; / 已經(jīng)起飛的飛機數(shù)int num_land_accepted; / 等待降落的飛機數(shù)int num_takeoff_accepted; / 等待起飛的飛機數(shù)int num_land_refused; / 被拒絕降落的飛機數(shù)int num_takeoff_refused; / 被拒絕起飛的飛機數(shù)int land_wait; / 降落飛機等待總時間int takeoff_wait; / 起飛等待總時間int idle_time; / 跑道空閑總時間;class Ran

13、dompublic:Random(bool pseudo=true);int poisson(double mean);double random_real();private:int seed,multiplier,add_on;int reseed();實現(xiàn)cpp:#include<iostream>#include"r.h"#include "p.h"#include "q.h"#include <time.h>#include <cmath>#include <limits>us

14、ing namespace std;Runway:Runway(int limit)/*Post: 將統(tǒng)計數(shù)據(jù)清零,并設(shè)置隊列大小限制*/ queue_limit = limit; num_land_requests = num_takeoff_requests = 0; num_landings = num_takeoffs = 0; num_land_refused = num_takeoff_refused = 0; num_land_accepted = num_takeoff_accepted = 0; land_wait = takeoff_wait = idle_time = 0

15、;Error_code Runway:can_land(const Plane &current)/*Post: 若等待隊列不滿,將飛機current加入,否則返回overflow 更新統(tǒng)計數(shù)據(jù)Uses: Extended_queue類*/ Error_code result; if (landing.size() < queue_limit) result = landing.append(current); else result = fail; num_land_requests+; if (result != success) num_land_refused+; els

16、e num_land_accepted+; return result;Error_code Runway:can_depart(const Plane &current)Error_code result;if (takeoff.size() < queue_limit)result = takeoff.append(current);elseresult = fail;num_takeoff_requests+; if (result != success)num_takeoff_refused+;elsenum_takeoff_accepted+;return result

17、;Runway_activity Runway:activity(int time, Plane &moving)/*Post: 降落的優(yōu)先級是優(yōu)于起飛的。如果降落等待隊列中有飛 機等待,則隊列頭Plane對象復(fù)制到moving,返回land。 否則,才考慮起飛等待隊列,若不空,將其隊列頭Plane 對象復(fù)制到moving,返回takeoff。否則,返回idle。 更新統(tǒng)計數(shù)據(jù)Uses: Extended_queue類*/ Runway_activity in_progress; if (!landing.empty() landing.retrieve(moving); land_w

18、ait += time - moving.started(); num_landings+; in_progress = land; landing.serve(); else if (!takeoff.empty() takeoff.retrieve(moving); takeoff_wait += time - moving.started(); num_takeoffs+; in_progress =take_off; takeoff.serve(); else idle_time+; in_progress = idle; return in_progress;Plane:Plane(

19、int flt, int time, Plane_status status)/*Post: 根據(jù)參數(shù)設(shè)置對應(yīng)的數(shù)據(jù)成員*/ flt_num = flt; clock_start = time; state = status; cout << "Plane number " << flt << " ready to " if (status = arriving) cout << "land." << endl; else cout << "take o

20、ff." << endl;Plane:Plane()/*Post: 數(shù)據(jù)成員設(shè)置成非法值*/ flt_num = -1; clock_start = -1; state = null;void Plane:refuse() const/*Post: 隊列滿時,拒絕飛機的請求*/ cout << "Plane number " << flt_num; if (state = arriving) cout << " directed to another airport" << endl;

21、 else cout << " told to try to takeoff again later" << endl;void Plane:land(int time) const/*Post: 處理飛機在某一時刻降落*/ int wait = time - clock_start; cout << time << ": Plane number " << flt_num << " landed after " << wait << &q

22、uot; time unit" << (wait = 1) ? "" : "s") << " in the takeoff queue." << endl;void Plane:fly(int time) const/*Post: 處理飛機在某一時刻起飛*/ int wait = time - clock_start; cout << time << ": Plane number " << flt_num << &quo

23、t; took off after " << wait << " time unit" << (wait = 1) ? "" : "s") << " in the takeoff queue." << endl;int Plane:started()constreturn clock_start;void Runway:shut_down(int time) const/*Post: 計算并打印統(tǒng)計信息*/ cout << "

24、;Simulation has concluded after " << time << " time units." << endl << "Total number of planes processed " << (num_land_requests + num_takeoff_requests) << endl << "Total number of planes asking to land " << num_land_

25、requests << endl << "Total number of planes asking to take off " << num_takeoff_requests << endl << "Total number of planes accepted for landing " << num_land_accepted << endl << "Total number of planes accepted for takeoff &qu

26、ot; << num_takeoff_accepted << endl << "Total number of planes refused for landing " << num_land_refused << endl << "Total number of planes refused for takeoff " << num_takeoff_refused << endl << "Total number of planes t

27、hat landed " << num_landings << endl << "Total number of planes that took off " << num_takeoffs << endl << "Total number of planes left in landing queue " << landing.size() << endl << "Total number of planes left in

28、takeoff queue " << takeoff.size() << endl; cout << "Percentage of time runway idle " << 100.0 * ( float ) idle_time) / ( float ) time) << "%" << endl; cout << "Average wait in landing queue " << ( float ) land_wait) /

29、 ( float ) num_landings) << " time units" cout << endl << "Average wait in takeoff queue " << ( float ) takeoff_wait) / ( float ) num_takeoffs) << " time units" << endl; cout << "Average observed rate of planes wanting to l

30、and " << ( float ) num_land_requests) / ( float ) time) << " per time unit" << endl; cout << "Average observed rate of planes wanting to take off " << ( float ) num_takeoff_requests) / ( float ) time) << " per time unit" <<

31、 endl;/鏈式隊列的實現(xiàn)文件Queue:Queue()count=0;front=0;rear=999;bool Queue:empty()constreturn count=0;Error_code Queue:append(const Queue_entry &item)if(count>1000)return overflow;count+;rear=(rear+1)=1000)?0:(rear+1);entryrear=item;return success;Error_code Queue:serve()if(count<=0) return underflo

32、w;count-;front=(front+1)=1000)?0:(front+1);return success;Error_code Queue:retrieve(Queue_entry &item)constif(count<=0) return underflow;item=entryfront;return success;int Extended_Queue:size()constreturn count;double Random:random_real()double max=INT_MAX+1.0;double temp=reseed();if(temp<

33、0)temp=temp+max;return temp/max;Random:Random(bool pseudo)if (pseudo)seed=1; elseseed=time(NULL)%INT_MAX;multiplier=2743;add_on=5923;int Random:reseed()seed=seed*multiplier +add_on;return seed;int Random:poisson(double mean)double limit=exp(-mean);double product=random_real();int count=0;while (prod

34、uct>limit)count+;product*=random_real();return count; 附錄B說明:以下是P1的程序清單(用文件輸入輸出)用于文件輸入輸出的程序#include<iostream>#include<fstream>#include"r.h"#include"p.h"#include "q.h"using namespace std;int main()ifstream infile;ofstream outfile;int end_time; / time to run

35、 simulationint queue_limit; / size of Runway queuesint flight_number = 0;double arrival_rate, departure_rate;infile.open("test.txt");outfile.open("result.txt");while (infile)infile>>queue_limit>>end_time>>arrival_rate>>departure_rate;Random variable;Runway

36、 small_airport(queue_limit);for (int current_time = 0; current_time < end_time; current_time+) int number_arrivals = variable.poisson(arrival_rate);/ 請求降落飛機數(shù)for (int i = 0; i < number_arrivals; i+) Plane current_plane(flight_number+, current_time, arriving); /if (small_airport.can_land(current

37、_plane) != success)current_plane.refuse(); /請求被拒絕int number_departures= variable.poisson(departure_rate); / 請求起飛飛機數(shù)for (int j = 0; j < number_departures; j+) Plane current_plane(flight_number+, current_time, departing); if (small_airport.can_depart(current_plane) != success)current_plane.refuse()

38、;Plane moving_plane;switch (small_airport.activity(current_time, moving_plane) case land:moving_plane.land(current_time);break;case take_off:moving_plane.fly(current_time);break;case idle:break;small_airport.shut_down(end_time,arrival_rate,departure_rate); infile.close();outfile.close();return 0;同時修

39、改了shut down函數(shù)void Runway:shut_down(int time,double arrival_rate,double departure_rate) const/*Post: 計算并打印統(tǒng)計信息*/ifstream infile;ofstream outfile; outfile.open("result.txt",ios:app);outfile <<setw(5) <<time<<setw(4)<<num_land_requests + num_takeoff_requests<<set

40、w(4)<<num_land_requests<<setw(4)<<num_takeoff_requests<<setw(4) <<num_land_accepted<<setw(4) <<num_takeoff_accepted<<setw(2) <<num_land_refused<<setw(3) <<num_takeoff_refused<<setw(4)<<num_landings<<setw(4) <<n

41、um_takeoffs<<setw(3) <<landing.size()<<setw(3)<<takeoff.size()<<setw(8) <<100.0 * ( float ) idle_time) / ( float ) time) << "%" <<setw(9) <<( float ) land_wait) / ( float ) num_landings) <<setw(8) <<( float ) num_takeoff_req

42、uests) / ( float ) time)<<setw(8) <<arrival_rate<<setw(8) <<departure_rate<<endl; 附錄 CP1:(輸入輸出用cin、cout)#include<iostream>#include"r.h"#include"p.h"#include "q.h"using namespace std;void initialize(int &end_time,int &queue_lim

43、it,double &arrival_rate,double &departure_rate); int main()int end_time; / time to run simulationint queue_limit; / size of Runway queuesint flight_number = 0;double arrival_rate, departure_rate;initialize(end_time, queue_limit, arrival_rate, departure_rate);Random variable;Runway small_airp

44、ort(queue_limit);for (int current_time = 0; current_time < end_time; current_time+) / 主循環(huán)int number_arrivals = variable.poisson(arrival_rate);/ 請求降落飛機數(shù)for (int i = 0; i < number_arrivals; i+) Plane current_plane(flight_number+, current_time, arriving); / 飛機加入降落等待隊列if (small_airport.can_land(cu

45、rrent_plane) != success)current_plane.refuse(); /請求被拒絕int number_departures= variable.poisson(departure_rate); / 請求起飛飛機數(shù)for (int j = 0; j < number_departures; j+) Plane current_plane(flight_number+, current_time, departing); / 飛機加入起飛等待隊列if (small_airport.can_depart(current_plane) != success)curre

46、nt_plane.refuse();Plane moving_plane;switch (small_airport.activity(current_time, moving_plane) / 某一時刻,只允許一架飛機使用跑道case land:moving_plane.land(current_time);break;case take_off:moving_plane.fly(current_time);break;case idle:cout<<current_time<<": The runway is idle.n"small_airpo

47、rt.shut_down(end_time);return 0; void initialize(int &end_time, int &queue_limit, double &arrival_rate, double &departure_rate)/*Pre: 用戶指定仿真的總時間,最大隊列長度,以及飛機起飛、降落頻率Post: 函數(shù)打印幫助信息,并根據(jù)用戶輸入初始化各個參數(shù)Uses: 函數(shù)user_says_yes*/cout << "This program simulates an airport with only one r

48、unway." << endl << "One plane can land or depart in each unit of time." << endl;cout << "Up to what number of planes can be waiting to land " << "or take off at any time? " << flush;cin >> queue_limit;cout << "Ho

49、w many units of time will the simulation run?" << flush;cin >> end_time;bool acceptable;do cout << "Expected number of arrivals per unit time?" << flush;cin >> arrival_rate;cout << "Expected number of departures per unit time?" << fl

50、ush;cin >> departure_rate;if (arrival_rate < 0.0 | departure_rate < 0.0)cerr << "These rates must be nonnegative." << endl;elseacceptable = true;if (acceptable && arrival_rate + departure_rate > 1.0)cerr << "Safety Warning: This airport will be

51、come saturated. " << endl; while (!acceptable);P2:#include<iostream>#include"r.h"#include"p.h"#include "q.h"using namespace std;void initialize(int &end_time,int &queue_limit,double &arrival_rate,double &departure_rate);void run_idle(cha

52、r *runway_id, int time);int main() / Airport simulation program/*Pre: The user must supply the number of time intervals the simulation is to run, the expected number of planes arriving, the expected numberof planes departing per time interval, and themaximum allowed size for runway queues. Post: The

53、 program performs a random simulation of the airport, showingthe status of the runway at each time interval, and prints out a summary of airport operation at the conclusion. Uses: Classes Runway, Plane, Random and functions run_idle, initialize.*/int end_time; / time to run simulation int queue_limi

54、t; / size of Runway queues int flight_number = 0; double arrival_rate, departure_rate; initialize(end_time, queue_limit, arrival_rate, departure_rate); Random variable; Runway arrivals(queue_limit); / set up the two runways. Runway departures(queue_limit); for (int current_time = 0; current_time <

55、; end_time; current_time+)int number_arrivals = variable.poisson(arrival_rate); for(int i=0;i< number_arrivals; i+)Plane current_plane(flight_number+, current_time, arriving); if (arrivals.can_land(current_plane) != success)current_plane.refuse();int number_departures= variable.poisson(departure_

56、rate);for(int j=0;j< number_departures; j+) Plane current_plane(flight_number+, current_time, departing);if (departures.can_depart(current_plane) != success)current_plane.refuse();Plane arriving_plane;switch (arrivals.activity(current_time, arriving_plane) case land:arriving_plane.land(current_ti

57、me);break;case take_off:cout << "WARNING: Unexpected, catastrophic program failur"<< endl;break;case idle:run_idle("Arrival", current_time);Plane departing_plane;switch (departures.activity(current_time, departing_plane)case take_off:departing_plane.fly(current_time);break;case land:cout <<

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論