0

I am trying to implement a fuzzy inference system in R using frbs package. Here is my code -

varinp.mf <- matrix(c(1,1,1,1,3,1,1,4,3,1,1,1,1,3,1,4,4,3,
                  0,20,40,70,85,0,5,30,65,0,25,40,70,85,0,20,45,70,
                  15,35,65,85,95,5,25,45,90,20,30,50,75,95,15,30,50,90,
                  30,45,75,90,0,10,35,65,90,30,45,75,90,100,25,40,65,100,
                  0,0,0,0,0,0,0,75,100,0,0,0,0,0,0,50,75,0
 ), nrow=5, byrow=TRUE)

 ## Define number of linguistic terms of input variables.
num.fvalinput <- matrix(c(5, 4, 5, 4), nrow=1)

varinput.1 <- c("veryPoor", "Poor", "Average","Good","veryGood")
varinput.2 <- c("Less", "Average", "Many", "aLot")
varinput.3 <- c("veryPoor", "Poor", "Average","Good","veryGood")
varinput.4 <- c("Less", "Average", "More","High")
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.
 ## Detailed information about values can be seen in the inference function.
type.defuz <- "WAM"
type.tnorm <- "MIN"
type.snorm <- "MAX"
type.implication.func <- "ZADEH"

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

## the names of variables
colnames.var <- c("Hotel_Facility_Score", "Visited_Count", "Room_facility_score", "Average_price", "Relative_Class")

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

## Give the names of the linguistic terms of the output variable.
varoutput.1 <- c("veryGood", "Good", "Average","Poor","veryPoor")
names.varoutput <- c(varoutput.1)


## Define the shapes and parameters of the membership functions of the output variables.
varout.mf <- matrix(c(5,5,5,5,5,
                  95,75,55,35,20,
                  4,8,5,7,5,
                  0,0,0,0,0,
                  0,0,0,0,0),
                nrow = 5, byrow = TRUE)

## Define the fuzzy IF-THEN rules;

rule <- matrix(
 c("veryGood", "and", "aLot", "and", "veryGood", "and", "less", "->", "veryGood",
"veryGood", "and", "Many", "and", "veryGood", "and", "Average", "->", "veryGood",
"veryGood", "and", "aLot", "and", "veryGood", "and", "Average", "->", "Good",
"veryGood", "and", "aLot", "and", "veryGood", "and", "Less", "->", "Good",
"Good", "and", "Many", "and", "Good", "and", "Less", "->", "Good",
"Good", "and", "aLot", "and", "Good", "and", "Average", "->", "Good",
"Average", "and", "aLot", "and", "Good", "and", "Less", "->", "Average",
"veryGood", "and", "Average", "and", "veryGood", "and", "More", "->", "Average",
"Good", "and", "Many", "and", "Good", "and", "Average", "->", "Average",
"Average", "and", "Average", "and", "Average", "and", "More", "->", "Poor",
"Good", "and", "Many", "and", "vGood", "and", "High", "->", "Poor",
"Average", "and", "Average", "and", "Average", "and", "High", "->", "Poor",
"Poor", "and", "Less", "and", "Poor","and", "High", "->", "veryPoor",
"veryPoor", "and", "Less", "and", "veryPoor", "and", "High", "->", "veryPoor"),
  nrow = 14, byrow = TRUE)

## Set type of model which is "MAMDANI".
type.model <- "MAMDANI"



## Generate a fuzzy model with frbs.gen.
object <- frbs.gen(range.data, num.fvalinput, names.varinput,
               num.fvaloutput, varout.mf, names.varoutput, rule,
               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)


newdata <- matrix(c(15, 80, 85, 85, 45, 75, 78, 70), nrow = 2, byrow = TRUE)

## Fuzzification Module:

num.varinput <- ncol(num.fvalinput)
MF <- fuzzifier(newdata, num.varinput, num.fvalinput, varinp.mf)


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


## Inference Module:
miu.rule <- inference(MF, ruleb, names.varinput, type.tnorm, type.snorm)

I am getting this error -

miu.rule <- inference(MF, ruleb, names.varinput, type.tnorm, type.snorm)

Error in MF[k, temp[j + 2]] : subscript out of bounds

I took help from the example give at this link - http://www.inside-r.org/packages/cran/frbs/docs/frbs.gen

But the example runs fine. I am not able to find what is the error in my code.

maggs
  • 763
  • 2
  • 9
  • 15

1 Answers1

0

I found a way to block the error: decrease your rule list down to 3 elements as in the example :-(.
But still don't know why!
BTW creating some additional rules (up to 5) in the example doesn't trigger error.
Edit 16/07/19: I don't retest the whole stuff but this is surely linked to the data/structure format.
Look the data/structure format of the example and stick thoroughly to this format for every data/structure you create and send to the package. This should solve your current error.

Pilip38
  • 11
  • 2