float WalkOptimisationProvider::calculateFitness()
{
	#if defined(USE_SPEED)
		return calculateFitnesses()[0];
	#elif defined(USE_COST)
		return calculateFitnesses()[1];
	#endif
}
void WalkOptimisationProvider::tickOptimiser()
{    
    #if DEBUG_BEHAVIOUR_VERBOSITY > 1
        debug << "WalkOptimisationProvider::tickOptimiser" << endl;
    #endif 
    // update the optimiser and give the next set of parameters to the walk engine
	#ifdef USE_MO
        vector<float> fitness = calculateFitnesses();
	#else
        float fitness = calculateFitness();
	#endif
    if (m_optimiser)
    {            
		m_optimiser->setParametersResult(fitness);
        vector<float> nextparameters = m_optimiser->getNextParameters();
        m_parameters.set(nextparameters);
    }
    m_jobs->addMotionJob(new WalkParametersJob(m_parameters));
    
    #if DEBUG_BEHAVIOUR_VERBOSITY > 1
			debug << "WalkOptimisationProvider::tickOptimiser() new parameters: " << m_parameters.getAsVector() << endl;
    #endif

    // save the state of the optimiser, and the walk parameters in case of hardware failure.
    if (m_optimiser)
        m_optimiser->save();
    m_iteration_count++;
}
예제 #3
0
/**
 * Selects the new generation by mating two individuals selected 
 * probabilistically by their fitness.
 * Point 3 of page 7 in paper by Mitchell.
 */
void make_new_generation() {
	int i,j,k;
    int totalFitness = calculateFitnesses();
	rule_t* next_population = populations[1-p_buf];
    
    if (generation % GENERATION_PRINT_SEQUENCE == 0) {
        printGeneration();
    }
    
    // Create new generation
    for (i=0; i<POPULATION_SIZE; i++) {
        
        int index1 = 0, index2 = 0;
        int map1 = (int) random_max(totalFitness);
        int map2 = (int) random_max(totalFitness);
        bool done1 = false;
        bool done2 = false;
 
        // Find individuals to mate
        for (j=0, k=0; j < POPULATION_SIZE; j++) {
            if (done1 && done2) break;
            
            k += population[j].fitness;
            if (!done1 && k >= map1) {
                index1 = j;
                done1 = true;
            }
            if (!done2 && k >= map2) {
                index2 = j;
                done2 = true;
            }
        }
		rule_mate(population+index1, population+index2, next_population+i, next_population+i+1);
        i++;
    }

	p_buf = 1-p_buf;
	population = next_population;
}