/**************************/ /* infection.cxxプログラム */ /**************************/ /* "感染"のエージェントシミュレーション */ // 2次元平面で動作するエージェント群 // 2種類のエージェントが相互作用します // gnuplotで1秒ごとに出力します #include #include #include //乱数 #include #include //gnuplot using namespace std; #define BUFSISE 256 //入力バッファ用配列の長さ #define N 100 //エージェントの個数:a[]がN個 #define N_OF_A 2 //エージェントの属性値の個数 #define TIMELIMIT 100 //打ち切り時間 #define SEED RAND_MAX-1 //乱数の種 #define R 0.1 //近距離を規定する数値 //カテゴリ1のエージェントの速度 #define DX 0.1 #define DY 0.1 //以下はgnuplot利用のための定義 #define ATMPFILE "Non-infected.tmp" //一時ファイルのファイル名 #define BTMPFILE "infected.tmp" //一時ファイルのファイル名 #define GNUPLOT "/usr/local/Cellar/gnuplot/5.2.8/bin/gnuplot" //gnuplotのパスを指定 #define RANGE 10 //gnuplotの出力領域 //座標値(2次元ベクトル)の表現 struct coordinate { double x; //x座標 double y; //y座標 }; //エージェントの表現 struct agent{ int category ; //エージェントの種類 struct coordinate coord ; //座標 double dattribute[N_OF_A]; //属性値(実数) int iattribute[N_OF_A] ; //属性値(整数) }; //関数のプロダクト宣言 void calcnext(struct agent a[]); //次時刻の状態を計算 void fputstate(struct agent a[], int t); //状態を出力 double frand(void); //実数乱数 void cat0(struct agent *cat0agent, struct agent a[]);//カテゴリの計算メソッド void cat1(struct agent *cat1agent) ; //カテゴリ1の計算メソッド //大域変数 double factor=1.0 ; //カテゴリ1のエージェントの歩幅 /********************************************/ /* main()関数 ******************************/ /*******************************************/ int main(int argc, char *argv[]) { struct agent a[N]={0}; //エージェント int t ; FILE *gnuplot ; //gnuplotと通信用パイプ //factorの初期化 if(argc>=2){//factorの指定がある。なければ1のまま factor=atof(argv[1]); } cout << "factor:" << factor <<"\n"; //パイプを開く if((gnuplot=popen(GNUPLOT " -persist", "w"))==NULL){ //パイプがひらけない cout <<"パイプがひらけません\n"; exit(1) ; } //gnuplotの初期設定 srand(SEED) ;//乱数の初期化 //エージェントの設定 //初めの感染者a[0]は(x,y)にいる a[0].category=1 ; a[0].coord.x=2 ; a[0].coord.y=-2 ; //エージェントシミュレーション for(t=1; t<=TIMELIMIT; t++){//打ち切り時間まで計算 cout << "t=" << t << "\n"; calcnext(a) ;//次時刻の状態を計算 fputstate(a,t) ;//状態ファイル出力(t秒ごとに出力される) //gnuplotで描画 fprintf(gnuplot, "plot \"" ATMPFILE "\" ,\"" BTMPFILE "\"with points pt 4\n" ) ; fflush(gnuplot) ; sleep(1) ; //表示の待ち合わせ } return 0; } /*******************************/ /* calcnext()関数 */ /* 次時刻の状態を計算 */ /********************************/ void calcnext(struct agent a[]) { int i; for(i=0; i