如何使用 R 的集合库有效地绘制模糊曲面

问题描述 投票:0回答:2

在 R 中,如何使用模糊逻辑的 set 包最好地在 3D 绘图中绘制模糊曲面?服务变量和食物变量应该是它们的x和y轴,z轴应该是通过模糊推理变量去模糊找到的质心。

我是 R 的初学者,尝试使用 Expand.grid 绘制模糊曲面的 4,000 个点,我的计算机磁盘达到 100%,即使在 2 次重新启动后也保持这种状态。 为了您计算机的安全,我故意不向您展示我编写的代码。

他们文档中的这个示例系统将是一个很好的起点。 你能帮我绘制结果吗? 谢谢。

library(sets)
# set universe
sets_options("universe", seq(from = 0, to = 25, by = 1))

# set up fuzzy variables
variables <-
  set(service = fuzzy_partition(varnames = c(poor = 0, good = 5, excellent = 10), sd = 1.5),
      food = fuzzy_variable(rancid = fuzzy_trapezoid(corners = c(-2, 0, 2, 4)),
                            delicious = fuzzy_trapezoid(corners = c(7, 9, 11, 13))),
      tip = fuzzy_partition(varnames = c(cheap = 5, average = 12.5, generous = 20),
                            FUN = fuzzy_cone, radius = 5)
  )

# set up rules
rules <-
  set(
    fuzzy_rule(service %is% poor || food %is% rancid, tip %is% cheap),
    fuzzy_rule(service %is% good, tip %is% average),
    fuzzy_rule(service %is% excellent || food %is% delicious, tip %is% generous)
  )

# combine to a system
system <- fuzzy_system(variables, rules)
print(system)
plot(system) ## plots variables

# do inference
fi <- fuzzy_inference(system, list(service = 3, food = 8.123))

# plot resulting fuzzy set
plot(fi)

# defuzzify
print(gset_defuzzify(fi, "centroid"))

# reset universe
sets_options("universe", NULL)
r plot set fuzzy-logic
2个回答
0
投票

尽管我对我的计算机造成了伤害,我还是决定继续试验。 下面的代码有效,但我仍然希望来自更有经验的程序员的回答。

# Please help me take this example from the docs and make a nice 3d plot
options(show.error.locations = TRUE)

library(sets)
## set universe
sets_options("universe", seq(from = 0, to = 25, by = 1))

## set up fuzzy variables
variables <-
  set(service = fuzzy_partition(varnames = c(poor = 0, good = 5, excellent = 10), sd = 1.5),
      food = fuzzy_variable(rancid = fuzzy_trapezoid(corners = c(-2, 0, 2, 4)),
                            delicious = fuzzy_trapezoid(corners = c(7, 9, 11, 13))),
      tip = fuzzy_partition(varnames = c(cheap = 5, average = 12.5, generous = 20),
                            FUN = fuzzy_cone, radius = 5)
  )

## set up rules
rules <-
  set(
    fuzzy_rule(service %is% poor || food %is% rancid, tip %is% cheap),
    fuzzy_rule(service %is% good, tip %is% average),
    fuzzy_rule(service %is% excellent || food %is% delicious, tip %is% generous)
  )

## combine to a system
system <- fuzzy_system(variables, rules)
print(system)
plot(system) ## plots variables

## do inference
fi <- fuzzy_inference(system, list(service = 3, food = 8))

## plot resulting fuzzy set
#plot(fi)

# define a function to compute a tip given a row 
# that has a service column and food column
defuzzify <- function(row){
  fi <- fuzzy_inference(system, list(service = row$service, food = row$food))
  gset_defuzzify(fi, "centroid")
}

# create a dataframe with food and service combinations to plot
food.sequence = seq(from = 0, to = 10, by = 1)
service.sequence = seq(from = 0, to = 10, by = 1)
df.to.plot <- expand.grid(food = food.sequence, service = service.sequence)

# for each food and service combination, compute a tip
# "by" is supposed to be better than a for loop
df.to.plot$tip <- by(df.to.plot, 1:nrow(df.to.plot), function(row) defuzzify(row))[]

# the plotting function that comes later requires a numeric matrix,
# so pivot by food and service and convert to a matrix
library(reshape2)
df <- dcast(df.to.plot, food ~ service, value.var = "tip")
df <- df[,-1] # get rid of the food column
row.names(df) <- food.sequence # name the rows
tip.matrix <- data.matrix(df)

# make a 3D interactive plot
library(plotly)
p <- plot_ly(z = tip.matrix) %>% add_surface() %>% 
  layout(title = "Tipping Plan",
         scene = list(
           xaxis = list(title = "Service (x)"),
           yaxis = list(title = "Food (y)"),
           zaxis = list(title = "Tip (z)")
         ) )

print(p)

## reset universe
sets_options("universe", NULL)

0
投票

你可以换成3维图吗?因为我有 3 种类型的变量。我无法修改你的代码。 “dcast 中的错误(df.to.plot,噪声〜Exposure_time〜年龄,value.var =“风险”): 数据框最多有两个输出维度”

© www.soinside.com 2019 - 2024. All rights reserved.