TriangularTerm.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * TriangularTerm.cpp
  14. *
  15. * Created on: Dec 13, 2009
  16. * Author: jcrada
  17. */
  18. #include "TriangularTerm.h"
  19. namespace fl {
  20. TriangularTerm::TriangularTerm() :
  21. LinguisticTerm(), _b(0) {
  22. }
  23. TriangularTerm::TriangularTerm(const std::string& name, flScalar minimum,
  24. flScalar maximum) :
  25. LinguisticTerm(name, minimum, maximum), _b((minimum + maximum) / 2) {
  26. }
  27. TriangularTerm::TriangularTerm(const FuzzyOperator& fuzzy_op,
  28. const std::string& name, flScalar minimum, flScalar maximum) :
  29. LinguisticTerm(fuzzy_op, name, minimum, maximum), _b((minimum + maximum) / 2) {
  30. }
  31. TriangularTerm::~TriangularTerm() {
  32. }
  33. void TriangularTerm::setA(flScalar a) {
  34. setMinimum(a);
  35. }
  36. flScalar TriangularTerm::a() const {
  37. return minimum();
  38. }
  39. void TriangularTerm::setB(flScalar b) {
  40. this->_b = b;
  41. }
  42. flScalar TriangularTerm::b() const {
  43. return this->_b;
  44. }
  45. void TriangularTerm::setC(flScalar c) {
  46. setMaximum(c);
  47. }
  48. flScalar TriangularTerm::c() const {
  49. return maximum();
  50. }
  51. TriangularTerm* TriangularTerm::clone() const {
  52. return new TriangularTerm(*this);
  53. }
  54. flScalar TriangularTerm::membership(flScalar crisp) const {
  55. if (crisp > maximum() || crisp < minimum()) {
  56. return flScalar(0.0);
  57. }
  58. flScalar result = crisp < b() ? FuzzyOperation::Scale(a(), b(), crisp, 0, 1)
  59. : FuzzyOperation::Scale(b(), c(), crisp, 1, 0);
  60. return fuzzyOperator().modulation().execute(result, modulation());
  61. }
  62. LinguisticTerm::eMembershipFunction TriangularTerm::type() const {
  63. return MF_TRIANGULAR;
  64. }
  65. std::string TriangularTerm::toString() const {
  66. std::stringstream ss;
  67. ss << LinguisticTerm::toString();
  68. ss << "Triangular (" << a() << " " << b() << " " << c() << ")";
  69. return ss.str();
  70. }
  71. } // namespace fuzzy_lite