knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Many examples in this vignette are taken from yaml files. If you try to cut and paste code from the text boxes in this document into yaml files of your own, you may find that they don't work. Yaml is hypersensitive to spacing and indentation and the formatting during generation of this vignette may not preserve spaces and indentation.
If you're having trouble getting the tzar project.yaml file to work, one thing to try is to cut and paste from the example yaml files associated with this vignette rather than from this document. Even then, you have to be careful. If things still don't work right, the first things to check are:
There are currently two main ways of doing experiments using bdpg. One is to do a single type of action repeatedly, e.g, create a new biodiversity problem or run a reserve selector on an existing problem. The other is perform many different actions at one time, e.g., create a new problem then add error to it and wrap another problem around it and run various reserve selections on all of these different problems. These two views are represented by the bdpg functions called single_action_using_tzar_reps
and gen_4_basic_variants
.
The single action view is most useful when running a large number of experiments that will take a long time and you want to be able to examine the all of the results of one type of action while other actions are still running. For example, computing igraph network metrics on a problem is currently quite slow, but the results are only relevant to one kind of experimental question. Consequently, it may be more practical to generate a set of problems and run reserve selectors on them before doing any of the network calculations. Then, you can examine the reserve selector performance while the network metrics are running. Similarly, you may want to add a new kind of reserve selector after all of the other experiments have been done and so you want to just run that reserve selector across all of the existing problems.
The 4 variants view is most useful when developing some new option that needs exercising over the full set of problem types, i.e., the four variants. These are a basic correct problem, a basic apparent problem, a wrapped correct problem, and an apparent version of a wrapped problem. A different use of the 4 variants approach is in generating a large initial set of problems and results all at one time. In that case, a generator would just run the 4 variants code many times with different random seeds.
Unlike the single action view, the 4 variants view does not generally need a list of input files to act over.
While these two views are the most commonly used ones, you can write a custom handler that does something more specific to your own needs, e.g., a method that generates many apparent problems for a given existing correct problem and then runs network metrics and reserve selectors on each of the apparent problems.
================================================================================
Here is some example mainline code for calling these runset controllers (without any error checking) given an input parameter list generated by tzar or by your own code.
if (parameters$gen_4_basic_variants) { gen_4_basic_variants (parameters, round) } else if (parameters$single_action_using_tzar_reps) { single_action_using_tzar_reps (parameters, round) } else { stop ("No matching actions found.") }
================================================================================
Except for the gen_COR_prob
action, all of the single actions require you to specify the location of an rds file or set of locations of rds files to use as input to the desired action. If you're specifying more than one rds file, then you will also have to give a value for the cur_input_prob_idx
variable to index into the set of rds files. All of this is detailed below, but in short, it means that most single actions will include commands similar to the following in the yaml file:
base_params: single_action_using_tzar_reps: TRUE run_network_metrics_on_prob: TRUE prob_src: rds_file_set_from_file rds_file_set_path: "~/Work/net_rds_input_file_paths.txt" repetitions: generators: - key: cur_input_prob_idx generator_type: linear_step start: 1 step_size: 1 count: 3
Details of each aspect of single actions are given below, however, if you prefer to start from examples, examples of each action are given at the end of this section. Very specific details of exactly how the single action view works can be found in the code for the function single_action_using_tzar_reps()
and other functions in the source file single_action_using_tzar_reps.R.
Finally, note that each single action creates a separate tzar run for each value of parameters$cur_input_prob_idx when running under tzar.
single_action_using_tzar_reps
set to TRUEsingle_action_using_tzar_reps
parameter is TRUEgen_COR_prob
gen_single_bdprob_COR ()
gen_WRAP_prob
gen_single_bdprob_WRAP ()
gen_APP_prob
gen_single_bdprob_APP ()
run_rs_on_COR_prob
do_COR_rs_analysis_and_output ()
run_rs_on_APP_prob
do_APP_rs_analysis_and_output ()
run_network_metrics_on_prob
init_object_graph_data ()
net_batch_out_dir_name
parameter to specify the location where the modified problems will be written out after the network results are attached to them.Any problem that is to be acted on by one of the single action commands, is read in from a ".rds" file. These are files in the format R uses to save objects to disk. Whenever bdpg creates a problem, it saves that problem to disk in a ".rds" file using R's saveRDS()
function and that saved file can be used as input to a single action command.
There are 3 boolean parameters that can be used to specify the problem source for a single action and each of these has a complementary parameter specifying the location of the associated information.
rds_file
rds_file_path
. prob_src: "rds_file" rds_file_path: "~/Work/some_problem.rds"
rds_file_set_from_file
rds_file_set_path
. prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/NET_rds_input_file_paths.txt"
- Example input text file "~/Work/Tzar_input_files/NET_rds_input_file_paths.txt" containing locations:
~/tzar/outputdata/bdpgxupaper/default_runset/49_marxan_simulated_annealing/RSprob-COR-Base.fb9e78fc-5a6d-49b7-b8a9-74e918a86c92/saved.RSprob-COR-Base.fb9e78fc-5a6d-49b7-b8a9-74e918a86c92.rds ~/tzar/outputdata/bdpgxupaper/default_runset/51_marxan_simulated_annealing/RSprob-COR-Base.7151efe2-e13b-428b-ac76-62edc3b6b6a5/saved.RSprob-COR-Base.7151efe2-e13b-428b-ac76-62edc3b6b6a5.rds ~/tzar/outputdata/bdpgxupaper/default_runset/50_marxan_simulated_annealing/RSprob-COR-Base.0881452f-709a-4a4e-bd4c-9f502e1306c5/saved.RSprob-COR-Base.0881452f-709a-4a4e-bd4c-9f502e1306c5.rds
rds_file_set_from_yaml_array
rds_file_set_yaml_array
. prob_src: "rds_file_set_from_yaml_array" rds_file_set_yaml_array: - "~/tzar/outputdata/bdpgxupaper/default_runset/49_marxan_simulated_annealing/RSprob-COR-Base.fb9e78fc-5a6d-49b7-b8a9-74e918a86c92/saved.RSprob-COR-Base.fb9e78fc-5a6d-49b7-b8a9-74e918a86c92.rds" - "~/tzar/outputdata/bdpgxupaper/default_runset/51_marxan_simulated_annealing/RSprob-COR-Base.7151efe2-e13b-428b-ac76-62edc3b6b6a5/saved.RSprob-COR-Base.7151efe2-e13b-428b-ac76-62edc3b6b6a5.rds" - "~/tzar/outputdata/bdpgxupaper/default_runset/50_marxan_simulated_annealing/RSprob-COR-Base.0881452f-709a-4a4e-bd4c-9f502e1306c5/saved.RSprob-COR-Base.0881452f-709a-4a4e-bd4c-9f502e1306c5.rds"
While the sets of ".rds" file locations can be generated by hand, an easier way is often to pipe the unix find
and grep
commands into a file, as in the following example:
find ~/tzar/outputdata/single_action_WRAP_prob/default_runset | grep "saved" > WRAP_rds_input_file_paths.txt
Single actions that are to be run across a set of problems need to have an index variable for that set passed in as an element of the parameters list. This is essentially an index to use in a for
loop over the elements of the set. Tzar's generators load this variable automatically if you set up a repetitions section in the project.yaml file that specifies cur_input_prob_idx
as the key
. The index just specifies which element of the set will be accessed on a given run.
cur_input_prob_idx
to satisfy what single_action_using_tzar_reps()
expects.count
. It just has to match the number of lines/files given in the yaml array or text file of rds file locations that you are feeding to bdpg.repetitions: generators: - key: cur_input_prob_idx generator_type: linear_step start: 1 step_size: 1 count: 3
If you're running repetitions that have a count > 1, then tzar emulation can't handle that. It will run tzar to generate a directory for each of the runs, but it will only run your code on the last run of the set. If you're going to run the code under tzar instead of under the emulator, there are several things you need to do:
emulating_tzar: FALSE #TRUE
cp model.R.tzar model.R
> java -jar <your-tzar-jar-dir>/tzar.jar execlocalruns <your-R-code-dir> Created 3 runs. Outputdir: ~/tzar/outputdata/Easy/default_runset/15_Easy_single_action_gen_3_COR_prob.inprogress Running model: ~/D/Projects/ProblemDifficulty/pkgs/bdpgxupaper/R, run_id: 15, Project name: Easy, Scenario name: Easy_single_action_gen_3_COR_prob, Flags: Run 15 succeeded. Outputdir: ~/tzar/outputdata/Easy/default_runset/16_Easy_single_action_gen_3_COR_prob.inprogress Running model: ~/D/Projects/ProblemDifficulty/pkgs/bdpgxupaper/R, run_id: 16, Project name: Easy, Scenario name: Easy_single_action_gen_3_COR_prob, Flags: Run 16 succeeded. Outputdir: ~/tzar/outputdata/Easy/default_runset/17_Easy_single_action_gen_3_COR_prob.inprogress Running model: ~/D/Projects/ProblemDifficulty/pkgs/bdpgxupaper/R, run_id: 17, Project name: Easy, Scenario name: Easy_single_action_gen_3_COR_prob, Flags: Run 17 succeeded. Executed 3 runs: 3 succeeded. 0 failed
This section gives an example of the tzar project.yaml file commands for each of the recognized single action types.
gen_COR_prob
gen_COR_prob: TRUE
gen_WRAP_prob
on base COR problemgen_WRAP_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/COR_rds_input_file_paths.txt"
gen_APP_prob
on base COR problemgen_APP_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/COR_rds_input_file_paths.txt"
gen_APP_prob
on wrap COR problem#find ~/tzar/outputdata/bdpgxupaper_single_action_WRAP_prob/default_runset | grep saved > WRAP_rds_input_file_paths.txt gen_APP_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/WRAP_rds_input_file_paths.txt"
run_rs_on_COR_prob
on base COR problemrun_rs_on_COR_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/COR_rds_input_file_paths.txt"
run_rs_on_APP_prob
on base APP problem#find ~/tzar/outputdata/bdpgxupaper_single_action_APP_of_cor_base_prob/default_runset | grep saved > ~/Work/Tzar_input_files/APP_cor_rds_input_file_paths.txt run_rs_on_APP_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/APP_cor_rds_input_file_paths.txt"
run_rs_on_APP_prob
on wrap APP problem#find ~/tzar/outputdata/bdpgxupaper_single_action_APP_of_cor_base_prob/default_runset | grep saved > ~/Work/Tzar_input_files/APP_cor_rds_input_file_paths.txt run_rs_on_APP_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/APP_wrap_rds_input_file_paths.txt"
run_network_metrics_on_prob
#find ~/tzar/outputdata/bdpgxupaper_single_action_APP_of_cor_base_prob/default_runset | grep saved > ~/Work/Tzar_input_files/APP_cor_rds_input_file_paths.txt run_network_metrics_on_prob: TRUE prob_src: "rds_file_set_from_file" rds_file_set_path: "~/Work/Tzar_input_files/APP_wrap_rds_input_file_paths.txt"
This section gives an example of R code to invoke each of the recognized single action types if you were not using the tzar project.yaml file to control the bdpg code.
gen_COR_prob
num_runs = 3 parameters <- list (gen_COR_prob = TRUE) for (cur_idx in 1:num_runs) { parameters$cur_input_prob_idx = cur_idx single_action_using_tzar_reps (parameters, round) }
gen_WRAP_prob
gen_APP_prob
run_rs_on_COR_prob
run_rs_on_APP_prob
run_network_metrics_on_prob
gen_4_basic_variants
set to TRUE================================================================================
The figure sizes have been customised so that you can easily put two images side-by-side.
plot(1:10) plot(10:1)
You can enable figure captions by fig_caption: yes
in YAML:
output: rmarkdown::html_vignette: fig_caption: yes
Then you can use the chunk option fig.cap = "Your figure caption."
in knitr.
You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using knitr::kable()
.
knitr::kable(head(mtcars, 10))
Also a quote using >
:
"He who gives up [code] safety for [code] speed deserves neither." (via)
================================================================================
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.