/****************************************************************** * * * Nest version 2 - 3D Wasp Nest Simulator * * Copyright (C) 1997 Sylvain GUERIN * * LIASC, ENST Bretagne & Santa Fe Institute * * * ****************************************************************** E-mail: Sylvain.Guerin@enst-bretagne.fr or: Sylvain Guerin 13 rue des Monts Lorgeaux, 51460 L'EPINE, FRANCE ****************************************************************** This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ****************************************************************** Name : genetic.c Component : genetic algorihtm Fonctionality : contains methods for objects SimulationStep and GeneticSimulation (used in genetic algorithm) ******************************************************************/ #include "algo.h" #include "simu.h" #include "genetic.h" #include "config.h" #include #include #include /* les variables externes globales dont on a besoin ici */ extern FileDatas* geneticFileDatas; extern char nest2Server[256]; /* extern char nest2RSH[256]; extern char nest2NICE[256]; */ /* ---------------------------------------------------- */ SimulationParameters::SimulationParameters () { setToDefault (); } SimulationParameters::SimulationParameters (double initPRedCell, double initPYellowCell, double initAverageRuleNb, double initSdRuleNb, double initPCrossOver, double initPUsed, double initPUnused, double initPSwap, unsigned int initArchSize, unsigned long initIterationsNb, unsigned int initMaxCells, FitnessType initFitnessType, CrossoverType initCrossoverType, int initToBuildRulesNumber, SimulationType initSimulationType, RandomGenerationType initRandomGenerationType, unsigned int initColoniesNb) { pRedCell = initPRedCell; pYellowCell = initPYellowCell; averageRuleNb = initAverageRuleNb; sdRuleNb = initSdRuleNb; pCrossOver = initPCrossOver; pUsed = initPUsed; pUnused = initPUnused; pSwap = initPSwap; archSize = initArchSize; maxCells = initMaxCells; iterationsNb = initIterationsNb; fitnessType = initFitnessType; crossoverType = initCrossoverType; toBuildRulesNumber = initToBuildRulesNumber; simulationType = initSimulationType; randomGenerationType = initRandomGenerationType; coloniesNb = (int)initColoniesNb; } SimulationParameters::~SimulationParameters () { } void SimulationParameters::setToDefault () { pRedCell = defaultPRedCell; pYellowCell = defaultPYellowCell; averageRuleNb = defaultAverageRuleNb; sdRuleNb = defaultSdRuleNb; pCrossOver = defaultPCrossOver; pUsed = defaultPUsed; pUnused = defaultPUnused; pSwap = defaultPSwap; archSize = defaultArchSize; iterationsNb = defaultIterationsNb; maxCells = defaultMaxCells; fitnessType = defaultFitnessType; crossoverType = defaultCrossoverType; toBuildRulesNumber = defaultToBuildRulesNumber; simulationType = defaultSimulationType; randomGenerationType = defaultRandomGenerationType; coloniesNb = defaultColoniesNb; } void SimulationParameters::setFromOtherSimulationParameters (SimulationParameters* simuP) { pRedCell = simuP->pRedCell; pYellowCell = simuP->pYellowCell; averageRuleNb = simuP->averageRuleNb; sdRuleNb = simuP->sdRuleNb; pCrossOver = simuP->pCrossOver; pUsed = simuP->pUsed; pUnused = simuP->pUnused; pSwap = simuP->pSwap; archSize = simuP->archSize; iterationsNb = simuP->iterationsNb; maxCells = simuP->maxCells; fitnessType = simuP->fitnessType; crossoverType = simuP->crossoverType; toBuildRulesNumber = simuP->toBuildRulesNumber; simulationType = simuP->simulationType; randomGenerationType = simuP->randomGenerationType; coloniesNb = simuP->coloniesNb; } /* ---------------------------------------------------- */ SimulationStep::SimulationStep (unsigned int aColoniesNumber, GeneticSimulation* aGeneticSimulation) { int i; coloniesNumber = aColoniesNumber; colony = new Simulation*[coloniesNumber]; colonyState = new SimuState[coloniesNumber]; for (i=0; i<(int)coloniesNumber; i++) { colony[i] = new Simulation(i, maxRules, aGeneticSimulation->params.simulationType); colony[i]->simulationRuleArray->nbElements = 1; colonyState[i] = Unused; } geneticSimulation = aGeneticSimulation; oldColony = new Simulation*[coloniesNumber]; for (i=0; i<(int)coloniesNumber; i++) { oldColony[i] = new Simulation(i, maxRules, aGeneticSimulation->params.simulationType); oldColony[i]->simulationRuleArray->nbElements = 1; } probaRep = new double[aColoniesNumber]; } SimulationStep::~SimulationStep () { int i; for (i=0; i<(int)coloniesNumber; i++) { delete colony[i]; delete oldColony[i]; } delete[] colony; delete[] oldColony; delete[] colonyState; delete[] probaRep; } int SimulationStep::loadStep (char* aPathName, char* aFileName, unsigned int step) { if (aPathName == aFileName && step == 0) /* avoid warnings about unused par */ return 1; return 1; /* to be implemented ! Lucky guy. */ } int SimulationStep::saveCurrentStep () { return 1; /* to be implemented ! Happy guy. */ } char* SimulationStep::getStepName (char* aName, unsigned int step) { if (step<10) { sprintf (stepName, "%s.step00%d", aName, step); } else if (step<100) { sprintf (stepName, "%s.step0%d", aName, step); } else { sprintf (stepName, "%s.step%d", aName, step); } return stepName; } char* SimulationStep::getColonyName (char* aName, unsigned int step, unsigned int index) { if (step<10) { if (index<10) { sprintf (colonyName, "%s:00%d.00%d", aName, step, index); } else if (index<100) { sprintf (colonyName, "%s:00%d.0%d", aName, step, index); } else { sprintf (colonyName, "%s:00%d.%d", aName, step, index); } } else if (step<100) { if (index<10) { sprintf (colonyName, "%s:0%d.00%d", aName, step, index); } else if (index<100) { sprintf (colonyName, "%s:0%d.0%d", aName, step, index); } else { sprintf (colonyName, "%s:0%d.%d", aName, step, index); } } else { if (index<10) { sprintf (colonyName, "%s:%d.00%d", aName, step, index); } else if (index<100) { sprintf (colonyName, "%s:%d.0%d", aName, step, index); } else { sprintf (colonyName, "%s:%d.%d", aName, step, index); } } return colonyName; } void SimulationStep::runStep (unsigned int step) { char temp[256]; char fname[256]; int i; currentStep = step; sprintf (temp, " Running generation %d", step); addComment ("*****************************************"); addComment (temp); addComment ("*****************************************"); openDirectory (); if (step == 1) { runFirstStep (); } else { /* on reteste le reseau */ addComment ("Checking net..."); geneticSimulation->net->testAll (); geneticSimulation->refreshNetInterface (); loadDataFromOlderStep (); addComment ("Step 1: selection"); selection (); addComment ("Step 2: crossing over"); crossOver (); addComment ("Step 3: mutation"); mutation (); addComment ("Step 4: swap"); swap (); addComment ("Step 5: save"); /* save colonies */ strcpy (fname, geneticSimulation->pathName); strcat (fname, geneticSimulation->dirName); strcat (fname, "/"); strcat (fname, getStepName (geneticSimulation->dirName, currentStep)); strcat (fname, "/"); geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Saving step", "Nest2", " Saving colonies, please wait... "); for (i=0; i<(int)coloniesNumber; i++) { strcpy (colony[i]->pathName, fname); strcpy (colony[i]->fileName, getColonyName (geneticSimulation->dirName, currentStep, i)); strcat (colony[i]->fileName, ".rul"); strcpy (colony[i]->name, getColonyName (geneticSimulation->dirName, currentStep, i)); if (colony[i]->save (rulFile, archMustNotBeSaved, previewMustNotBeSaved, compression) == 0) { geneticSimulation->mainInterface->geneticInterface->displayDialogBox (geneticSimulation->mainInterface, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } colonyState[i] = Waiting; geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } geneticSimulation->geneticInterface ->freeInfoBox (); } } void SimulationStep::runFirstStep () { int i; char fname[256]; strcpy (fname, geneticSimulation->pathName); strcat (fname, geneticSimulation->dirName); strcat (fname, "/"); strcat (fname, getStepName (geneticSimulation->dirName, currentStep)); strcat (fname, "/"); geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Generation step", "Nest2", "Colonies are generated, please wait..."); for (i=0; i<(int)coloniesNumber; i++) { strcpy (colony[i]->pathName, fname); strcpy (colony[i]->fileName, getColonyName (geneticSimulation->dirName, 1, i)); strcat (colony[i]->fileName, ".rul"); strcpy (colony[i]->name, getColonyName (geneticSimulation->dirName, 1, i)); if (colony[i]->save (rulFile, archMustNotBeSaved, previewMustNotBeSaved, compression) == 0) { geneticSimulation->mainInterface->geneticInterface->displayDialogBox (geneticSimulation->mainInterface, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } colonyState[i] = Waiting; geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } geneticSimulation->geneticInterface ->freeInfoBox (); } void SimulationStep::addComment (char* text) { geneticSimulation->addComment (text); } void SimulationStep::openDirectory () { char temp[256]; char fname[256]; int aFile; DIR *dp; struct dirent *ep; strcpy (fname, geneticSimulation->pathName); strcat (fname, geneticSimulation->dirName); strcat (fname, "/"); strcat (fname, getStepName (geneticSimulation->dirName, currentStep)); strcat (fname, "/"); aFile = open (fname, O_RDONLY); if (aFile != -1) { addComment ("Directory already exists"); close (aFile); dp = opendir (fname); if (dp != NULL) { while ((ep = readdir (dp))) { if (strcmp (ep->d_name, ".") != 0 && strcmp (ep->d_name, "..") != 0) { strcpy (temp, fname); strcat (temp, ep->d_name); unlink (temp); } } (void) closedir (dp); } else { addComment ("Could not empty directory"); } } else { /* create directory */ addComment ("Create directory"); mkdir (fname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } } void SimulationStep::loadDataFromOlderStep () { int i; char pathName[256]; char fileName[256]; double totalMark; strcpy (pathName, geneticSimulation->pathName); strcat (pathName, geneticSimulation->dirName); strcat (pathName, "/"); strcat (pathName, getStepName (geneticSimulation->dirName, currentStep-1)); strcat (pathName, "/"); geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Load old colonies", "Nest2", "Old Colonies are loading, please wait..."); for (i=0; i<(int)coloniesNumber; i++) { strcpy (fileName, getColonyName (geneticSimulation->dirName, currentStep-1, i)); strcat (fileName, ".rul"); strcat (fileName, ".zhuf"); if (oldColony[i]->load (pathName, fileName, BFalse) == 0) { printf ("ERROR: unable to open %s%s\n", pathName, fileName); } strcpy (fileName, getColonyName (geneticSimulation->dirName, currentStep-1, i)); strcat (fileName, ".res"); if (oldColony[i]->loadResult (pathName, fileName) == 0) { printf ("ERROR: unable to open %s%s\n", pathName, fileName); } geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } geneticSimulation->geneticInterface ->freeInfoBox (); totalMark = 0.0; for (i=0; i<(int)coloniesNumber; i++) { // printf ("colonie %d mark= %f\n", i, oldColony[i]->mark); probaRep[i] = oldColony[i]->mark; totalMark = totalMark+oldColony[i]->mark; } for (i=0; i<(int)coloniesNumber; i++) { if (totalMark != 0) { probaRep[i] = probaRep[i]/totalMark; } else { probaRep[i]= 1.0; } // printf ("proba (%d): %f\n", i, probaRep[i]); } } void SimulationStep::selection () { int i; int ok; int choice, proba; RandomGenerator randomGenerator (PSEUDO, 0, coloniesNumber-1); RandomGenerator randomGenerator2 (PSEUDO, 0, 9999); char temp[256]; double total; geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Selection step", "Nest2", "Selecting colonies, please wait..."); /* est-ce que par hasard, toutes les fitness ne seraient pas a zero ? */ total = 0.0; for (i=0; i<(int)coloniesNumber; i++) { total += probaRep[i]; //printf ("Colonie %d: proba: %f\n", i, probaRep[i]); } if (total == 0) { /* oh, la la, on a bien fait de tester, hein ? */ for (i=0; i<(int)coloniesNumber; i++) { choice = randomGenerator.getNumber (); sprintf (temp, "Birth of a type %d colony", choice); addComment (temp); ok = 1; colony[i]->setFromOtherSimulation (oldColony[choice]); geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } } else { /* meme pas mal, on continue... */ for (i=0; i<(int)coloniesNumber; i++) { ok = 0; while (!ok) { choice = randomGenerator.getNumber (); proba = randomGenerator2.getNumber (); if ((double)((double)proba/10000.0) < probaRep[choice] ) { addComment (temp); ok = 1; colony[i]->setFromOtherSimulation (oldColony[choice]); } } geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } } geneticSimulation->geneticInterface ->freeInfoBox (); } void SimulationStep::crossOver () { int i; int choice; RandomGenerator randomGenerator (PSEUDO, 0, coloniesNumber-1); RandomGenerator randomGenerator2 (PSEUDO, 0, 9999); char temp[256]; geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Crossing-over step", "Nest2", "Crossing over colonies, please wait..."); for (i=0; i<(int)coloniesNumber; i++) { if (colony[i]->mark > geneticSimulation->bestMark[geneticSimulation->currentStep] *eugenismRate) { // printf ("Eugenism applied for colony %d\n", i); } else { choice = randomGenerator2.getNumber (); if (((double)choice/10000.0 < geneticSimulation->params.pCrossOver) && (choice != i)) { /* apply crosssing over */ choice = randomGenerator.getNumber (); sprintf (temp, "Crossing over for colonies %d and %d", i, choice); addComment (temp); colony[i]->crossOver (colony[choice], geneticSimulation->params.crossoverType); } geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } } geneticSimulation->geneticInterface->freeInfoBox (); } void SimulationStep::mutation () { int i; char temp[256]; int nbMutations; geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Mutation step", "Nest2", "Colonies are mutating, please wait..."); for (i=0; i<(int)coloniesNumber; i++) { if (colony[i]->mark > geneticSimulation->bestMark[geneticSimulation->currentStep] *eugenismRate) { // printf ("Eugenism applied for colony %d\n", i); } else { nbMutations = colony[i]->mutation (geneticSimulation->params.pRedCell, geneticSimulation->params.pYellowCell, geneticSimulation->params.pUsed, geneticSimulation->params.pUnused, geneticSimulation->params.randomGenerationType); sprintf (temp, "%d mutations on colony %d", nbMutations, i); addComment (temp); geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } } geneticSimulation->geneticInterface ->freeInfoBox (); } void SimulationStep::swap () { int i; char temp[256]; int nbSwaps; geneticSimulation->geneticInterface ->displayInfoBox (geneticSimulation->mainInterface, "Swap step", "Nest2", "Colonies are swapping, please wait..."); for (i=0; i<(int)coloniesNumber; i++) { nbSwaps = colony[i]->swap (geneticSimulation->params.pSwap); sprintf (temp, "%d swaps on colony %d", nbSwaps, i); addComment (temp); geneticSimulation->geneticInterface ->anInfoBox->setStatWindow ((double)(i+1)/(double)(coloniesNumber)); geneticSimulation->geneticInterface ->anInfoBox->refreshStatWindow (); } geneticSimulation->geneticInterface ->freeInfoBox (); } /* -------------------------------------------------------------- */ GeneticSimulation::GeneticSimulation (char* aName, char* aDirName, char* aPath, Net* aNet, MainInterface* aMainInterface, int initColoniesNumber, SimulationType aSimulationType) { int i; mainInterface = aMainInterface; net = aNet; strcpy (name, aName); strcpy (pathName, aPath); strcpy (dirName, aDirName); isRunning = 0; currentStep = 0; totalSteps = 0; for (i=0; igeneticInterface->displayDialogBox (mainInterface, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } } openCommentFile (); sprintf (temp, "Running simulation from gen %d to gen %d", beginIndex, endIndex); addComment (temp); isRunning = 1; geneticInterface->resetStatWindow ((endIndex-beginIndex+1)*params.coloniesNb); lastStepToCompute = endIndex; currentSimulationStep->runStep (beginIndex); } void GeneticSimulation::stop () { DeferedSimulation* currentSimu; int i; addComment ("End of current simulation"); isRunning = 0; geneticInterface->resetStatWindow (1); closeCommentFile (); /* on met a state=sleeping le reseau */ for (i=0; i<(int)net->getNumber (); i++) { if (net->host[i]->hostState == running) { net->host[i]->hostState = sleeping; strcpy (net->host[i]->beginSimulationDate, "not running"); net->update (i); } } refreshNetInterface (); if (save () == 0) { mainInterface->geneticInterface->displayDialogBox (mainInterface, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } /* si c'etait une simulation en differe on positionne l'etat a DSDone et on rafraichit */ if (geneticInterface->deferedSimulationRunning) { currentSimu = mainInterface->deferedSimuList->get (geneticInterface->deferedSimulationIndex); geneticInterface->deferedSimulationRunning = 0; currentSimu->deferedSimuState = DSDone; mainInterface->deferedSimuList->update (geneticInterface->deferedSimulationIndex); if (geneticInterface->deferedSimuInterfaceDisplayed) { geneticInterface->deferedSimuInterface->deferedSimuArray->refreshAll (); } } } void GeneticSimulation::createDirectories () { char fname[256]; int aFile; strcpy (fname, pathName); strcat (fname, dirName); aFile = open (fname, O_RDONLY); if (aFile != -1) { /* directory already exists */ close (aFile); } else { /* create directory */ strcat (fname, "/"); mkdir (fname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); } } void GeneticSimulation::openCommentFile () { char fname[256]; char temp[256]; strcpy (fname, pathName); strcat (fname, dirName); strcat (fname, "/"); strcat (fname, dirName); strcat (fname, ".comment"); if ((commentFile = open (fname, O_RDONLY)) != -1) { /* file already exists, it will be updated */ close (commentFile); if ((commentFile = open (fname, O_WRONLY | O_APPEND)) == -1) { printf ("Error: unable to open comment file\n"); } } else { if ((commentFile = creat (fname, S_IRUSR|S_IWUSR)) == -1) { printf ("Error: unable to create comment file\n"); } } sprintf (temp, "Updated on %s %s\n", currentDate.getDate (), currentDate.getTime ()); write (commentFile, (void *)temp, strlen (temp)); } void GeneticSimulation::addComment (char* text) { char temp[256]; strcpy (temp, text); strcat (temp, "\n"); write (commentFile, (void *)temp, strlen (temp)); geneticInterface->displayNewText (text); } void GeneticSimulation::closeCommentFile () { close (commentFile); } void GeneticSimulation::simulationControl () { long i; char host[50]; int hostNb; int simuLeft = 0; char temp[256]; /* Cette methode gere le deroulement de l'algo genetique donc elle regarde les simu finies, en lance d'autres quand des machines sont disponibles, etc... */ for (i=0; icolonyState[i] == Waiting) { if (net->getFreeHost (host, &hostNb) == 1) { launchProcess (i, host, hostNb); simuLeft++; } else { simuLeft++; refreshNetInterface (); } } else if (currentSimulationStep->colonyState[i] == Running) { if (checkIfDone (i)) { } else { simuLeft++; } } } if (simuLeft == 0) { sprintf (temp, "Generation %d complete", currentSimulationStep->currentStep); loadResults (); addComment (temp); currentStep = currentSimulationStep->currentStep; totalSteps = currentStep; geneticInterface->displayGraph (); geneticInterface->displayNewLabels (); if (currentStep < lastStepToCompute) { currentSimulationStep->runStep (currentStep+1); if (save () == 0) { mainInterface->geneticInterface->displayDialogBox (mainInterface, "Error", "Nest2", " Unable to save current simulation. ", "Continue"); } } else { stop (); } } } void GeneticSimulation::refreshNetInterface () { if (mainInterface->netInterfaceDisplayed) { mainInterface->netInterface->netArray->refreshAll (); /* refresh of the Net window if genetic simulation running */ } } void GeneticSimulation::launchProcess (unsigned int colonyIndex, char* host, int aHostNb) { char temp[512]; char fname[512]; char inputFile[512]; char outputFile[512]; char user[80]; double currentAverage; /* verification que la simulation n'a pas ete deja effectuee */ if (checkBeforeLaunching (colonyIndex)) { sprintf (temp, "Colony %d is already simulated", colonyIndex); addComment (temp); return; } /* sinon on lance effectivement la simulation */ currentAverage = averageMark[currentStep]; strcpy (user, geneticFileDatas->returnUserId ()); strcpy (fname, pathName); strcat (fname, dirName); strcat (fname, "/"); strcat (fname, currentSimulationStep->getStepName (dirName, currentSimulationStep->currentStep)); strcat (fname, "/"); strcpy (inputFile, currentSimulationStep->getColonyName (dirName, currentSimulationStep->currentStep, colonyIndex)); strcat (inputFile, ".rul"); strcpy (outputFile, currentSimulationStep->getColonyName (dirName, currentSimulationStep->currentStep, colonyIndex)); strcat (outputFile, ".res"); sprintf (temp, "Launching process for colony %d on %s", colonyIndex, host); addComment (temp); strcpy (currentSimulationStep->colony[colonyIndex]->host, host); currentSimulationStep->colony[colonyIndex]->hostNb = aHostNb; currentSimulationStep->colonyState[colonyIndex] = Running; sprintf (temp, "%s %s -n -l %s %s %s %s %s %d %ld %d %f %f %f %f %d %d %d %d %d %f&", RSH, host, user, nest2Server, fname, inputFile, outputFile, params.archSize, params.iterationsNb, params.maxCells, params.pRedCell, params.pYellowCell, params.averageRuleNb, params.sdRuleNb, (int)(params.fitnessType), (int)(params.simulationType), (int)(params.randomGenerationType), rand (), params.toBuildRulesNumber, currentAverage); system (temp); /* si on veut debugger */ // printf ("Launching process on %s:\n[%s]\n", host, temp); net->host[aHostNb]->hostState = running; strcpy (net->host[aHostNb]->beginSimulationDate, currentDate.getTime ()); net->host[aHostNb]->beginDate.set (); net->update (aHostNb); refreshNetInterface (); } int GeneticSimulation::checkIfDone (unsigned int colonyIndex) { int hostNb; char host[50]; char testFile[256]; int aFile; char temp[256]; Date testedDate; /* cette methode teste la fin des simulations */ strcpy (testFile, pathName); strcat (testFile, dirName); strcat (testFile, "/"); strcat (testFile, currentSimulationStep->getStepName (dirName, currentSimulationStep->currentStep)); strcat (testFile, "/."); strcat (testFile, currentSimulationStep->getColonyName (dirName, currentSimulationStep->currentStep, colonyIndex)); strcat (testFile, ".res.zhuf"); if ((aFile = open (testFile, O_RDONLY)) != -1) { /* file already exists, simulation is complete */ close (aFile); hostNb = currentSimulationStep->colony[colonyIndex]->hostNb; strcpy (host, currentSimulationStep->colony[colonyIndex]->host); currentSimulationStep->colonyState[colonyIndex] = Done; sprintf (temp, "Simulation %d on %s is complete", colonyIndex, host); addComment (temp); geneticInterface->incStatWindow (); net->host[hostNb]->hostState = sleeping; strcpy (net->host[hostNb]->beginSimulationDate, "not running"); net->update (hostNb); refreshNetInterface (); return 1; } else { hostNb = currentSimulationStep->colony[colonyIndex]->hostNb; strcpy (host, currentSimulationStep->colony[colonyIndex]->host); currentDate.set (); testedDate.setWithDate (net->host[hostNb]->beginDate); testedDate.addMin (mainInterface->timeOut); if (testedDate.compare (currentDate) == -1) { sprintf (temp, "Simulation on %s has failed", host); addComment (temp); net->host[hostNb]->hostState = down; strcpy (net->host[hostNb]->beginSimulationDate, "not running"); net->update (hostNb); refreshNetInterface (); currentSimulationStep->colonyState[colonyIndex] = Waiting; } return 0; } } int GeneticSimulation::checkBeforeLaunching (unsigned int colonyIndex) { char testFile[256]; int aFile; /* cette methode teste la fin des simulations */ strcpy (testFile, pathName); strcat (testFile, dirName); strcat (testFile, "/"); strcat (testFile, currentSimulationStep->getStepName (dirName, currentSimulationStep->currentStep)); strcat (testFile, "/."); strcat (testFile, currentSimulationStep->getColonyName (dirName, currentSimulationStep->currentStep, colonyIndex)); strcat (testFile, ".res.zhuf"); if ((aFile = open (testFile, O_RDONLY)) != -1) { /* file already exists, simulation is complete */ close (aFile); currentSimulationStep->colonyState[colonyIndex] = Done; geneticInterface->incStatWindow (); return 1; } else { return 0; } } void GeneticSimulation::loadResults () { int i; char path[256]; char file[256]; double totalMark, minMark, maxMark, averagMark; char temp[256]; strcpy (path, pathName); strcat (path, dirName); strcat (path, "/"); strcat (path, currentSimulationStep->getStepName (dirName, currentSimulationStep->currentStep)); strcat (path, "/"); for (i=0; igetColonyName (dirName, currentSimulationStep->currentStep, i)); strcat (file, ".res"); if (currentSimulationStep->colony[i]->loadResult (path, file) == 0) { printf ("ERROR: unable to open %s%s\n", path, file); } } totalMark = 0.0; minMark = 1.0; maxMark = 0.0; for (i=0; icolony[i]->mark; if (minMark > currentSimulationStep->colony[i]->mark) minMark = currentSimulationStep->colony[i]->mark; if (maxMark < currentSimulationStep->colony[i]->mark) maxMark = currentSimulationStep->colony[i]->mark; } averagMark = totalMark/(double)(params.coloniesNb); bestMark[currentSimulationStep->currentStep] = maxMark; worstMark[currentSimulationStep->currentStep] = minMark; averageMark[currentSimulationStep->currentStep] = averagMark; sprintf (temp, "Best fitness: %f", maxMark); addComment (temp); sprintf (temp, "Average fitness: %f", averagMark); addComment (temp); sprintf (temp, "Worst fitness: %f", minMark); addComment (temp); } /* ---------------------------------------------------- */ DeferedSimulation::DeferedSimulation (char* aName, char* aDirName, char* aPathName, int aBeginIndex, int aEndIndex, Date aDate) { execDate.setWithDate (aDate); strcpy (name, aName); strcpy (dirName, aDirName); strcpy (pathName, aPathName); deferedSimuState = DSWaiting; strcpy (cDate, execDate.getDate ()); strcpy (cTime, execDate.getTime ()); beginIndex = aBeginIndex; endIndex = aEndIndex; } DeferedSimulation::~DeferedSimulation () { } /* ---------------------------------------------------- */ DeferedSimuList::DeferedSimuList (int aMaxDeferedSimulations) { number = 0; maxDeferedSimu = aMaxDeferedSimulations; deferedSimulation = new DeferedSimulation* [aMaxDeferedSimulations]; deferedSimuListContent = new ArrayContent(7, aMaxDeferedSimulations); deferedSimuListContent->addColumn ("Nb", IntType, 3); deferedSimuListContent->addColumn ("Name", CharType, 17); deferedSimuListContent->addColumn ("Date", CharType, 11); deferedSimuListContent->addColumn ("Time", CharType, 10); deferedSimuListContent->addColumn ("1st", IntType, 4); deferedSimuListContent->addColumn ("lst", IntType, 4); deferedSimuListContent->addColumn ("State", CharType, 9); } DeferedSimuList::~DeferedSimuList () { int i; for (i=0; i<(int)number; i++) { delete deferedSimulation[i]; } delete[] deferedSimulation; delete deferedSimuListContent; } int DeferedSimuList::add (char* aName, char* aDirName, char* aPathName, int aBeginIndex, int aEndIndex, Date aDate) { int line; if (number < maxDeferedSimu) { deferedSimulation[number] = new DeferedSimulation (aName, aDirName, aPathName, aBeginIndex, aEndIndex, aDate); line = deferedSimuListContent->addLine (); update (number); number++; return number-1; } else { // printf ("Error: only %d defered simulations\n", maxDeferedSimu); return -1; } } int DeferedSimuList::freeLeft () { if (number < maxDeferedSimu) { return 1; } else { return 0; } } void DeferedSimuList::update (int index) { deferedSimuListContent->addData (index, 0, (int)index); deferedSimuListContent->addData (index, 1, deferedSimulation[index]->name); deferedSimuListContent->addData (index, 2, deferedSimulation[index]->cDate); deferedSimuListContent->addData (index, 3, deferedSimulation[index]->cTime); deferedSimuListContent->addData (index, 4, deferedSimulation[index]->beginIndex); deferedSimuListContent->addData (index, 5, deferedSimulation[index]->endIndex); if (deferedSimulation[index]->deferedSimuState == DSWaiting) { deferedSimuListContent->addData (index, 6, "Waiting"); } else if (deferedSimulation[index]->deferedSimuState == DSRunning) { deferedSimuListContent->addData (index, 6, "Running"); } else if (deferedSimulation[index]->deferedSimuState == DSDone) { deferedSimuListContent->addData (index, 6, "Done"); } } unsigned int DeferedSimuList::getNumber () { return number; } ArrayContent* DeferedSimuList::getContent () { return deferedSimuListContent; } DeferedSimulation* DeferedSimuList::get (int index) { if ((index < (int)number) && (index >= 0)) { return deferedSimulation[index]; } else { return NULL; } } unsigned int DeferedSimuList::getWaitingOrRunningNumber () { int i; int returnedValue; returnedValue = 0; for (i=0; i<(int)number; i++) { if ((deferedSimulation[i]->deferedSimuState == DSRunning) || (deferedSimulation[i]->deferedSimuState == DSWaiting)) { returnedValue++; } } return returnedValue; }