Moving function InitRamdom to eoParserUtils.h

This commit is contained in:
victor 1999-12-17 12:04:06 +00:00
commit 5a735c3915
4 changed files with 883 additions and 852 deletions

View file

@ -5,20 +5,22 @@
############################################################################### ###############################################################################
lib_LIBRARIES = libeo.a lib_LIBRARIES = libeo.a
libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp eoParserUtils.cpp
libeoincdir = $(includedir)/eo libeoincdir = $(includedir)/eo
libeoinc_HEADERS = EO.h eo eo1d.h eo2d.h eo2dVector.h eoAged.h eoAlgo.h\ libeoinc_HEADERS = EO.h eo eo1d.h eo2d.h eo2dVector.h eoAged.h eoAlgo.h\
eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \ eoAtomBitFlip.h eoAtomCreep.h eoAtomMutation.h eoAtomMutator.h \
eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\ eoAtomRandom.h eoBin.h eoBitOp.h eoBitOpFactory.h eoBreeder.h\
eoData.h eoDetTournament.h eoDup.h eoESChrom.h eoESFullChrom.h eoESFullMut.h eoEasyEA.h\ eoData.h eoDetTournament.h eoDup.h eoESChrom.h eoESFullChrom.h \
eoESFullMut.h eoEasyEA.h\
eoEvalFunc.h eoEvalFuncPtr.h eoFitTerm.h eoFitness.h\ eoEvalFunc.h eoEvalFuncPtr.h eoFitTerm.h eoFitness.h\
eoGenTerm.h eoGeneration.h eoID.h eoInclusion.h eoInsertion.h\ eoGenTerm.h eoGeneration.h eoID.h eoInclusion.h eoInsertion.h\
eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h eoMutation.h eoNegExp.h\ eoKill.h eoLottery.h eoMerge.h eoMultiBinOp.h eoMultiMonOp.h \
eoMutation.h eoNegExp.h\
eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\ eoNonUniform.h eoNormal.h eoObject.h eoOp.h eoOpSelector.h\
eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\ eoParser.h eoPersistent.h eoPop.h eoPopOps.h eoPrintable.h\
eoProblem.h eoProportionalOpSel.h eoRNG.h eoRnd.h eoString.h\ eoProblem.h eoProportionalOpSel.h eoRNG.h eoRnd.h eoString.h\
eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \ eoStochTournament.h eoTerm.h eoTranspose.h eoUniform.h \
eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h eoUniformSelect.h eoUniformXOver.h eoVector.h eoXOver2.h \
eoParserUtils.h

View file

@ -45,20 +45,20 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* This class manages exceptions. It´s barely an extension of the standard except * This class manages exceptions. It´s barely an extension of the standard except
ion, ion,
* but it can be initialized with an STL string. Called UException (utils-except * but it can be initialized with an STL string. Called UException (utils-except
ion)+ ion)+
* to avoid conflicts with other classes. * to avoid conflicts with other classes.
*/ */
class UException: public exception { class UException: public exception {
public: public:
/// ///
UException( const string& _msg ): msg( _msg ) { }; UException( const string& _msg ): msg( _msg ) { };
/// ///
virtual const char* what() const { return msg.c_str(); }; virtual const char* what() const { return msg.c_str(); };
private: private:
string msg; string msg;
}; };
@ -93,8 +93,8 @@ public:
bool _required=false ) bool _required=false )
: repShortName(_shortName), repLongName(_longName), : repShortName(_shortName), repLongName(_longName),
repDescription(_description ), repEnv(""), repDefault(_default), repDescription(_description ), repEnv(""), repDefault(_default),
repValue(_default), repValType( _valType), repValue(_default), repValType( _valType),
repRequired( _required), repChanged(false) { repRequired( _required), repChanged(false) {
const char *c = repLongName.c_str(); const char *c = repLongName.c_str();
for(unsigned i=0; i<repLongName.length() ; i++,c++) { for(unsigned i=0; i<repLongName.length() ; i++,c++) {
@ -223,8 +223,8 @@ public:
Param param (_sFileParamName, _lFileParamName, _default, Param::STRING, "Name of the input file", 0); Param param (_sFileParamName, _lFileParamName, _default, Param::STRING, "Name of the input file", 0);
// FIRST: look for the corresponding environment variable // FIRST: look for the corresponding environment variable
if( getenv( param.environment().c_str() ) ) if( getenv( param.environment().c_str() ) )
param.value(getenv(param.environment().c_str()) ); param.value(getenv(param.environment().c_str()) );
// LAST (highest priority) parse the command line arguments // LAST (highest priority) parse the command line arguments
for (int i=1 ; i<parse_argc ; i++) for (int i=1 ; i<parse_argc ; i++)
@ -286,8 +286,8 @@ public:
* @param see above * @param see above
*/ */
string getString (const string& _shortName, const string& _longName, string getString (const string& _shortName, const string& _longName,
const string& _default = "", const string& _default = "",
const string& _description="", bool _required=false) { const string& _description="", bool _required=false) {
Param param ( _shortName, _longName, _default, Param::STRING, _description, _required ); Param param ( _shortName, _longName, _default, Param::STRING, _description, _required );
parse( param ); parse( param );
params.push_back( param ); params.push_back( param );
@ -319,8 +319,8 @@ public:
* @param see above * @param see above
*/ */
vector<string> getArray (const string& _shortName, const string& _longName, vector<string> getArray (const string& _shortName, const string& _longName,
const string& _default = "", const string& _default = "",
const string& _description="", bool _required=false) { const string& _description="", bool _required=false) {
Param param ( _shortName, _longName, _default, Param::ARRAY, _description, _required ); Param param ( _shortName, _longName, _default, Param::ARRAY, _description, _required );
parse( param ); parse( param );
params.push_back( param ); params.push_back( param );
@ -344,8 +344,8 @@ public:
*/ */
int getInt (const string& _shortName, const string& _longName, int getInt (const string& _shortName, const string& _longName,
const string& _default = "", const string& _default = "",
const string& _description="", bool _required=false) { const string& _description="", bool _required=false) {
Param param ( _shortName, _longName, _default, Param::INT, _description, _required ); Param param ( _shortName, _longName, _default, Param::INT, _description, _required );
parse( param ); parse( param );
params.push_back( param ); params.push_back( param );
@ -370,8 +370,8 @@ public:
*/ */
int getUnsignedLong (const string& _shortName, const string& _longName, int getUnsignedLong (const string& _shortName, const string& _longName,
const string& _default = "", const string& _default = "",
const string& _description="", bool _required=false) { const string& _description="", bool _required=false) {
Param param ( _shortName, _longName, _default, Param::UL, _description, _required ); Param param ( _shortName, _longName, _default, Param::UL, _description, _required );
parse( param ); parse( param );
params.push_back( param ); params.push_back( param );
@ -396,8 +396,8 @@ public:
*/ */
float getFloat (const string& _shortName, const string& _longName, float getFloat (const string& _shortName, const string& _longName,
const string& _default = "", const string& _default = "",
const string& _description="", bool _required=false) { const string& _description="", bool _required=false) {
Param param ( _shortName, _longName, _default, Param::FLOAT, _description, _required ); Param param ( _shortName, _longName, _default, Param::FLOAT, _description, _required );
parse( param ); parse( param );
params.push_back( param ); params.push_back( param );
@ -453,130 +453,130 @@ public:
string tmpStr, ReadStr, FirstWord; string tmpStr, ReadStr, FirstWord;
// FIRST: look if the associated environment variables have any value, to use them. // FIRST: look if the associated environment variables have any value, to use them.
if( getenv( param.environment().c_str() ) ) { if( getenv( param.environment().c_str() ) ) {
//cout <<"\t\t ENV param: ,"<<p->shortName()<<", ,"<<getenv(p->environment().c_str())<<endl; //cout <<"\t\t ENV param: ,"<<p->shortName()<<", ,"<<getenv(p->environment().c_str())<<endl;
param.value(getenv(param.environment().c_str()) ); param.value(getenv(param.environment().c_str()) );
} }
// SECOND: search the file parameter, if present // SECOND: search the file parameter, if present
if ( InputFileName != "" ) { if ( InputFileName != "" ) {
ifstream is(InputFileName.c_str()); ifstream is(InputFileName.c_str());
while (is) { while (is) {
is >> tmpStr; is >> tmpStr;
if ( ( !strcmp(tmpStr.c_str(), param.shortName().c_str()) ) || if ( ( !strcmp(tmpStr.c_str(), param.shortName().c_str()) ) ||
( !strcasecmp(tmpStr.c_str(), param.longName().c_str()) ) ( !strcasecmp(tmpStr.c_str(), param.longName().c_str()) )
) { // found the keyword ) { // found the keyword
Param::valueType tmp = param.valType(); Param::valueType tmp = param.valType();
switch ( tmp ) { switch ( tmp ) {
case Param::TITLE: case Param::TITLE:
cerr << "Error, we should not be there" << endl; cerr << "Error, we should not be there" << endl;
exit(1); exit(1);
break; break;
case Param::BOOL : case Param::BOOL :
param.value("true" ); param.value("true" );
break; break;
case Param::INT: case Param::INT:
case Param::UL: case Param::UL:
case Param::FLOAT: case Param::FLOAT:
is >> tmpStr; is >> tmpStr;
param.value(tmpStr); param.value(tmpStr);
break; break;
case Param::STRING: case Param::STRING:
tmpStr = parse_string(is); tmpStr = parse_string(is);
param.value(tmpStr); param.value(tmpStr);
break; break;
case Param::ARRAY: case Param::ARRAY:
ReadStr = parse_string(is); ReadStr = parse_string(is);
if ( ReadStr != "<" ) { // no "<" ">" --> a single string in the array if ( ReadStr != "<" ) { // no "<" ">" --> a single string in the array
param.value(ReadStr); param.value(ReadStr);
break; break;
} }
// read next word - and keep it in case of <> mismatch // read next word - and keep it in case of <> mismatch
FirstWord = parse_string(is); FirstWord = parse_string(is);
// test for empty array // test for empty array
if (FirstWord == ">") { if (FirstWord == ">") {
param.value(""); param.value("");
break; break;
} }
// else, read all words until ">" // else, read all words until ">"
tmpStr = FirstWord; tmpStr = FirstWord;
ReadStr = parse_string(is); ReadStr = parse_string(is);
while ( is && (ReadStr != ">") ) { while ( is && (ReadStr != ">") ) {
tmpStr = tmpStr + " " + ReadStr; tmpStr = tmpStr + " " + ReadStr;
ReadStr = parse_string(is); ReadStr = parse_string(is);
} }
if (!is) { // there was a "<" without the corresponding ">" if (!is) { // there was a "<" without the corresponding ">"
throw Parser::BadArrayParam( param.longName(), FirstWord ); throw Parser::BadArrayParam( param.longName(), FirstWord );
param.value(FirstWord); // assume unique string param.value(FirstWord); // assume unique string
} }
else else
param.value(tmpStr); param.value(tmpStr);
break; break;
}
}
}
} }
}
}
}
// LAST (highest priority) parse the command line arguments // LAST (highest priority) parse the command line arguments
for (i=1 ; i<parse_argc ; i++) for (i=1 ; i<parse_argc ; i++)
if( ( ! strcasecmp(param.longName().c_str(), parse_argv[i]) ) || if( ( ! strcasecmp(param.longName().c_str(), parse_argv[i]) ) ||
( ! strcmp(param.shortName().c_str(), parse_argv[i]) ) ( ! strcmp(param.shortName().c_str(), parse_argv[i]) )
) { // found the parameter name ) { // found the parameter name
if (param.valType() == Param::BOOL) { if (param.valType() == Param::BOOL) {
//cout <<"BOOL: "<<parse_argv[i]<<" <-- true"<<endl; //cout <<"BOOL: "<<parse_argv[i]<<" <-- true"<<endl;
param.value("true"); param.value("true");
}else{ }else{
if (param.valType() != Param::ARRAY) { //only if it is not an array if (param.valType() != Param::ARRAY) { //only if it is not an array
//cout <<"TYPE: "<<parse_argv[i]<<" <-- "<<parse_argv[i+1]<<endl; //cout <<"TYPE: "<<parse_argv[i]<<" <-- "<<parse_argv[i+1]<<endl;
param.value(parse_argv[i+1]); param.value(parse_argv[i+1]);
}else{ //if it is an ARRAY }else{ //if it is an ARRAY
i++; i++;
ReadStr = parse_argv[i++]; ReadStr = parse_argv[i++];
//cout <<"ARRAY: <-- "; //cout <<"ARRAY: <-- ";
if ( ReadStr != "<" ) { // no "<" ">" --> a single string in the array if ( ReadStr != "<" ) { // no "<" ">" --> a single string in the array
param.value(ReadStr); param.value(ReadStr);
}else{ }else{
// read next word - and keep it in case of <> mismatch // read next word - and keep it in case of <> mismatch
FirstWord = parse_argv[i++]; FirstWord = parse_argv[i++];
// test for empty array // test for empty array
if (FirstWord == ">") { if (FirstWord == ">") {
param.value(""); param.value("");
}else{ }else{
// else, read all words until ">" // else, read all words until ">"
tmpStr = FirstWord; tmpStr = FirstWord;
ReadStr = parse_argv[i++]; ReadStr = parse_argv[i++];
while ( (i<parse_argc) && (ReadStr != ">") ) { while ( (i<parse_argc) && (ReadStr != ">") ) {
tmpStr = tmpStr + " " + ReadStr; tmpStr = tmpStr + " " + ReadStr;
ReadStr = parse_argv[i++]; ReadStr = parse_argv[i++];
} }
//cout <<"tmpStr ;"<<tmpStr<<"; ("<<i<<","<<parse_argc<<") "<<endl; //cout <<"tmpStr ;"<<tmpStr<<"; ("<<i<<","<<parse_argc<<") "<<endl;
if ( (i>=parse_argc) && (ReadStr != ">") ) { // there was a "<" without the corresponding ">" if ( (i>=parse_argc) && (ReadStr != ">") ) { // there was a "<" without the corresponding ">"
throw Parser::BadArrayParam( param.longName(), FirstWord ); throw Parser::BadArrayParam( param.longName(), FirstWord );
param.value(FirstWord); // assume unique string param.value(FirstWord); // assume unique string
}else{ }else{
param.value(tmpStr); param.value(tmpStr);
} }
} }
} }
} }
} }
break; break;
} }
//MS after trying all possibilities, and if the value has not changed //MS after trying all possibilities, and if the value has not changed
// though the parameter was required, protest! // though the parameter was required, protest!
if (param.required() && !param.changed()) if (param.required() && !param.changed())
throw Parser::MissingReqParam(param.shortName()); throw Parser::MissingReqParam(param.shortName());
}; };
@ -686,7 +686,7 @@ public:
*/ */
void printHelp() { void printHelp() {
vector<Param>::iterator p; vector<Param>::iterator p;
// unsigned i; // unsigned i;
// print program name and description // print program name and description
cout << this->programName <<": "<<programDescription<<endl<<endl; cout << this->programName <<": "<<programDescription<<endl<<endl;
@ -696,16 +696,16 @@ public:
// only short usage! // only short usage!
cout << "Options of the form \"-ShortName value\" or \"--LongName value\"" << endl; cout << "Options of the form \"-ShortName value\" or \"--LongName value\"" << endl;
// for ( i=0,p=params.begin(); p!=params.end(); i++,p++ ) // for ( i=0,p=params.begin(); p!=params.end(); i++,p++ )
// if( p->valType() != Param::TITLE ) { // if( p->valType() != Param::TITLE ) {
// if( p->valType() != Param::BOOL ){ // if( p->valType() != Param::BOOL ){
// cout << ( (!p->required())?"[":""); // cout << ( (!p->required())?"[":"");
// cout <<p->shortName()<<" value"<<i; // cout <<p->shortName()<<" value"<<i;
// cout << ( (!p->required())?"]":"")<<" "; // cout << ( (!p->required())?"]":"")<<" ";
// }else{ // }else{
// cout << "["<<p->shortName()<<"] "; // cout << "["<<p->shortName()<<"] ";
// } // }
// } // for p // } // for p
cout << "Where:"<<endl; cout << "Where:"<<endl;
for ( p=params.begin(); p!=params.end(); p++ ) { for ( p=params.begin(); p!=params.end(); p++ ) {
@ -814,34 +814,7 @@ private:
}; };
/// Reproducible random seed
// Maybe there is a better place for this subroutine (a separate .cpp?)
#include <eoRNG.h>
//----------------------------------
void InitRandom( Parser & parser) {
//----------------------------------
unsigned long _seed;
try {
_seed = parser.getUnsignedLong("-S", "--seed", "0", "Seed for Random number generator" );
}
catch (UException & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
if (_seed == 0) { // use clock to get a "random" seed
_seed = (unsigned long)( time( 0 ) );
ostrstream s;
s << _seed;
parser.setParamValue("--seed", s.str()); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
}
rng.reseed(_seed);
return;
}
#endif #endif

32
eo/src/eoParserUtils.cpp Normal file
View file

@ -0,0 +1,32 @@
// See eoParserUtils.h
#include <iostream.h>
#include <eoParserUtils.h>
/// Reproducible random seed
//----------------------------------
void InitRandom( Parser & parser) {
//----------------------------------
unsigned long _seed;
try {
_seed = parser.getUnsignedLong("-S", "--seed", "0",
"Seed for Random number generator" );
}
catch (UException & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
if (_seed == 0) { // use clock to get a "random" seed
_seed = (unsigned long)( time( 0 ) );
ostrstream s;
s << _seed;
parser.setParamValue("--seed", s.str()); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
}
rng.reseed(_seed);
return;
}

24
eo/src/eoParserUtils.h Normal file
View file

@ -0,0 +1,24 @@
/*-------------------------------------------------------
File..........: eoParserUtils.h
Author........: Geneura Team, Marc Shoenauer
(this file: Victor Rivas, vrivas@ujaen.es)
Date..........: 17-Dec-1999
Description...: Some useful things that use eoParser.
Modifications.:
------------------- 1 -------------------
Author.......:
Date.........:
Description..:
*/
#ifndef EO_PARSER_UTILS
#define EO_PARSER_UTILS
#include <eoRNG.h>
#include <eoParser.h>
/// Reproducible random seed
//----------------------------------
void InitRandom( Parser & parser);
//----------------------------------
#endif