summaryrefslogtreecommitdiff
path: root/examples/takagi-sugeno/approximation.R
blob: 89c06545638aa940e16aed03fb24ffd5fcbb2fe1 (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
#Code automatically generated with fuzzylite 6.0.

library(ggplot2);

engine.name = "approximation"
engine.fll = "Engine: approximation
InputVariable: inputX
  enabled: true
  range: 0.000 10.000
  lock-range: false
  term: NEAR_1 Triangle 0.000 1.000 2.000
  term: NEAR_2 Triangle 1.000 2.000 3.000
  term: NEAR_3 Triangle 2.000 3.000 4.000
  term: NEAR_4 Triangle 3.000 4.000 5.000
  term: NEAR_5 Triangle 4.000 5.000 6.000
  term: NEAR_6 Triangle 5.000 6.000 7.000
  term: NEAR_7 Triangle 6.000 7.000 8.000
  term: NEAR_8 Triangle 7.000 8.000 9.000
  term: NEAR_9 Triangle 8.000 9.000 10.000
OutputVariable: outputFx
  enabled: true
  range: -1.000 1.000
  lock-range: false
  aggregation: none
  defuzzifier: WeightedAverage TakagiSugeno
  default: nan
  lock-previous: true
  term: f1 Constant 0.840
  term: f2 Constant 0.450
  term: f3 Constant 0.040
  term: f4 Constant -0.180
  term: f5 Constant -0.190
  term: f6 Constant -0.040
  term: f7 Constant 0.090
  term: f8 Constant 0.120
  term: f9 Constant 0.040
OutputVariable: trueFx
  enabled: true
  range: -1.000 1.000
  lock-range: false
  aggregation: none
  defuzzifier: WeightedAverage Automatic
  default: nan
  lock-previous: true
  term: fx Function sin(inputX)/inputX
OutputVariable: diffFx
  enabled: true
  range: -1.000 1.000
  lock-range: false
  aggregation: none
  defuzzifier: WeightedAverage Automatic
  default: nan
  lock-previous: false
  term: diff Function fabs(outputFx-trueFx)
RuleBlock: 
  enabled: true
  conjunction: none
  disjunction: none
  implication: none
  activation: General
  rule: if inputX is NEAR_1 then outputFx is f1
  rule: if inputX is NEAR_2 then outputFx is f2
  rule: if inputX is NEAR_3 then outputFx is f3
  rule: if inputX is NEAR_4 then outputFx is f4
  rule: if inputX is NEAR_5 then outputFx is f5
  rule: if inputX is NEAR_6 then outputFx is f6
  rule: if inputX is NEAR_7 then outputFx is f7
  rule: if inputX is NEAR_8 then outputFx is f8
  rule: if inputX is NEAR_9 then outputFx is f9
  rule: if inputX is any  then trueFx is fx and diffFx is diff"

engine.fldFile = "approximation.fld"
if (require(data.table)) {
    engine.df = data.table::fread(engine.fldFile, sep="auto", header="auto")
} else {
    engine.df = read.table(engine.fldFile, header=TRUE)
}

engine.plot.i1_o1 = ggplot(engine.df, aes(inputX, outputFx)) + 
    geom_line(aes(color=outputFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    ggtitle("inputX vs outputFx")

engine.plot.o1_i1 = ggplot(engine.df, aes(inputX, outputFx)) + 
    geom_line(aes(color=outputFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    coord_flip() + 
    ggtitle("outputFx vs inputX")

engine.plot.i1_o2 = ggplot(engine.df, aes(inputX, trueFx)) + 
    geom_line(aes(color=trueFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    ggtitle("inputX vs trueFx")

engine.plot.o2_i1 = ggplot(engine.df, aes(inputX, trueFx)) + 
    geom_line(aes(color=trueFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    coord_flip() + 
    ggtitle("trueFx vs inputX")

engine.plot.i1_o3 = ggplot(engine.df, aes(inputX, diffFx)) + 
    geom_line(aes(color=diffFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    ggtitle("inputX vs diffFx")

engine.plot.o3_i1 = ggplot(engine.df, aes(inputX, diffFx)) + 
    geom_line(aes(color=diffFx), size=3, lineend="round", linejoin="mitre") + 
    scale_color_gradient(low="yellow", high="red") + 
    coord_flip() + 
    ggtitle("diffFx vs inputX")

if (require(gridExtra)) {
    engine.plots = arrangeGrob(engine.plot.i1_o1, engine.plot.o1_i1, engine.plot.i1_o2, engine.plot.o2_i1, engine.plot.i1_o3, engine.plot.o3_i1, ncol=2, top=engine.name)
    ggsave(paste0(engine.name, ".pdf"), engine.plots)
    if (require(grid)) {
        grid.newpage()
        grid.draw(engine.plots)
    }
}