/****************************************************************** * * * 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 : GenInt.c Component : genetic interface Fonctionality : contains methods refering to the genetic interface ******************************************************************/ #include "GenInt.h" #include "config.h" #include "file.h" extern FileDatas* geneticFileDatas; /* ----------------------------------------------------------- */ DeferedSimuInterface::DeferedSimuInterface (int x0, int y0, Display* aDisplay, DeferedSimuList* aDeferedSimuList) : Interface (x0, y0, 680, 360, 1, "Defered simulations", "Nest2", aDisplay) { closeButton = new Button (this, 20, 15, 100, 25, "Close", centerJustified, enabled); deferedSimuArray = new ArrayWindow (this, 20, 50, 640, 300, enabled, Grey, aDeferedSimuList->getContent()); deferedSimuArray->disableSelection (); } DeferedSimuInterface::~DeferedSimuInterface () { delete closeButton; delete deferedSimuArray; } actionType DeferedSimuInterface::manageInterface (XEvent report) { if (closeButton->manageEvent (report) == buttonPressed) { return endInterface; } deferedSimuArray->manageEvent (report); return noAction; } void DeferedSimuInterface::refresh () { deferedSimuArray->refresh (); closeButton->refresh (); } /* ----------------------------------------------------------- */ GeneticInterface::GeneticInterface (int x0, int y0, char* windowLabel, char* iconLabel, Display* aDisplay, MainInterface* aMainInterface) : Interface (x0, y0, 800, 720, 1, windowLabel, iconLabel, aDisplay) { int i; deferedSimuInterfaceDisplayed = 0; deferedSimulationRunning = 0; deferedSimulationIndex = -1; /* init of fitness type enumeration */ fitnessTypeEnum.add ("UsedRulesFraction"); fitnessTypeEnum.add ("GlobalUsedRules"); fitnessTypeEnum.add ("Compacity"); fitnessTypeEnum.add ("Complexity"); fitnessTypeEnum.add ("Coherence"); fitnessTypeEnum.add ("HybridMethod"); /* init of simulation type enumeration */ simulationTypeEnum.add ("cubic"); simulationTypeEnum.add ("hexa"); /* init of random generation type enumeration */ randomGenerationTypeEnum.add ("PseudoRandom"); randomGenerationTypeEnum.add ("ConnexityConstraint"); randomGenerationTypeEnum.add ("SpacialCorrelation"); randomGenerationTypeEnum.add ("ProbabilistTemplates"); /* init of crossover type enumeration */ crossoverTypeEnum.add ("SimpleCrossover"); crossoverTypeEnum.add ("DoubleCrossover"); crossoverTypeEnum.add ("AdvancedCrossover"); crossoverTypeEnum.add ("AdvancedCrossover2"); historyDisplayed = 0; mainInterface = aMainInterface; geneticSimulationSet = mainInterface->geneticSimulationSet; editedSimuParams = new SimulationParameters (); if (!geneticSimulationSet) { strcpy (editedName, "noname"); strcpy (editedDirName, "noname"); strcpy (editedPath, geneticFileDatas->returnCompleteDirectory ()); randomGenerationTypeEnum.setDefault (editedSimuParams->randomGenerationType); simulationTypeEnum.setDefault (editedSimuParams->simulationType); fitnessTypeEnum.setDefault (editedSimuParams->fitnessType); crossoverTypeEnum.setDefault (editedSimuParams->crossoverType); } else { strcpy (editedName, mainInterface->geneticSimulation->name); strcpy (editedDirName, mainInterface->geneticSimulation->dirName); strcpy (editedPath, mainInterface->geneticSimulation->pathName); editedSimuParams->setFromOtherSimulationParameters (&(mainInterface->geneticSimulation->params)); randomGenerationTypeEnum.setDefault (mainInterface->geneticSimulation->params.randomGenerationType); simulationTypeEnum.setDefault (mainInterface->geneticSimulation->params.simulationType); fitnessTypeEnum.setDefault (mainInterface->geneticSimulation->params.fitnessType); crossoverTypeEnum.setDefault (mainInterface->geneticSimulation->params.crossoverType); } /* creation of a default geneticSimulation */ if (!geneticSimulationSet) { mainInterface->geneticSimulation = new GeneticSimulation (editedName, editedDirName, editedPath, mainInterface->net, mainInterface, defaultColoniesNb, defaultSimulationType); geneticSimulationSet = 1; mainInterface->geneticSimulationSet = 1; } geneticSimulation = mainInterface->geneticSimulation; closeButton = new Button (this, 675, 10, 100, 25, "Close", centerJustified, enabled); loadButton = new Button (this, 345, 10, 100, 25, "Load", centerJustified, enabled); newButton = new Button (this, 455, 10, 100, 25, "New", centerJustified, enabled); goStepButton = new Button (this, 565, 10, 100, 25, "Go to", centerJustified, enabled); saveButton = new Button (this, 235, 10, 100, 25, "Save", centerJustified, enabled); simuButton = new Button (this, 125, 10, 100, 25, "Simul.", centerJustified, enabled); historyButton = new Button (this, 15, 10, 100, 25, "History", centerJustified, enabled); runButton = new Button (this, 355, 670, 100, 40, "Run", centerJustified, enabled); deferedButton = new Button (this, 465, 670, 100, 40, "Defered", centerJustified, enabled); initButton = new Button (this, 575, 670, 100, 40, "Initialize", centerJustified, enabled); stopButton = new Button (this, 685, 670, 100, 40, "Stop", centerJustified, enabled); previousStepButton = new Button (this, 30, 685, 140, 25, "Previous", centerJustified, enabled); nextStepButton = new Button (this, 180, 685, 140, 25, "Next", centerJustified, enabled); bar1 = new HorizontalBar (this, 10, 50, 320); simuTitle = new TextWidget (this, 120, 60, "Simulation"); bar2 = new HorizontalBar (this, 10, 80, 320); nameInput = new TextInput (this, 15, 95, 26, "Name ", editedName, enabled); dirNameInput = new TextInput (this, 15, 120, 16, "Directory name ", editedDirName, enabled); pathInput = new TextInput (this, 15, 145, 26, "Path ", editedPath, enabled); currentStepText = new TextWidget (this, 15, 170, "Current generation: ---"); totalStepsText = new TextWidget (this, 15, 195, "Total generations: ---"); bar3 = new HorizontalBar (this, 10, 230, 320); resultsTitle = new TextWidget (this, 53, 240, "Current generation results"); bar4 = new HorizontalBar (this, 10, 260, 320); bestMarkText = new TextWidget (this, 15, 275, "Best obtained fitness: --------"); averageMarkText = new TextWidget (this, 15, 300, "Average obtained fitness: --------"); worstMarkText = new TextWidget (this, 15, 325, "Worst obtained fitness: --------"); bar5 = new HorizontalBar (this, 10, 360, 320); parametersTitle = new TextWidget (this, 120, 370, "Parameters"); bar6 = new HorizontalBar (this, 10, 390, 320); coloniesNumberInput = new TextInput (this, 15, 405, 8, "Population size ", &(editedSimuParams->coloniesNb), enabled); simuTypeInput = new TextInput (this, 15, 430, 10, "Simulation type ", &simulationTypeEnum, enabled); bar7 = new VerticalBar (this, 340, 40, 600); initLabel = new TextWidget (this, 25, 465, "(1) Initialisation"); selectionLabel = new TextWidget (this, 25, 495, "(2) Selection"); crossOverLabel = new TextWidget (this, 25, 525, "(3) Crossing over"); mutationLabel = new TextWidget (this, 25, 555, "(4) Mutation"); swapLabel = new TextWidget (this, 25, 585, "(5) Swap"); simulationLabel = new TextWidget (this, 25, 615, "(6) Simulation"); evaluationLabel = new TextWidget (this, 25, 645, "(7) Evaluation"); initSetButton = new Button (this, 230, 460, 60, 25, "Set", centerJustified, enabled); crossOverSetButton = new Button (this, 230, 520, 60, 25, "Set", centerJustified, enabled); mutationSetButton = new Button (this, 230, 550, 60, 25, "Set", centerJustified, enabled); swapSetButton = new Button (this, 230, 580, 60, 25, "Set", centerJustified, enabled); simulationSetButton = new Button (this, 230, 610, 60, 25, "Set", centerJustified, enabled); evaluationSetButton = new Button (this, 230, 640, 60, 25, "Set", centerJustified, enabled); graphWindow = new GraphicWindow (this, 350, 375, 440, 280, enabled, ButtonPressMask | ExposureMask, LightGrey); statWindow = new GraphicWindow (this, 370, 80, 400, 20, enabled, ButtonPressMask | ExposureMask, Black); runningGenerationText = new TextWidget (this, 400, 55, " No running simulation "); resetStatWindow (1); for (i=0; igeneticEdition == BTrue) { enableEdition (); } else { disableEdition (); } } GeneticInterface::~GeneticInterface () { int i; delete closeButton; delete loadButton; delete newButton; delete saveButton; delete simuButton; delete historyButton; delete stopButton; delete initButton; delete runButton; delete deferedButton; delete goStepButton; delete nextStepButton; delete previousStepButton; delete nameInput; delete dirNameInput; delete pathInput; delete currentStepText; delete totalStepsText; delete bar3; delete resultsTitle; delete bar4; delete bestMarkText; delete averageMarkText; delete worstMarkText; delete bar5; delete parametersTitle; delete bar6; delete coloniesNumberInput; delete simuTypeInput; delete bar7; /* delete pRedCellInput; delete pYellowCellInput; delete averageRuleNumberInput; delete varianceRuleNumberInput; delete maxIterationsInput; delete archSizeInput; delete pUsefullInput; delete pUselessInput; delete pCrossOverInput; delete simuTypeText; delete evalTypeText; */ delete initLabel; delete initSetButton; delete selectionLabel; delete crossOverLabel; delete crossOverSetButton; delete mutationLabel; delete mutationSetButton; delete swapLabel; delete swapSetButton; delete simulationLabel; delete simulationSetButton; delete evaluationLabel; delete evaluationSetButton; for (i=0; imanageEvent (report) == buttonPressed) { if (mainInterface->geneticRunning) { if (displayDialogBox (mainInterface, "Close genetic algorithm", "Nest2", "A simulation is running. Abort current simulation ?", "Abort", "Cancel") == 0) { manageStop (); /* close history and defered simulation windows when opened */ if (historyDisplayed) { delete historyInterface; historyDisplayed = 0; } if (deferedSimuInterfaceDisplayed) { delete deferedSimuInterface; deferedSimuInterfaceDisplayed = 0; } setParameters (); return endInterface; } else { return noAction; } } else if (mainInterface->deferedSimuList->getWaitingOrRunningNumber() > 0) { displayDialogBox (mainInterface, "Close genetic algorithm", "Nest2", "Unable to close while defered simulations are waiting or running.", "Continue"); } else { /* close history and defered simulation windows when opened */ if (historyDisplayed) { delete historyInterface; historyDisplayed = 0; } if (deferedSimuInterfaceDisplayed) { delete deferedSimuInterface; deferedSimuInterfaceDisplayed = 0; } setParameters (); return endInterface; } } if (historyDisplayed) { if (historyInterface->manageInterface (report) == endInterface) { delete historyInterface; historyDisplayed = 0; } } if (deferedSimuInterfaceDisplayed) { if (deferedSimuInterface->manageInterface (report) == endInterface) { delete deferedSimuInterface; deferedSimuInterfaceDisplayed = 0; } } if (statWindow->manageEvent (report) == mustBeRefreshed) { refreshStatWindow (); } bar1->manageEvent (report); simuTitle->manageEvent (report); bar2->manageEvent (report); nameInput->manageEvent (report); dirNameInput->manageEvent (report); pathInput->manageEvent (report); currentStepText->manageEvent (report); totalStepsText->manageEvent (report); bar3->manageEvent (report); resultsTitle->manageEvent (report); bar4->manageEvent (report); bestMarkText->manageEvent (report); averageMarkText->manageEvent (report); worstMarkText->manageEvent (report); bar5->manageEvent (report); parametersTitle->manageEvent (report); bar6->manageEvent (report); coloniesNumberInput->manageEvent (report); simuTypeInput->manageEvent (report); bar7->manageEvent (report); /* pRedCellInput->manageEvent (report); pYellowCellInput->manageEvent (report); averageRuleNumberInput->manageEvent (report); varianceRuleNumberInput->manageEvent (report); maxIterationsInput->manageEvent (report); archSizeInput->manageEvent (report); pUsefullInput->manageEvent (report); pUselessInput->manageEvent (report); pCrossOverInput->manageEvent (report); simuTypeText->manageEvent (report); evalTypeText->manageEvent (report); */ initLabel->manageEvent (report); selectionLabel->manageEvent (report); crossOverLabel->manageEvent (report); mutationLabel->manageEvent (report); swapLabel->manageEvent (report); simulationLabel->manageEvent (report); evaluationLabel->manageEvent (report); if (initSetButton->manageEvent (report) == buttonPressed) { setInitialisation (); } if (crossOverSetButton->manageEvent (report) == buttonPressed) { setCrossOver (); } if (mutationSetButton->manageEvent (report) == buttonPressed) { setMutation (); } if (swapSetButton->manageEvent (report) == buttonPressed) { setSwap (); } if (simulationSetButton->manageEvent (report) == buttonPressed) { setSimulation (); } if (evaluationSetButton->manageEvent (report) == buttonPressed) { setEvaluation (); } dialogPanel->manageEvent (report); runningGenerationText->manageEvent (report); for (i=0; imanageEvent (report); } if (graphWindow->manageEvent (report) == mustBeRefreshed) { displayGraph (); } if (loadButton->manageEvent (report) == buttonPressed) { manageLoad (); } if (newButton->manageEvent (report) == buttonPressed) { manageNew (); } if (saveButton->manageEvent (report) == buttonPressed) { manageSave (); } if (simuButton->manageEvent (report) == buttonPressed) { manageSimu (); } if (historyButton->manageEvent (report) == buttonPressed) { manageHistory (); } if (initButton->manageEvent (report) == buttonPressed) { manageInitialize (); } if (runButton->manageEvent (report) == buttonPressed) { manageRun (); } if (deferedButton->manageEvent (report) == buttonPressed) { manageDefered (); } if (goStepButton->manageEvent (report) == buttonPressed) { manageGoStep (); } if (stopButton->manageEvent (report) == buttonPressed) { manageStop (); } if (nextStepButton->manageEvent (report) == buttonPressed) { manageNext (); } if (previousStepButton->manageEvent (report) == buttonPressed) { managePrevious (); } return noAction; } void GeneticInterface::refresh () { int i; closeButton->refresh (); loadButton->refresh (); newButton->refresh (); saveButton->refresh (); simuButton->refresh (); historyButton->refresh (); initButton->refresh (); runButton->refresh (); deferedButton->refresh (); stopButton->refresh (); goStepButton->refresh (); nextStepButton->refresh (); previousStepButton->refresh (); bar1->refresh (); simuTitle->refresh (); bar2->refresh (); nameInput->refresh (); dirNameInput->refresh (); pathInput->refresh (); currentStepText->refresh (); totalStepsText->refresh (); bar3->refresh (); resultsTitle->refresh (); bar4->refresh (); bestMarkText->refresh (); averageMarkText->refresh (); worstMarkText->refresh (); bar5->refresh (); parametersTitle->refresh (); bar6->refresh (); coloniesNumberInput->refresh (); simuTypeInput->refresh (); bar7->refresh (); initLabel->refresh (); initSetButton->refresh (); selectionLabel->refresh (); crossOverLabel->refresh (); crossOverSetButton->refresh (); mutationLabel->refresh (); mutationSetButton->refresh (); swapLabel->refresh (); swapSetButton->refresh (); simulationLabel->refresh (); simulationSetButton->refresh (); evaluationLabel->refresh (); evaluationSetButton->refresh (); if (graphWindow->shouldBeRefreshed ()) displayGraph (); if (statWindow->shouldBeRefreshed ()) refreshStatWindow (); runningGenerationText->refresh (); dialogPanel->refresh (); for (i=0; irefresh (); if (historyDisplayed) historyInterface->refresh (); if (deferedSimuInterfaceDisplayed) deferedSimuInterface->refresh (); } void GeneticInterface::displayGraph () { int i; int stepsNb; /* USELESS double currentMin, currentMax, currentAverage; */ double total; unsigned int barHeight1, barHeight2, barHeight3, posY; unsigned int oldBarHeight1, oldBarHeight2, oldBarHeight3; char text[20]; double barWidth; XPoint pts[3]; double maxFitness; stepsNb = geneticSimulation->totalSteps ; if (stepsNb < 5) { stepsNb = 5; for (i=geneticSimulation->totalSteps+1; iworstMark[i] = 0; geneticSimulation->averageMark[i] = 0; geneticSimulation->bestMark[i] = 0; } } XClearWindow (display, graphWindow->win); barWidth = (380.0/(double)stepsNb/3); maxFitness = 0.0; for (i=0; ibestMark[i+1]) { maxFitness = geneticSimulation->bestMark[i+1]; } } XSetBackground (display, *gc, color[LightGrey]); for (i=0; i<11; i++) { posY = (unsigned int)((10-i)*24+20); XSetForeground (display, *gc, color[Black]); XDrawLine (display, graphWindow->win, *gc, 40, posY, 430, posY); XSetForeground (display, *gc, color[White]); XDrawLine (display, graphWindow->win, *gc, 40, posY+1, 430, posY+1); } if (maxFitness < 0.2) { total = 0.2; for (i=0; i<11; i+=5) { posY = (unsigned int)((10-i)*24+20); sprintf (text, "0.%d", (int)(i/5)); XSetForeground (display, *gc, color[Black]); XDrawImageString (display, graphWindow->win, *gc, 5, posY+3, text, strlen(text)); } } else if (maxFitness < 0.5) { total = 0.5; for (i=0; i<11; i+=2) { posY = (unsigned int)((10-i)*24+20); sprintf (text, "0.%d", (int)(i/2)); XSetForeground (display, *gc, color[Black]); XDrawImageString (display, graphWindow->win, *gc, 5, posY+3, text, strlen(text)); } } else { total = 1.0; for (i=0; i<11; i++) { posY = (unsigned int)((10-i)*24+20); if (i==10) { sprintf (text, "1.0"); } else { sprintf (text, "0.%d", i); } XSetForeground (display, *gc, color[Black]); XDrawImageString (display, graphWindow->win, *gc, 5, posY+3, text, strlen(text)); } } XSetForeground (display, *gc, color[Black]); XDrawLine (display, graphWindow->win, *gc, 40, 20, 40, 260); XDrawLine (display, graphWindow->win, *gc, 40, 260, 430, 260); pts[0].x=(unsigned int)(49+(geneticSimulation->currentStep-1)*3*barWidth+(barWidth-1)-5); pts[0].y=265; pts[1].x=(unsigned int)(49+(geneticSimulation->currentStep-1)*3*barWidth+(barWidth-1)-11); pts[1].y=277; pts[2].x=(unsigned int)(49+(geneticSimulation->currentStep-1)*3*barWidth+(barWidth-1)); pts[2].y=277; if (geneticSimulation->currentStep > 0) { XSetForeground (display, *gc, color[Red]); XFillPolygon (display, graphWindow->win, *gc, pts, 3, Convex, CoordModeOrigin); } if (stepsNb < 20) { /* trace en histogrammes */ for (i=0; iworstMark[i+1]/total); barHeight2 = (int)(240.0*geneticSimulation->averageMark[i+1]/total); barHeight3 = (int)(240.0*geneticSimulation->bestMark[i+1]/total); XSetForeground (display, *gc, color[Red]); XFillRectangle (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth), 260-barHeight1, (unsigned int)(barWidth-1), barHeight1); XSetForeground (display, *gc, color[Blue]); XFillRectangle (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth+(barWidth-1)), 260-barHeight2, (unsigned int)(barWidth-1), barHeight2); XSetForeground (display, *gc, color[Gold]); XFillRectangle (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth+2*(barWidth-2)), 260-barHeight3, (unsigned int)(barWidth-1), barHeight3); } } else { /* trace en courbe */ XSetLineAttributes (display, *gc, 2, LineSolid, CapRound, JoinRound ); oldBarHeight1 = (int)(240.0*geneticSimulation->worstMark[1]/total); oldBarHeight2 = (int)(240.0*geneticSimulation->averageMark[1]/total); oldBarHeight3 = (int)(240.0*geneticSimulation->bestMark[1]/total); for (i=0; iworstMark[i+1]/total); barHeight2 = (int)(240.0*geneticSimulation->averageMark[i+1]/total); barHeight3 = (int)(240.0*geneticSimulation->bestMark[i+1]/total); XSetForeground (display, *gc, color[Red]); XDrawLine (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth), 260-oldBarHeight1, 45+(unsigned int)((i+1)*3*barWidth), 260-barHeight1); XSetForeground (display, *gc, color[Blue]); XDrawLine (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth), 260-oldBarHeight2, 45+(unsigned int)((i+1)*3*barWidth), 260-barHeight2); XSetForeground (display, *gc, color[Gold]); XDrawLine (display, graphWindow->win, *gc, 45+(unsigned int)(i*3*barWidth), 260-oldBarHeight3, 45+(unsigned int)((i+1)*3*barWidth), 260-barHeight3); oldBarHeight1 = barHeight1 ; oldBarHeight2 = barHeight2; oldBarHeight3 = barHeight3; } } XSetLineAttributes (display, *gc, 1, LineSolid, CapRound, JoinRound ); } void GeneticInterface::displayNewText (char* aText) { int i; for (i=0; ichange (scrollText[i]); } } void GeneticInterface::displayTexts () { char temp[256]; int currentStep; currentStep = geneticSimulation->currentStep; sprintf (temp, "Current generation: %d ", currentStep); currentStepText->change (temp); sprintf (temp, "Total generations: %d ", geneticSimulation->totalSteps); totalStepsText->change (temp); sprintf (temp, "Best obtained fitness: %f", geneticSimulation->bestMark[currentStep]); bestMarkText->change (temp); sprintf (temp, "Average obtained fitness: %f", geneticSimulation->averageMark[currentStep]); averageMarkText->change (temp); sprintf (temp, "Worst obtained fitness: %f", geneticSimulation->worstMark[currentStep]); worstMarkText->change (temp); } void GeneticInterface::manageLoad () { char aDirName[256]; /* 30 */ char aPathName[256]; /* 80 */ int coloniesNumberArg; SimulationType simulationTypeArg; if (displayDialogBox (mainInterface, "Load genetic simulation", "Nest2", " Save current simulation before ? ", "Save", "No") == 0) { if (geneticSimulation->save () == 0) { displayDialogBox (mainInterface, "Error", "Nest2", " Unable to save current simulation ", "Continue"); } } strcpy (aDirName, "noname"); strcpy (aPathName, geneticFileDatas->returnCompleteDirectory ()); displayInputBox (mainInterface, "Load genetic simulation", "nest2", 2, 50); addCharInput ( " Directory name ", 30, aDirName); addCharInput ( "Simulation Path ", 30, aPathName); if (manageInputBox ()) { /* cancelled operation */ } else { aDirName[29] = '\0'; aPathName[79] = '\0'; /* on charge une premiere fois pour avoir coloniesNumber */ if (geneticSimulation->load (aPathName, aDirName) == 0) { displayDialogBox (mainInterface, "Error", "Nest2", " Unable to load genetic simulation ", "Continue"); } coloniesNumberArg = geneticSimulation->params.coloniesNb; simulationTypeArg = geneticSimulation->params.simulationType; /* on vire l'ancienne simulation */ delete mainInterface->geneticSimulation; /* on en recree une jolie a la place */ mainInterface->geneticSimulation = new GeneticSimulation (editedName, editedDirName, editedPath, mainInterface->net, mainInterface, coloniesNumberArg, simulationTypeArg); /* on met le pointeur comme il faut */ geneticSimulation = mainInterface->geneticSimulation; /* maintenant on charge la nouvelle */ if (geneticSimulation->load (aPathName, aDirName) == 0) { displayDialogBox (mainInterface, "Error", "Nest2", " Unable to load genetic simulation ", "Continue"); } /* on repositionne les parametres edites */ editedSimuParams->setFromOtherSimulationParameters (&(geneticSimulation->params)); strcpy (editedName, geneticSimulation->name); strcpy (editedDirName, geneticSimulation->dirName); strcpy (editedPath, geneticSimulation->pathName); randomGenerationTypeEnum.setDefault (geneticSimulation->params.randomGenerationType); simulationTypeEnum.setDefault (geneticSimulation->params.simulationType); fitnessTypeEnum.setDefault (geneticSimulation->params.fitnessType); crossoverTypeEnum.setDefault (geneticSimulation->params.crossoverType); /* on reaffiche les nouveaux parametres edites*/ resetEditedValues (); displayGraph (); displayNewLabels (); disableEdition (); } } void GeneticInterface::testAndManageDeferedSimulations () { int i; DeferedSimulation* currentSimu; Date currentD; int launchSimu = -1; /* ici on regarde s'il y a des simu a lancer (si leur date d'execution est inferieure a la date courante) et on les lance s'il y a lieu avec launchDeferedSimulation () */ for (i=0; i<(int)mainInterface->deferedSimuList->getNumber (); i++) { currentSimu = mainInterface->deferedSimuList->get (i); if ((currentSimu->execDate.compare (currentD) <= 0) && (currentSimu->deferedSimuState == DSWaiting) && (launchSimu == -1)) { // printf ("launch defered simu %d\n", i); launchSimu = i; } } if (launchSimu != -1) { currentSimu = mainInterface->deferedSimuList->get (launchSimu); currentSimu->deferedSimuState = DSRunning; mainInterface->deferedSimuList->update (launchSimu); if (deferedSimuInterfaceDisplayed) { deferedSimuInterface->deferedSimuArray->refreshAll (); } deferedSimulationRunning = 1; deferedSimulationIndex = launchSimu; launchDeferedSimulation (currentSimu); } } void GeneticInterface::launchDeferedSimulation (DeferedSimulation* aDeferedSimulation) { char aDirName[256]; /* 30 */ char aPathName[256]; /* 80 */ int coloniesNumberArg; SimulationType simulationTypeArg; char temp[256]; sprintf (temp, "Launch defered simulation %s", aDeferedSimulation->name); displayNewText (temp); /* dans un premier temps, on va charger la bonne simulation */ strcpy (aDirName, aDeferedSimulation->dirName); strcpy (aPathName, aDeferedSimulation->pathName); /* on charge une premiere fois pour avoir coloniesNumber */ if (geneticSimulation->load (aPathName, aDirName) == 0) { printf ("ERROR: unable to load simulation\nname: %s%s\n", aPathName, aDirName); displayNewText ("Unable to load defered simulation."); displayNewText ("Aborting."); } else { coloniesNumberArg = geneticSimulation->params.coloniesNb; simulationTypeArg = geneticSimulation->params.simulationType; /* on vire l'ancienne simulation */ delete mainInterface->geneticSimulation; /* on en recree une jolie a la place */ mainInterface->geneticSimulation = new GeneticSimulation (editedName, editedDirName, editedPath, mainInterface->net, mainInterface, coloniesNumberArg, simulationTypeArg); /* on met le pointeur comme il faut */ geneticSimulation = mainInterface->geneticSimulation; /* maintenant on recharge la nouvelle */ if (geneticSimulation->load (aPathName, aDirName) == 0) { displayDialogBox (mainInterface, "Error", "Nest2", " Unable to load genetic simulation ", "Continue"); } /* on repositionne les parametres edites */ editedSimuParams->setFromOtherSimulationParameters (&(geneticSimulation->params)); strcpy (editedName, geneticSimulation->name); strcpy (editedDirName, geneticSimulation->dirName); strcpy (editedPath, geneticSimulation->pathName); /* on reaffiche les nouveaux parametres edites*/ resetEditedValues (); displayGraph (); displayNewLabels (); setParameters (); /* maintenant, on va vraiment lancer la simulation */ mainInterface->geneticRunning = 1; geneticSimulation->run (aDeferedSimulation->beginIndex, aDeferedSimulation->endIndex, this); } } void GeneticInterface::manageNew () { char aName[256]; /* 30 */ char aDirName[256]; /* 30 */ char aPathName[256]; /* 80 */ enableEdition (); strcpy (aName, "noname"); strcpy (aDirName, "noname"); strcpy (aPathName, geneticFileDatas->returnCompleteDirectory ()); displayInputBox (mainInterface, "Create new genetic simulation", "nest2", 3, 50); addCharInput ( "Simulation name ", 30, aName); addCharInput ( " Directory name ", 30, aDirName); addCharInput ( "Simulation Path ", 30, aPathName); if (manageInputBox ()) { /* cancelled operation */ } else { aName[29] = '\0'; aDirName[29] = '\0'; aPathName[79] = '\0'; strcpy (editedName, aName); strcpy (editedDirName, aDirName); strcpy (editedPath, aPathName); editedSimuParams->setToDefault (); resetEditedValues (); simulationTypeEnum.setDefault ((int)editedSimuParams->simulationType); setParameters (); geneticSimulation->currentStep = 0; geneticSimulation->totalSteps = 0; resetEditedValues (); displayGraph (); displayNewLabels (); } } void GeneticInterface::manageRun () { int begin, end; /* USELESS char temp[256]; */ if (mainInterface->geneticRunning == 1) { displayDialogBox (mainInterface, "Error", "Nest2", "A simulation is already running.", "Continue"); } else if (historyDisplayed) { displayDialogBox (mainInterface, "Error", "Nest2", "Please close the history file window before.", "Continue"); } else { setParameters (); begin = geneticSimulation->currentStep+1; end = geneticSimulation->currentStep+1; displayInputBox (mainInterface, "Run genetic simulation", "Nest2", 2, 30); addIntInput ( "Begin at generation ", 5, &begin); addIntInput ( " End at generation ", 5, &end); if (manageInputBox ()) { /* cancelled operation */ } else { if (end < begin) { displayDialogBox (mainInterface, "Error", "Nest2", "Beginning generation must be less than end generation.", "Continue"); } else { disableEdition (); mainInterface->geneticRunning = 1; geneticSimulation->run (begin, end, this); } } } } void GeneticInterface::manageDefered () { Date currentD; int begin, end; if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Current genetic simulation is already running.", "Continue"); } else { setParameters (); disableEdition (); if (geneticSimulation->save () == 0) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to save current genetic simulation.", "Continue"); } else { /* on y va vraiment la, on arrete de jouer */ if (mainInterface->deferedSimuList->freeLeft) { begin = geneticSimulation->currentStep+1; end = geneticSimulation->currentStep+1; displayInputBox (mainInterface, "Defered genetic simulation", "Nest2", 11, 30); addIntInput ( "Begin at generation ", 5, &begin); addIntInput ( " End at generation ", 5, &end); addIntInput ( " Date year ", 5, &(currentD.year)); addIntInput ( " Date month ", 5, &(currentD.month)); addIntInput ( " Date day ", 5, &(currentD.day)); addIntInput ( " Date hour ", 5, &(currentD.hour)); addIntInput ( " Date minutes ", 5, &(currentD.min)); addIntInput ( " Date seconds ", 5, &(currentD.sec)); if (manageInputBox ()) { /* cancelled operation */ } else { if (end < begin) { displayDialogBox (mainInterface, "Error", "Nest2", "Beginning generation must be less than end generation.", "Continue"); } else { mainInterface->deferedSimuList->add (geneticSimulation->name, geneticSimulation->dirName, geneticSimulation->pathName, begin, end, currentD); if (deferedSimuInterfaceDisplayed) { deferedSimuInterface->deferedSimuArray->refreshAll (); } } } } else { displayDialogBox (mainInterface, "Error", "Nest2", "Too much defered simulations in memory.", "Continue"); } } } } void GeneticInterface::manageGoStep () { int gen; gen = 1; if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to display generation that while simulation is running.", "Continue"); } else { displayInputBox (mainInterface, "Go generation", "Nest2", 1, 30); addIntInput ( " Go to generation ", 5, &gen); if (manageInputBox ()) { /* cancelled operation */ } else { if ((gen < 1) || (gen > (int)geneticSimulation->totalSteps)) { displayDialogBox (mainInterface, "Error", "Nest2", " Invalid generation number. ", "Continue"); } else { geneticSimulation->currentStep = gen; displayNewLabels (); displayGraph (); } } } } void GeneticInterface::managePrevious () { if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to display previous generation while simulation is running.", "Continue"); } else if (geneticSimulation->currentStep > 1) { geneticSimulation->currentStep = geneticSimulation->currentStep - 1; displayNewLabels (); displayGraph (); } } void GeneticInterface::manageNext () { if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to display next generation while simulation is running.", "Continue"); } else if (geneticSimulation->currentStep < geneticSimulation->totalSteps) { geneticSimulation->currentStep = geneticSimulation->currentStep + 1; displayNewLabels (); displayGraph (); } } void GeneticInterface::manageStop () { if (mainInterface->geneticRunning == 0) { displayDialogBox (mainInterface, "Error", "Nest2", "There are no running simulation.", "Continue"); } else { mainInterface->geneticRunning = 0; geneticSimulation->stop (); } } void GeneticInterface::manageInitialize () { } void GeneticInterface::setParameters () { strcpy (editedName, nameInput->returnCharValue ()); strcpy (editedDirName, dirNameInput->returnCharValue ()); strcpy (editedPath, pathInput->returnCharValue ()); editedSimuParams->coloniesNb = coloniesNumberInput->returnIntValue (); editedSimuParams->simulationType = (SimulationType)simulationTypeEnum.defaultValue (); if (geneticSimulation->params.coloniesNb != editedSimuParams->coloniesNb) { /* printf ("on recree une nouvelle simu avec %d colonies\n", editedSimuParams->coloniesNb); */ delete mainInterface->geneticSimulation; mainInterface->geneticSimulation = new GeneticSimulation (editedName, editedDirName, editedPath, mainInterface->net, mainInterface, editedSimuParams->coloniesNb, editedSimuParams->simulationType); geneticSimulation = mainInterface->geneticSimulation; resetEditedValues (); displayNewLabels (); displayGraph (); } strcpy (geneticSimulation->name, editedName); strcpy (geneticSimulation->dirName, editedDirName); strcpy (geneticSimulation->pathName, editedPath); geneticSimulation->params.setFromOtherSimulationParameters(editedSimuParams); } void GeneticInterface::manageSave () { if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to save current genetic simulation while running.", "Continue"); } else { setParameters (); if (geneticSimulation->save () == 0) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to save current genetic simulation.", "Continue"); } else { displayDialogBox (mainInterface, "Save genetic simulation", "Nest2", "Current genetic simulation has been saved.", "Continue"); } } } void GeneticInterface::manageSimu () { if (deferedSimuInterfaceDisplayed) { displayDialogBox (mainInterface, "Error", "Nest2", "Defered simulations window already opened.", "Continue"); } else { deferedSimuInterface = new DeferedSimuInterface (70, 70, display, mainInterface->deferedSimuList); deferedSimuInterfaceDisplayed = 1; } } void GeneticInterface::manageHistory () { char pName[256]; char fName[256]; strcpy (pName, geneticSimulation->pathName); strcat (pName, geneticSimulation->dirName); strcat (pName, "/"); strcpy (fName, geneticSimulation->dirName); strcat (fName, ".comment"); if (historyDisplayed) { displayDialogBox (mainInterface, "Error", "Nest2", "History file window already opened.", "Continue"); } else if (mainInterface->geneticRunning) { displayDialogBox (mainInterface, "Error", "Nest2", "Unable to open history file window while simulation's running.", "Continue"); } else { historyInterface = new HistoryInterface (50, 50, display, pName, fName); historyDisplayed = 1; } } void GeneticInterface::incStatWindow () { char temp[256]; currentStat++; refreshStatWindow (); sprintf (temp, " Generation %d is running ", geneticSimulation->currentSimulationStep->currentStep); runningGenerationText->change (temp); } void GeneticInterface::resetStatWindow (unsigned int max) { char temp[256]; maxStat = max; currentStat = 0; // printf ("%d %d\n", currentStat, maxStat); if (geneticSimulation->isRunning) { sprintf (temp, " Generation %d is running ", geneticSimulation->currentSimulationStep->currentStep+1); runningGenerationText->change (temp); } else { runningGenerationText->change (" No running simulation "); refreshStatWindow (); } } void GeneticInterface::refreshStatWindow () { XEvent report; double stat; stat = (double)(currentStat)/(double)(maxStat); // printf ("%f\n", stat); XClearWindow(display, statWindow->win); XSetForeground (display, *gc, color[White]); XDrawRectangle (display, statWindow->win, *gc, 0, 0, 399, 19); XSetForeground (display, *gc, color[Red]); XFillRectangle (display, statWindow->win, *gc, 1, 1, (unsigned int)(stat*398.0), 18); XCheckWindowEvent (display, statWindow->win, ExposureMask, &report); } void GeneticInterface::resetEditedValues () { coloniesNumberInput->changeIntValue (&(editedSimuParams->coloniesNb)); simuTypeInput->changeEnumValue (simulationTypeEnum.defaultValue ()); nameInput->changeCharValue (editedName); dirNameInput->changeCharValue (editedDirName); pathInput->changeCharValue (editedPath); } void GeneticInterface::displayNewLabels () { char temp[256]; sprintf (temp, "Current generation: %d ", geneticSimulation->currentStep); currentStepText->change (temp); sprintf (temp, "Total generations: %d ", geneticSimulation->totalSteps); totalStepsText->change (temp); sprintf (temp, "Best obtained fitness: %f ", geneticSimulation->bestMark[geneticSimulation->currentStep]); bestMarkText->change (temp); sprintf (temp, "Average obtained fitness: %f ", geneticSimulation->averageMark[geneticSimulation->currentStep]); averageMarkText->change (temp); sprintf (temp, "Worst obtained fitness: %f ", geneticSimulation->worstMark[geneticSimulation->currentStep]); worstMarkText->change (temp); } void GeneticInterface::setInitialisation () { double oldPRedCell; double oldPYellowCell; double oldAverageRuleNb; double oldSdRuleNb; oldPRedCell = editedSimuParams->pRedCell; oldPYellowCell = editedSimuParams->pYellowCell; oldAverageRuleNb = editedSimuParams->averageRuleNb; oldSdRuleNb = editedSimuParams->sdRuleNb; displayInputBox (mainInterface, "Initialisation parameters", "nest2", 6, 43); if (mainInterface->geneticEdition == BTrue) { addDoubleInput (" Red Cell probability ", 10, &(editedSimuParams->pRedCell), enabled); addDoubleInput (" Yellow Cell probability ", 10, &(editedSimuParams->pYellowCell), enabled); addDoubleInput (" Average rules number ", 10, &(editedSimuParams->averageRuleNb), enabled); addDoubleInput ("Rules number st. deviation", 10, &(editedSimuParams->sdRuleNb), enabled); addEnumInput ( " Random generation", 20, &randomGenerationTypeEnum, enabled); } else { addDoubleInput (" Red Cell probability ", 10, &(editedSimuParams->pRedCell), disabled); addDoubleInput (" Yellow Cell probability ", 10, &(editedSimuParams->pYellowCell), disabled); addDoubleInput (" Average rules number ", 10, &(editedSimuParams->averageRuleNb), disabled); addDoubleInput ("Rules number st. deviation", 10, &(editedSimuParams->sdRuleNb), disabled); addEnumInput ( " Random generation", 20, &randomGenerationTypeEnum, disabled); } if (manageInputBox ()) { /* cancelled operation */ editedSimuParams->pRedCell = oldPRedCell ; editedSimuParams->pYellowCell = oldPYellowCell; editedSimuParams->averageRuleNb = oldAverageRuleNb; editedSimuParams->sdRuleNb = oldSdRuleNb; } else { editedSimuParams->randomGenerationType = (RandomGenerationType)(randomGenerationTypeEnum.defaultValue ()); displayNewText ("Initialisation parameters have been changed."); } } void GeneticInterface::setCrossOver () { double oldPCrossover; oldPCrossover = editedSimuParams->pCrossOver; displayInputBox (mainInterface, "Crossing over parameter", "nest2", 2, 40); if (mainInterface->geneticEdition == BTrue) { addDoubleInput ("Crossing Over prob. ", 8, &(editedSimuParams->pCrossOver), enabled); addEnumInput ( " Crossing over type ", 18, &crossoverTypeEnum, enabled); } else { addDoubleInput ("Crossing Over prob. ", 8, &(editedSimuParams->pCrossOver), disabled); addEnumInput ( " Crossing over type ", 17, &crossoverTypeEnum, disabled); } if (manageInputBox ()) { /* cancelled operation */ editedSimuParams->pCrossOver = oldPCrossover; } else { editedSimuParams->crossoverType = (CrossoverType)(crossoverTypeEnum.defaultValue ()); displayNewText ("Crossing-over parameter has been changed."); } } void GeneticInterface::setMutation () { double oldPUsed; double oldPUnused; oldPUsed = editedSimuParams->pUsed; oldPUnused = editedSimuParams->pUnused; displayInputBox (mainInterface, "Mutation parameter", "nest2", 2, 45); if (mainInterface->geneticEdition == BTrue) { addDoubleInput (" Mutation for used cell prob. ", 10, &(editedSimuParams->pUsed), enabled); addDoubleInput ("Mutation for unused cell prob. ", 10, &(editedSimuParams->pUnused), enabled); } else { addDoubleInput (" Mutation for used cell prob. ", 10, &(editedSimuParams->pUsed), disabled); addDoubleInput ("Mutation for unused cell prob. ", 10, &(editedSimuParams->pUnused), disabled); } if (manageInputBox ()) { /* cancelled operation */ editedSimuParams->pUsed = oldPUsed; editedSimuParams->pUnused = oldPUnused; } else { displayNewText ("Mutation parameters have been changed."); } } void GeneticInterface::setSwap () { double oldPSwap; oldPSwap = editedSimuParams->pSwap; displayInputBox (mainInterface, "Swap parameter", "nest2", 1, 30); if (mainInterface->geneticEdition == BTrue) { addDoubleInput ("Swap probability ", 8, &(editedSimuParams->pSwap), enabled); } else { addDoubleInput ("Swap probability ", 8, &(editedSimuParams->pSwap), disabled); } if (manageInputBox ()) { /* cancelled operation */ editedSimuParams->pSwap = oldPSwap; } else { displayNewText ("Swap parameter has been changed."); } } void GeneticInterface::setSimulation () { int oldArchSize; long oldIterationsNb; int oldMaxCells; int oldToBuildRulesNumber; oldArchSize = editedSimuParams->archSize; oldIterationsNb = editedSimuParams->iterationsNb; oldMaxCells = editedSimuParams->maxCells; oldToBuildRulesNumber = editedSimuParams->toBuildRulesNumber; displayInputBox (mainInterface, "Simulation parameters", "nest2", 5, 30); if (mainInterface->geneticEdition == BTrue) { addIntInput ( "Architecture size ", 8, &(editedSimuParams->archSize), enabled); addLongInput (" Max iterations ", 8, &(editedSimuParams->iterationsNb), enabled); addIntInput ( " Max cells ", 8, &(editedSimuParams->maxCells), enabled); addIntInput ( " Learned rules ", 8, &(editedSimuParams->toBuildRulesNumber), enabled); } else { addIntInput ( "Architecture size ", 8, &(editedSimuParams->archSize), disabled); addLongInput (" Max iterations ", 8, &(editedSimuParams->iterationsNb), disabled); addIntInput ( " Max cells ", 8, &(editedSimuParams->maxCells), disabled); addIntInput ( " Learned rules ", 8, &(editedSimuParams->toBuildRulesNumber), disabled); } if (manageInputBox ()) { /* cancelled operation */ editedSimuParams->archSize = oldArchSize; editedSimuParams->iterationsNb = oldIterationsNb; editedSimuParams->maxCells = oldMaxCells; editedSimuParams->toBuildRulesNumber = oldToBuildRulesNumber; } else { displayNewText ("Simulation parameters have been changed."); } } void GeneticInterface::setEvaluation () { displayInputBox (mainInterface, "Evaluation parameter", "nest2", 1, 43); if (mainInterface->geneticEdition == BTrue) { addEnumInput (" Fitness function ", 20, &fitnessTypeEnum, enabled); } else { addEnumInput (" Fitness function ", 20, &fitnessTypeEnum, disabled); } if (manageInputBox ()) { /* cancelled operation */ } else { editedSimuParams->fitnessType = (FitnessType)(fitnessTypeEnum.defaultValue ()); displayNewText ("Simulation parameters have been changed."); } } void GeneticInterface::enableEdition () { mainInterface->geneticEdition = BTrue; nameInput->enable (); dirNameInput->enable (); pathInput->enable (); coloniesNumberInput->enable (); simuTypeInput->enable (); } void GeneticInterface::disableEdition () { mainInterface->geneticEdition = BFalse; nameInput->disable (); dirNameInput->disable (); pathInput->disable (); coloniesNumberInput->disable (); simuTypeInput->disable (); } /* ----------------------------------------------------------- */ HistoryInterface::HistoryInterface (int x0, int y0, Display* aDisplay, char* pathName, char* fileName) : Interface (x0, y0, 500, 500, 1, "History file", "Nest2", aDisplay) { closeButton = new Button (this, 390, 10, 100, 25, "Close", centerJustified, enabled); fileViewer = new FileViewer (this, 10, 50, 480, 440, enabled, Grey, pathName, fileName, "Edition of history file"); } HistoryInterface::~HistoryInterface () { delete closeButton; delete fileViewer; } actionType HistoryInterface::manageInterface (XEvent report) { if (closeButton->manageEvent (report) == buttonPressed) { return endInterface; } fileViewer->manageEvent (report); return noAction; } void HistoryInterface::refresh () { closeButton->refresh (); fileViewer->refresh (); }