123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #pragma once
- #include "IOptimizer.h"
- #include "exprtk.hpp"
- #include <thread>
- 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<IModule> _module;
- std::vector<std::shared_ptr<IInput>> _inputs;
- std::vector<std::shared_ptr<IOutput>> _outputs;
- std::vector<double> _output_vals;
- exprtk::expression<double> _func_expr;
- std::function<json(const json&)> _callback;
- std::shared_ptr<std::thread> _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<std::vector<double>> 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<IModule> 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<json(const json&)> callback) override;
- //state update() override;
- void callback_add_step(const std::vector<Permutation>& pers);
- void callback_evaluate_opt(const opt_state& state, const Permutation& per);
- ~OptimizerBase();
- };
- }
|