模糊逻辑中推理系统的错误 - R

时间:2016-03-10 10:40:01

标签: r fuzzy-logic

我正在尝试使用frbs包在R中实现模糊推理系统。 这是我的代码 -

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)

我收到此错误 -

  

miu.rule&lt; - 推理(MF,ruleb,names.varinput,type.tnorm,type.snorm)

     

MF [k,temp [j + 2]]:下标超出范围

时出错

我从这个链接的例子中得到了帮助 - http://www.inside-r.org/packages/cran/frbs/docs/frbs.gen

但是这个例子运行正常。我无法找到代码中的错误。

1 个答案:

答案 0 :(得分:0)

names.varinput添加一个值,使其维度等于varinput的维度