#pragma once #include "IOptimizer.h" #include "exprtk.hpp" #include namespace mdd { //////////////////////////////////////////////////////////// // //Optimization is always minimization! // //////////////////////////////////////////////////////////// class OptimizerBase : public IOptimizer { protected: json _base_config; std::string key; const std::string type = "optimizer"; std::shared_ptr _module; std::vector> _inputs; std::vector> _outputs; std::vector _output_vals; exprtk::expression _func_expr; std::function _callback; std::shared_ptr _thread_ptr; int step = -1; struct opt_state { state module_state = state::STATE_ERROR; double opt_value = 0; }; opt_state updateOutputs(); OptimizerBase(const std::string& base_config = "{}"); bool configureChild(const json& config) override; public: struct Permutation { public: std::vector> dna; double fitness = 0; bool operator== (const Permutation& ind) { return (dna == ind.dna); }; static bool same(Permutation const& a, Permutation const& b) { bool check1 = (a.dna == b.dna); bool check2 = true; if (a.dna.size() != b.dna.size()) { check2 = false; } else { for (size_t i = 0; i < a.dna.size(); i++) { if (a.dna[i].size() != b.dna[i].size()) { check2 = false; } else { for (size_t j = 0; j < a.dna[i].size(); j++) { if (a.dna[i][j] != b.dna[i][j]) { check2 = false; } } } } } if (check1 != check2) { int test = 0; } return check1; } static bool smaller(Permutation const& a, Permutation const& b) { return (a.dna < b.dna); } }; bool connect(std::shared_ptr module) override; void updateLayout(); bool setEvaluation(std::string func) override; bool configure(const json& config) override; const json& getConfiguration() override; //virtual std::string getGeneratorID() = 0; void load(const json& j); json dump(); json getIdentifier() override; void attachCallback(std::function callback) override; //state update() override; void callback_add_step(const std::vector& pers); void callback_evaluate_opt(const opt_state& state, const Permutation& per); ~OptimizerBase(); }; }