|
@@ -9,7 +9,7 @@ namespace mdd {
|
|
|
{
|
|
|
for (size_t j = 0; j < _inputs.size(); j++)
|
|
|
{
|
|
|
- std::cout << "set: " << j << ": " << it->dna[j] << std::endl;
|
|
|
+ //std::cout << "set: " << j << ": " << it->dna[j] << std::endl;
|
|
|
_inputs[j]->setDefaultValue() = it->dna[j];
|
|
|
}
|
|
|
auto opt = updateReader();
|
|
@@ -21,25 +21,29 @@ namespace mdd {
|
|
|
}
|
|
|
}
|
|
|
//find fitest
|
|
|
- double max = _children[0].fitness;
|
|
|
+ double min = _children[0].fitness;
|
|
|
for (size_t i = 1; i < _children.size(); i++)
|
|
|
{
|
|
|
- if (max < _children[i].fitness) {
|
|
|
- max = _children[i].fitness;
|
|
|
+ if (min > _children[i].fitness) {
|
|
|
+ min = _children[i].fitness;
|
|
|
_best = _children[i];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- OptimizerEvolutionary::OptimizerEvolutionary(std::shared_ptr<IModule> module, size_t grow_generation, double max_fitness, int min_generations)
|
|
|
+ OptimizerEvolutionary::OptimizerEvolutionary(std::shared_ptr<IModule> module, size_t converges, size_t grow_generation, double max_fitness, int min_generations)
|
|
|
{
|
|
|
_module = module;
|
|
|
_grow_generation = grow_generation;
|
|
|
_min_generations = min_generations;
|
|
|
_max_fitness = max_fitness;
|
|
|
+ _converges = converges;
|
|
|
}
|
|
|
json OptimizerEvolutionary::update()
|
|
|
{
|
|
|
- size_t gen = -1;
|
|
|
+ int gen = -1;
|
|
|
+ bool check;
|
|
|
+ Individual old_best;
|
|
|
+ size_t same_counter = 0;
|
|
|
do
|
|
|
{
|
|
|
if (_children.empty())
|
|
@@ -53,8 +57,26 @@ namespace mdd {
|
|
|
else {
|
|
|
evolve(_children);
|
|
|
}
|
|
|
+ //std::cout << _children.size() << " | " << gen << std::endl;
|
|
|
++gen;
|
|
|
- } while (gen < _min_generations || _best.fitness > _max_fitness);
|
|
|
+ check = gen < _min_generations || _best.fitness > _max_fitness;
|
|
|
+ if (!check && _converges > 0)
|
|
|
+ {
|
|
|
+ if (_best == old_best)
|
|
|
+ {
|
|
|
+ ++same_counter;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ old_best = _best;
|
|
|
+ same_counter = 0;
|
|
|
+ }
|
|
|
+ if (same_counter < _converges)
|
|
|
+ {
|
|
|
+ check = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } while (check);
|
|
|
|
|
|
return _best.dna;
|
|
|
}
|
|
@@ -164,7 +186,9 @@ namespace mdd {
|
|
|
std::vector<Individual> gen_pool = parents;
|
|
|
for (size_t i = 0; i < parents.size(); i++)
|
|
|
{
|
|
|
- for (size_t j = 1; j < std::round((max - parents[i].fitness)/sum*20); i++)
|
|
|
+ int additional = std::round((max - parents[i].fitness) / sum * 20);
|
|
|
+ //std::cout << additional << std::endl;
|
|
|
+ for (size_t j = 1; j < additional; j++)
|
|
|
{
|
|
|
gen_pool.push_back(parents[i]);
|
|
|
}
|