User Tools

The Capsis training online

Back to the table of contents

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 return void
  • 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 a public instance variable in the TraInitialParameters class, now part of the initial parameters to be chosen by user at simulation initialisation time, with its default value of set to 10 (below growthP4), and the training.gui.TraInitialDialog must be adapted to ask the user a value for it. You can make it by analogy with growthP4: each line / section for growthP4 should be followed by a line / section for regenerationMax. All new keys sent to the user interace with Translator.swap () statements must be translated in english and french in resp. capsis4/src/training/ and
// 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 to numberOfTrees
  • use random again to get x and y 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 of Vertex3d with x, y and z of the point with the lower (x, y) coordinates
  • getXSize () and getYSize () return the size of the scene along resp. the x coordinates axis and the y 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 existing treeIdDispenser of TraModel to get a new id each time you need: int newId = ()
  • once you have all the values you need (choose values for crownBaseHeight and crownRadius, in meters, and possibly age = 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 elements, to be used in a TraModel.regeneration (TraScene newScene) method A close example to be adapted from a former training module: MaddModel.processRegeneration(MaddScene newStand) A view of the initial dialog for option 2: A textfield has been added to ask user a value for regenerationMax, but translation are missing in the langage properties files Checking: opening a 3D viewer from the 2D viewer after a 20 years long simulation: the new trees are visible, they are growing with the others The initial dialog with the fixed translations

Back to the table of contents

solution_of_10._random_regeneration.txt · Last modified: 2021/02/04 15:06 by coligny