OptimizerBase.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "OptimizerBase.h"
  2. #include <iostream>
  3. namespace mdd{
  4. bool OptimizerBase::connect(std::shared_ptr<IModule> module) {
  5. _module = module;
  6. return true;
  7. }
  8. OptimizerBase::opt_state OptimizerBase::updateOutputs()
  9. {
  10. opt_state ret;
  11. ret.module_state = _module->update();
  12. if (ret.module_state != state::STATE_ERROR)
  13. {
  14. for (size_t i = 0; i < _output_vals.size(); ++i)
  15. {
  16. _output_vals[i] = _outputs[i]->getValue()[0];
  17. //std::cout << "get: " << i << ": " << _output_vals[i] << std::endl;
  18. }
  19. ret.opt_value = _func_expr.value();
  20. //std::cout << "get: " << "opt" << ": " << ret.opt_value << std::endl;
  21. }
  22. return ret;
  23. }
  24. OptimizerBase::OptimizerBase(const std::string& base_config) {
  25. _base_config = base_config;
  26. }
  27. void OptimizerBase::updateLayout()
  28. {
  29. _inputs = _module->getOptimizableInputs();
  30. _outputs = _module->getOptimizableOutputs();
  31. _output_vals.clear();
  32. if (_inputs.empty())
  33. {
  34. std::cout << "ERROR: No optimizable inputs detected!" << std::endl;
  35. }
  36. if (_outputs.empty())
  37. {
  38. std::cout << "ERROR: No optimizable outputs detected!" << std::endl;
  39. }
  40. for (auto& out : _outputs)
  41. {
  42. _output_vals.push_back(out->getValue()[0]);
  43. }
  44. }
  45. bool OptimizerBase::setEvaluation(std::string func)
  46. {
  47. updateLayout();
  48. exprtk::symbol_table<double> symbol_table;
  49. for (size_t i = 0; i < _output_vals.size(); ++i)
  50. {
  51. symbol_table.add_variable("out" + std::to_string(i), _output_vals[i]);
  52. }
  53. symbol_table.add_constants();
  54. _func_expr.register_symbol_table(symbol_table);
  55. exprtk::parser<double> parser;
  56. return parser.compile(func, _func_expr);
  57. }
  58. bool OptimizerBase::configure(const json& config) {
  59. json jconfig = config;
  60. auto jit = jconfig.find("configure");
  61. if (jit != jconfig.end())
  62. {
  63. return configureChild(jit.value());
  64. }
  65. return false;
  66. }
  67. bool OptimizerBase::configureChild(const json& config) {
  68. return true;
  69. }
  70. const json& OptimizerBase::getConfiguration() {
  71. return _base_config;
  72. }
  73. //virtual std::string getGeneratorID() = 0;
  74. void OptimizerBase::load(const json& j) {
  75. if (j.contains("configure"))
  76. {
  77. configure(j["configure"].get<std::string>());
  78. }
  79. }
  80. json OptimizerBase::dump() {
  81. json ret;
  82. ret["configure"] = _base_config;
  83. return ret;
  84. }
  85. json OptimizerBase::getIdentifier() {
  86. json jID;
  87. /*
  88. jID["name"] = _name;
  89. jID["id"] = getAppendix();
  90. jID["type"] = type;
  91. jID["key"] = key;*/
  92. jID["prefix"] = std::vector<std::string>();
  93. return jID;
  94. }
  95. }