double *linear_fit(dataset *dat, fit_conf *conf) {
	double *fit, *fit_act;
	double chng = 1;
	double chng_o = 0;
	int k, l;

	fit = malloc(9 * sizeof(double));
	if (!fit)
		return NULL;

	if (!(fit_act = init_fit(dat, conf)))
		return NULL;
	for (k = 0;
			(k < conf->max_iter) && (chng >= conf->min_chng)
					&& (fabs(chng_o - chng) > 1e-20); ++k) {
		for (l = 0; l < 5; ++l)
			fit[l] = fit_act[l];
		free(fit_act);
		fit_act = m_fit(dat, conf, fit[0], fit[2]);
		if (fit_act != NULL) {
			chng_o = chng;
			chng = fabs(fit[0] - fit_act[0]);
		} else {
			fit_act = fit;
			fit_act[5] = 4;
			/*printf("Speicher konnte nicht zugewiesen werden,\n");*/
			break;
		}
	}
	if (k == conf->max_iter) {
		/*printf("Maximale Anzahl Iterationen erreicht.\n");*/
		fit_act[5] = 1;
	} else if (chng < conf->min_chng) {
		/*printf("Aenderung wurde zu klein.\n");*/
		fit_act[5] = 2;

	} else if (fabs(chng_o - chng) < 1e-20) {
		/*printf("Daten eingependelt.\n");*/
		fit_act[5] = 3;
	}
	/* 4 = speicher konnte nicht zugewiesen werden, iteration musste abgebrochen werden*/

	/*Rueckgabe der Anzahl an Iterationen*/
	fit_act[6] = k;
	/*Rueckgabe der letzten Aenderung*/
	fit_act[7] = chng;
	/*Berechnung des Korellationskoeffizienten */
	fit_act[4] = correlation(dat, conf);

	if (fit != fit_act)
		free(fit);
	return fit_act;
}
Esempio n. 2
0
    void simulate( ) {
        std::swap( m_child, m_parent );     // use the current child population as the parent population for the next round

        // at the start of each simulate round, m_fit has already been updated from the previous
        // round with the fitness of the "then child/now parent" popualtions fitness
        //
        size_t pN = select_gen.individual_count();
        if( m_pop_growth ) {
            pN = m_pop_growth->operator()( pN, m_generation );
        }

        size_type pM = m_mut_alloc.allocate( 2 * pN );        // generate the number of new mutations
        size_type free_count = updateFixedAlleles( m_parent );    // update the fixed alleles with those of parent population
        size_t all_size = child_max_alleles( m_allele_space.size(), free_count, pM );   // rescale allele space for child population given free space from parent population and new allele count (pM)

#ifdef DEBUGGING 
        BOOST_LOG_TRIVIAL( debug ) << "Generation " << m_generation << ": " << pN << " individuals; " << pM << " new alleles";
        BOOST_LOG_TRIVIAL( debug ) << "Free space: " << free_count << "; alleles: " << m_allele_space.size();
        BOOST_LOG_TRIVIAL( debug ) << "Rescaling child population to be: " << pN << " individuals x " << all_size << " alleles";
        std::cerr << "Rescaling child population to be: " << pN << " individuals x " << all_size << " alleles" << std::endl;
#endif // DEBUGGING

        m_child->grow( pN, all_size );                        // grow the child population accordingly

        select_gen.update( m_fit, pN );

        cross_gen( select_gen, m_parent, m_child, &m_allele_space, m_thread_pool );

        generate_child_mutations( pM );

        if( !m_allele_space.isAllNeutral() ) {
            m_pheno( m_child, &m_trait_space, m_thread_pool );
        } else {
            m_pheno.constant_phenotype( m_child, &m_trait_space );
        }
        m_fit( m_pheno.begin(), m_pheno.end() );

        m_child->finalize();

        ++m_generation;
    }