How to save the values of all iterations in R

4

Hello,

I am making a code in R to calculate the various values of a Fuzzy code (using the "Sets" package).

I made the logic for it to calculate three Fuzzy variables from smallest to largest, but when I use logic to save these values, it just saves the last values.

I wanted to create a logic to save all possible values.

Follow the code below:

library(sets)

package:datasets
## set universe
sets_options("universe", seq(from = 0, to = 100, by = 0.1))
## set up fuzzy var
iables
variables <-
  set(  Ratio =
          fuzzy_variable(low =
                           fuzzy_trapezoid(corners = c(0, 40, 50, 60)),
                         medium =
                           fuzzy_trapezoid(corners = c(40, 50, 80, 90)),
                         ok =
                           fuzzy_trapezoid(corners = c(80, 90, 95, 100))),

        TamanhoFila =
          fuzzy_variable(ok =
                           fuzzy_trapezoid(corners = c(0,10, 20, 30)),
                         alerta=
                           fuzzy_trapezoid(corners = c(20, 40, 70 , 80)),
                         ng =
                           fuzzy_trapezoid(corners = c(70, 80, 95, 100))),

        TempoFila =
          fuzzy_variable(ok =
                           fuzzy_trapezoid(corners = c(0, 10, 20, 30)),
                         alerta=
                           fuzzy_trapezoid(corners = c(20, 30, 60 , 70)),
                         ng =
                           fuzzy_trapezoid(corners = c(70, 80, 95, 110))),

        recursos =
          fuzzy_partition(varnames =
                            c(ok = 25, alerta = 50, metade = 75, total = 100),
                          FUN = fuzzy_cone, radius = 25)
  )
## set up rules
rules <-
  set(
    fuzzy_rule(Ratio %is% low || TamanhoFila %is% NG || TempoFila %is% NG,
               recursos %is% total),
    fuzzy_rule(Ratio %is% low || TamanhoFila %is% alerta || TempoFila %is% alerta,
               recursos %is% metade),
    fuzzy_rule(Ratio %is% medium || TamanhoFila %is% alerta || TempoFila %is% alerta,
               recursos %is% alerta),
    fuzzy_rule(Ratio %is% medium || TamanhoFila %is% OK || TempoFila %is% OK,
               recursos %is% ok),
    fuzzy_rule(Ratio %is% ok ,
               recursos %is% ok)


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


for(x in 1:2){

  for (y in 1:2){

    for (z in 1:2){

      print(x)
      print(y)
      print(z)

      x1<- c(x*10)
      x2<- c(y*10)
      x3<- c(z*10)

      fi <- (fuzzy_inference(system, list(Ratio = x*10, TempoFila = y*10, TamanhoFila = z*10)))
      ## plot resulting fuzzy set
      #plot(fi)

      ## defuzzify
      fuzzy <- ( gset_defuzzify(fi, "centroid"))

      FuzzyT<- data.frame(fuzzy)

      #print(x)
      print(fuzzy)


      Fuzzy_T <- c(x1, x2, x3, FuzzyT)
      Fuzzy_T2 <- write.csv(Fuzzy_T, file="fuzzy.csv")



      }


  }


}


## reset universe

sets_options("universe", NULL)

Thank you very much

    
asked by anonymous 16.11.2017 / 21:22

1 answer

4

For each interaction x , y or z , you are defining a new csv. One solution is to simply create a data.frame before the loop and save each interaction on a line of data.frame :

# create an empty data frame
Fuzzy_T <- as.data.frame(matrix(NA, ncol = 4, nrow = 1))

# name data frame 
names(Fuzzy_T) <- c("x1", "x2", "x3", "fuzzy")

# value for count
count <- 0

for(x in 1:2){

  for (y in 1:2){

    for (z in 1:2){

      print(x)
      print(y)
      print(z)

      x1<- c(x*10)
      x2<- c(y*10)
      x3<- c(z*10)

      fi <- (fuzzy_inference(system, list(Ratio = x*10, TempoFila = y*10, TamanhoFila = z*10)))
      ## plot resulting fuzzy set
      #plot(fi)

      ## defuzzify
      fuzzy <- ( gset_defuzzify(fi, "centroid"))

      #print(x)
      print(fuzzy)

      # add count
      count <- count + 1

      # assign value
      Fuzzy_T[count, ] <- c(x1, x2, x3, fuzzy)

    }
  }
}

write.csv(Fuzzy_T, file="fuzzy.csv")

Result:

> Fuzzy_T
  x1 x2 x3    fuzzy
1 10 10 10 76.55291
2 10 10 20 76.55291
3 10 20 10 76.55291
4 10 20 20 76.55291
5 20 10 10 78.00137
6 20 10 20 78.00137
7 20 20 10 78.00137
8 20 20 20 78.00137
>
    
16.11.2017 / 21:58