2
0

TakagiSugenoConsequent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright 2010 Juan Rada-Vilela
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. #include "TakagiSugenoConsequent.h"
  13. #include <stack>
  14. #include <iosfwd>
  15. #include "FunctionTerm.h"
  16. #include "SingletonTerm.h"
  17. #include "TakagiSugenoTerm.h"
  18. namespace fl {
  19. TakagiSugenoConsequent::TakagiSugenoConsequent() {
  20. _infix2postfix.loadMathOperators();
  21. }
  22. TakagiSugenoConsequent::~TakagiSugenoConsequent() {
  23. }
  24. const FuzzyEngine& TakagiSugenoConsequent::fuzzyEngine() const {
  25. return *this->_fuzzy_engine;
  26. }
  27. void TakagiSugenoConsequent::setFuzzyEngine(const FuzzyEngine& fuzzy_engine) {
  28. this->_fuzzy_engine = &fuzzy_engine;
  29. }
  30. std::string TakagiSugenoConsequent::consequent() const {
  31. return this->_consequent;
  32. }
  33. void TakagiSugenoConsequent::setConsequent(const std::string& consequent) {
  34. this->_consequent = consequent;
  35. }
  36. std::string TakagiSugenoConsequent::postfixFunction() const {
  37. return this->_postfix_function;
  38. }
  39. void TakagiSugenoConsequent::setPostfixFunction(const std::string& postfix) {
  40. this->_postfix_function = postfix;
  41. }
  42. void TakagiSugenoConsequent::parse(const std::string& consequent,
  43. const FuzzyEngine& engine) throw (ParsingException) {
  44. setFuzzyEngine(engine);
  45. setConsequent(consequent);
  46. std::stringstream ss(consequent);
  47. std::string output_lvar;
  48. ss >> output_lvar;
  49. if (!fuzzyEngine().outputLVar(output_lvar)) {
  50. throw ParsingException(FL_AT, "Output variable <" + output_lvar +
  51. "> not found in fuzzy engine");
  52. }
  53. setOutputLVar(fuzzyEngine().outputLVar(output_lvar));
  54. std::string equal;
  55. ss >> equal;
  56. if (equal != "=") {
  57. throw ParsingException(FL_AT, "<=> expected but found <" + equal + ">");
  58. }
  59. std::string right_side;
  60. std::string token;
  61. while (ss >> token) {
  62. right_side += token + " ";
  63. }
  64. setPostfixFunction(_infix2postfix.transform(right_side));
  65. std::map<std::string, flScalar> variables;
  66. for (int i = 0; i < fuzzyEngine().numberOfInputLVars(); ++i) {
  67. variables[fuzzyEngine().inputLVar(i)->name()] =
  68. fuzzyEngine().inputLVar(i)->input();
  69. }
  70. for (int i = 0; i < fuzzyEngine().numberOfOutputLVars(); ++i) {
  71. variables[fuzzyEngine().outputLVar(i)->name()] =
  72. fuzzyEngine().outputLVar(i)->output().defuzzify();
  73. }
  74. try {
  75. _infix2postfix.evaluate(postfixFunction(), &variables);
  76. } catch (FuzzyException& e) {
  77. throw ParsingException(FL_AT, e.message(), e);
  78. }
  79. }
  80. void TakagiSugenoConsequent::execute(flScalar degree) {
  81. std::map<std::string, flScalar> variables;
  82. for (int i = 0; i < fuzzyEngine().numberOfInputLVars(); ++i) {
  83. variables[fuzzyEngine().inputLVar(i)->name()] =
  84. fuzzyEngine().inputLVar(i)->input();
  85. }
  86. for (int i = 0; i < fuzzyEngine().numberOfOutputLVars(); ++i) {
  87. variables[fuzzyEngine().outputLVar(i)->name()] =
  88. fuzzyEngine().outputLVar(i)->output().defuzzify();
  89. }
  90. TakagiSugenoTerm result(fuzzyEngine().fuzzyOperator(), postfixFunction());
  91. result.setValue(_infix2postfix.evaluate(postfixFunction(), &variables));
  92. result.setWeight(degree);
  93. outputLVar()->output().addTerm(result);
  94. }
  95. std::string TakagiSugenoConsequent::toString() const {
  96. return consequent();
  97. }
  98. void TakagiSugenoConsequent::main(int args, char** argv) {
  99. }
  100. }