TrapezoidalTerm.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * TrapezoidalTerm.cpp
  14. *
  15. * Created on: Dec 13, 2009
  16. * Author: jcrada
  17. */
  18. #include "TrapezoidalTerm.h"
  19. namespace fl {
  20. TrapezoidalTerm::TrapezoidalTerm() :
  21. LinguisticTerm(), _b(0), _c(0) {
  22. }
  23. TrapezoidalTerm::TrapezoidalTerm(const std::string& name, flScalar minimum,
  24. flScalar maximum) :
  25. LinguisticTerm(name, minimum, maximum), _b(0), _c(0) {
  26. setB(minimum + (maximum - minimum) * 1 / 5);
  27. setC(minimum + (maximum - minimum) * 4 / 5);
  28. }
  29. TrapezoidalTerm::TrapezoidalTerm(const FuzzyOperator& fuzzy_op,
  30. const std::string& name, flScalar minimum, flScalar maximum) :
  31. LinguisticTerm(fuzzy_op, name, minimum, maximum), _b(0), _c(0) {
  32. setB(minimum + (maximum - minimum) * 1 / 5);
  33. setC(minimum + (maximum - minimum) * 4 / 5);
  34. }
  35. TrapezoidalTerm::~TrapezoidalTerm() {
  36. }
  37. void TrapezoidalTerm::setA(flScalar a) {
  38. setMinimum(a);
  39. }
  40. flScalar TrapezoidalTerm::a() const {
  41. return minimum();
  42. }
  43. void TrapezoidalTerm::setB(flScalar b) {
  44. this->_b = b;
  45. }
  46. flScalar TrapezoidalTerm::b() const {
  47. return this->_b;
  48. }
  49. void TrapezoidalTerm::setC(flScalar c) {
  50. this->_c = c;
  51. }
  52. flScalar TrapezoidalTerm::c() const {
  53. return this->_c;
  54. }
  55. void TrapezoidalTerm::setD(flScalar d) {
  56. setMaximum(d);
  57. }
  58. flScalar TrapezoidalTerm::d() const {
  59. return maximum();
  60. }
  61. TrapezoidalTerm* TrapezoidalTerm::clone() const {
  62. return new TrapezoidalTerm(*this);
  63. }
  64. flScalar TrapezoidalTerm::membership(flScalar crisp) const {
  65. if (crisp < minimum() || crisp > maximum()) {
  66. return flScalar(0);
  67. }
  68. if (crisp < b()) {
  69. return fuzzyOperator().modulation().execute(modulation(),
  70. FuzzyOperation::Scale(a(), b(), crisp, 0, 1));
  71. }
  72. if (crisp < c()) {
  73. return fuzzyOperator().modulation().execute(modulation(), flScalar(1.0));
  74. }
  75. if (crisp < d()) {
  76. return fuzzyOperator().modulation().execute(modulation(),
  77. FuzzyOperation::Scale(c(), d(), crisp, 1, 0));
  78. }
  79. return flScalar(0.0);
  80. }
  81. LinguisticTerm::eMembershipFunction TrapezoidalTerm::type() const {
  82. return MF_TRAPEZOIDAL;
  83. }
  84. std::string TrapezoidalTerm::toString() const {
  85. std::stringstream ss;
  86. ss << LinguisticTerm::toString();
  87. ss << "Trapezoidal (" << a() << " " << b() << " " << c() << " " << d() << ")";
  88. return ss.str();
  89. }
  90. } // namespace fuzzy_lite