summaryrefslogtreecommitdiff
path: root/fuzzylite/test/norm/NormFunctionTest.cpp
blob: afce4c898bc26411f60562a96e1dbdced083332a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 fuzzylite (R), a fuzzy logic control library in C++.
 Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
 Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>

 This file is part of fuzzylite.

 fuzzylite is free software: you can redistribute it and/or modify it under
 the terms of the FuzzyLite License included with the software.

 You should have received a copy of the FuzzyLite License along with
 fuzzylite. If not, see <http://www.fuzzylite.com/license/>.

 fuzzylite is a registered trademark of FuzzyLite Limited.
 */

#include "test/catch.hpp"
#include "fl/Headers.h"

namespace fl {

    /**
     * Tests: norm/NormFunctions
     *
     * @author Juan Rada-Vilela, Ph.D.
     *
     */

    static std::string snormEngine() {
#ifdef FL_CPP98
        return "";
#else
        return R""(
Engine: tipper
InputVariable: service
  enabled: true
  range: 0.000 10.000
  lock-range: false
  term: poor Gaussian 0.000 1.500
  term: good Gaussian 5.000 1.500
  term: excellent Gaussian 10.000 1.500
InputVariable: food
  enabled: true
  range: 0.000 10.000
  lock-range: false
  term: rancid Trapezoid 0.000 0.000 1.000 3.000
  term: delicious Trapezoid 7.000 9.000 10.000 10.000
OutputVariable: tip
  enabled: true
  range: 0.000 30.000
  lock-range: false
  aggregation: Maximum
  defuzzifier: Centroid 200
  default: nan
  lock-previous: false
  term: cheap Triangle 0.000 5.000 10.000
  term: average Triangle 10.000 15.000 20.000
  term: generous Triangle 20.000 25.000 30.000
RuleBlock:
  enabled: true
  conjunction: Minimum
  disjunction: Maximum
  implication: Minimum
  activation: General
  rule: if service is poor or food is rancid then tip is cheap
  rule: if service is good then tip is average
  rule: if service is excellent or food is delicious then tip is generous
)"";
#endif
    }

    static SNorm* myMaximumNorm() {
        return new SNormFunction("max(a,b)");
    }

    static SNorm* myNotSoMaximumNorm() {
        return new SNormFunction("max(a,b) * 0.5");
    }

    TEST_CASE("SNormFunction (max(a,b)) is equivalent to Maximum", "[snorm][maximum]") {
#ifdef FL_CPP98
        FL_IUNUSED(&(myMaximumNorm));
        FL_IUNUSED(&(myNotSoMaximumNorm));
        FL_IUNUSED(&(snormEngine));
        WARN("Test only runs with -DFL_CPP98=OFF");
        return;
#else
        std::string fllEngine = snormEngine();
        FL_unique_ptr<Engine> engine(FllImporter().fromString(fllEngine));
        std::string fld = FldExporter().toString(engine.get(), 1024);

        SNormFactory* factory = FactoryManager::instance()->snorm();
        factory->registerConstructor("Maximum", &(myMaximumNorm));

        //Check our custom SNorm is registered
        FL_unique_ptr<SNorm> x(factory->constructObject("Maximum"));
        CHECK(Op::isEq(x->compute(0, 0.5), 0.5));

        //Test creating an engine with the new SNorm
        engine.reset(FllImporter().fromString(fllEngine));
        std::string anotherFld = FldExporter().toString(engine.get(), 1024);

        CHECK(fld == anotherFld);

        //Make sure a different SNorm fails in results

        factory->registerConstructor("Maximum", &(myNotSoMaximumNorm));
        engine.reset(FllImporter().fromString(fllEngine));
        anotherFld = FldExporter().toString(engine.get(), 1024);

        CHECK(fld != anotherFld);
#endif
    }

    static std::string tnormEngine() {
#ifdef FL_CPP98
        return "";
#else
        return R""(
Engine: mam21
InputVariable: angle
  enabled: true
  range: -5.000 5.000
  lock-range: false
  term: small Bell -5.000 5.000 8.000
  term: big Bell 5.000 5.000 8.000
InputVariable: velocity
  enabled: true
  range: -5.000 5.000
  lock-range: false
  term: small Bell -5.000 5.000 2.000
  term: big Bell 5.000 5.000 2.000
OutputVariable: force
  enabled: true
  range: -5.000 5.000
  lock-range: false
  aggregation: Maximum
  defuzzifier: Centroid 200
  default: nan
  lock-previous: false
  term: negBig Bell -5.000 1.670 8.000
  term: negSmall Bell -1.670 1.670 8.000
  term: posSmall Bell 1.670 1.670 8.000
  term: posBig Bell 5.000 1.670 8.000
RuleBlock:
  enabled: true
  conjunction: Minimum
  disjunction: Maximum
  implication: Minimum
  activation: General
  rule: if angle is small and velocity is small then force is negBig
  rule: if angle is small and velocity is big then force is negSmall
  rule: if angle is big and velocity is small then force is posSmall
  rule: if angle is big and velocity is big then force is posBig
)"";
#endif
    }

    static TNorm* myMinimumNorm() {
        return new TNormFunction("min(a,b)");
    }

    static TNorm* myNotSoMinimumNorm() {
        return new TNormFunction("min(a,b) * 0.5");
    }

    TEST_CASE("TNormFunction (min(a,b)) is equivalent to Minimum", "[tnorm][minimum]") {
#ifdef FL_CPP98
        FL_IUNUSED(&(myMinimumNorm));
        FL_IUNUSED(&(myNotSoMinimumNorm));
        FL_IUNUSED(&(tnormEngine));
        WARN("Test only runs with -DFL_CPP98=OFF");
        return;
#else
        std::string fllEngine = tnormEngine();
        FL_unique_ptr<Engine> engine(FllImporter().fromString(fllEngine));
        std::string fld = FldExporter().toString(engine.get(), 1024);

        TNormFactory* factory = FactoryManager::instance()->tnorm();
        factory->registerConstructor("Minimum", &(myMinimumNorm));

        //Check our custom SNorm is registered
        FL_unique_ptr<TNorm> x(factory->constructObject("Minimum"));
        CHECK(Op::isEq(x->compute(0.5, 1), 0.5));

        //Test creating an engine with the new SNorm
        engine.reset(FllImporter().fromString(fllEngine));
        std::string anotherFld = FldExporter().toString(engine.get(), 1024);

        CHECK(fld == anotherFld);

        //Make sure a different SNorm fails in results

        factory->registerConstructor("Minimum", &(myNotSoMinimumNorm));
        engine.reset(FllImporter().fromString(fllEngine));
        anotherFld = FldExporter().toString(engine.get(), 1024);

        CHECK(fld != anotherFld);
#endif
    }

}