OptimizerBase.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include "IOptimizer.h"
  3. #include "exprtk.hpp"
  4. #include <thread>
  5. namespace mdd {
  6. ////////////////////////////////////////////////////////////
  7. //
  8. //Optimization is always minimization!
  9. //
  10. ////////////////////////////////////////////////////////////
  11. class OptimizerBase
  12. : public IOptimizer
  13. {
  14. protected:
  15. json _base_config;
  16. std::string key;
  17. const std::string type = "optimizer";
  18. std::shared_ptr<IModule> _module;
  19. std::vector<std::shared_ptr<IInput>> _inputs;
  20. std::vector<std::shared_ptr<IOutput>> _outputs;
  21. std::vector<double> _output_vals;
  22. exprtk::expression<double> _func_expr;
  23. std::function<json(const json&)> _callback;
  24. std::shared_ptr<std::thread> _thread_ptr;
  25. int step = -1;
  26. struct opt_state {
  27. state module_state = state::STATE_ERROR;
  28. double opt_value = 0;
  29. };
  30. opt_state updateOutputs();
  31. OptimizerBase(const std::string& base_config = "{}");
  32. bool configureChild(const json& config) override;
  33. public:
  34. struct Permutation {
  35. public:
  36. std::vector<std::vector<double>> dna;
  37. double fitness = 0;
  38. bool operator== (const Permutation& ind) {
  39. return (dna == ind.dna);
  40. };
  41. };
  42. bool connect(std::shared_ptr<IModule> module) override;
  43. void updateLayout();
  44. bool setEvaluation(std::string func) override;
  45. bool configure(const json& config) override;
  46. const json& getConfiguration() override;
  47. //virtual std::string getGeneratorID() = 0;
  48. void load(const json& j);
  49. json dump();
  50. json getIdentifier() override;
  51. void attachCallback(std::function<json(const json&)> callback) override;
  52. //state update() override;
  53. void callback_add_step(const std::vector<Permutation>& pers);
  54. void callback_evaluate_opt(const opt_state& state, const Permutation& per);
  55. ~OptimizerBase();
  56. };
  57. }