CompoundTerm.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /*
  13. * CompoundTerm.cpp
  14. *
  15. * Created on: Dec 13, 2009
  16. * Author: jcrada
  17. */
  18. #include "CompoundTerm.h"
  19. namespace fl {
  20. CompoundTerm::CompoundTerm() :
  21. LinguisticTerm() {
  22. }
  23. CompoundTerm::CompoundTerm(const std::string& name, flScalar minimum,
  24. flScalar maximum) :
  25. LinguisticTerm(name, minimum, maximum) {
  26. }
  27. CompoundTerm::CompoundTerm(const FuzzyOperator& fuzzy_op, const std::string& name,
  28. flScalar minimum, flScalar maximum) :
  29. LinguisticTerm(fuzzy_op, name, minimum, maximum) {
  30. }
  31. CompoundTerm::~CompoundTerm() {
  32. clear();
  33. }
  34. void CompoundTerm::addTerm(const LinguisticTerm& term) {
  35. _terms.push_back(term.clone());
  36. if (term.minimum() < minimum() || isinf(minimum())) {
  37. setMinimum(term.minimum());
  38. }
  39. if (term.maximum() > maximum() || isinf(maximum())) {
  40. setMaximum(term.maximum());
  41. }
  42. }
  43. const LinguisticTerm& CompoundTerm::term(int index) const {
  44. return *this->_terms[index];
  45. }
  46. void CompoundTerm::removeTerm(int index){
  47. LinguisticTerm* t = this->_terms[index];
  48. this->_terms.erase(this->_terms.begin() + index);
  49. delete t;
  50. }
  51. int CompoundTerm::numberOfTerms() const{
  52. return this->_terms.size();
  53. }
  54. void CompoundTerm::clear() {
  55. for (size_t i = 0; i < _terms.size(); ++i) {
  56. delete _terms[i];
  57. }
  58. _terms.clear();
  59. setMinimum(-INFINITY);
  60. setMaximum(INFINITY);
  61. }
  62. CompoundTerm* CompoundTerm::clone() const {
  63. CompoundTerm* result = new CompoundTerm;
  64. for (int i = 0 ; i < numberOfTerms(); ++i){
  65. result->addTerm(term(i));
  66. }
  67. return result;
  68. }
  69. flScalar CompoundTerm::membership(flScalar crisp) const {
  70. flScalar max = flScalar(0);
  71. for (size_t i = 0; i < _terms.size(); ++i) {
  72. max = fuzzyOperator().aggregation().execute(max, _terms[i]->membership(crisp));
  73. }
  74. return fuzzyOperator().modulation().execute(modulation(), max);
  75. }
  76. LinguisticTerm::eMembershipFunction CompoundTerm::type() const {
  77. return MF_COMPOUND;
  78. }
  79. std::string CompoundTerm::toString() const {
  80. std::stringstream ss;
  81. ss << LinguisticTerm::toString();
  82. ss << "Compound (";
  83. for (size_t i = 0; i < _terms.size(); ++i) {
  84. ss << _terms[i]->toString();
  85. if (i < _terms.size() -1 ) ss << " + ";
  86. }
  87. ss << ")";
  88. return ss.str();
  89. }
  90. } // namespace fuzzy_lite