summaryrefslogtreecommitdiff
path: root/examples/hybrid/tipper.R
blob: ba113515caa354d2094879c6ec96b1bd30fb5c12 (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
#Code automatically generated with fuzzylite 6.0.

library(ggplot2);

engine.name = "tipper"
engine.description = "(service and food) -> (tip)"
engine.fll = "Engine: tipper
description: (service and food) -> (tip)
InputVariable: service
  description: quality of service
  enabled: true
  range: 0.000 10.000
  lock-range: true
  term: poor Trapezoid 0.000 0.000 2.500 5.000
  term: good Triangle 2.500 5.000 7.500
  term: excellent Trapezoid 5.000 7.500 10.000 10.000
InputVariable: food
  description: quality of food
  enabled: true
  range: 0.000 10.000
  lock-range: true
  term: rancid Trapezoid 0.000 0.000 2.500 7.500
  term: delicious Trapezoid 2.500 7.500 10.000 10.000
OutputVariable: mTip
  description: tip based on Mamdani inference
  enabled: true
  range: 0.000 30.000
  lock-range: false
  aggregation: Maximum
  defuzzifier: Centroid 100
  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
OutputVariable: tsTip
  description: tip based on Takagi-Sugeno inference
  enabled: true
  range: 0.000 30.000
  lock-range: false
  aggregation: none
  defuzzifier: WeightedAverage TakagiSugeno
  default: nan
  lock-previous: false
  term: cheap Constant 5.000
  term: average Constant 15.000
  term: generous Constant 25.000
RuleBlock: mamdani
  description: Mamdani inference
  enabled: true
  conjunction: AlgebraicProduct
  disjunction: AlgebraicSum
  implication: Minimum
  activation: General
  rule: if service is poor or food is rancid then mTip is cheap
  rule: if service is good then mTip is average
  rule: if service is excellent or food is delicious then mTip is generous with 0.5
  rule: if service is excellent and food is delicious then mTip is generous with 1.0
RuleBlock: takagiSugeno
  description: Takagi-Sugeno inference
  enabled: true
  conjunction: AlgebraicProduct
  disjunction: AlgebraicSum
  implication: none
  activation: General
  rule: if service is poor or food is rancid then tsTip is cheap
  rule: if service is good then tsTip is average
  rule: if service is excellent or food is delicious then tsTip is generous with 0.5
  rule: if service is excellent and food is delicious then tsTip is generous with 1.0"

engine.fldFile = "tipper.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.i1i2_o1 = ggplot(engine.df, aes(service, food)) + 
    geom_tile(aes(fill=mTip)) + 
    scale_fill_gradient(low="yellow", high="red") + 
    stat_contour(aes(x=service, y=food, z=mTip), color="black") + 
    ggtitle("(service, food) = mTip")

engine.plot.i2i1_o1 = ggplot(engine.df, aes(food, service)) + 
    geom_tile(aes(fill=mTip)) + 
    scale_fill_gradient(low="yellow", high="red") + 
    stat_contour(aes(x=food, y=service, z=mTip), color="black") + 
    ggtitle("(food, service) = mTip")

engine.plot.i1i2_o2 = ggplot(engine.df, aes(service, food)) + 
    geom_tile(aes(fill=tsTip)) + 
    scale_fill_gradient(low="yellow", high="red") + 
    stat_contour(aes(x=service, y=food, z=tsTip), color="black") + 
    ggtitle("(service, food) = tsTip")

engine.plot.i2i1_o2 = ggplot(engine.df, aes(food, service)) + 
    geom_tile(aes(fill=tsTip)) + 
    scale_fill_gradient(low="yellow", high="red") + 
    stat_contour(aes(x=food, y=service, z=tsTip), color="black") + 
    ggtitle("(food, service) = tsTip")

if (require(gridExtra)) {
    engine.plots = arrangeGrob(engine.plot.i1i2_o1, engine.plot.i2i1_o1, engine.plot.i1i2_o2, engine.plot.i2i1_o2, ncol=2, top=engine.name)
    ggsave(paste0(engine.name, ".pdf"), engine.plots)
    if (require(grid)) {
        grid.newpage()
        grid.draw(engine.plots)
    }
}