2024-03-27 15:09:20 +01:00
library ( " tidyverse" )
library ( " gridExtra" )
library ( " patchwork" )
library ( " knitr" )
library ( RColorBrewer )
2024-03-27 15:58:10 +01:00
library ( latex2exp )
2024-03-27 15:09:20 +01:00
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" ) }
2024-06-30 14:49:37 +02:00
simkeys = unique ( read_csv ( " ../results/ccgrid2023.csv" ) $ simkey )
2024-03-27 15:09:20 +01:00
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" )
2024-03-27 15:58:10 +01:00
return ( " Hints+\nExtended" )
2024-03-27 15:09:20 +01:00
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
2024-06-30 14:49:37 +02:00
stats_sor = build_stats ( " ../results/strategy_sor.csv" ) %>% filter ( type == " Sender" ) %>% mutate ( strategy = " SOR" )
stats_uor = build_stats ( " ../results/strategy_uor.csv" ) %>% filter ( type == " Sender" ) %>% mutate ( strategy = " UOR" )
stats_fh = build_stats ( " ../results/strategy_farhint.csv" ) %>% filter ( type == " Sender" ) %>% mutate ( strategy = " FH" )
stats_cmb = build_stats ( " ../results/strategy_combined.csv" ) %>% filter ( type == " Sender" ) %>% mutate ( strategy = " Combined" )
stats_none = build_stats ( " ../results/ccgrid2023.csv" ) %>% filter ( type == " Sender" ) %>% mutate ( strategy = " None" )
2024-04-04 13:15:56 +02:00
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)
2024-03-27 15:09:20 +01:00
pareto = tibble ( )
stats %>% ungroup ( ) %>% group_by ( wakeupfor , wireless ) %>% group_walk ( function ( data , t ) {
2024-04-04 13:15:56 +02:00
for ( i in 1 : NROW ( data ) ) {
2024-03-27 15:09:20 +01:00
pt = data [i , ]
e = pt $ energy
s = pt $ success
domE = data $ energy < e
2024-04-04 13:15:56 +02:00
domS = data $ success >= s
2024-03-27 15:09:20 +01:00
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 )
2024-04-04 13:15:56 +02:00
pareto = pareto %>% dformat ( )
2024-03-27 15:58:10 +01:00
2024-06-29 14:27:13 +02:00
##### Policies
ggplot ( stats %>% dformat ( ) , aes ( energy , success , color = simkey , shape = simkey ) ) +
2024-03-27 15:09:20 +01:00
geom_line ( data = pareto , aes ( energy , success ) , linetype = " dashed" , size = 1 , inherit.aes = FALSE ) +
2024-06-29 14:27:13 +02:00
geom_point ( size = 4 ) +
2024-03-27 15:09:20 +01:00
geom_point ( data = pareto , size = 4 ) + scale_y_reverse ( ) +
2024-06-29 14:27:13 +02:00
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 ) ) +
2024-03-27 15:58:10 +01:00
xlab ( " Sender energy consumption (J)" ) + ylab ( TeX ( r ' (#Succ$_p$)' ) )
# +facet_wrap(~wakeupfor+wireless,scale="free")
2024-06-30 14:49:37 +02:00
ggsave ( " figures/pareto_policies.pdf" , width = 10 , height = 9 )
2024-06-29 14:27:13 +02:00
##### 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")
2024-06-30 14:49:37 +02:00
ggsave ( " figures/pareto_strategies.pdf" , width = 10 , height = 9 )
2024-06-29 14:27:13 +02:00
2024-04-04 13:15:56 +02:00
message ( " Pareto infos:" )
print ( pareto %>% group_by ( simkey ) %>% summarize ( count = n ( ) ) )
print ( pareto %>% group_by ( strategy ) %>% summarize ( count = n ( ) ) )