Exemple #1
0
/**
 * \brief Register mutation related but Genotype unrelated parameters
 */
void Mutation::registerParameters(StateP state)
{
	state->getRegistry()->registerEntry("mutation.indprob", (voidP) new double(0.3), ECF::DOUBLE,
		"individual mutation probability (unless the algorithm overrides it) (default: 0.3)");
//	state->getRegistry()->registerEntry("mutation.geneprob", (voidP) new double(0.01), ECF::DOUBLE);
	state->getRegistry()->registerEntry("mutation.genotypes", (voidP) new std::string("random"), ECF::STRING,
		"if there are multiple genotypes, which to mutate? 'random': a random one, all: mutate all (default: random)");
	state->getRegistry()->registerEntry("mutation.protected", (voidP) new std::string(""), ECF::STRING,
		"indexes of genotypes (separated by spaces) that are excluded (protected) from mutation (default: none)");
}
Exemple #2
0
void RegEvalOp::registerParameters(StateP state) {

    state->getRegistry()->registerEntry("inputfile", (voidP)(new std::string("learning.txt")), ECF::STRING);

    state->getRegistry()->registerEntry("testfile", (voidP)(new std::string("test.txt")), ECF::STRING);

    state->getRegistry()->registerEntry("classesfile", (voidP)(new std::string("classes.txt")), ECF::STRING);

    state->getRegistry()->registerEntry("resultsfile", (voidP)(new std::string("results.txt")), ECF::STRING);

    state->getRegistry()->registerEntry("classesNum", (voidP)(new uint(2)), ECF::UINT);
}
void ModularRobotEvalOp::registerParameters(StateP state)
{
    state->getRegistry()->registerEntry("robot.modules", (voidP) (new uint(1)), ECF::UINT, "Number of modules" );
    state->getRegistry()->registerEntry("robot.runtime", (voidP) (new uint(10000)), ECF::UINT, "Max robot runtime (ms)" );
    state->getRegistry()->registerEntry("robot.timestep", (voidP) (new float(1.0)), ECF::FLOAT, "Time step (ms)" );
    state->getRegistry()->registerEntry("robot.configfile", (voidP) (new std::string()), ECF::STRING, "Robot description file");

    state->getRegistry()->registerEntry("osc.maxamplitude", (voidP) (new uint(90)), ECF::UINT, "Max amplitude of oscillators");
    state->getRegistry()->registerEntry("osc.maxoffset", (voidP) (new uint(90)), ECF::UINT, "Max offset of oscillators");
    state->getRegistry()->registerEntry("osc.maxphase", (voidP) (new uint(360)), ECF::UINT, "Max phase of oscillators");
    state->getRegistry()->registerEntry("osc.maxfrequency", (voidP) (new float(1.0f)), ECF::FLOAT, "Max frequency of oscillators");
}
bool SymbRegEvalOp::initialize(StateP state)
{
	domain.clear();
	codomain.clear();

	double datum;
	std::stringstream ss;

	// check if the parameters are defined in the conf. file
	// if not, we return false so the initialization fails
	if(!state->getRegistry()->isModified("domain") ||
		!state->getRegistry()->isModified("codomain"))
		return false;

	voidP sptr = state->getRegistry()->getEntry("domain"); // get parameter value
	ss << *((std::string*) sptr.get()); // convert from voidP to user defined type
	while(ss >> datum) {	// read all the data from string
		domain.push_back(datum);
	}
	ss.str("");
	ss.clear();	// reset stringstream object
	sptr = state->getRegistry()->getEntry("codomain");	// get codomain parameter
	ss << *((std::string*) sptr.get());
	while(ss >> datum) {	// read values
		codomain.push_back(datum);
	}

	if(domain.size() != codomain.size())	// if the parameters are ill defined, return false
		return false;
	nSamples = (uint) domain.size();

	return true;

	//nSamples = 10;
	//double x = -10;
	//for(uint i = 0; i < nSamples; i++) {
	//	domain.push_back(x);
	//	codomain.push_back(x + sin(x));
	//	x += 2;
	//}
	//return true;
}
bool TermStagnationOp::initialize(StateP state)
{
	voidP sptr = state->getRegistry()->getEntry("term.stagnation");
	termStagnation_ = *((uint*) sptr.get());

	// define the default criterion
	if(termStagnation_ == 0) {
		voidP sptr = state->getRegistry()->getEntry("population.size");
		uint demeSize = *((uint*) sptr.get());
		termStagnation_ = 5000 / demeSize;
		if(termStagnation_ < 10)
			termStagnation_ = 5;
		if(termStagnation_ > 200)
			termStagnation_ = 200;
	}

	if(!state->getRegistry()->isModified("term.stagnation"))
		return false;

	return true;
}
bool ArtificialBeeColony::initialize(StateP state)
{
	// initialize all operators
	selFitOp->initialize(state);
	selFitOp->setSelPressure(2);
	selBestOp->initialize(state);
	selWorstOp->initialize(state);
	selRandomOp->initialize(state);

	voidP sptr = state->getRegistry()->getEntry("population.size");
	uint size = *((uint*) sptr.get());
	probability_.resize(size);

	// this algorithm accepts a single FloatingPoint Genotype
	FloatingPointP flp (new FloatingPoint::FloatingPoint);
	if(state->getGenotypes()[0]->getName() != flp->getName()) {
		ECF_LOG_ERROR(state, "Error: ABC algorithm accepts only a single FloatingPoint genotype!");
		throw ("");
	}

	voidP limitp = getParameterValue(state, "limit");
	limit_ = *((uint*) limitp.get());

	voidP lBound = state->getGenotypes()[0]->getParameterValue(state, "lbound");
	lbound_ = *((double*) lBound.get());
	voidP uBound = state->getGenotypes()[0]->getParameterValue(state, "ubound");
	ubound_ = *((double*) uBound.get());

	// batch run check
	if(isTrialAdded_)
		return true;

	FloatingPointP flpoint[2];
	for(uint iGen = 1; iGen < 2; iGen++) {

		flpoint[iGen] = (FloatingPointP) new FloatingPoint::FloatingPoint;
		state->setGenotype(flpoint[iGen]);

		flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(1));					

		// initial value of trial parameter should be (as close as possible to) 0				
		flpoint[iGen]->setParameterValue(state, "lbound", (voidP) new double(0));
		flpoint[iGen]->setParameterValue(state, "ubound", (voidP) new double(0.01));
	}
	ECF_LOG(state, 1, "ABC algorithm: added 1 FloatingPoint genotype (trial)");

	// mark adding of trial genotype
	isTrialAdded_ = true;

    return true;
}
Exemple #7
0
bool StatCalc::initialize(StateP state)
{
	state_ = state;
	average_.clear();
	stdDev_.clear();
	max_.clear();
	min_.clear();
	time_.clear();
	sampleSize_.clear();
	evaluations_.clear();
	nEvaluations_ = 0;
	lowest_ = highest_ = 0;

	if(state->getRegistry()->isModified("stats.file")) {
		voidP sptr = state->getRegistry()->getEntry("stats.file");
		statsFileName_ = *((std::string*) sptr.get());

		statsFile_.open(statsFileName_.c_str());
		if(!statsFile_) {
			throw std::string("Error: can't open stats file (") + statsFileName_ + ")";
		}
	}
	return true;
}
void SymbRegEvalOp::registerParameters(StateP state)
{
	state->getRegistry()->registerEntry("domain", (voidP) (new std::string), ECF::STRING);
	state->getRegistry()->registerEntry("codomain", (voidP) (new std::string), ECF::STRING);
}
Exemple #9
0
bool RegEvalOp::initialize(StateP state) {

    state->getContext()->environment = this;
    
    voidP sptr = state->getRegistry()->getEntry("classesNum");
    classesNum = *((uint *) sptr.get());

    std::string outputPath = *((std::string*) state->getRegistry()->getEntry("resultsfile").get());
    
    std::ofstream outfile;
    outfile.open(outputPath.c_str());
   
    if (!outfile.is_open()) {
        ECF_LOG_ERROR(state, "Error: Can't open output file " + outputPath);
        return false;
    }
    
    outfile << "Gen_No,Training,Test" << std::endl;
    outfile.close();

    sptr = state->getRegistry()->getEntry("inputfile");
    std::string filePath = *((std::string *) sptr.get());

    ifstream file;
    file.open(filePath.c_str());

    if (!file.is_open()) {
        ECF_LOG_ERROR(state, "Error: Can't open input file " + filePath);
        return false;
    }

    parseFile(domain, codomain, file);
    file.close();


    sptr = state->getRegistry()->getEntry("testfile");
    filePath = *((std::string *) sptr.get());
    file.open(filePath.c_str());

    if (!file.is_open()) {
        ECF_LOG_ERROR(state, "Error: Can't open test file " + filePath);
        return false; 
    }

    parseFile(testDomain, testCodomain, file);
    file.close();
    
    sptr = state->getRegistry()->getEntry("classesfile");
    filePath = *((std::string *) sptr.get());

    file.open(filePath.c_str());

    if (!file.is_open()) {
        generateDefaultClasses();
    } else {
        generateParsedClasses(file);
    }

    file.close();

    for (auto& entry: classes) {
        f1Score.insert(std::make_pair(entry.first, std::vector<uint>(3, 0)));
    }
    
    return true;
}
void TermStagnationOp::registerParameters(StateP state)
{
	uint *value = new uint(50);
	state->getRegistry()->registerEntry("term.stagnation", (voidP) value, ECF::UINT,
		"max number of consecutive generations without improvement (default: 5000 / pop_size)");
}
Exemple #11
0
/**
 * \brief Initialize all mutation operators of all active genotypes
 */
bool Mutation::initialize(StateP state)
{
	state_ = state;
	protectedGenotypes_.clear();
	protectedGenotypes_.insert(protectedGenotypes_.begin(), operators.size(), false);
	opProb.clear();

	voidP sptr = state->getRegistry()->getEntry("mutation.indprob");
	indMutProb_ = *((double*)sptr.get());

	sptr = state->getRegistry()->getEntry("mutation.geneprob");
//	geneMutProb_ = *((double*)sptr.get());
//	if(state->getRegistry()->isModified("mutation.geneprob") == false)
//		geneMutProb_ = 0;

	sptr = state->getRegistry()->getEntry("mutation.genotypes");
	std::string mutGen = *((std::string*)sptr.get());

	mutateGenotypes_ = RANDOM_GENOTYPE;
	if(mutGen == "random")
		mutateGenotypes_ = RANDOM_GENOTYPE;
	else if(mutGen == "all")
		mutateGenotypes_ = ALL_GENOTYPES;
	else
		ECF_LOG_ERROR(state, "Warning: invalid parameter value (key: mutation.genotypes)");

	// read protected genotypes
	std::stringstream ss;
	sptr = state->getRegistry()->getEntry("mutation.protected");
	ss << *((std::string*) sptr.get());
	uint genId;
	while(ss >> genId) {	// read all the data from string
		if(genId >= protectedGenotypes_.size()) {
			ECF_LOG_ERROR(state, "Error: invalid genotype index (key: mutation.protected)!");
			throw("");
		}
		protectedGenotypes_[genId] = true;
	}

	// initialize operators for all genotypes
	for(uint gen = 0; gen < operators.size(); gen++) {
		uint nOps = (uint) operators[gen].size();
		// if the genotype doesn't define mutation operators
		if(nOps == 0) {
			protectedGenotypes_[gen] = true;
			std::vector<double> empty;
			opProb.push_back(empty);
			break;
		}
		for(uint i = 0; i < nOps; i++) {
			operators[gen][i]->state_ = state;
			operators[gen][i]->initialize(state);
		}
		// calculate cumulative operator probabilities
		std::vector<double> probs(nOps);
		probs[0] = operators[gen][0]->probability_;
		for(uint i = 1; i < nOps; i++) {
			probs[i] = probs[i - 1] + operators[gen][i]->probability_;
		}
		if(probs[nOps - 1] == 0) {
			std::vector<double> none(1);
			none[0] = -1;
			opProb.push_back(none);
		} else {
			if(probs[nOps - 1] != 1) {
				double normal = probs[nOps - 1];
				ECF_LOG_ERROR(state, "Warning: " + operators[gen][0]->myGenotype_->getName() +
					" mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) + ")");
				for(uint i = 0; i < probs.size(); i++)
					probs[i] /= normal;
			}
			opProb.push_back(probs);
		}
	}
	return true;
}
Exemple #12
0
void StatCalc::registerParameters(StateP state)
{
	state->getRegistry()->registerEntry("stats.file", (voidP) (new std::string("")), ECF::STRING);
}
bool ModularRobotEvalOp::initialize(StateP state)
{
    //-- Get the robot values from the registry:
    //-----------------------------------------------------------------------------------------
    //-- Number of modules:
    voidP sptr = state->getRegistry()->getEntry("robot.modules");
    n_modules = *((uint*) sptr.get() );
    std::cout << "[Evolve] Info: Loaded \"robot.modules\"="<< n_modules << std::endl;

    //-- Max runtime:
    sptr = state->getRegistry()->getEntry("robot.runtime");
    max_runtime =(unsigned long) *((uint*) sptr.get() );
    std::cout << "[Evolve] Info: Loaded \"robot.runtime\"="<< max_runtime << std::endl;

    //-- Time step:
    sptr = state->getRegistry()->getEntry("robot.timestep");
    timestep = *((float*) sptr.get() );
    std::cout << "[Evolve] Info: Loaded \"robot.timestep\"="<< timestep << std::endl;

    //-- Gait table file:
    sptr = state->getRegistry()->getEntry("robot.configfile");
    config_file = *((std::string*) sptr.get() );
    std::cout << "[Evolve] Info: Loaded \"robot.configfile\"="<< config_file << std::endl;

    //-- Get the oscillator parameters from the registry:
    //---------------------------------------------------------------------------------------
    //-- Max amplitude:
    sptr = state->getRegistry()->getEntry("osc.maxamplitude");
    int max_amplitude = *((uint*) sptr.get());
    max_amp_0_5 = max_amplitude / 2.0;
    std::cout << "[Evolve] Info: Loaded \"osc.maxamplitude\"="<< max_amplitude << std::endl;

    //-- Max offset:
    sptr = state->getRegistry()->getEntry("osc.maxoffset");
    max_offset = *((uint*) sptr.get());
    std::cout << "[Evolve] Info: Loaded \"osc.maxoffset\"="<< max_offset << std::endl;

    //-- Max phase:
    sptr = state->getRegistry()->getEntry("osc.maxphase");
    int max_phase = *((uint*) sptr.get());
    max_pha_0_5 = max_phase / 2.0;
    std::cout << "[Evolve] Info: Loaded \"osc.maxphase\"="<< max_phase << std::endl;

    //-- Max frequency:
    sptr = state->getRegistry()->getEntry("osc.maxfrequency");
    float max_frequency = *((float*) sptr.get());
    max_freq_0_5 = max_frequency / 2.0;
    std::cout << "[Evolve] Info: Loaded \"osc.maxfrequency\"="<< max_frequency << std::endl;

    //-- Create and configure the robot parts
    //---------------------------------------------------------------------------------------
    //-- Read test data
    if ( configParser.parse(config_file) != 0)
    {
        std::cerr << "[Evolve] Error: error parsing xml config file!" << std::endl;
        return false;
    }

    if ( configParser.getNumModules() != n_modules )
    {
        std::cerr << "[Evolve] Error: number of modules not consitent between config file and "
                  << "robot model." << std::endl;
        return false;
    }
    //-- Create robot, simulated type
    robotInterface = createModularRobotInterface( "simulated", configParser);

    //-- Create sinusoidal oscillators with the test parameters
    oscillators.clear();
    for ( int i = 0; i < configParser.getNumModules(); i++)
        oscillators.push_back(new SinusoidalOscillator( 0, 0, 0, 4000));

    robotInterface->reset();

    return true;
}
bool CgdaPaintFitnessFunction::initialize(StateP state) {

    voidP sptr = state->getRegistry()->getEntry("function"); // get parameter value

	return true;
}
void CgdaPaintFitnessFunction::registerParameters(StateP state) {
	state->getRegistry()->registerEntry("function", (voidP) (new uint(1)), ECF::UINT);
}
bool PSOInheritance::initialize(StateP state)
{
	// initialize all operators
	selBestOp->initialize(state);

	voidP weightType = getParameterValue(state, "weightType");
	m_weightType = *((InertiaWeightType*) weightType.get());

	voidP weight = getParameterValue(state, "weight");
	m_weight = *((double*) weight.get());

	voidP maxV = getParameterValue(state, "maxVelocity");
	m_maxV = *((double*) maxV.get());

	// test if inertia weight type is time variant and if so, check if max iterations specified
	if(m_weightType == TIME_VARIANT) {
		if(state->getRegistry()->isModified("term.maxgen")) {
			// read maxgen parameter
			m_maxIter = *(boost::static_pointer_cast<int>( state->getRegistry()->getEntry("term.maxgen") ));
		}
		else {
			ECF_LOG_ERROR(state, "Error: term.maxgen has to be specified in order to use time variant inertia eight in PSO algorithm");
			throw("");
		}
	}

	// algorithm accepts a single FloatingPoint Genotype
	FloatingPointP flp (new FloatingPoint::FloatingPoint);
	if(state->getGenotypes()[0]->getName() != flp->getName()) {
		ECF_LOG_ERROR(state, "Error: PSO algorithm accepts only a single FloatingPoint genotype!");
		throw ("");
	}

	voidP sptr = state->getGenotypes()[0]->getParameterValue(state, "dimension");
	uint numDimension = *((uint*) sptr.get());

	voidP bounded = getParameterValue(state, "bounded");
	bounded_ = *((bool*) bounded.get());

	sptr = state->getGenotypes()[0]->getParameterValue(state, "lbound");
	lbound_ = *((double*) sptr.get());

	sptr = state->getGenotypes()[0]->getParameterValue(state, "ubound");
	ubound_ = *((double*) sptr.get());

	// batch run check
	if(areGenotypesAdded_)
		return true;

	FloatingPointP flpoint[4];
	for(uint iGen = 1; iGen < 4; iGen++) {

		flpoint[iGen] = (FloatingPointP) new FloatingPoint::FloatingPoint;
		state->setGenotype(flpoint[iGen]);

		if(iGen == 3)
			flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(1));
		else
			flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(numDimension));

		// other parameters are proprietary (ignored by the algorithm)
		flpoint[iGen]->setParameterValue(state, "lbound", (voidP) new double(0));
		flpoint[iGen]->setParameterValue(state, "ubound", (voidP) new double(1));
	}
	ECF_LOG(state, 1, "PSO algorithm: added 3 FloatingPoint genotypes (particle velocity, best-so-far postition, best-so-far fitness value)");

	// mark adding of genotypes
	areGenotypesAdded_ = true;

	return true;
}