# NOT RUN {
# load data and compute required properties
demo(radioactivePlumes_addProperties)
# initial, fix, possible sensor locations
I = nLocations(radioactivePlumes)
set.seed(1000)
locInit_l = t(replicate(10, sample.int(I, 5)))
locKeep_l = sample(setdiff(1:I, locInit_l), 2)
locAll_l = c(sample(setdiff(1:I, c(locInit_l, locKeep_l)),
round(I - (5 + 2)) * 0.5), locInit_l)
# the function is to be used inside of optimiseSD
# change algorithm specific parameters 'evalFunc', 'popSize', 'iters'
optimiseSD_genetic_1 = replaceDefault(
optimiseSD_genetic, newDefaults = list(
evalFunc = numberPenalty,
popSize = 20,
iters = 10
),
type = "optimisationFun.optimiseSD")[[1]]
# run optimisation
# }
# NOT RUN {
## takes some time
OptSD_gen1 = optimiseSD(
simulations = radioactivePlumes,
costFun = singleDetection,
optimisationFun = optimiseSD_genetic_1,
locationsAll = locAll_l,
locationsFix = locKeep_l,
locationsInitial = locInit_l[1,])
# }
# NOT RUN {
## this result is also in data(SDgenetic)
# }
# NOT RUN {
# result is not returned but saved to file
## case with 'aimCost'
optimiseSD_genetic_2 = replaceDefault(
optimiseSD_genetic, newDefaults = list(
evalFunc = numberPenalty,
popSize = 20,
iters = 20
),
type = "optimisationFun.optimiseSD")[[1]]
set.seed(07021916)
OptSD_gen2 = optimiseSD(
simulations = radioactivePlumes,
costFun = singleDetection,
optimisationFun = optimiseSD_genetic_2,
locationsAll = locAll_l,
locationsFix = locKeep_l,
locationsInitial = locInit_l[1,],
aimCost = 0.05,
nameSave = "optSD_genetic_2" # result is saved to file here
)
# OptSD_gen2 not found
## load generated populations and extract sampling designs and cost
load("optSD_genetic_2.Rdata")
finalIteration = sum(!is.na(populations[1,1,]))
costs = matrix(0, nrow = finalIteration, ncol = 20)
SDs = list()
for (j in 1:finalIteration){
SDs[[j]] = list()
for (i in 1:20){
costs[j,i] = numberPenalty(populations[i,,j],
simulations = radioactivePlumes,
costFun = singleDetection,
locationsAll = locAll_l,
locationsFix = locKeep_l,
aimNumber = length(locInit_l[1,]))
SDs[[j]][[i]] = c(locAll_l[populations[i,,j] == 1], locKeep_l)
if (sum(populations[i,,j]) > 5){
costs[j,i] = 2
}
print(paste(j,i))
}
}
apply(FUN = min, X = costs, MARGIN = 1) # best cost in each iteration
# best sampling design:
SDs[[finalIteration]][[which(costs[finalIteration,] == min(costs[finalIteration,]))]]
# }
Run the code above in your browser using DataLab