Skip to content

Instantly share code, notes, and snippets.

@mcfrank
Created November 8, 2016 23:40
Show Gist options
  • Save mcfrank/05c5c7a4ee21c0500aca5890dcfd4157 to your computer and use it in GitHub Desktop.
Save mcfrank/05c5c7a4ee21c0500aca5890dcfd4157 to your computer and use it in GitHub Desktop.
Simple utility-theoretic planning for eye-movements
library(purr)
library(tidyverse)
library(stringr)
# constants
locations <- 1:3
timestep <- 1
t_end <- 6
t <- seq(0, t_end, timestep)
# utility cost of shifting
u_shift <- function() {
return(-1)
}
# utility of staying on any salient target
u_stay <- function(len) {
ts <- timestep:len
return(sum(ts^-.7))
}
# score plan
score_plan <- function(d) {
u <- d %>%
group_by(epoch) %>%
summarise(u_stay = u_stay(n() * timestep)) %>%
rowwise %>%
mutate(u_shift = ifelse(epoch == 1, 0, u_shift())) %>%
ungroup %>%
summarise(u_total = sum(u_stay) + sum(u_shift))
d$u_total <- u$u_total
return(d)
}
# do stuff
plans <- expand.grid(t0 = locations,
t1 = locations,
t2 = locations,
t3 = locations,
t4 = locations,
t5 = locations,
t6 = locations) %>%
mutate(plan_num = 1:n()) %>%
gather(t, location, t0:t6) %>%
arrange(plan_num) %>%
mutate(t = as.numeric(str_sub(t, 2, 2))) %>%
group_by(plan_num) %>%
mutate(shift = c(0,diff(location) != 0),
epoch = cumsum(shift) + 1) %>%
split(.$plan_num) %>%
map_df(score_plan)
# analyze paths
plans %>%
ungroup %>%
filter(u_total == max(u_total))
# n_shifts
n_shifts <- plans %>%
group_by(plan_num) %>%
summarise(n_shifts = sum(shift),
u_total = u_total[1])
ggplot(n_shifts, aes(x = n_shifts, y = u_total)) +
geom_jitter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment