# NOT RUN {
# The examples provided here are illustration of how to set up and run Monte Carlo
# analyses. Even though they are stripped-down minimalist examples they each still
# take 10-15 minutes to run.
# --------------------------------------------------------------------------
# }
# NOT RUN {
# A quick demonstration of baseline mode with postprocessing, but csv output disabled.
# Load the 1970-1999 version of the North Sea model supplied with the package
# Run the Monte Carlo process and generate some data but disable csv output.
# model <- e2e_read("North_Sea", "1970-1999")
# demo <- e2e_run_mc(model,nyears=2,baseline.mode=TRUE,n_iter=5,csv.output=FALSE)
# View the structure of the returned list:
# str(demo,max.level=1)
# View the structure of a returned list element containing sub-sets:
# str(demo$CI_annual_avmass,max.level=1)
# View the structure of a returned list element containing sub-sets:
# str(demo$CI_annual_fluxes,max.level=1)
# View the top few rows on the whole domain data on annual averge mass:
# head(demo$CI_annual_avmass$whole)
# }
# NOT RUN {
# --------------------------------------------------------------------------
# Dummy example to illustrate a more meaningful run. Output directed to
# "../Folder/results" relative to the current working directory (REPLACE with your
# own results.path before running):
# model <- e2e_read("North_Sea", "1970-1999",results.path="Folder/results")
# mc_results <- e2e_run_mc(model,baseline.mode=TRUE,nyears=50,n_iter=1000,csv,output=TRUE)
# WARNING: This run will take about 48h to complete, much better to split up and spread
# across multiple processors !
# --------------------------------------------------------------------------
# }
# NOT RUN {
# A quick demonstration run in baseline mode, save the data to a temporary folder:
basemodel <- e2e_read("North_Sea", "1970-1999",model.ident="mcbaseline")
basedemo <- e2e_run_mc(basemodel,nyears=2,baseline.mode=TRUE,
n_iter=5,csv.output=TRUE)
# }
# NOT RUN {
# --------------------------------------------------------------------------
# }
# NOT RUN {
# Then a quick demonstration run in scenario mode using the saved baseline parameter data,
# from the previous example, and save to csv. It is assumed that the baseline parameter
# data are in the temporary folder in which they were created, and that the same temporary
# folder is used for this example.
# First create an extreme fishing scenario - quadruple some gear activities, run for 10 years
scenariomodel<-basemodel
scenariomodel$setup$model.ident <- "mcscenario"
# Gear 1 (Pelagic trawls) activity rate rescaled to 4*baseline:
scenariomodel$data$fleet.model$gear_mult[1] <- 4
# Gear 4 (Beam_Trawl_BT1+BT2) activity rate rescaled to 4*baseline:
scenariomodel$data$fleet.model$gear_mult[4] <- 4
scendemo <- e2e_run_mc(scenariomodel,nyears=2,baseline.mode=FALSE,
use.example.baseparms=FALSE, baseparms.ident="mcbaseline",
begin.sample=1, n_iter=5,csv.output=TRUE)
# Compare the results of the baseline and scenario:
basemodel <- e2e_read("North_Sea", "1970-1999", model.ident="mcbaseline")
scenariomodel <- e2e_read("North_Sea","1970-1999",model.ident="mcscenario")
e2e_compare_runs_box(selection="ANNUAL", model1=basemodel, ci.data1=TRUE, use.saved1=TRUE,
model2=scenariomodel, ci.data2=TRUE, use.saved2=TRUE )
dev.new()
e2e_compare_runs_box(selection="MONTHLY", model1=basemodel, ci.data1=TRUE, use.saved1=TRUE,
model2=scenariomodel, ci.data2=TRUE, use.saved2=TRUE )
# }
# NOT RUN {
# --------------------------------------------------------------------------
# }
# NOT RUN {
# Quick demonstration of parallelizing the process in baseline mode, with output folder
# to a temporary folder. To explore further details of results.path="YourFolder"
# relative to the current working directory.
# Launch batch 1 (on processor 1):
model1 <- e2e_read("North_Sea", "1970-1999", model.ident="BATCH1")
results1 <- e2e_run_mc(model1,nyears=2,baseline.mode=TRUE,
n_iter=5,csv.output=TRUE,postprocess=FALSE)
# Launch batch 2 (on processor 2):
model2 <- e2e_read("North_Sea", "1970-1999", model.ident="BATCH2")
results2 <- e2e_run_mc(model2,nyears=2,baseline.mode=TRUE,
n_iter=6,csv.output=TRUE,postprocess=FALSE)
# Note that these two runs return only raw data since postprocess=FALSE
# Note that Batch 2 requests 6 iterations, rather that 5 in Batch 1.
# The number of iterations do not have to be the same in each batch.
# However, the first in each batch has to use the initial parameter set from the model setup,
# as this is the parent for all the subsequent samples generated during the run.
# When we come to merge the data from separate batches, data from the first iteration are
# stripped off and discarded for all but the first batch as we do not want to include duplicate
# data in the combined files. Hence we choose 6 iterations here in Batch 2 to make the point,
# and we expect the combined data to include 10 iterations.
#
# Then, afterwards, merge the two raw results files with text-tags BATCH1 and BATCH2,
# and post process the combined file:
model3 <- e2e_read("North_Sea", "1970-1999", model.ident="COMBINED")
processed_data <- e2e_merge_sens_mc(model3, selection="MC",
ident.list<-c("BATCH1","BATCH2"), postprocess=TRUE, csv.output=TRUE)
# or...
combined_data <- e2e_merge_sens_mc(model3, selection="MC",
ident.list<-c("BATCH1","BATCH2"), postprocess=FALSE, csv.output=TRUE)
processed_data<-e2e_process_sens_mc(model3,selection="MC",use.example=FALSE,
csv.output=TRUE)
# Plot the parameter likelihood distributions from the combined data
e2e_plot_sens_mc(model3, selection="MC")
# }
# NOT RUN {
# --------------------------------------------------------------------------
# }
# NOT RUN {
# Example of parallelizing the process in scenario mode, using the baseline parameter
# sets 'COMBINED' from above (assuming that this is sitting in the same temporary folder
# in which it was created in the example above.
# The activity of all fishing gears is reduced to zero to create a no-fishing scenario.
# Run each batch for 10 years as a relatively quick demo - a real run would need to
# run for at least 40 year
# Launch batch 1 (in processor 1):
model1s <- e2e_read("North_Sea", "1970-1999", model.ident="BATCH1_S")
# Activity rates of all 12 gears rescaled to 0*baseline:
model1s$data$fleet.model$gear_mult[1:12] <- 0
results1s <- e2e_run_mc(model1s,nyears=2,baseline.mode=FALSE, baseparms.ident="COMBINED",
begin.sample=1, n_iter=5,csv.output=TRUE,postprocess=FALSE)
# Launch batch 2 (on processore 2):
model2s <- e2e_read("North_Sea", "1970-1999", model.ident="BATCH2_S")
# Activity rates of all 12 gears rescaled to 0*baseline:
model2s$data$fleet.model$gear_mult[1:12] <- 0
results2s <- e2e_run_mc(model2s,nyears=2,baseline.mode=FALSE, baseparms.ident="COMBINED",
begin.sample=6, n_iter=5,csv.output=TRUE,postprocess=FALSE)
# Note that Batch 1 samples rows 1:5 of the baseline mode parameter set archive "COMBINED"
# (begin.sample=1, n_iter=5), so Batch 2 needs to start sampling at row 6 (begin.sample=6).
# The baseline archive contains 10 rows, so Batch 2 has the capacity to undertake up to 5
# sample iterations (rows 6:10). If we select more than 5 iterations (e.g. n_iter=8) then
# the code will automatically restrict to 5. Note that in fact, to be consistent with the
# format of output files from the baseline mode, each scenario mode run where
# 'begin.sample' > 1 will complete n_iter+1 iterations, by adding an initial run using the
# parameter values from row 1 of the baseline parameter set - which is then stripped off
# during merging.
#
# Then, merge the two raw results files with text-tags BATCH1_S and BATCH2_S, and post
# process the combined file:
model3s <- e2e_read("North_Sea", "1970-1999", model.ident="COMBINED_S")
processed_datas <- e2e_merge_sens_mc(model3s, selection="MC",
ident.list<-c("BATCH1_S","BATCH2_S"), postprocess=TRUE, csv.output=TRUE)
# Finally plot comparisons of the baseline and scenario model runs:
e2e_compare_runs_box(selection="ANNUAL", model1=model3, ci.data1=TRUE, use.saved1=TRUE,
model2=model3s, ci.data2=TRUE, use.saved2=TRUE )
dev.new()
e2e_compare_runs_box(selection="MONTHLY", model1=model3, ci.data1=TRUE, use.saved1=TRUE,
model2=model3s, ci.data2=TRUE, use.saved2=TRUE )
# }
# NOT RUN {
# --------------------------------------------------------------------------
# }
Run the code above in your browser using DataLab