OptimizerBase.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. exprtk::expression<double> _func_restirct_expr;
  27. std::function<json(const json&)> _callback;
  28. std::shared_ptr<std::thread> _thread_ptr;
  29. int step = -1;
  30. int _precision = 3;
  31. struct opt_state {
  32. state module_state = state::STATE_ERROR;
  33. double opt_value = 0;
  34. bool fullfill_restriction = false;
  35. };
  36. OptimizerBase::opt_state updateOutputs();
  37. OptimizerBase(const std::string& base_config = "{}");
  38. bool configureChild(const json& config) override;
  39. public:
  40. struct Permutation {
  41. public:
  42. std::vector<std::vector<double>> dna;
  43. double fitness = 0;
  44. bool accepted = false;
  45. double time = 0;
  46. state status = state::UNCHANGED;
  47. bool operator== (const Permutation& ind);
  48. static bool same(Permutation const& a, Permutation const& b);
  49. static bool smaller(Permutation const& a, Permutation const& b);
  50. };
  51. bool connect(std::shared_ptr<IModule> module) override;
  52. void updateLayout();
  53. bool setEvaluation(std::string func) override;
  54. bool setRestriction(std::string func) override;
  55. bool configure(const json& config) override;
  56. const json& getConfiguration() override;
  57. //virtual std::string getGeneratorID() = 0;
  58. void load(const json& j);
  59. json dump();
  60. json getIdentifier() override;
  61. void attachCallback(std::function<json(const json&)> callback) override;
  62. //state update() override;
  63. void callback_add_step(const std::vector<Permutation>& pers);
  64. void callback_evaluate_opt(const Permutation& per, bool sql = false);
  65. void callback_autosave();
  66. void callback_reset();
  67. ~OptimizerBase();
  68. private:
  69. bool exec_sql(const std::string& sql);
  70. json exec_sql_with_callback( const std::string& sql);
  71. json _jcall;
  72. protected:
  73. std::string _db_path;
  74. json Permutation_to_JSON(const Permutation& per);
  75. Permutation JSON_to_Permutation(const json& j);
  76. bool createDB(const std::string& db_path);
  77. bool clearDB();
  78. virtual bool loadDB(const json& j);
  79. virtual bool loadStepDB(const json& j);
  80. virtual bool loadPermutationDB(const json& j);
  81. json dumpPermutations();
  82. json dumpSteps();
  83. bool addPermutation(const json& per);
  84. bool addStep(int step, json dna);
  85. bool changePermutation(const json& per);
  86. virtual void reset();
  87. virtual bool reachAbort(const std::vector<Permutation>& pers);
  88. public:
  89. std::shared_ptr<Permutation> findPermutation(std::vector<std::vector<double>> dna);
  90. json findPermutationJSON(std::vector<std::vector<double>> dna);
  91. std::shared_ptr<Permutation> getPermutation(int step, std::vector<std::vector<double>> dna);
  92. json getPermutations() override;
  93. std::vector<Permutation> findBest(int step);
  94. int sql_callback(int argc, char** argv, char** azColName);
  95. };
  96. }