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;
}
void evolve(Population& population)
{
	Population newGeneration;
	while (newGeneration < population) {
		Chromosome chromosome1 = select(population).getChromosome();
		Chromosome chromosome2 = select(population).getChromosome();
		crossover(chromosome1, chromosome2);
		mutate(chromosome1, chromosome2);

		Individual newInd1;
		newInd1.setChromosome(chromosome1);
		newInd1.setFitness(evaluate(chromosome1));
		newGeneration.addIndividual(newInd1);
		if (newGeneration < population) {
			Individual newInd2;
			newInd2.setChromosome(chromosome2);
			newInd2.setFitness(evaluate(chromosome2));
			newGeneration.addIndividual(newInd2);
		}
	}
	population = newGeneration;
}
//essentially an update
void Evolve::evolve(Population &pop)
{
    typedef std::pair<MEMBER*, SCORE>  data_pair;
    
    auto sort = findBest(pop);
    
    std::vector<data_pair>fin_sort(sort.begin(), sort.end() );
    std::sort(fin_sort.begin(), fin_sort.end(), [](const data_pair& that, const data_pair& thus)
              {
                  return that.second < thus.second;
              }
              );
    
    //find two best and copy them temporarily
    auto end_it = fin_sort.rbegin();
    MEMBER first = std::move(*end_it->first);
    end_it++;
    MEMBER second = std::move(*end_it->first);
    
    //checks best
    if(sort.rbegin()->second > best.first)
    {
        best = {sort.rbegin()->second, *first};
    }
    
    int START = 1;
    
    //adds to mode
    decltype(mode)::value_type data = {START, {first->getLemons(), first->getSugar(), first->getPrice()} };
    int val = DATA_FIND(mode, data);
    if(val != 0)
    {
        //adds one to mode if value is already placed
        decltype(data) newdata = {val, data.second};
        int pos = std::find(mode.begin(), mode.end(), decltype(newdata){newdata}) - mode.begin();
        mode.at(pos).first++;
    }
    else
    {
        mode.push_back(data);
    }
    
    //debug
    std::cout << first->getLemons() << std::endl;
    std::cout << first->getSugar() << std::endl;
    std::cout << first->getPrice() << std::endl;
    std::cout << "Profit assuming 100 chances: " << sort.rbegin()->second << std::endl;
    
    
    
    //clear the old vector
    clear(pop);
    
    //generate rest to fill up population size
    for(int i = 0; i < POP_SIZE - 2; i++)
    {
        pop.addIndividual(generate(*first, *second) );
    }
    
    //add in parents
    pop.addIndividual(std::move(first) );
    pop.addIndividual(std::move(second) );
}