// Рабочая процедура подготовки к запуску эволюции void PrepareEvolutionProcess(TRunSettings* RunSettings, TModeSettings* ModeSettings, TTimeSettings* TimeSettings, TMutationSettings* MutationSettings, TDirectorySettings* DirectorySettings, TPrimarySystemogenesisSettings* PrimarySystemogenesisSettings, TLearningSettings* LearningSettings, double EnVariableProbability) { TFilenameSettings FilenameSettings; // Получение имени файла среды GetEnvironmentFilename(FilenameSettings.EnvironmentFilename, DirectorySettings->EnvironmentDirectory, RunSettings->EnvironmentNumber); // Получение имени файла результатов bool EnvironmentStability = (EnVariableProbability == 0); GetResultsFilename(FilenameSettings.ResultFilename, RunSettings, ModeSettings, DirectorySettings->ResultDirectory, EnvironmentStability); // Получение имени файлы гистограмм GetHistogramFilename(FilenameSettings.HistogramFilename, RunSettings, ModeSettings, DirectorySettings->ResultDirectory, EnvironmentStability); GetBestPopulationFilename(FilenameSettings.BestPopulationFilename, RunSettings, ModeSettings, DirectorySettings->ResultDirectory, EnvironmentStability); GetBestAgentFilename(FilenameSettings.BestAgentDumpFilename, RunSettings, ModeSettings, DirectorySettings->ResultDirectory, EnvironmentStability); srand(RunSettings->RandomizeRank); // Инициализация генератора случайных числе // Три тестовых запуска генератора (чтобы развести в стороны значения) int tmp_rand = rand()%1000; // Тестовый запуск генератора случайных чисел tmp_rand = rand()%1000; // Тестовый запуск генератора случайных чисел tmp_rand = rand()%1000; // Тестовый запуск генератора случайных чисел Evolution(ModeSettings, TimeSettings, MutationSettings, &FilenameSettings, RunSettings, PrimarySystemogenesisSettings, LearningSettings, EnVariableProbability); }
////////////////////////////////////////////////////////////////////// // Parse command line, print settings, and execute one of two modes. ////////////////////////////////////////////////////////////////////// void main( int argc, char** argv ) { ParseCmdLine (argc,argv); PrintSettings(cout); if (MODE=='e') Evolution(); else Match(); }
/*__________________________________________________________________________ MAIN CYCLE (FLTK CYCLE) ____*/ void mainidle_cb(void*){ //this routine updates the program. //thus, it computes the EVOLUTION double shooted; double dens, err; double totdens, toterr, totimerr; char s[100]; // ---- running controls AND PRINTING if( (amstepping==0 && runningcontrol==1 && graphisloaded==1 && ticks<=maxtime ) || (amstepping==1 && runningcontrol==1 && graphisloaded==1 && ticks<=maxtime && tickstep<=step-1) ) { Evolution(deltat); //PRINTS if((int)printdatabutton->value()==1){ //if have steady state if(usesteady==1){ igraph_matrix_t activation; igraph_matrix_init(&activation,nodesnumber,totrun); igraph_matrix_null(&activation); igraph_vector_t correlation; igraph_vector_init(&correlation,(nodesnumber*nodesnumber)); igraph_vector_null(&correlation); fprintf(output1,"%i ", ticks); fprintf(output2,"%i ", ticks); fprintf(output5,"%i ", ticks); fprintf(output6,"%i ", ticks); totdens=0; toterr=0; for(int i=0;i<nodesnumber;++i){ shooted=0; dens=0; err=0; for(int j=0; j<totrun; ++j){ dens=dens+MATRIX(density,i,j); err=err+((VECTOR(statstate)[i]-MATRIX(density,i,j))*(VECTOR(statstate)[i]-MATRIX(density,i,j))); shooted=shooted+MATRIX(loss,i,j); if(MATRIX(loss,i,j)!=0){++MATRIX(activation,i,j);} } dens=dens/totrun; err=sqrt(err)/totrun; shooted=shooted/totrun; totdens=totdens+dens; toterr=toterr+err; fprintf(output1,"%f ",dens); fprintf(output2,"%f ",err); fprintf(output5,"%f ",shooted); } totdens=totdens/nodesnumber; toterr=toterr/nodesnumber; fprintf(output1,"%f ",totdens); fprintf(output2,"%f ",toterr); // printf("\n\n ACTIVATION MATRIX \n \n"); print_matrix_ur(&activation,stdout); printf("\n\n"); // ---- CORRELATION --- igraph_vector_t meanactivation; igraph_vector_init(&meanactivation,nodesnumber); igraph_vector_null(&meanactivation); //calculate mean activation for (int j=0; j<totrun; ++j) { for (int i=0; i<nodesnumber; ++i) { VECTOR(meanactivation)[i]=VECTOR(meanactivation)[i]+MATRIX(activation,i,j); } } igraph_vector_scale(&meanactivation,1./totrun); //calculate actual correlation for (int x=0; x<nodesnumber ; ++x) { for(int y=0; y<nodesnumber; ++y){ double prod=0; for (int j=0; j<totrun; ++j) { prod=prod+ ( MATRIX(activation,x,j)*MATRIX(activation,y,j) ); } prod=prod/totrun; VECTOR(correlation)[(x*nodesnumber+y)] = prod - (VECTOR(meanactivation)[x]*VECTOR(meanactivation)[y]); } } igraph_vector_destroy(&meanactivation); for (int i=0; i<(nodesnumber*nodesnumber); ++i) { fprintf(output6,"%f ",VECTOR(correlation)[i]); } //calculate error on run igraph_matrix_t distl1; igraph_matrix_t distimel1; igraph_matrix_init(&distl1,nodesnumber,totrun); igraph_matrix_init(&distimel1,nodesnumber,totrun); igraph_matrix_null(&distl1); igraph_matrix_null(&distimel1); igraph_vector_t rundistl1; igraph_vector_t rundistimel1; igraph_vector_init(&rundistl1,totrun); igraph_vector_init(&rundistimel1,totrun); igraph_vector_null(&rundistl1); igraph_vector_null(&rundistimel1); toterr=0; totimerr=0; //for every run for(int j=0;j<totrun;++j){ //i evaluate the distance between the state and the stationary state (toterr) and the distance between old and new density (totimerr) for(int i=0; i<nodesnumber; ++i) { //L1 DISTANCE WRT STATSTATE & DENSITY MATRIX(distl1,i,j)=fabs(VECTOR(statstate)[i]-MATRIX(density,i,j)); //L1 DISTANCE WRT OLD DENSITY & DENSITY MATRIX(distimel1,i,j)=fabs(MATRIX(densityold,i,j)-MATRIX(density,i,j)); } } igraph_matrix_rowsum(&distl1,&rundistl1); igraph_matrix_rowsum(&distimel1,&rundistimel1); igraph_vector_scale(&rundistl1,(1./nodesnumber)); igraph_vector_scale(&rundistimel1,(1./nodesnumber)); toterr= (double)( igraph_vector_sum(&rundistl1) ) / (double)totrun ; totimerr= (double)( igraph_vector_sum(&rundistimel1)) / (double)totrun; igraph_vector_destroy(&rundistl1); igraph_vector_destroy(&rundistimel1); igraph_matrix_destroy(&distl1); igraph_matrix_destroy(&distimel1); fprintf(output2,"%f %f",toterr, totimerr); fprintf(output1,"\n"); fprintf(output2,"\n"); fprintf(output5,"\n"); fprintf(output6,"\n"); //if i have BRIDGES ("clustered" graph), I print the traffic on the BRIDGES, using "output3" file if(isclustered==1){ //for each bridge fprintf(output3,"%i ",ticks); for(int nbri=0; nbri<igraph_matrix_ncol(&bridgeslinks); ++nbri){ for(int i=0; i<igraph_matrix_nrow(&bridgeslinks);++i){ double tfl=0; for(int j=0; j<totrun; ++j){ int beid; beid=(int)MATRIX(bridgeslinks,i,nbri); tfl=tfl+MATRIX(flux,beid,j); } fprintf(output3,"%f ",tfl); } } fprintf(output3,"\n"); } igraph_matrix_destroy(&activation); igraph_vector_destroy(&correlation); } //if i HAVENT STEADY STATE else { fprintf(output1,"%i ", ticks); fprintf(output5,"%i ", ticks); for(int i=0;i<nodesnumber;++i){ shooted=0; dens=0; for(int j=0; j<totrun; ++j){ dens=dens+MATRIX(density,i,j); shooted=shooted+MATRIX(loss,i,j); } dens=dens/totrun; shooted=shooted/totrun; fprintf(output1,"%f " ,dens); fprintf(output5,"%f " ,shooted); } fprintf(output1,"\n"); fprintf(output5,"\n"); } } } if((ticks==maxtime || tickstep==step) && runningcontrol==1){ run(); } // ---- no graph loaded if(graphisloaded==0 && rewrite==1) { runbutton->deactivate(); sprintf(s,"No\nnetwork\nloaded"); databuff->text(s); } // ---- graph loaded else { runbutton->activate(); if(islattice==1 && rewrite==1){ if(istoro==1){ sprintf(s,"Nodes=%i\nToroidal\nLattice\n%iD Side=%i",nodesnumber, latticedim, latticeside); } else{ sprintf(s,"Nodes=%i\nLattice\n%iD Side=%i",nodesnumber, latticedim, latticeside); } databuff->text(s); } else if(rewrite==1){ sprintf(s,"Nodes=%i",nodesnumber); databuff->text(s); } } //have path if(havepath==1 && rewrite==1){pathbuff->text(path); } else if(havepath==0 && rewrite==1){pathbuff->text("No Path");} if(error==1 && rewrite==1){ sprintf(s,errorstring); databuff->text(s); } if (ticks<=maxtime){ scene->redraw(); datascene->redraw(); } rewrite=0; //Fl::repeat_timeout(1.0, mainidle_cb); }
void placing_boats(Input *in, Context *C, navire *navires) { int i; SDL_Rect R; /*Ici on crée un tableau de coordonnées sur la grille à ne pas dépasser*/ int coord_limit[4] = {0}; /*Les longeurs des navires sont de :*/ int len_sprites[NB_BOATS] = {67, 101, 101, 135, 169}; int nextTime, actualTime; int nav_places = 0; /*Boucle qui va permettre de placer un navire un par un...*/ for (i = 0; i < NB_BOATS; i++) { /*Une position initiale de notre navire a placer...*/ C->cNavires[i].x = OFFSET_L; C->cNavires[i].y = OFFSET_T; /*Limites sur la grille à ne pas dépasser (dépend de la longueur du navire et de sa rotation)*/ coord_limit[0] = OFFSET_L; coord_limit[1] = OFFSET_R-len_sprites[i]; coord_limit[2] = OFFSET_T; coord_limit[3] = OFFSET_B; nextTime = SDL_GetTicks() + 100; R.x = C->cNavires[i].x; R.y = C->cNavires[i].y; SDL_BlitSurface(C->images_h[i], NULL, C->screen, &R); /*Boucle*/ while (!in->quit) { SDL_Delay(20); actualTime = SDL_GetTicks(); if(actualTime - nextTime >= 0) { UpdateEvents(in); if(in->key[SDLK_q] || in->key[SDLK_r]) return; if(Evolution(in, &C->cNavires[i].x, &C->cNavires[i].y, coord_limit, &navires[i], len_sprites[i])) { if(!in->quit) { if(!navires[i].r) { navires[i].x[0] = (C->cNavires[i].x-OFFSET_L)/CELL_W+1; navires[i].x[1] = (C->cNavires[i].x-OFFSET_L)/CELL_W+((C->images_h[i]->w)/CELL_W); navires[i].y[0] = ((C->cNavires[i].y-450)/CELL_W)+1; navires[i].y[1] = ((C->cNavires[i].y-450)/CELL_W)+1; } else { navires[i].x[0] = (C->cNavires[i].x-OFFSET_L)/CELL_W+1; navires[i].x[1] = (C->cNavires[i].x-OFFSET_L)/CELL_W+1; navires[i].y[0] = ((C->cNavires[i].y-450)/CELL_W)+1; navires[i].y[1] = ((C->cNavires[i].y-450)/CELL_W)+((C->images_v[i]->h)/CELL_W); } } /*On regarde si il n'y a pas de chevauchement*/ if(overlap_detection(nav_places, navires)) { nav_places++; break; } } UpdateScreen(C, navires); while(in->key[SDLK_SPACE]) UpdateEvents(in); if(!in->key[SDLK_LEFT] && !in->key[SDLK_RIGHT] && !in->key[SDLK_DOWN] && !in->key[SDLK_UP] && !in->key[SDLK_RETURN]) nextTime = SDL_GetTicks(); else nextTime = SDL_GetTicks() + 160; } } } }
int main(int argc, char** argv) { Input in; Context C; navire nav_j[NB_BOATS]; navire nav_c[NB_BOATS]; int limit[4] = {43, 340, 43, 348}; int i, j; int** grille_c= NULL; int** grille_j = NULL; int done = 0; int gagne = 0; grille_c= malloc(T_GRILLE*sizeof(int*)); grille_j = malloc(T_GRILLE*sizeof(int*)); if(grille_c== NULL || grille_j == NULL) exit(EXIT_FAILURE); for(i = 0; i < T_GRILLE; i++) { grille_c[i] = calloc(T_GRILLE, sizeof(int)); grille_j[i] = calloc(T_GRILLE, sizeof(int)); if(grille_c[i] == NULL || grille_j == NULL) exit(EXIT_FAILURE); } (void) argc; (void) argv; /*Initialisation SDL, SDL_ttf*/ if(initSDL(&C) == -1) return EXIT_FAILURE; srand(time(NULL)); do { /*Initialisation contexte*/ if(initC(&C) == -1) return EXIT_FAILURE; for(i = 0; i < T_GRILLE; i++) { for(j = 0; j < T_GRILLE; j++) { grille_c[j][i] = 0; grille_j[j][i] = 0; } } for(i = 0; i < NB_BOATS; i++) { nav_j[i].c = 0; nav_c[i].c = 0; } done = 0; gagne = 0; memset(&in, 0, sizeof(in)); memset(nav_j, 0, sizeof(nav_j)); memset(nav_c, 0, sizeof(nav_c)); /*Placement des nav_j...*/ chtxt(&C, PLACE_NAV_J); placing_boats(&in, &C, nav_j); while(in.key[SDLK_RETURN]) { UpdateEvents(&in); SDL_Delay(20); } computer_placing_boats(nav_c); chtxt(&C, PLACE_NAV_C); /*On place maintenant le viseur*/ C.cViseur.x = 43; C.cViseur.y = 43; /*Boucle de jeu*/ while (!gagne && !in.key[SDLK_q] && !in.key[SDLK_r]) { done = 0; while(!done) { UpdateEvents(&in); if(in.quit || in.key[SDLK_q] || in.key[SDLK_r]) break; if(Evolution(&in, &C.cViseur.x, &C.cViseur.y, limit, NULL, 0)) { if(case_test(C.cViseur.x, C.cViseur.y, grille_c)) { grille_c[((C.cViseur.y-43)/CELL_W)][((C.cViseur.x-43)/CELL_W)] = 1; if(!joueurAttaque(&C, nav_c)) { done = 1; chtxt(&C, "..."); } else chtxt(&C, TOUCHE_NAV_J); coule(&C, nav_j, nav_c); if(victoire(nav_j, nav_c) == 2) { gagne = 1; chtxt(&C, VICTOIRE_J); UpdateScreen(&C, nav_j); while(1) { UpdateEvents(&in); if(in.key[SDLK_r] || in.key[SDLK_q]) break; SDL_Delay(30); } } } } UpdateScreen(&C, nav_j); if(!in.key[SDLK_LEFT] && !in.key[SDLK_RIGHT] && !in.key[SDLK_DOWN] && !in.key[SDLK_UP] && !in.key[SDLK_RETURN]) SDL_Delay(20); else SDL_Delay(200); } UpdateEvents(&in); if(in.quit || in.key[SDLK_q] || in.key[SDLK_r]) break; UpdateScreen(&C, nav_j); SDL_Delay(200); while(ordiAttaque(&C, nav_j, grille_j)) { chtxt(&C, TOUCHE_NAV_C); coule(&C, nav_j, nav_c); UpdateScreen(&C, nav_j); SDL_Delay(400); } if(victoire(nav_j, nav_c) == 1) { gagne = 1; chtxt(&C, VICTOIRE_C); UpdateScreen(&C, nav_j); while(1) { UpdateEvents(&in); if(in.key[SDLK_r] || in.key[SDLK_q]) break; SDL_Delay(30); } } } if(in.quit || in.key[SDLK_q]) break; } while(1); /*On libère tout*/ C.nbimg = sizeof(C.images_v)/sizeof(SDL_Surface); for(i = 0; i < C.nbimg; i++) SDL_FreeSurface(C.images_v[i]); C.nbimg = sizeof(C.images_h)/sizeof(SDL_Surface); for(i = 0; i < C.nbimg; i++) SDL_FreeSurface(C.images_h[i]); SDL_FreeSurface(C.viseur); SDL_FreeSurface(C.touche); SDL_FreeSurface(C.c_rate); SDL_FreeSurface(C.coule); SDL_FreeSurface(C.infobar); SDL_FreeSurface(C.texte); C.nbimg = sizeof(C.misc)/sizeof(SDL_Surface); for(i = 0; i < C.nbimg; i++) SDL_FreeSurface(C.misc[i]); free(C.cNavires); for(i = 0; i < T_GRILLE; i++) { free(grille_j[i]); free(grille_c[i]); } free(grille_c); free(grille_j); TTF_CloseFont(C.police); TTF_Quit(); SDL_Quit(); return EXIT_SUCCESS; }