123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include "OptimizerBase.h"
- #include <iostream>
- namespace mdd{
- bool OptimizerBase::connect(std::shared_ptr<IModule> module) {
- _module = module;
- return true;
- }
- OptimizerBase::opt_state OptimizerBase::updateOutputs()
- {
- opt_state ret;
- ret.module_state = _module->update();
- if (ret.module_state != state::STATE_ERROR)
- {
- for (size_t i = 0; i < _output_vals.size(); ++i)
- {
- _output_vals[i] = _outputs[i]->getValue()[0];
- //std::cout << "get: " << i << ": " << _output_vals[i] << std::endl;
- }
- ret.opt_value = _func_expr.value();
- //std::cout << "get: " << "opt" << ": " << ret.opt_value << std::endl;
- }
-
- return ret;
- }
- OptimizerBase::OptimizerBase(const std::string& base_config) {
- _base_config = base_config;
- }
- void OptimizerBase::updateLayout()
- {
- _inputs = _module->getOptimizableInputs();
- _outputs = _module->getOptimizableOutputs();
- _output_vals.clear();
- if (_inputs.empty())
- {
- std::cout << "ERROR: No optimizable inputs detected!" << std::endl;
- }
- if (_outputs.empty())
- {
- std::cout << "ERROR: No optimizable outputs detected!" << std::endl;
- }
- for (auto& out : _outputs)
- {
- _output_vals.push_back(out->getValue()[0]);
- }
- }
-
- bool OptimizerBase::setEvaluation(std::string func)
- {
- updateLayout();
- exprtk::symbol_table<double> symbol_table;
- for (size_t i = 0; i < _output_vals.size(); ++i)
- {
- symbol_table.add_variable("out" + std::to_string(i), _output_vals[i]);
- }
- symbol_table.add_constants();
-
- _func_expr.register_symbol_table(symbol_table);
-
- exprtk::parser<double> parser;
- return parser.compile(func, _func_expr);
- }
-
- bool OptimizerBase::configure(const json& config) {
- json jconfig = config;
- auto jit = jconfig.find("configure");
- if (jit != jconfig.end())
- {
- return configureChild(jit.value());
- }
- return false;
- }
- bool OptimizerBase::configureChild(const json& config) {
- return true;
- }
- const json& OptimizerBase::getConfiguration() {
- return _base_config;
- }
- //virtual std::string getGeneratorID() = 0;
- void OptimizerBase::load(const json& j) {
- if (j.contains("configure"))
- {
- configure(j["configure"].get<std::string>());
- }
- }
- json OptimizerBase::dump() {
- json ret;
- ret["configure"] = _base_config;
- return ret;
- }
- json OptimizerBase::getIdentifier() {
- json jID;
- /*
- jID["name"] = _name;
- jID["id"] = getAppendix();
- jID["type"] = type;
- jID["key"] = key;*/
- jID["prefix"] = std::vector<std::string>();
- return jID;
- }
- }
|