loosely-coupled-dss-extended/results/pareto.R

119 lines
5.7 KiB
R

library("tidyverse")
library("gridExtra")
library("patchwork")
library("knitr")
library(RColorBrewer)
library(latex2exp)
r_=function(x){round(x,digits=2)}
color=function(){scale_fill_brewer(palette = "Accent")}
color2=function(){scale_fill_brewer(palette = "Set2")}
nolegend=function(){theme(legend.position="none")}
simkeys=unique(read_csv("results.csv")$simkey)
nsimkeys=length(simkeys)
s_=function(x){if(x<0){return("")}else{return("+")}}
simkey_rename=function(key){
if(key=="hint")
return("Hints")
if(key=="baseline")
return("Baseline")
if(key=="extended")
return("Extended")
if(key=="hintandextended")
return("Hints+\nExtended")
return(key)
}
dformat=function(data){
data%>%rowwise()%>%mutate(simkey=simkey_rename(simkey))%>%mutate(wireless=ifelse(wireless=="lora","LoRa","NbIoT"))
}
build_stats=function(file){
data=read_csv(file)
energy=data%>%group_by(simkey,wireless,seed,isSender,wakeupfor)%>%summarise(sd=sd(energy),energy=mean(energy))%>%mutate(type=ifelse(isSender,"Sender","Receiver"))
# Only sender knows success:
success=data%>%filter(isSender==1)%>%group_by(simkey,wireless,seed,wakeupfor)%>%summarise(success_orig=mean(nSend),success=mean(nSend))
return(energy%>%ungroup()%>%left_join(success))
energy60=data%>%filter(wakeupfor==60)
energy60Snd=energy60%>%filter(isSender==1)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy60Rcv=energy60%>%filter(isSender==0)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy60=energy60Snd%>%ungroup()%>%rbind(energy60Rcv)
print(energy60)
stopifnot(1)
success60Snd=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Sender")
success60Rcv=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Receiver")
success60=rbind(success60Snd,success60Rcv)
total60=energy60%>%left_join(success60,by=c("simkey","wireless","type"))
energy180=data%>%filter(wakeupfor==180)
energy180Snd=energy180%>%filter(isSender==1)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy180Rcv=energy180%>%filter(isSender==0)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy180=energy180Snd%>%ungroup()%>%rbind(energy180Rcv)
success180Snd=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Sender")
success180Rcv=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Receiver")
success180=rbind(success180Snd,success180Rcv)
total180=energy180%>%left_join(success180,by=c("simkey","wireless","type"))
return(rbind(total60%>%mutate(wakeupfor=60),total180%>%mutate(wakeupfor=180)))
}
# Load stats
stats_sor=build_stats("logs/sor_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="SOR")
stats_uor=build_stats("logs/uor_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="UOR")
stats_fh=build_stats("logs/farhint_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="FH")
stats_cmb=build_stats("logs/combined/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="Combined")
stats_none=build_stats("results_prev.csv")%>%filter(type=="Sender")%>%mutate(strategy="None")
stats=rbind(stats_sor,stats_uor,stats_fh,stats_cmb,stats_none)%>%filter(wireless=="nbiot",wakeupfor==60) # Change filter as you which (which plot you want down bellow)
pareto=tibble()
stats%>%ungroup()%>%group_by(wakeupfor,wireless)%>%group_walk(function(data, t){
for(i in 1:NROW(data)){
pt=data[i,]
e=pt$energy
s=pt$success
domE=data$energy<e
domS=data$success>=s
if(!any(domE & domS)){
pareto<<-rbind(pareto,cbind(pt,t))
}
}})
pareto = pareto %>% mutate(energy = as.numeric(energy))
pareto = pareto %>% mutate(success = as.numeric(success))
pareto = pareto %>% arrange(energy,success)
pareto = pareto %>% dformat()
##### Policies
ggplot(stats%>%dformat(),aes(energy,success,color=simkey,shape=simkey))+
geom_line(data=pareto,aes(energy,success),linetype="dashed", size=1,inherit.aes=FALSE)+
geom_point(size=4)+
geom_point(data=pareto,size=4)+scale_y_reverse()+
labs(color="Policies:",shape="Policies:")+scale_color_brewer(palette = "Set1")+theme_minimal()+theme(text=element_text(size=20), legend.position=c(.8,.75),legend.box.background = element_rect(color="black", size=1, fill="white"))+scale_shape_manual(values = c(17,18,20,3,4))+
xlab("Sender energy consumption (J)")+ylab(TeX(r'(#Succ$_p$)'))
# +facet_wrap(~wakeupfor+wireless,scale="free")
ggsave("pareto_policies.pdf",width=10,height=9)
##### Strategies
ggplot(stats%>%dformat(),aes(energy,success,color=strategy,shape=strategy))+
geom_line(data=pareto,aes(energy,success),linetype="dashed", size=1,inherit.aes=FALSE)+
geom_point(size=4)+
geom_point(data=pareto,size=4)+scale_y_reverse()+
labs(color="Strategies:",shape="Strategies:")+scale_color_brewer(palette = "Dark2")+theme_minimal()+theme(text=element_text(size=20), legend.position=c(.8,.75),legend.box.background = element_rect(color="black", size=1, fill="white"))+scale_shape_manual(values = c(17,18,20,3,4))+
xlab("Sender energy consumption (J)")+ylab(TeX(r'(#Succ$_p$)'))
# +facet_wrap(~wakeupfor+wireless,scale="free")
ggsave("pareto_strategies.pdf",width=10,height=9)
message("Pareto infos:")
print(pareto%>%group_by(simkey)%>%summarize(count=n()))
print(pareto%>%group_by(strategy)%>%summarize(count=n()))