Skip to content

Commit

Permalink
Update plan.R from probability errors
Browse files Browse the repository at this point in the history
The error with probability estimation is resolved by creating this new function.
  • Loading branch information
SapanTiwari authored Apr 12, 2024
1 parent d526476 commit b762a69
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion R/plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ generatePlans <- function(N, csv, endcsv, binCols, outdir, writeInterval) {
# binCols <-3:50 # specifies that columns 3-50 correspond to 48 time bins, i.e., 30-mins each
# outdir <-'../output/3.plan'
# writeInterval<-20 # write to file every so many plans

selectIndexFromProbabilities <- function(probs) {
if(is.null(probs) || sum(is.na(probs)) > 0 || length(probs) == 0) {
stop("probs is null, contains NAs, or is empty")
}

# Normalize probabilities to ensure they sum to 1
probs <- probs / sum(probs)

# Use sample() to select an index based on the weighted probabilities
# This approach ensures that the function will work even if probs is a vector
selected_index <- sample(x = 1:length(probs), size = 1, prob = probs)

return(selected_index)
}


suppressPackageStartupMessages(library(dplyr))
# Suppress summarise info
Expand Down Expand Up @@ -551,4 +567,4 @@ writePlan2Agent2GroupMap <- function(groupIds,
}
write.table(plan2agent, out_csv, row.names=FALSE, col.names=TRUE, quote=TRUE, sep=",", append=FALSE)

}
}

0 comments on commit b762a69

Please sign in to comment.