/****************************************************************** * * * 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 : serverN2.c Component : simulation server Fonctionality : genetic simulations ******************************************************************/ #include "algo.h" #include "simu.h" #include "graph.h" #include "date.h" /* #define SERVERN2_DEBUG */ main (int argc, char* argv[]) { char inputFile[256]; char outputFile[256]; char directory[256]; int archSize; int iterationsNumber; int maxCells; double pRedCell; double pYellowCell; double averageRuleNumber; double varianceRuleNumber; Simulation* aSimulation; FitnessType fitnessType; SimulationType simulationType; RandomGenerationType randomGenerationType; int randomNumber; int toBuildRulesNumber; double averageFitness; /* sert pour l'apprentissage learnWhenBad */ Date date1; Date date2; Date date3; Boolean continueSearching; if (argc != 17) { printf ("Usage: serverN2 directory inputfile outputfile size iterations maxCells pRedCell pYellowCell average variance fitnessType simuType randGenType randomNumber toBuidRulesNumber averageFitness\n"); } else { nice (19); strcpy (directory, argv[1]); strcpy (inputFile, argv[2]); strcpy (outputFile, argv[3]); archSize = atoi(argv[4]); iterationsNumber = atoi(argv[5]); maxCells = atoi(argv[6]); pRedCell = atof (argv[7]); pYellowCell = atof (argv[8]); averageRuleNumber = atof (argv[9]); varianceRuleNumber = atof (argv[10]); fitnessType = (FitnessType)(atoi(argv[11])); simulationType = (SimulationType)(atoi(argv[12])); randomGenerationType = (RandomGenerationType)(atoi(argv[13])); randomNumber = (unsigned int)(atoi (argv[14])); toBuildRulesNumber = atoi(argv[15]); averageFitness = atof (argv[16]); #ifdef SERVERN2_DEBUG printf ("Directory: %s\n", directory); printf ("Input file: %s\n", inputFile); printf ("Output file: %s\n", outputFile); printf ("Arch. size: %d\n", archSize); printf ("Iterations number: %d\n", iterationsNumber); printf ("Max. cells: %d\n", maxCells); printf ("pRedCells: %f\n", pRedCell); printf ("pYellowCell: %f\n", pYellowCell); printf ("Average rules nb: %f\n", averageRuleNumber); printf ("Variance rules nb: %f\n", varianceRuleNumber); printf ("Fitness type: %d\n", fitnessType); printf ("Simulation type: %d\n", simulationType); printf ("Random generation type: %d\n", randomGenerationType); printf ("Random number: %d\n", randomNumber); printf ("toBuidRulesNumber = %d\n", toBuildRulesNumber); printf ("averageFitness = %f\n", averageFitness); #endif /* on recupere un nombre aleatoire de l'interface principale pour ne pas avoir tout le temps les memes simulations */ srand (randomNumber); aSimulation = new Simulation (0, maxRules, simulationType); strcat (inputFile, ".zhuf"); /* At this point, NFS could have problems to process files (for example if the net is busy), because of decompression processes. So, the needed file could be not ready. That's why a time-out could be necessary. */ date1.set (); date2.set (); continueSearching = BTrue; while (continueSearching) { if (aSimulation->load (directory, inputFile, BFalse) == 0) { /* file not found */ date2.set (); date3.setWithDate (date1); date3.addSec (NFSTimeOut); if (date3.compare (date2) <= 0) { /* date2 >= date3 */ printf ("ERROR: unable to open %s%s\n", directory, inputFile); printf ("After %d seconds\n", NFSTimeOut); delete aSimulation; exit (-1); } /* we try to open the file during NFSTimeOut seconds */ } else { continueSearching = BFalse; } } /* file was correctly loaded -> continue */ if (aSimulation->ruleNumber == 1) { /* this is the first step, we must initialize colonies */ aSimulation->simulationRuleArray ->randomGenerate (pRedCell, pYellowCell, averageRuleNumber, varianceRuleNumber, randomGenerationType); } aSimulation->save (rulFile, archMustBeSaved, previewMustNotBeSaved, compression); aSimulation->simulate (archSize, iterationsNumber, maxCells-toBuildRulesNumber, fitnessType, noLearning, 0, 1, 0.0); /* pas de tolerance au bruit */ if ((toBuildRulesNumber > 0) && (aSimulation->mark < averageFitness)) { aSimulation->learn (toBuildRulesNumber, 1); } /* si la structure est mauvaise, on fait de l'apprentissage dessus */ aSimulation->classifyRules (); if (aSimulation->saveAsResult (directory, outputFile) == 0) { printf ("ERROR: unable to save %s%s\n", directory, outputFile); exit (-1); } aSimulation->save (rulFile, archMustBeSaved, previewMustNotBeSaved, compression); delete aSimulation; } }