OptimizerEvolutionary.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include "OptimizerBase.h"
  3. #include <iostream>
  4. namespace mdd {
  5. class OptimizerEvolutionary : public OptimizerBase {
  6. protected:
  7. static int random_num(double min, double max, double inc = 1.0)
  8. {
  9. int range = (max - min) / inc + 1;
  10. return min + inc * (std::rand() % range);
  11. }
  12. Permutation generatePermutation();
  13. Permutation combine(Permutation par1, Permutation par2, int try_counter = 0);
  14. std::vector<Permutation> _children;
  15. std::vector<Permutation> _bests;
  16. size_t _same_counter = 0;
  17. Permutation _old_best;
  18. int _min_generations;
  19. double _max_fitness;
  20. size_t _grow_generation;
  21. size_t _converges;
  22. double _precision;
  23. void evolve();
  24. void evaluate(size_t ignore_len = 0);
  25. std::vector<double> mutateGene(std::shared_ptr<IInput> input, std::vector<double> seed = std::vector<double>());
  26. bool configureChild(const json& config) override;
  27. bool loadStepDB(const json& j) override;
  28. bool loadDB(const json& j) override;
  29. void reset() override;
  30. bool reachAbort(const std::vector<Permutation>& pers) override;
  31. public:
  32. OptimizerEvolutionary();
  33. double update() override;
  34. std::vector<Permutation> getBests();
  35. double evaluateFitness(const Permutation& ind);
  36. };
  37. }