ShoulderTerm.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * ShoulderTerm.cpp
  14. *
  15. * Created on: Dec 13, 2009
  16. * Author: jcrada
  17. */
  18. #include "ShoulderTerm.h"
  19. namespace fl {
  20. ShoulderTerm::ShoulderTerm() :
  21. LinguisticTerm(), _left(true) {
  22. }
  23. ShoulderTerm::ShoulderTerm(const std::string& name, flScalar minimum,
  24. flScalar maximum, bool left) :
  25. LinguisticTerm(name, minimum, maximum), _left(left) {
  26. }
  27. ShoulderTerm::ShoulderTerm(const FuzzyOperator& fuzzy_op, const std::string& name,
  28. flScalar minimum, flScalar maximum, bool left) :
  29. LinguisticTerm(fuzzy_op, name, minimum, maximum), _left(left) {
  30. }
  31. ShoulderTerm::~ShoulderTerm() {
  32. }
  33. void ShoulderTerm::setLeft(bool left) {
  34. this->_left = left;
  35. }
  36. bool ShoulderTerm::isLeft() const {
  37. return this->_left;
  38. }
  39. void ShoulderTerm::setRight(bool right) {
  40. this->_left = !right;
  41. }
  42. bool ShoulderTerm::isRight() const {
  43. return !this->_left;
  44. }
  45. ShoulderTerm* ShoulderTerm::clone() const {
  46. return new ShoulderTerm(*this);
  47. }
  48. flScalar ShoulderTerm::membership(flScalar crisp) const {
  49. if (isLeft()) {
  50. if (crisp < minimum())
  51. return flScalar(1.0);
  52. if (crisp > maximum())
  53. return flScalar(0.0);
  54. return FuzzyOperation::Scale(minimum(), maximum(), crisp, 1, 0);
  55. } else {
  56. if (crisp < minimum())
  57. return flScalar(0.0);
  58. if (crisp > maximum())
  59. return flScalar(1.0);
  60. return FuzzyOperation::Scale(minimum(), maximum(), crisp, 0, 1);
  61. }
  62. }
  63. LinguisticTerm::eMembershipFunction ShoulderTerm::type() const {
  64. return MF_SHOULDER;
  65. }
  66. std::string ShoulderTerm::toString() const {
  67. std::stringstream ss;
  68. ss << LinguisticTerm::toString();
  69. ss << "Shoulder " << (isLeft() ? "left" : "right") << "(" << minimum() << " " << maximum() << ")";
  70. return ss.str();
  71. }
  72. //**********Right and left...
  73. RightShoulderTerm::RightShoulderTerm() : ShoulderTerm() {
  74. setRight(true);
  75. }
  76. RightShoulderTerm::RightShoulderTerm(const std::string& name, flScalar minimum, flScalar maximum)
  77. : ShoulderTerm(name, minimum, maximum) {
  78. setRight(true);
  79. }
  80. RightShoulderTerm::RightShoulderTerm(const FuzzyOperator& fuzzy_op, const std::string& name, flScalar minimum, flScalar maximum)
  81. : ShoulderTerm(fuzzy_op, name, minimum, maximum) {
  82. setRight(true);
  83. }
  84. RightShoulderTerm::~RightShoulderTerm() {
  85. }
  86. LeftShoulderTerm::LeftShoulderTerm() : ShoulderTerm() {
  87. setLeft(true);
  88. }
  89. LeftShoulderTerm::LeftShoulderTerm(const std::string& name, flScalar minimum, flScalar maximum)
  90. : ShoulderTerm(name, minimum, maximum) {
  91. setLeft(true);
  92. }
  93. LeftShoulderTerm::LeftShoulderTerm(const FuzzyOperator& fuzzy_op, const std::string& name, flScalar minimum, flScalar maximum)
  94. : ShoulderTerm(fuzzy_op, name, minimum, maximum) {
  95. setLeft(true);
  96. }
  97. LeftShoulderTerm::~LeftShoulderTerm() {
  98. }
  99. } // namespace fuzzy_lite