|
@@ -12,26 +12,7 @@ namespace mdd {
|
|
|
_inputs[j]->setValue() = it->dna[j];
|
|
|
}
|
|
|
auto opt = updateOutputs();
|
|
|
- json ret;
|
|
|
- ret["generation"] = generation;
|
|
|
- json jind;
|
|
|
- jind["fitness"] = it->fitness;
|
|
|
- jind["dna"] = it->dna;
|
|
|
- ret["individual"] = jind;
|
|
|
- if (opt.module_state == state::STATE_ERROR)
|
|
|
- {
|
|
|
- ret["state"] = "error";
|
|
|
- }
|
|
|
- else {
|
|
|
- ret["state"] = "ok";
|
|
|
- }
|
|
|
- ret["processor"] = _module->dump();
|
|
|
- json jcall;
|
|
|
- jcall["operation"] = "change";
|
|
|
- jcall["args"]["subject"] = getIdentifier();
|
|
|
- jcall["args"]["object"] = ret;
|
|
|
- _callback(jcall);
|
|
|
-
|
|
|
+ callback_evaluate_opt(opt, *it);
|
|
|
if (opt.module_state == state::STATE_ERROR) {
|
|
|
_children.erase(it);
|
|
|
}
|
|
@@ -66,20 +47,20 @@ namespace mdd {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- json ret;
|
|
|
- ret["generation"] = generation;
|
|
|
- for (auto& ind : _bests)
|
|
|
- {
|
|
|
- json jind;
|
|
|
- jind["fitness"] = ind.fitness;
|
|
|
- jind["dna"] = ind.dna;
|
|
|
- ret["bests"].push_back(jind);
|
|
|
- }
|
|
|
- json jcall;
|
|
|
- jcall["operation"] = "add";
|
|
|
- jcall["args"]["subject"] = getIdentifier();
|
|
|
- jcall["args"]["object"] = ret;
|
|
|
- _callback(jcall);
|
|
|
+ // json ret;
|
|
|
+ // ret["step"] = step;
|
|
|
+ // for (auto& ind : _bests)
|
|
|
+ // {
|
|
|
+ // json jind;
|
|
|
+ // jind["fitness"] = ind.fitness;
|
|
|
+ // jind["dna"] = ind.dna;
|
|
|
+ // ret["bests"].push_back(jind);
|
|
|
+ // }
|
|
|
+ // json jcall;
|
|
|
+ // jcall["operation"] = "add";
|
|
|
+ // jcall["args"]["subject"] = getIdentifier();
|
|
|
+ // jcall["args"]["object"] = ret;
|
|
|
+ // _callback(jcall);
|
|
|
}
|
|
|
|
|
|
OptimizerEvolutionary::OptimizerEvolutionary()
|
|
@@ -182,52 +163,34 @@ namespace mdd {
|
|
|
|
|
|
double OptimizerEvolutionary::update()
|
|
|
{
|
|
|
- generation = -1;
|
|
|
+ step = -1;
|
|
|
bool check;
|
|
|
- Individual old_best;
|
|
|
+ Permutation old_best;
|
|
|
size_t same_counter = 0;
|
|
|
if (_inputs.size() == 0)
|
|
|
{
|
|
|
std::cout << "ERROR: No optimizable inputs detected!" << std::endl;
|
|
|
return -1.0;
|
|
|
}
|
|
|
+ _children.clear();
|
|
|
+ _bests.clear();
|
|
|
do
|
|
|
{
|
|
|
- std::cout << _children.size() << " | " << generation << std::endl;
|
|
|
+ ++step;
|
|
|
+ std::cout << _children.size() << " | " << step << std::endl;
|
|
|
if (_children.empty())
|
|
|
{
|
|
|
for (size_t i = 0; i < _grow_generation*2; i++)
|
|
|
{
|
|
|
- _children.push_back(generateIndividual());
|
|
|
+ _children.push_back(generatePermutation());
|
|
|
}
|
|
|
- json ret;
|
|
|
- ret["generation"] = generation;
|
|
|
- for (auto& ind : _children)
|
|
|
- {
|
|
|
- json jind;
|
|
|
- jind["fitness"] = ind.fitness;
|
|
|
- jind["dna"] = ind.dna;
|
|
|
- ret["individuals"] = jind;
|
|
|
- }
|
|
|
- json jcall;
|
|
|
- jcall["operation"] = "add";
|
|
|
- jcall["args"]["subject"] = getIdentifier();
|
|
|
- jcall["args"]["object"] = ret;
|
|
|
- _callback(jcall);
|
|
|
+ callback_add_step(_children);
|
|
|
evaluate();
|
|
|
}
|
|
|
else {
|
|
|
- if (generation != -1)
|
|
|
- {
|
|
|
- evolve(_children);
|
|
|
- }
|
|
|
- else {
|
|
|
- evaluate();
|
|
|
- }
|
|
|
+ evolve(_children);
|
|
|
}
|
|
|
-
|
|
|
- ++generation;
|
|
|
- check = generation < _min_generations || _bests[0].fitness > _max_fitness;
|
|
|
+ check = step < _min_generations || _bests[0].fitness > _max_fitness;
|
|
|
if (!check && _converges > 0)
|
|
|
{
|
|
|
bool found = false;
|
|
@@ -344,7 +307,7 @@ namespace mdd {
|
|
|
return input->getValue();//ERROR
|
|
|
}
|
|
|
|
|
|
- void OptimizerEvolutionary::evolve(std::vector<Individual> parents)
|
|
|
+ void OptimizerEvolutionary::evolve(std::vector<Permutation> parents)
|
|
|
{
|
|
|
////fittest should breed more
|
|
|
//detect lower border
|
|
@@ -366,7 +329,7 @@ namespace mdd {
|
|
|
sum = 1.0;
|
|
|
}
|
|
|
//multiply fitter parents
|
|
|
- std::vector<Individual> gen_pool = parents;
|
|
|
+ std::vector<Permutation> gen_pool = parents;
|
|
|
for (size_t i = 0; i < parents.size(); i++)
|
|
|
{
|
|
|
int additional = std::round((max - parents[i].fitness) / sum * 20);
|
|
@@ -387,27 +350,14 @@ namespace mdd {
|
|
|
int p2 = (int)random_num(0, gen_pool.size() - 1);
|
|
|
_children.push_back(combine(gen_pool[p1], gen_pool[p2]));
|
|
|
}
|
|
|
- json ret;
|
|
|
- ret["generation"] = generation;
|
|
|
- for (auto& ind : _children)
|
|
|
- {
|
|
|
- json jind;
|
|
|
- jind["fitness"] = ind.fitness;
|
|
|
- jind["dna"] = ind.dna;
|
|
|
- ret["individuals"] = jind;
|
|
|
- }
|
|
|
- json jcall;
|
|
|
- jcall["operation"] = "add";
|
|
|
- jcall["args"]["subject"] = getIdentifier();
|
|
|
- jcall["args"]["object"] = ret;
|
|
|
- _callback(jcall);
|
|
|
+ callback_add_step(_children);
|
|
|
evaluate(init_len);
|
|
|
}
|
|
|
|
|
|
- OptimizerEvolutionary::Individual OptimizerEvolutionary::combine(Individual par1, Individual par2)
|
|
|
+ OptimizerEvolutionary::Permutation OptimizerEvolutionary::combine(Permutation par1, Permutation par2)
|
|
|
{
|
|
|
size_t len = par1.dna.size();
|
|
|
- Individual child;
|
|
|
+ Permutation child;
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
{
|
|
|
double p = random_num(0.0, 100.0, 1.0);
|
|
@@ -426,9 +376,9 @@ namespace mdd {
|
|
|
return child;
|
|
|
}
|
|
|
|
|
|
- OptimizerEvolutionary::Individual OptimizerEvolutionary::generateIndividual()
|
|
|
+ OptimizerEvolutionary::Permutation OptimizerEvolutionary::generatePermutation()
|
|
|
{
|
|
|
- Individual ret;
|
|
|
+ Permutation ret;
|
|
|
for (size_t i = 0; i < _inputs.size(); i++)
|
|
|
{
|
|
|
ret.dna.push_back(mutateGene(_inputs[i]));
|
|
@@ -436,11 +386,11 @@ namespace mdd {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- std::vector<OptimizerEvolutionary::Individual> OptimizerEvolutionary::getBests() {
|
|
|
+ std::vector<OptimizerEvolutionary::Permutation> OptimizerEvolutionary::getBests() {
|
|
|
return _bests;
|
|
|
}
|
|
|
|
|
|
- double OptimizerEvolutionary::evaluateFitness(const Individual& ind) {
|
|
|
+ double OptimizerEvolutionary::evaluateFitness(const Permutation& ind) {
|
|
|
for (size_t j = 0; j < _inputs.size(); j++)
|
|
|
{
|
|
|
_inputs[j]->setValue() = ind.dna[j];
|