#include #include #include #include #include "team.h" #include "common.h" #include "maze.h" #include "ghost.h" #include "pacman.h" extern const int NUM_PACMEN = 10; extern int TURN_NUMBER = 0; const int NUM_TEAMS = 25; const int MAX_GENERATIONS = 250; const int ROUNDS_PER_GENERATION = 40; const int COPY_CHANCE = 20; int NPES; // the number of cooperating PEs int PE; // this processes PE number int MAX_GAME_LENGTH = 150; int DEADCOUNT; int TOTAL_FITNESS; bool DRAW_SCREEN = false; Maze MAZE; Pacman* PACMAN; Team* TEAM; extern void srand48(long); void initProgram (); void giveProgramInformation (); void doGenerations (int numGenerations); void initGeneration (); void runGeneration (); void doRound (); void initGame (int teamNum); void playGame(int teamNum); void movePacmen(); void moveGhosts(int teamNum); void checkPacmen(); void createNewGeneration (); int fitnessMap (int fitness); Team& chooseTeam(); int doFitnessCalculations (); void displayBestTeam (bool verbose = false); void generateDistributionData (); void drawScreen (); void endProgram (); void main (int argc, char** argv) //Initializes the parallel environment, then executes the main body of the program { int ierr; ierr = MPI_Init(&argc, &argv); if (ierr) { cerr << "MPI_Init failed, ierr = " << ierr << endl; exit(1); } ierr = MPI_Comm_size (MPI_COMM_WORLD, &NPES); if (ierr) { cerr << "MPI_Comm_size failed, ierr = " << ierr << endl; MPI_Finalize(); exit(1); } ierr = MPI_Comm_rank (MPI_COMM_WORLD, &PE); if (ierr) { cerr << "MPI_Comm_rank failed, ierr = " << ierr << endl; MPI_Finalize(); exit(1); } initProgram(); doGenerations(MAX_GENERATIONS); endProgram(); } void initProgram() //Spews out the program information and initializes some variables { if (PE == 0) giveProgramInformation(); // These two lines will give each PE a different random number generator seed time_t t; srand48(PE * PE + time(&t)); PACMAN = new Pacman[NUM_PACMEN]; TEAM = new Team[NUM_TEAMS]; Pacman::maze = &MAZE; Ghost::maze = &MAZE; } void giveProgramInformation () //Displays the set conditions of the game { cout << "Pacman" << endl; cout << "Ghost evolving project" << endl; cout << "Compiled " << __DATE__ << " " << __TIME__ << endl; cout << endl; cout << "NUM_PACMEN = " << NUM_PACMEN << endl; cout << "GHOSTS_PER_TEAM = " << Team::GHOSTS_PER_TEAM << endl; cout << "NUM_TEAMS = " << NUM_TEAMS << endl; cout << "MAX_GENERATIONS = " << MAX_GENERATIONS << endl; cout << "ROUNDS_PER_GENERATION = " << ROUNDS_PER_GENERATION << endl; cout << "MAX_GAME_LENGTH = " << MAX_GAME_LENGTH << endl; cout << endl; cout << "XSIZE = " << Maze::XSIZE << endl; cout << "YSIZE = " << Maze::YSIZE << endl; cout << endl; #ifdef ALLOW_SPECIALIZATION cout << "Specialization on" << endl; #else cout << "Specialization off" << endl; #endif #ifdef PHEROMONES cout << "Pheromones on" << endl; #else cout << "Pheromones off" << endl; #endif cout << endl; } void doGenerations (int numGenerations) // Runs runGeneration a certain amount of times // Also displays some data between generations { initGeneration(); for(; numGenerations > 0; numGenerations--) { runGeneration(); if (PE == 0) cout << numGenerations << " left to go." << endl; displayBestTeam(); /* if (numGenerations % 50 == 1 || numGenerations == MAX_GENERATIONS) generateDistributionData (); */ } } void initGeneration () //Initializes a new generation { int i; for(i = 0; i < NUM_TEAMS; i++) { TEAM[i].newGeneration(); } for(i = 0; i < NUM_PACMEN; i++) { PACMAN[i].newGeneration(); } } void runGeneration () //Initializes a new generation and runs a defined number of rounds with it { MPI_Barrier(MPI_COMM_WORLD); initGeneration (); int i; for(i = 0; i < ROUNDS_PER_GENERATION; i++) { doRound(); } createNewGeneration (); } void doRound () //Starts and runs a round for each team { MAZE.newRound(); int i; for(i=0; i