我一直在尝试使用R统计软件来构建模糊推理系统(FIS)。使用R包'frbs'我已经设法按照演示文件中的示例设置了FIS的大部分组件。不幸的是我遇到了一个问题:
我想修改规则库,使用'和'来'或'。修改没有问题,但frbs.gen命令似乎只能使用'和'生成规则。虽然我在帮助文件规则库中看到了一些使用'或'逻辑运算符的示例。以下是帮助文件的网址:http://127.0.0.1:12572/library/frbs/html/rulebase.html
当我重新运行frbs.gen
命令时,它似乎仍然自动使用原始规则库。它忽略了我对rule
变量所做的更改,并且即使在我更改规则库时也继续使用'和'逻辑运算符。 T_T其他人有同样的问题吗?
PS。更新到R(3.1.2)的最新版本,甚至在旧版本R(3.0.2)上尝试过,但frbs.gen仍然不会使用/允许'或'逻辑运算符....
这是完整的代码。
> demo(FRBS.Mamdani.Manual)
demo(FRBS.Mamdani.Manual)
---- ~~~~~~~~~~~~~~~~~~~
Type <Return> to start :
> ## This example shows how to use frbs without
> ## learning process.
> ## Note: some variables might be shared for other examples.
>
> ## Define shape and parameters of membership functions of input variables.
> ## Please see fuzzifier function to contruct the matrix.
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 fuzzy terms of input variables.
> ## Suppose, we have 3, 2, 3, and 3 numbers of fuzzy terms
> ## for first, second, third and fourth variables, respectively.
> num.fvalinput <- matrix(c(3, 2, 3, 3), nrow=1)
> ## Give the names of the fuzzy terms of each input variable.
> ## It should be noted that the names of the fuzzy terms must be unique,
> ## so we put a number for making it unique.
> 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)
> ## Set weighted average method to be used as defuzzification method.
> type.defuz <- "WAM"
> ## We are using standard t-norm and s-norm.
> 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(25, 40, 35, 15, 45, 75, 78, 70), nrow= 2, byrow = TRUE)
> ## the names of variables
> colnames.var <- c("input1", "input2", "input3", "input4", "output1")
> ###################################################################
> ## 1. The following codes show how to generate a fuzzy model using the frbs.gen function
> ## 1a. Using Mamdani Model
> ####################################################################
> ## Define number of fuzzy terms of output variable.
> ## In this case, we set the number of fuzzy terms to 3.
> num.fvaloutput <- matrix(c(3), nrow=1)
> ## Give the names of the fuzzy terms of the output variable.
> ## Note: the names of the fuzzy terms must be unique.
> 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 1 or 2 for Mamdani or Takagi Sugeno Kang model, respectively.
> ## In this case, we choose Mamdani model.
> type.model <- "MAMDANI"
> ## Define the fuzzy IF-THEN rules;
> ## there are two kinds of model: Mamdani and Takagi Sugeno Kang model
> ## 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.
> ## In this example we are using the Mamdani model
> ## (see the type.model parameter).
> ## Note:
> ## "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.
> 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)
> ## 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)
> ## We can plot the membership function
> plotMF(object)
Waiting to confirm page change...
> ## Predicting using new data.
> res <- predict(object, newdata)$predicted.val
R-demo文件结束。接下来的部分是我调整规则库然后重建FIS,最后检查它使用的规则库。
> rule
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "a1" "and" "b1" "and" "c1" "and" "d1" "->" "e1"
[2,] "a2" "and" "b2" "and" "c2" "and" "d2" "->" "e2"
[3,] "a3" "and" "b2" "and" "c2" "and" "d1" "->" "e3"
> rule <- 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)
> rule
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "a1" "or" "b1" "or" "c1" "or" "d1" "->" "e1"
[2,] "a2" "and" "b2" "and" "c2" "and" "d2" "->" "e2"
[3,] "a3" "and" "b2" "and" "c2" "and" "d1" "->" "e3"
> 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)
> object
$num.labels
[,1] [,2] [,3] [,4] [,5]
[1,] 3 2 3 3 3
$varout.mf
e1 e2 e3
[1,] 2 4 3
[2,] 0 20 60
[3,] 20 40 80
[4,] 40 60 100
[5,] NA 80 NA
$rule
[,1] [,2] [,3] [,4] [,5] [,6][,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14][,15] [,16] [,17] [,18][,19] [,20]
[1,] "IF" "input1" "is" "a1" "and" "input2" "is" "b1" "and" "input3" "is" "c1" "and" "input4"
"is" "d1" "THEN" "output1" "is" "e1"
[2,] "IF" "input1" "is" "a2" "and" "input2" "is" "b2" "and" "input3" "is" "c2" "and" "input4"
"is" "d2" "THEN" "output1" "is" "e2"
[3,] "IF" "input1" "is" "a3" "and" "input2" "is" "b2" "and" "input3" "is" "c2" "and" "input4"
"is" "d1" "THEN" "output1" "is" "e3"
$varinp.mf
a1 a2 a3 b1 b2 c1 c2 c3 d1 d2 d3
[1,] 2 4 3 2 3 2 1 3 2 4 3
[2,] 0 20 60 0 35 0 20 60 0 20 60
[3,] 20 40 80 35 75 20 50 80 20 40 80
[4,] 40 60 100 75 100 40 80 100 40 60 100
[5,] NA 80 NA NA NA NA NA NA NA 80 NA
$range.data.ori
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 100 100 100 100 100
$type.model
[1] "MAMDANI"
$type.tnorm
[1] "MIN"
$type.implication.func
[1] "ZADEH"
$type.mf
[1] "MIX"
$type.defuz
[1] "WAM"
$type.snorm
[1] "MAX"
$func.tsk
NULL
$method.type
[1] "MANUAL"
$name
[1] "Sim-0"
$colnames.var
[1] "input1" "input2" "input3" "input4" "output1"
$class
function (x) .Primitive("class")
attr(,"class")
[1] "frbs"
答案 0 :(得分:1)
我已向作者发送了一封电子邮件。他们提到这是代码中的一个错误,因此他们会修复它并尽快将最新版本上传到CRAN。以下是作者的电子邮件回复:
嗨Harakhun,
首先,我要感谢你指出我的包装上的错误。实际上,转换规则库时出现了错误的代码,我只是修复了它。因此,我随附了用于Windows的.zip版本3.0-0的软件包安装程序和用于unix的.tar.gz。您可以通过从本地文件安装精炼包来使用它。此外,我附上一个使用&#34;和&#34;的例子。和&#34;或&#34; R脚本中的运算符。如果您有任何其他问题,请告诉我。
更新的软件包以及新功能将尽快提交给CRAN。
致以最诚挚的问候,
Lala SR。