I'm developing a model that simulates the growth of grass in different seasons.
Suppose a year in the simulation has 368 days (ticks). A year has four seasons: winter, spring, summer, fall. I want two different scenarios: 1) a scenario where each season is equally long: 368 days / 4 seasons = 92 days per season; 2) a scenario where the user can choose the length of each season.
For the first scenario, I coded it into the model as follows (see the "go" procedure):
to go
if season-days >= 92 [set number-of-season number-of-season + 1 ;; the season change and duration is determined in this line
ifelse current-season = 0
[set current-season 1]
[ifelse current-season = 1
[set current-season 2]
[ifelse current-season = 2
[set current-season 3]
[set current-season 0]
]
]
]
set simulation-time simulation-time + days-per-tick
set season-days season-days + days-per-tick
if season-days >= 93 [set season-days 1]
grow-grass
tick
end
My problem is when implementing the second scenario. I want to add four sliders at the interface that allows the user to change the length of each season (from a minimum of 0 days to a maximum of 368 days. It is important that the total sum of the four seasons are 368. For example, if user set winter-length = 368 days, the length of the remaining seasons should be 0). For example, suppose the duration for each season are set as follows:
winter-length 122 days
spring-length 62 days
summer-length 122 days
fall-length 62 days
(Total: 368 days)
Any ideas how to implement something like that in the code?
You can use the values of other global variables within a slider's minimum, increment, and maximum parameters. You can also include other code elements such as math operations (e.g., +
, -
, *
, /
, mod
) and other reporter primitives (e.g., min
, round
).
I would set the maximum of each parameter as 365 minus other parameters. For example, I'd set the maximum of the winter-length
slider as 365 - spring-length - summer-length - fall-length
. That way, whenever another slider's value is updated, the maximum of this slider will be automatically adjusted in the interface.
Here's a screenshot showing how to do it:
A minor caveat: if a slider's value is set to a number higher than its maximum, NetLogo does not automatically reduce the selected value to the new max. The red selector bar disappears, but the value remains the same. It should not be too big of a problem in this case because, in this setup, each slider's max depends on the other, so a user cannot accidentally create a setup of total days more than 365. Still, if you have an algorithm in your code that arbitrarily changes these global parameters, you would need to implement a validation algorithm in your code for this edge case.