OptimizerBase.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include "IOptimizer.h"
  3. #include "exprtk.hpp"
  4. #include <thread>
  5. #include <sqlite3.h>
  6. #include <mutex>
  7. namespace mdd {
  8. ////////////////////////////////////////////////////////////
  9. //
  10. //Optimization is always minimization!
  11. //
  12. ////////////////////////////////////////////////////////////
  13. class OptimizerBase
  14. : public IOptimizer
  15. {
  16. protected:
  17. std::mutex _mutex;
  18. json _base_config;
  19. std::string key;
  20. const std::string type = "optimizer";
  21. std::shared_ptr<IModule> _module;
  22. std::vector<std::shared_ptr<IInput>> _inputs;
  23. std::vector<std::shared_ptr<IOutput>> _outputs;
  24. std::vector<double> _output_vals;
  25. exprtk::expression<double> _func_expr;
  26. std::function<json(const json&)> _callback;
  27. std::shared_ptr<std::thread> _thread_ptr;
  28. int step = -1;
  29. struct opt_state {
  30. state module_state = state::STATE_ERROR;
  31. double opt_value = 0;
  32. };
  33. opt_state updateOutputs();
  34. OptimizerBase(const std::string& base_config = "{}");
  35. bool configureChild(const json& config) override;
  36. public:
  37. struct Permutation {
  38. public:
  39. std::vector<std::vector<double>> dna;
  40. double fitness = 0;
  41. double time = 0;
  42. state status = state::UNCHANGED;
  43. bool operator== (const Permutation& ind);
  44. static bool same(Permutation const& a, Permutation const& b);
  45. static bool smaller(Permutation const& a, Permutation const& b);
  46. };
  47. bool connect(std::shared_ptr<IModule> module) override;
  48. void updateLayout();
  49. bool setEvaluation(std::string func) override;
  50. bool configure(const json& config) override;
  51. const json& getConfiguration() override;
  52. //virtual std::string getGeneratorID() = 0;
  53. void load(const json& j);
  54. json dump();
  55. json getIdentifier() override;
  56. void attachCallback(std::function<json(const json&)> callback) override;
  57. //state update() override;
  58. void callback_add_step(const std::vector<Permutation>& pers);
  59. void callback_evaluate_opt(const Permutation& per);
  60. void callback_reset();
  61. ~OptimizerBase();
  62. private:
  63. bool exec_sql(const std::string& sql);
  64. json exec_sql_with_callback( const std::string& sql);
  65. json _jcall;
  66. protected:
  67. std::string _db_path;
  68. json Permutation_to_JSON(const Permutation& per);
  69. Permutation JSON_to_Permutation(const json& j);
  70. bool createDB(const std::string& db_path);
  71. bool clearDB();
  72. virtual bool loadDB(const json& j);
  73. virtual bool loadStepDB(const json& j);
  74. virtual bool loadPermutationDB(const json& j);
  75. json dumpPermutations();
  76. json dumpSteps();
  77. bool addPermutation(const json& per);
  78. bool addStep(int step, json dna);
  79. bool changePermutation(const json& per);
  80. virtual void reset();
  81. virtual bool reachAbort(const std::vector<Permutation>& pers);
  82. public:
  83. std::shared_ptr<Permutation> findPermutation(std::vector<std::vector<double>> dna);
  84. std::shared_ptr<Permutation> getPermutation(int step, std::vector<std::vector<double>> dna);
  85. json getPermutations() override;
  86. std::vector<Permutation> findBest(int step);
  87. int sql_callback(int argc, char** argv, char** azColName);
  88. };
  89. }