int main()
{
	srand(time(NULL));
	Population population;

	for (int i = 0; i < POPULATION_COUNT; ++i) {
		Individual ind;
		Chromosome chromosome(targetString.size());
		chromosome.randomize();
		float fitness = evaluate(chromosome);
		ind.setChromosome(chromosome);
		ind.setFitness(fitness);

		population.addIndividual(ind);
	}

	population.sortPopulation();


	std::string solution = "";
	Individual bestInd;

	int generationCount = 0;

	int nth = 64;
	for (int i = 0; i < GENERATION_COUNT; ++i) {
		generationCount++;

		evolve(population);
		population.sortPopulation();
		if (bestInd < population[0]) {
			bestInd = population[0];
		}

		if (i % nth == 0) {
			std::cout << "Generation: " << generationCount << std::endl;
			std::cout << "Best individual: " << bestInd << std::endl;
		}

		if (bestInd.getFitness() >= 1){
			break;
		}
	}

	std::cout << std::endl;

	std::cout << "Solved on generation " << generationCount << '!' << std::endl;

	std::cout << bestInd << std::endl << std::endl;
	std::cout << "Press enter to exit";
	

	std::cin.get();
	return 0;
}
예제 #2
0
void eStrategy::initialize() {
    for(size_t indiv = 0; indiv < CONF_SIZE_POP; indiv++) {
        chromosome::data_block genes, sigmas;
        for(size_t gene = 0; gene < CONF_DIM; gene++) {
            sigmas.push_back(frand());
            genes.push_back(frand() * (CONF_BOUND_UPPER - CONF_BOUND_LOWER) + CONF_BOUND_LOWER);
        }
        auto c = chromosome(genes, sigmas);
        c.set_fitness(this->eval(c));
        this->parent_pop.push_back(c);
    }
    this->sort_population();
}
예제 #3
0
chromosome eStrategy::xover_and_mut(const chromosome* const p1, const chromosome* const p2) const {
    p1->validate(), p2->validate();
    assert(p1->sigmas.size() == p2->sigmas.size() && p1->sigmas.size() == CONF_DIM);
    double prob = frand();
    auto sp = p1;
    if(prob > 0.5) sp = p2;
    chromosome::data_block new_sigmas, new_genes;
    if(prob < CONF_PROB_MUT) {
        double step = exp(PARAM_TAW_PRIME * normrnd() + CONST_STEP_GLOB);
        auto xd = normrnd();
        for(size_t index = 0; index < sp->sigmas.size(); index++) {
            auto s = sp->sigmas.at(index) * step;
            if(s < CONF_RANGE_SIGMA && s > -CONF_RANGE_SIGMA)
                s = s / abs(s) * CONF_RANGE_SIGMA;
            new_sigmas.push_back(s);
            auto g = sp->genes.at(index) + s * xd;
            if(g > CONF_BOUND_UPPER) g = CONF_BOUND_UPPER;
            else if(g < CONF_BOUND_LOWER) g = CONF_BOUND_LOWER;
            new_genes.push_back(g);
        }
        assert(new_genes.size() == new_sigmas.size());
    } else if((1 - prob) < CONF_PROB_XOVER) {
        double rp = frand(), rq = frand();
        for(size_t index = 0; index < sp->genes.size(); index++) {
            auto g = (p1->genes.at(index) * rp + p2->genes.at(index) * rq);
            if(g > CONF_BOUND_UPPER) g = CONF_BOUND_UPPER;
            else if(g < CONF_BOUND_LOWER) g = CONF_BOUND_LOWER;
            new_genes.push_back(g);
            if(frand() < 0.5) new_sigmas.push_back(p1->sigmas.at(index));
            else new_sigmas.push_back(p2->sigmas.at(index));
        }
    }
    auto c = chromosome(new_genes, new_sigmas);
    if(this->eval(c) < sp->get_fitness())
        return c;
    return chromosome(*sp);
}
예제 #4
0
파일: reader.cpp 프로젝트: ablab/mgra-old
void reader::read_infercars(const ProblemInstance& cfg, std::vector<Genome>& genome) {
	
	std::ifstream input(cfg.get_blk_file().c_str());
	if(!input) {
		std::cerr << "Unable to open " << cfg.get_blk_file() << std::endl;
		exit(1);
	}
    
	std::string gene;
	std::vector<std::string> chromosome(cfg.get_count_genomes());
	std::vector<std::string> sign(cfg.get_count_genomes()); 
	std::vector<int> start_block(cfg.get_count_genomes());  
	std::vector<int> end_block(cfg.get_count_genomes()); 
	std::vector<int> count_block(cfg.get_count_genomes());

	while(!input.eof()) {
		std::string line;
		std::getline(input, line);
    		line = reader::trim(line);
 
		if (line.empty()) {
			if (gene.empty()) { 	
				continue;
    			} 

			bool ambig = false;
			for(int i = 0; i < count_block.size(); ++i) { 
				if (count_block[i] != 1) { 
					ambig = true;
					break;
				}  
			} 	
			if (ambig) {
				std::clog << "Ambiguous block: " << gene << std::endl;
			    	continue;
			}

			for(int i = 0; i < count_block.size(); ++i) { 
				if (count_block[i] == 1) {
					int sign_ = (sign[i] == "+")? +1: -1;
					genome[i].insert(gene, chromosome[i], (start_block[i] + end_block[i]) / 2, sign_, std::min(start_block[i], end_block[i]), std::max(start_block[i], end_block[i]));         				
				}
			} 
			gene.clear();
		} else if (line[0] == '#') { 
				continue;
    		} else if(line[0] == '>') {
			gene = line.substr(1);
			std::fill(count_block.begin(), count_block.end(), 0);
		} else { 
    			line[line.find(".")] = ' ';
			line[line.find(":")] = ' ';
			line[line.find("-")] = ' ';
	    
			std::istringstream istr(line);
 
			std::string genome_name;
 			istr >> genome_name;
    			if (!cfg.member_name(genome_name)) {
				std::cerr << "Unknown genome name: " << genome_name << std::endl;
				continue;
    			}

			size_t k = cfg.get_number(genome_name);
			istr >> chromosome[k] >> start_block[k] >> end_block[k] >> sign[k];
			++count_block[k];
		}
	} 
	input.close();
}