solution_of_10._random_regeneration
The Capsis training online
Solution: 11. Random regeneration
Try to work with the helping elements before looking at the solution…
Helping elements
- this exercice can be done in a new method called
regeneration ()
in the model class:TraModel
- this class is located in
capsis4/src/training/
, open it in your text editor
- the new regeneration method can be added anywhere under the constructors, it takes one parameter, the scene in which it must add new trees:
regeneration(TraScene newScene)
, it may returnvoid
- this method can be called from within the annual evolution loop in
TraModel.processEvolution ()
, after the growth of the trees (thus, the added trees will only grow next year)
a. Options for regenerationMax
- as a first option:
regenerationMax
can be declared as a local variable in the method, with a value of 10
- as a second option: (after finishing with option 1, be curious)
regenerationMax
can be declared instead as apublic
instance variable in theTraInitialParameters
class, now part of the initial parameters to be chosen by user at simulation initialisation time, with its default value of set to 10 (belowgrowthP4
), and thetraining.gui.TraInitialDialog
must be adapted to ask the user a value for it. You can make it by analogy withgrowthP4
: each line / section forgrowthP4
should be followed by a line / section forregenerationMax
. All new keys sent to the user interace withTranslator.swap ()
statements must be translated in english and french in resp.capsis4/src/training/TraLabels_en.properties
andTraLabels_fr.properties
// beware in okAction (), regenerationMax is not a double like gp4: double gp4 = TextFieldChecker.getPositiveDoubleValue(growthP4, Translator.swap("TraInitialDialog.growthP4")); // A possible check for the user value of rm: int rm = TextFieldChecker.getPositiveIntValue (regenerationMax, Translator.swap("TraInitialDialog.regenerationMax"))
b. Random numbers and tree coordinates
- Add an instance variable in TraModel for a random number generator:
private Random random;
- Add code to create the random number generator in the constructor of TraModel:
random = new Random ();
, now random can be used in all methods to draw random numbers
The java random numbers generator
Random
is a random number generator. an import java.util.Random
is needed.
It can be asked random ints or random doubles by calling nextInt(n)
: returns a value between 0 included and n excluded, or nextDouble()
: returns a double value between 0 included and 1 excluded (uniform distribution)
- create an instance with
random = new Random ()
- to draw a random integer in [0, n[, use
random.nextInt(n)
- to draw a random integer in [1, regenerationMax], use
random.nextInt(regenerationMax) + 1
- to draw an random number (double) in [0, 1[, use
random.nextDouble ()
- to draw an random number (double) in [0, xSize[, use
random.nextDouble () * xSize
- to draw an random number (double) in [originX, originX + xSize[, use
originX + random.nextDouble () * xSize
- Check the online javadoc by Oracle for more details on the Random class
- e.g.
double proba = random.nextDouble ();
# between 0 and 1 (1 excluded)
- use the random generator to get a value for
numberOfTrees
to be added, in [0, regenerationMax], make a loop from 1 tonumberOfTrees
- use random again to get
x
andy
coordinates for the tree (z
is 0 here)
Origin and size of the scene
In Capsis, the scene coordinates and sizes can be found in the scene class (i.e. the subclass of capsis.kernel.GScene. You can get the values with these accessors, they are all in meters:
getOrigin ()
: returns an instance ofVertex3d
withx
,y
andz
of the point with the lower (x, y) coordinatesgetXSize ()
andgetYSize ()
return the size of the scene along resp. thex
coordinates axis and they
axis- you will need to
import jeeb.lib.util.Vertex3d;
- e.g.
Vertex3d origin = newScene.getOrigin ()
,double xSize = newScene.getXSize ()
…
- e.g.
double x = origin.x + random.nextDouble () * xSize;
c. Tree identifiers and trees creation
- get a unique identifier for each new tree you create: all trees have an integer identifier, unique in the scene, to be able to follow a given tree along the simulation until it disappears (dead, cut…)
- Capsis proposes a
TicketDispenser
class to get unique ids: use the existingtreeIdDispenser
ofTraModel
to get a new id each time you need:int newId = treeIdDispenser.next ()
- once you have all the values you need (choose values for
crownBaseHeight
andcrownRadius
, in meters, and possiblyage = 1
…), to create a new Tree, use TraTree's constructor and pass it the expected values in order like this:
TraTree t = new TraTree (id, newScene, age, height, dbh, crownBaseHeight, crownRadius, x, y, z);
- and finally add this new tree in the scene like that:
newScene.addTree (t);
A possible solution
solution_of_10._random_regeneration.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1