OptimizerEvolutionary.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. size_t _grow_generation;
  20. size_t _converges;
  21. void evolve();
  22. void evaluate(size_t ignore_len = 0);
  23. std::vector<double> mutateGene(std::shared_ptr<IInput> input, std::vector<double> seed = std::vector<double>());
  24. bool configureChild(const json& config) override;
  25. bool loadStepDB(const json& j) override;
  26. bool loadDB(const json& j) override;
  27. void reset() override;
  28. bool reachAbort(const std::vector<Permutation>& pers) override;
  29. public:
  30. OptimizerEvolutionary();
  31. double update() override;
  32. std::vector<Permutation> getBests();
  33. double evaluateFitness(const Permutation& ind);
  34. };
  35. }