```{r ch_opt, child="include/chunk_options.rmd", cache=FALSE}
```


```{r, echo = FALSE}
library(nlexperiment)
# Set the path to your NetLogo instalation
nl_netlogo_path("c:/Program Files/NetLogo 6.0.1/app/") 
```

The following example is using NetLogo Fur model (Wilensky 2003) to show
explicit parameter values definition.

<aside>
Wilensky, U. (2003). 
[NetLogo Fur model](http://ccl.northwestern.edu/netlogo/models/Fur). 
Center for Connected Learning and Computer-Based Modeling, 
Northwestern University, Evanston, IL.
</aside>

There are 5 parameters in the NetLogo Fur model:

* `ratio`
* `outer-radius-y`
* `inner-radius-y`
* `outer-radius-x`
* `inner-radius-x`

But considering constraints and model symmetry
we can reduce the parameters to:

* `ratio` (the inhibitor concentration parameter)
* `rdiff` (the difference between x and y radius) and
* `gap` (distance between inner and outer ellipse)

<aside>
See also 
[this site](http://classes.yale.edu/fractals/Panorama/Biology/Leopard/Leopard.html)
for model description.
</aside>

Example below still uses factorial design but on transformed parameter space
`ratio * rdiff` while keeping the `gap` between the circles constant.

```{r furParamSets, title = "Prepare parameter sets"}
#All combinations of ratio x rdiff 
param_sets <- expand.grid(
  gap = 3,
  rdiff = seq(0, 2, by = 0.5),
  ratio = seq(0.30, 0.65, by = 0.05)
)
# Add NetLogo variables
param_sets <- transform(param_sets,
  inner_radius_x = 3,                         
  outer_radius_x = 3 + gap,
  inner_radius_y = 3 + rdiff,
  outer_radius_y = 3 + rdiff + gap
)
```
<aside>
R function `expand.grid` creates a data frame from all combinations of factors

`transform` adds new columns to the data frame as defined by expressions
</aside>

Instead of list with parameter values as in previous examples, 
we can use this data frame with explicit parameter sets 
to define `param_values`:

```{r furModel, title = "Define the experiment"}
experiment <- nl_experiment(
  model_file = "models/Sample Models/Biology/Fur.nlogo",
  iterations = 20,
  
  param_values = param_sets,      # here param_sets is a data frame
  
  mapping = c(
    gap = "",
    rdiff = "",
    inner_radius_x = "inner-radius-x",
    outer_radius_x = "outer-radius-x",
    inner_radius_y = "inner-radius-y",
    outer_radius_y = "outer-radius-y"
  ),
  patches_after = list(
    patches = patch_set(
      vars = c("pxcor", "pycor", "pcolor"),
      patches = "patches"
    )
  ),
  random_seed = 3
)
```

<aside>
Note that variables `gap` and `radius_diff` must be mapped to empty string (since 
they are not NetLogo model variables).
</aside>


```{r furRun, title = "Run the experiment", cache=TRUE}
result <- nl_run(experiment, parallel = TRUE, max_cores = 3) 
```
<aside>
Parallel execution is limited to 3 cores since there will be
only $5 \times 8 = 40$ simulation runs.
</aside>


```{r title = "Show fur patterns", furShowPatches}
library(ggplot2)
nl_show_patches(result, x_param = "ratio", y_param = "rdiff") +
  scale_fill_manual(values = c("black","white")) +
  coord_fixed() +
  labs(y=expression(radius[y] - radius[x]), title = "Fur patterns (gap = 3)")
```


## Related

[Flocking Example](flocking.html) shows simple measure definition and observation in parameter space. It is the first in a series of parameter space exploration of the Flocking model.

[Categorical Criteria](flocking_categorical.html) and 
[Hyper Latinc Cube Sampling](flocking_sampling.html) examples
demontrate how to explore parameter space with categorical 
criteria and sampling methods.

<asideclose>
Get [rmarkdown source](`r knitr::current_input()`) of this page.
</asideclose>
