Skip to content

Instantly share code, notes, and snippets.

@huberflores
Last active November 11, 2015 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huberflores/a555b245e148c4cbad58 to your computer and use it in GitHub Desktop.
Save huberflores/a555b245e148c4cbad58 to your computer and use it in GitHub Desktop.
A fuzzy logic engine for computational offloading - simulation version
#
# author Huber Flores
#
# The input of the engine is a dataset. For each tuple of the dataset a decision is expected
# Each attribute in the tuple is normalized in a 0-1 interval
library(iterators)
library(foreach)
source("fuzzy-model.R")
myTraces <- read.csv("traces.csv")
accuracy <- c()
#0 wrong
#1 right
myAccuracy <- function(d){
decision(d$Energy.consumed.mAh., d$Bandwidth.RTT.ms., d$Processing.time.ms., d$Pre.cache.server, d$Decision)
}
accuracy <- foreach(i = iter(myTraces, by = "row")) %do% myAccuracy(i)
df <- as.data.frame(do.call(rbind, accuracy), col.names=FALSE)
write.table(df, "decision.csv")
#
# author Huber Flores
#
library(sets)
U1 <- seq(from = 0, to = 1, by = 0.0001)
#DIMENSIONS
variables <-
set(
code =
fuzzy_partition(varnames=
c(ELow = 0.2 , ENormal = 0.5, EHigh = 0.8),
FUN= fuzzy_cone, radius = 0.2, universe=U1),
bandwidth =
fuzzy_partition(varnames=
c(SLow = 0.2, SNormal=0.5, SHigh=0.8),
FUN = fuzzy_cone, radius = 0.2, universe=U1),
acceleration =
fuzzy_partition(varnames=
c(ALow = 0.2, ANormal=0.5, AHigh=0.8),
FUN = fuzzy_cone, radius = 0.2, universe=U1),
processing =
fuzzy_partition(varnames=
c(Local = 0.3, Remote = 0.7),
FUN = fuzzy_cone, radius = 0.3, universe=U1)
)
rules <-
set(
#set complete
fuzzy_rule(code %is% EHigh && bandwidth %is% SHigh && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SHigh && acceleration %is% ANormal, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SHigh && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% EHigh && bandwidth %is% SNormal && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SNormal && acceleration %is% ANormal, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SNormal && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% EHigh && bandwidth %is% SLow && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SLow && acceleration %is% ANormal, processing %is% Local),
fuzzy_rule(code %is% EHigh && bandwidth %is% SLow && acceleration %is% ALow, processing %is% Local),
#set complete
fuzzy_rule(code %is% ENormal && bandwidth %is% SHigh && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SHigh && acceleration %is% ANormal, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SHigh && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% ENormal && bandwidth %is% SNormal && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SNormal && acceleration %is% ANormal, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SNormal && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% ENormal && bandwidth %is% SLow && acceleration %is% AHigh, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SLow && acceleration %is% ANormal, processing %is% Local),
fuzzy_rule(code %is% ENormal && bandwidth %is% SLow && acceleration %is% ALow, processing %is% Local),
#set complete
fuzzy_rule(code %is% ELow && bandwidth %is% SHigh && acceleration %is% AHigh, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SHigh && acceleration %is% ANormal, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SHigh && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SNormal && acceleration %is% AHigh, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SNormal && acceleration %is% ANormal, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SNormal && acceleration %is% ALow, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SLow && acceleration %is% AHigh, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SLow && acceleration %is% ANormal, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SLow && acceleration %is% ALow, processing %is% Local),
#set complete
fuzzy_rule(code %is% EHigh && bandwidth %is% SHigh, processing %is% Remote),
fuzzy_rule(code %is% EHigh && bandwidth %is% SLow, processing %is% Local),
fuzzy_rule(code %is% EHigh && bandwidth %is% SNormal, processing %is% Local),
fuzzy_rule(code %is% ENormal && bandwidth %is% SLow, processing %is% Local),
fuzzy_rule(code %is% ENormal && bandwidth %is% SHigh, processing %is% Remote),
fuzzy_rule(code %is% ENormal && bandwidth %is% SNormal, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SLow, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SNormal, processing %is% Local),
fuzzy_rule(code %is% ELow && bandwidth %is% SHigh, processing %is% Local)
)
context <- fuzzy_system(variables, rules)
decision <- function(var1, var2, var3, var4, expected){
if (var4>=1){
return(1)
}
fi <- fuzzy_inference(context, list(code=var1, bandwidth=var2, acceleration=var3))
degree <- gset_defuzzify(fi, "centroid")
if (is.nan(degree)){
return(0)
}
if (degree>0.5){
result <- 1
}else{
result <- 0
}
if (all(result==expected)){
return(1)
}else{
return(0)
}
}
U1 <- NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment