2017-01-08 5 views
0

R에서는 퍼지 로직 세트 패키지를 사용하여 3D 플롯에서 퍼지 표면을 가장 잘 플로팅 할 수 있습니까? 서비스 변수와 푸드 변수는 x 축과 y 축이어야하며 z 축은 퍼지 추론 변수를 비 퍼징 (defuzzifying)하여 발견 된 중심 값이어야합니다.R의 세트 라이브러리를 사용하여 퍼지 표면을 효율적으로 플롯하는 방법

저는 expand.grid를 사용하여 퍼지 표면의 4,000 개 점을 그려 본 R의 초보자입니다. 컴퓨터 디스크는 2 % 다시 시작한 후에도 100 %로 이동하고 그대로있었습니다. 컴퓨터의 안전을 위해 작성한 코드를 의도적으로 표시하지 않겠습니다.

이 문서의 예제 시스템은 좋은 출발점이 될 것입니다. 결과를 줄 수있게 도와 주시겠습니까? 감사.

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) 

답변

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)