The frbs model generator

Share:

Description

The purpose of this function is to generate a FRBS model from user-given input without a learning process.

Usage

1
2
3
4
5
frbs.gen(range.data, num.fvalinput, names.varinput, num.fvaloutput = NULL,
  varout.mf = NULL, names.varoutput = NULL, rule, varinp.mf,
  type.model = "MAMDANI", type.defuz = "WAM", type.tnorm = "MIN",
  type.snorm = "MAX", func.tsk = NULL, colnames.var = NULL,
  type.implication.func = "ZADEH", name = "Sim-0")

Arguments

range.data

a matrix (2 \times n) containing the range of the data, where n is the number of variables, and first and second rows are the minimum and maximum values, respectively.

num.fvalinput

a matrix representing the number of linguistic terms of each input variables.

For example: num.fvalinput <- matrix(c(3,2), nrow = 1)

means that there are two variables where the first variable has three linguistic terms and the second one has two linguistic terms.

names.varinput

a list containing names to the linguistic terms for input variables. See rulebase.

num.fvaloutput

the number of linguistic terms of the output variable. This parameter is required for the Mamdani model only.

For example: num.fvaloutput <- matrix(3, nrow = 1)

means there are 3 linguistic terms for the output variable.

varout.mf

a matrix for constructing the membership functions of the output variable. The form is the same as for the varinp.mf parameter. This parameter is required for the Mamdani model only. See fuzzifier.

names.varoutput

a list giving names of the linguistic terms for the output variable. The form is the same as for the names.varinput parameter. This parameter is required for the Mamdani model only. See rulebase.

rule

a list of fuzzy IF-THEN rules. There are some types of rule structures, for example: Mamdani, Takagi Sugeno Kang, and fuzzy rule-based classification systems (FRBCS). If we use the Mamdani model then the consequent part is a linguistic term, but if we use Takagi Sugeno Kang then we build a matrix representing linear equations in the consequent part. e.g., "a1", "and", "b1, "->", "e1" means that "IF inputvar.1 is a1 and inputvar.2 is b1 THEN outputvar.1 is e1". Make sure that each rule has a "->" sign. Furthermore, we are allowed to use linguistic hedges (e.g., "extremely", "slightly", etc), negation (i.e., "not"), and the "dont_care" value representing degree of membership is always 1. For more detail, see rulebase.

varinp.mf

a matrix for constructing the shapes of the membership functions. See how to construct it in fuzzifier.

type.model

the type of the model. There are three types available as follows.

  • MAMDANI means we are using the Mamdani model.

  • TSK means we are using the Takagi Sugeno Kang model.

  • FRBCS means we are using fuzzy rule-based classification systems (FRBCS).

type.defuz

the type of the defuzzification method. It is used in the Mamdani model only. See defuzzifier.

type.tnorm

the type of the t-norm method. See inference.

type.snorm

the type of the s-norm method. See inference.

func.tsk

a matrix of parameters of the function on the consequent part using the Takagi Sugeno Kang model. This parameter must be defined when we are using Takagi Sugeno Kang. See rulebase.

colnames.var

a list of names of input and output variables.

type.implication.func

a type of implication function. See WM.

name

a name of the simulation.

Details

It can be used if rules have already been obtained manually, without employing the learning process. In the examples shown, we generate a fuzzy model using frbs.gen and generate the fuzzy rule-based systems step by step manually. Additionally, the examples show several scenarios as follows.

  • Using frbs.gen for constructing the Mamdani model on a regression task.

  • Using frbs.gen for constructing the Takagi Sugeno Kang model on a regression task.

  • Constructing the Mamdani model by executing internal functions such as rulebase, fuzzifier, inference, and defuzzifier for the Mamdani model.

  • Using frbs.gen for constructing fuzzy rule-based classification systems (FRBCS) model.

Value

The frbs-object.

Examples

  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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#################################################
## 1. The following codes show how to generate a fuzzy model
## using the frbs.gen function for regression tasks.
## The following are three scenarios:
## 1a. Using the Mamdani model
## 1b. Using the Takagi Sugeno Kang model
## 1c. Using the Mamdani model and internal functions: fuzzifier, etc.
## Note:
## In the examples, let us consider four input variabels and one output variable.
## Some variables could be shared together for other examples.
#################################################

## Define shape and parameters of membership functions of input variables.
## Please see the fuzzifier function to construct the matrix.
## It can be seen that in this case we employ TRAPEZOID as the membership functions.
varinp.mf <- matrix(c(2, 0, 20, 40, NA, 4, 20, 40, 60, 80, 3, 60, 80, 100, NA,
                      2, 0, 35, 75, NA, 3, 35, 75, 100, NA,
                      2, 0, 20, 40, NA, 1, 20, 50, 80, NA, 3, 60, 80, 100, NA,
                      2, 0, 20, 40, NA, 4, 20, 40, 60, 80, 3, 60, 80, 100, NA),
                      nrow = 5, byrow = FALSE)

## Define number of linguistic terms of the input variables.
## Suppose, we have 3, 2, 3, and 3 numbers of linguistic terms
## for the first, second, third and fourth variables, respectively.
num.fvalinput <- matrix(c(3, 2, 3, 3), nrow=1)

## Give the names of the linguistic terms of each input variables.
varinput.1 <- c("a1", "a2", "a3")
varinput.2 <- c("b1", "b2")
varinput.3 <- c("c1", "c2", "c3")
varinput.4 <- c("d1", "d2", "d3")
names.varinput <- c(varinput.1, varinput.2, varinput.3, varinput.4)

## Set interval of data.
range.data <- matrix(c(0,100, 0, 100, 0, 100, 0, 100, 0, 100), nrow=2)

## Define inference parameters.
type.defuz <- "WAM"
type.tnorm <- "MIN"
type.snorm <- "MAX"
type.implication.func <- "ZADEH"

## Give the name of simulation.
name <- "Sim-0"

## Provide new data for testing.
newdata <- matrix(c(15, 80, 85, 85, 45, 75, 78, 70), nrow = 2, byrow = TRUE)
## the names of variables
colnames.var <- c("input1", "input2", "input3", "input4", "output1")

###################################################################
## 1a. Using the Mamdani Model
####################################################################
## Define number of linguistic terms of output variable.
## In this case, we set the number of linguistic terms to 3.
num.fvaloutput <- matrix(c(3), nrow = 1)

## Give the names of the linguistic terms of the output variable.
varoutput.1 <- c("e1", "e2", "e3")
names.varoutput <- c(varoutput.1)

## Define the shapes and parameters of the membership functions of the output variables.
varout.mf <- matrix(c(2, 0, 20, 40, NA, 4, 20, 40, 60, 80, 3, 60, 80, 100, NA),
                      nrow = 5, byrow = FALSE)

## Set type of model which is "MAMDANI" or "TSK" for Mamdani or
## Takagi Sugeno Kang models, respectively.
## In this case, we choose the Mamdani model.
type.model <- "MAMDANI"

## Define the fuzzy IF-THEN rules; In this case, we provide two scenarios using different operators:
rule.or <- matrix(c("a1", "or", "b1", "or", "c1", "or", "d1", "->", "e1",
                 "a2", "and", "b2", "and", "c2", "and", "d2", "->", "e2",
                 "a3", "and", "b2", "and", "c2", "and", "d1", "->", "e3"),
                 nrow = 3, byrow = TRUE)

## Define the fuzzy IF-THEN rules;
rule.and <- matrix(c("a1", "and", "b1", "and", "c1", "and", "d1", "->", "e1",
                 "a2", "and", "b2", "and", "c2", "and", "d2", "->", "e2",
                 "a3", "and", "b2", "and", "c2", "and", "d1", "->", "e3"),
                 nrow = 3, byrow = TRUE)

## Generate a fuzzy model with frbs.gen.
object.or <- frbs.gen(range.data, num.fvalinput, names.varinput,
                 num.fvaloutput, varout.mf, names.varoutput, rule.or,
                 varinp.mf, type.model, type.defuz, type.tnorm,
                 type.snorm, func.tsk = NULL, colnames.var, type.implication.func, name)

object.and <- frbs.gen(range.data, num.fvalinput, names.varinput,
                 num.fvaloutput, varout.mf, names.varoutput, rule.and,
                 varinp.mf, type.model, type.defuz, type.tnorm,
                 type.snorm, func.tsk = NULL, colnames.var, type.implication.func, name)

## Plot the membership function.
plotMF(object.and)

## Predicting using new data.
res.or <- predict(object.or, newdata)$predicted.val
res.and <- predict(object.and, newdata)$predicted.val

#####################################################################
## 1b. Using the Takagi Sugeno Kang (TSK) Model
#####################################################################
## Define "TSK" for the Takagi Sugeno Kang model
type.model <- "TSK"

## Define linear equations for consequent parts.
## The following command means that we have three equation related to the rules we have.
## e.g., the first equation is 1*inputvar.1 + 1*inputvar.2 + 5*inputvar.3 + 2*inputvar.4 + 1,
## where inputvar.i is a value of the i-th input variable.
func.tsk <- matrix(c(1, 1, 5, 2, 1, 3, 1, 0.5, 0.1, 2, 1, 3, 2, 2, 2),
            nrow = 3, byrow = TRUE)

## Define the fuzzy IF-THEN rules;
## For TSK model, it isn't necessary to put linguistic term in consequent parts.
## Make sure that each rule has a "->" sign.
rule <- matrix(c("a1", "and", "b1", "and", "c1", "and", "d1", "->",
                 "a2", "and", "b2", "and", "c2", "and", "d2", "->",
                 "a3", "and", "b2", "and", "c2", "and", "d1", "->"),
                 nrow = 3, byrow = TRUE)

## Generate a fuzzy model with frbs.gen.
## It should be noted that for TSK model, we do not need to input:
## num.fvaloutput, varout.mf, names.varoutput, type.defuz.
object <- frbs.gen(range.data, num.fvalinput, names.varinput,
             num.fvaloutput = NULL, varout.mf = NULL, names.varoutput = NULL, rule,
				varinp.mf, type.model, type.defuz = NULL, type.tnorm, type.snorm,
             func.tsk, colnames.var, type.implication.func, name)

## Plot the membership function.
plotMF(object)

## Predicting using new data.
res <- predict(object, newdata)$predicted.val

######################
## 1c. Using the same data as in the previous example, this example performs
## step by step of the generation of a fuzzy rule-based system
######################
## Using the Mamdani model.
type.model <- "MAMDANI"

## Construct rules.
rule <- matrix(c("a1", "and", "b1", "and", "c1", "and", "d1", "->", "e1",
                 "a2", "and", "b2", "and", "c2", "and", "d2", "->", "e2",
                 "a3", "and", "b2", "and", "c2", "and", "d1", "->", "e3"),
                 nrow = 3, byrow = TRUE)

## Check input data given by user.
rule <- rulebase(type.model, rule, func.tsk = NULL)

## Fuzzification Module:
## In this function, we convert crisp into linguistic values/terms
## based on the data and the parameters of the membership function.
## The output: a matrix representing the degree of the membership of the data
num.varinput <- ncol(num.fvalinput)
MF <- fuzzifier(newdata, num.varinput, num.fvalinput, varinp.mf)

## Inference Module:
## In this function, we will calculate the confidence factor on the antecedent for each rule
## considering t-norm and s-norm.
miu.rule <- inference(MF, rule, names.varinput, type.tnorm, type.snorm)

## Defuzzification Module.
## In this function, we calculate and convert the linguistic values back into crisp values.
range.output <- range.data[, ncol(range.data), drop = FALSE]
result <- defuzzifier(newdata, rule, range.output, names.varoutput,
                  varout.mf, miu.rule, type.defuz, type.model, func.tsk = NULL)


#################################################
## 2. The following codes show how to generate a fuzzy model
## using the frbs.gen function for classification tasks using the Mamdani model.
#################################################
## define range of data.
## Note. we only define range of input data.
range.data.input <- matrix(c(0, 1, 0, 1, 0, 1, 0, 1), nrow=2)

## Define shape and parameters of membership functions of input variables.
## Please see fuzzifier function to construct the matrix.
## In this case, we are using TRIANGLE for membership functions.
varinp.mf <- matrix(c(1, 0, 0, 0.5, NA, 1, 0, 0.5, 1, NA, 1, 0.5, 1, 1, NA,
                      1, 0, 0, 0.5, NA, 1, 0, 0.5, 1, NA, 1, 0.5, 1, 1, NA,
                      1, 0, 0, 0.5, NA, 1, 0, 0.5, 1, NA, 1, 0.5, 1, 1, NA,
                      1, 0, 0, 0.5, NA, 1, 0, 0.5, 1, NA, 1, 0.5, 1, 1, NA),
                      nrow = 5, byrow = FALSE)

## Define number of linguistic terms of input variables.
## Suppose, we have 3, 3, 3, and 3 numbers of linguistic terms
## for first up to fourth variables, respectively.
num.fvalinput <- matrix(c(3, 3, 3, 3), nrow=1)

## Give the names of the linguistic terms of each input variable.
varinput.1 <- c("v.1_a.1", "v.1_a.2", "v.1_a.3")
varinput.2 <- c("v.2_a.1", "v.2_a.2", "v.2_a.3")
varinput.3 <- c("v.3_a.1", "v.3_a.2", "v.3_a.3")
varinput.4 <- c("v.4_a.1", "v.4_a.2", "v.4_a.3")
names.varinput <- c(varinput.1, varinput.2, varinput.3, varinput.4)

## Provide inference parameters.
type.tnorm <- "MIN"
type.snorm <- "MAX"
type.implication.func <- "ZADEH"
type.model <- "FRBCS"

## Give the name of simulation.
name <- "Sim-0"

## Provide new data for testing.
newdata<- matrix(c(0.45, 0.5, 0.89, 0.44, 0.51, 0.99, 0.1, 0.98, 0.51,
                   0.56, 0.55, 0.5), nrow = 3, byrow = TRUE)

## the names of variables
colnames.var <- c("input1", "input2", "input3", "input4", "output1")

## Construct rules.
## It should be noted that on consequent parts we define categorical values instead of
## linguistic terms.
rule <- matrix(
       c("v.1_a.2", "and", "v.2_a.2", "and", "v.3_a.3", "and", "v.4_a.2", "->", "3",
         "v.1_a.2", "and", "v.2_a.3", "and", "v.3_a.1", "and", "v.4_a.3", "->", "1",
         "v.1_a.2", "and", "v.2_a.2", "and", "v.3_a.2", "and", "v.4_a.2", "->", "2"),
         nrow = 3, byrow = TRUE)

## Generate frbs object.
object <- frbs.gen(range.data = range.data.input, num.fvalinput,
             names.varinput, num.fvaloutput = NULL, varout.mf = NULL,
             names.varoutput = NULL, rule, varinp.mf, type.model,
             type.defuz = NULL, type.tnorm, type.snorm, func.tsk = NULL,
             colnames.var, type.implication.func, name)

## Plot the shape of membership functions.
plotMF(object)

## Predicting using new data.
res <- predict(object, newdata)

####################################################
## 3. The following example shows how to convert
##    the frbs model into frbsPMML
####################################################
## In this example, we are using the last object of FRBS.
## Display frbsPMML in R
objPMML <- frbsPMML(object)

## Write into a file with .frbsPMML extention
## Not run: write.frbsPMML(objPMML, fileName="obj_frbsPMML")

## Read the frbsPMML file into an R object of FRBS
obj <- read.frbsPMML("obj_frbsPMML.frbsPMML")
## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.