Class IBSMCPopulation

Direct Known Subclasses:
IBSCPopulation

public class IBSMCPopulation extends IBSPopulation<Continuous, IBSMCPopulation>
The core class for individual based simulations with multiple continuous traits. Manages the traits of the population, while delegating the management of the population and individual fitness as well as simulation steps to super. Note that some further optimizations and simplifications are possible in the special case of a single continuous trait, which is handled in the subclass IBSCPopulation.
Author:
Christoph Hauert
See Also:
  • Field Details

    • pairmodule

      protected Model.HasIBS.MCPairs pairmodule
      For pairwise interaction modules module==pairmodule holds and null otherwise. Convenience field to reduce the number of (unnecessary) casts.
      See Also:
    • groupmodule

      protected Model.HasIBS.MCGroups groupmodule
      For group interaction modules module==groupmodule holds and null otherwise. Convenience field to reduce the number of (unnecessary) casts.
      See Also:
    • traitRangeMin

      protected double[] traitRangeMin
      The array with the minimal values for each trait. Convenience variable to reduce calls to module.

      Note: Internally traits are always scaled to [0, 1].

      See Also:
    • traitRangeMax

      protected double[] traitRangeMax
      The array with the maximal values for each trait. Convenience variable to reduce calls to module.

      Note: Internally traits are always scaled to [0, 1].

      See Also:
    • traits

      double[] traits
      The array of individual traits. The traits of individual i are stored at traits[i * nTraits] through traits[(i + 1) * nTraits - 1]
    • traitsNext

      double[] traitsNext
      The array for temporarily storing traits during updates.
    • tmpGroup

      double[] tmpGroup
      Temporary storage for traits of individuals in group interactions.
    • smallTrait

      double[] smallTrait
      Temporary storage for traits of individuals in small sub-group interactions.
    • myTraits

      double[] myTraits
      Temporary storage for the traits of the focal individual.
    • oldTraits

      double[] oldTraits
      Temporary storage for the traits of the focal individual before the update. Used for adjusting scores.
    • oldScores

      double[] oldScores
      Temporary storage for the scores of each participant prior to group interactions.
    • meantrait

      private double[] meantrait
      The array for calculating and storing the mean traits and their standard deviation. Must be of length > 2 * nTraits.
    • init

      protected IBSC.Init init
      Type of initial configuration.
      See Also:
  • Constructor Details

    • IBSMCPopulation

      public IBSMCPopulation(EvoLudo engine, Continuous module)
      Creates a population of individuals with multiple continuous traits for IBS simulations.
      Parameters:
      engine - the pacemaker for running the model
      module - the module that defines the game
  • Method Details

    • setOpponentPop

      public void setOpponentPop(IBSPopulation<?,?> opponent)
      Description copied from class: IBSPopulation
      Set the interaction partner/opponent of this population.
      Overrides:
      setOpponentPop in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      opponent - the interaction partner/opponent
    • checkConvergence

      public boolean checkConvergence()
      Description copied from class: IBSPopulation
      Check if population has converged. By default true if population is monomorphic and no (zero) mutations. However, different implementations may have different criteria for convergence.

      Note: This tends to be less restrictive than reaching an absorbing state. Typically convergence is used as a criterion to abort simulations.

      Overrides:
      checkConvergence in class IBSPopulation<Continuous, IBSMCPopulation>
      Returns:
      true if converged.
    • getTraitRangeMin

      public double[] getTraitRangeMin()
      Get the minima for all traits.
      Returns:
      the array with the trait minima
    • getTraitRangeMax

      public double[] getTraitRangeMax()
      Get the maxima for all traits.
      Returns:
      the array with the trait maxima
    • getTraitAt

      public double getTraitAt(int idx)
      Get the trait of individual with index idx.
      Parameters:
      idx - the index of the individual
      Returns:
      the trait of the individual
    • getTraitAt

      public double getTraitAt(int idx, int d)
      Get trait d of individual with index idx.
      Parameters:
      idx - the index of the individual
      d - the trait index
      Returns:
      the trait of the individual
    • setTraitAt

      public void setTraitAt(int idx, double trait)
      Set the trait of individual with index idx to trait.
      Parameters:
      idx - the index of the individual
      trait - the trait of the individual
    • setTraitAt

      public void setTraitAt(int idx, int d, double trait)
      Set trait d of the individual with index idx.
      Parameters:
      idx - the index of the individual
      d - the trait index
      trait - the new trait value
    • getTraitsAt

      public double[] getTraitsAt(int idx)
      Get the trait array of individual with index idx. Allocates array to store the trait values.
      Parameters:
      idx - the index of the individual
      Returns:
      the traits of the individual
    • getTraitsAt

      public double[] getTraitsAt(int idx, double[] idxtraits)
      Get the trait array of individual with index idx. Stores the trait values in the array idxtraits.
      Parameters:
      idx - the index of the individual
      idxtraits - the array for storing the traits
      Returns:
      the traits of the individual
    • setTraitsAt

      public void setTraitsAt(int idx, double[] idxtraits)
      Set the trait array of individual with index idx to array idxtraits.
      Parameters:
      idx - the index of the individual
      idxtraits - the traits of the individual
    • updateFromModelAt

      public void updateFromModelAt(int index, int modelPlayer)
      Description copied from class: IBSPopulation
      Update individual with index me and adopt the trait of individual with index you.

      Note: method must be subclassed to deal with different data types of traits but should also include a call to super.

      Overrides:
      updateFromModelAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      index - the index of the focal individual
      modelPlayer - the index of the model individual to adopt trait from
      See Also:
    • haveSameTrait

      public boolean haveSameTrait(int a, int b)
      Description copied from class: IBSPopulation
      Check if individuals with index a and index b have the same traits.
      Specified by:
      haveSameTrait in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      a - the index of first individual
      b - the index of second individual
      Returns:
      true if the two individuals have the same traits
    • isSameTrait

      public boolean isSameTrait(int a)
      Description copied from class: IBSPopulation
      Check if individual with index a has switched traits.

      Note: this test is only meaningful before trait are committed.

      Specified by:
      isSameTrait in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      a - index of individual
      Returns:
      true if trait remained unchanged
      See Also:
    • swapTraits

      public void swapTraits(int a, int b)
      Description copied from class: IBSPopulation
      Swap traits of individuals with index a and index b.

      Note: the traits still need to be committed.

      Specified by:
      swapTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      a - the index of first individual
      b - the index of second individual
      See Also:
    • mutateAt

      public int mutateAt(int focal)
      Description copied from class: IBSPopulation
      Mutate the trait of the focal individual with index focal. The mutated trait is committed and the scores updated.
      Specified by:
      mutateAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      focal - the index of the focal individual
      Returns:
      the number of elapsed realtime units
    • maybeMutateAt

      protected boolean maybeMutateAt(int focal, boolean switched)
      Description copied from class: IBSPopulation
      Consider mutating the trait of the focal individual with index focal. The trait of the focal individual is stored in the array traits unless the focal individual switched trait. In that case the current trait is stored in the array traitsNext.

      Important: The trait is not committed regardless of whether a mutation occurred.

      Specified by:
      maybeMutateAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      focal - the index of the focal individual
      switched - true if the focal individual switched trait
      Returns:
      true if the trait of the focal individual changed
    • maybeMutateMoran

      protected void maybeMutateMoran(int source, int dest)
      Description copied from class: IBSPopulation
      Consider mutating the trait of the parent individual with index source. The mutated trait is committed and the scores updated.
      Specified by:
      maybeMutateMoran in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      source - the index of the parent individual
      dest - the index of the location for the offspring placement
    • mutateAt

      private boolean mutateAt(int focal, boolean switched)
      Mutate all traits of the focal individual with index focal if mutate == true. In all cases commit traits and update scores.
      Parameters:
      focal - the index of the focal individual that gets updated
      switched - true if focal already switched trait
      Returns:
      true if the trait has changed
    • preferredPlayerBest

      public boolean preferredPlayerBest(int me, int best, int sample)
      For deterministic updating with multiple traits (more than two), it must be specified which trait is the preferred one.

      Summary: does 'me' prefer 'sample' over 'best'?

      Here we introduce the convention the trait closer to me is preferred.

      Specified by:
      preferredPlayerBest in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      me - the index of the focal individual
      best - the index of the best performing individual
      sample - the index of the sample type
      Returns:
      true if sample is preferred over best
    • deltaTraits

      private double deltaTraits(int a, int b)
      Measure the (Cartesian) distance between traits at a and b
      Parameters:
      a - the index where the traits of the first individual start
      b - the index where the traits of the second individual start
      Returns:
      the distance between a and b
    • gatherPlayers

      private void gatherPlayers(IBSGroup group)
      Gather the traits of all individuals in the interaction group group.
      Parameters:
      group - the interaction group
    • doAdjustScores

      protected boolean doAdjustScores()
      Description copied from class: IBSPopulation
      Check if scores can be adjusted rather than recalculated after an individual changed its trait. This requires that individuals interact with all their neighbours and that the structure of the population is not well-mixed. Some implementations may be able to extend adjustments to other structures. For example, adjusting scores is feasible in well-mixed populations for discrete traits.

      Requirements:

      Group.SAMPLING_ALL
      individuals need to be interacting with all their neighbours (not just a randomly selected subset).
      AbstractGeometry.MEANFIELD
      interactions with everyone are not feasible (impossible to model efficiently), in general, for unstructured populations (subclasses can do better, e.g. for discrete trait it is possible, see IBSDPopulation.doAdjustScores()).
      playerScoreReset
      if scores are reset whenever an individual adopts the trait of another (regardless of whether an actual trait change occurred) then the expected number of interactions of each individual remains constant over time (even though the interaction count may differ for individuals on heterogeneous structures).
      Specified by:
      doAdjustScores in class IBSPopulation<Continuous, IBSMCPopulation>
      Returns:
      true if adjusting scores is feasible
      See Also:
    • adjustScoreAt

      public void adjustScoreAt(int index, double before, double after)
      Description copied from class: IBSPopulation
      Adjust score of individual with index index from before to after and update all applicable helper variables, e.g. sumFitness.

      Important: Use only to adjust scores of individuals that did not change trait.

      Specified by:
      adjustScoreAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      index - the index of the individual
      before - the score before adjustments
      after - the score after adjustments
    • adjustScoreAt

      public void adjustScoreAt(int index, double adjust)
      Description copied from class: IBSPopulation
      Adjust score of individual with index index by adjust and update all applicable helper variables, e.g. sumFitness.
      Specified by:
      adjustScoreAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      index - the index of the individual
      adjust - the score adjustment
    • playPairGameAt

      public void playPairGameAt(IBSGroup group)
      Play a pairwise interaction with the individuals in group.

      Continuous modules with a single trait never get here.

      Specified by:
      playPairGameAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      group - the group of individuals interacting in pairs
      See Also:
    • adjustPairGameScoresAt

      public void adjustPairGameScoresAt(int me)
      Adjusts scores of focal individual with index me and its neighbors after me changed trait. Only works if adjustScores==true.

      Important: new trait must not yet have been committed.

      Continuous modules with a single trait never get here.

      Specified by:
      adjustPairGameScoresAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      me - the index of the focal individual
      See Also:
    • playGroupGameAt

      public void playGroupGameAt(IBSGroup group)
      Play a group interaction with the individuals in group.

      Continuous modules with a single trait never get here.

      Specified by:
      playGroupGameAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      group - the group of interacting individuals
      See Also:
    • playNoGameAt

      void playNoGameAt(int me)
      Handle the case where a focal player has no opponents and should not play a game.
      Parameters:
      me - focal player index
    • playGroupSequentiallyAt

      void playGroupSequentiallyAt(int me, IBSGroup group, int nGroup)
      Evaluate sequential group interactions when not enough opponents are available simultaneously.
      Parameters:
      me - focal player index
      group - sampled opponent group
      nGroup - number of players participating in each interaction
    • playGroupOnceAt

      void playGroupOnceAt(int me, IBSGroup group)
      Evaluate a single group interaction with the sampled opponents.
      Parameters:
      me - focal player index
      group - sampled opponent group
    • yalpGroupGameAt

      public void yalpGroupGameAt(IBSGroup group)
      Counterpart of IBSPopulation.playGroupGameAt(IBSGroup), IBSPopulation.playGameAt(int) and/or IBSPopulation.playGameSyncAt(int). Removes the payoffs of group interactions.

      Continuous modules with a single trait never get here.

      Specified by:
      yalpGroupGameAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      group - the interaction group
      See Also:
    • prepareTraits

      public void prepareTraits()
      Description copied from class: IBSPopulation
      Prior to a synchronous update step the current state must be duplicated in preparation for processing the next step.
      Specified by:
      prepareTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      See Also:
    • commitTraits

      public void commitTraits()
      Description copied from class: IBSPopulation
      After a synchronous update step the new state must be copied back to become the current state.
      Specified by:
      commitTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      See Also:
    • commitTraitAt

      public void commitTraitAt(int me)
      Description copied from class: IBSPopulation
      The change of a trait of the player at index is stored in a temporary variable and must be committed before proceeding.
      Specified by:
      commitTraitAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      me - the index of the player that needs to have its new trait committed
    • getTraitData

      public <T> void getTraitData(T[] colors, ColorMap<T> colorMap)
      Description copied from class: IBSPopulation
      Returns the traits of all individuals in this population coded as colors in the array colors using the map colorMap. Used by GUI to visualize the current state of this IBS model. Colors are coded in different data types <T> depending on the runtime environment (GWT or JRE) as well as the graph (e.g. PopGraph2D or PopGraph3D).
      Specified by:
      getTraitData in class IBSPopulation<Continuous, IBSMCPopulation>
      Type Parameters:
      T - the type of color data (String or MeshLambertMaterial for GWT and Color for JRE).
      Parameters:
      colors - the array where the colors of all nodes are stored
      colorMap - the map that converts traits into colors
    • getTraitHistogramData

      public void getTraitHistogramData(double[][] bins)
      Creates a histogram for each trait separately (if there are multiple) and returns the result in the array bins where the first index denotes the trait and the second refers to the bin.
      Parameters:
      bins - the array to store the histogram(s)
    • getTraitHistogramData

      public void getTraitHistogramData(double[] bins, int trait)
      Creates a histogram for the trait with index trait and returns the result in the array bins.
      Parameters:
      bins - the array to store the histogram(s)
      trait - the index of the trait
    • get2DTraitHistogramData

      public void get2DTraitHistogramData(double[] bins, int trait1, int trait2)
      Creates 2D histogram for traits trait1 and trait2. The result is returned in the linear array bins and arranged in a way that is compatible with square lattice geometries for visualization by Distribution and PopGraph2D (GWT only).
      Parameters:
      bins - the linear array to store the 2D histogram
      trait1 - the index of the first trait
      trait2 - the index of the second trait
    • getTraits

      public String getTraits(int digits)
      Gets all traits of all individuals. The traits are returned as a formatted string with an accuracy of digits decimals. With multiple traits they are listed sequentially for each individual.
      Parameters:
      digits - the number of decimals of the formatted string
      Returns:
      the formatted traits
    • getTraitNameAt

      public String getTraitNameAt(int index)
      Description copied from class: IBSPopulation
      Gets the formatted name of the trait of the individual at site index.
      Specified by:
      getTraitNameAt in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      index - the index of the
      Returns:
      the string describing the trait
    • getMeanTraits

      public double[] getMeanTraits(double[] mean)
      Returns the mean trait(s) of this population in the array mean. Used by GUI to visualize the current state of this IBS model.

      For continuous traits the first nTraits entries represent the mean of each trait and the second nTraits entries denote the standard deviation.

      Specified by:
      getMeanTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      mean - the array for returning the trait values
      Returns:
      the array mean containing the mean trait values
      See Also:
    • getMinTraits

      public double[] getMinTraits()
      Gets the minimal value of each trait in the population and stores it in the array min. The array must be of length >= nTraits.
      Returns:
      the array with the minimal trait values (same as input)
    • getMinTraits

      public double[] getMinTraits(double[] min)
      Gets the minimal value of each trait in the population and stores it in the array min. The array must be of length >= nTraits.
      Parameters:
      min - the array to store the minimal trait values
      Returns:
      the array with the minimal trait values (same as input)
    • getMaxTraits

      public double[] getMaxTraits()
      Gets the maximal value of each trait in the population and stores it in the array max.
      Returns:
      the array with the maximal trait values (same as input)
    • getMaxTraits

      public double[] getMaxTraits(double[] max)
      Gets the maximal value of each trait in the population and stores it in the array max. The array must be of length >= nTraits.
      Parameters:
      max - the array to store the maximal trait values
      Returns:
      the array with the maximal trait values (same as input)
    • getMeanFitness

      public double[] getMeanFitness(double[] mean)
      Returns the mean fitness of this population in the array mean. Used by GUI to visualize the current state of this IBS model. Returns true if data point belongs to the same time series and false if a new series was started through IBSPopulation.init() or IBSPopulation.reset().

      For continuous traits the first nTraits entries represent the mean fitness of each trait and the second nTraits entries denote their standard deviation.

      Specified by:
      getMeanFitness in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      mean - the array for storing the mean fitness values
      Returns:
      the array mean containing the mean fitness values
      See Also:
    • addState

      public void addState(double[] state)
      Adds the traits of all individuals to the array state. The array must be of length nPopulation * nTraits.
      Parameters:
      state - the array to store the traits
    • getStatus

      public String getStatus()
      Description copied from class: IBSPopulation
      Gets the status of the as a formatted string. This is typically used in the GUI to summarize the progress of the model.
      Specified by:
      getStatus in class IBSPopulation<Continuous, IBSMCPopulation>
      Returns:
      the status of the population
    • check

      public boolean check()
      Description copied from class: IBSPopulation
      Check all model parameters for consistency and adjust if necessary (and feasible). Returns true if adjustments require a reset. Free memory if possible and request a reset if new memory needs to be allocated.
      Overrides:
      check in class IBSPopulation<Continuous, IBSMCPopulation>
      Returns:
      true if reset is required
      See Also:
    • setInit

      public void setInit(IBSC.Init init)
      Sets the type of the initial configuration and any accompanying arguments. If either type or args are null the respective current setting is preserved.
      Parameters:
      init - the type and arguments of the initial configuration
    • getInit

      public IBSC.Init getInit()
      Gets the type of the initial configuration and its arguments.
      Returns:
      the type and arguments of the initial configuration
    • init

      public void init()
      Description copied from class: IBSPopulation
      Initialize the model. All parameters must be consistent. Subclasses must override this method to generate the initial trait configuration and call super.

      Note: Initialization leaves the interaction and competition structures untouched

      Overrides:
      init in class IBSPopulation<Continuous, IBSMCPopulation>
      See Also:
    • initUniform

      void initUniform()
      Initialize each trait uniformly at random in its allowed range.
    • initMono

      void initMono()
      Initialize all individuals with the same (monomorphic) trait value.
    • initGaussian

      void initGaussian()
      Initialize traits by sampling from a (possibly truncated) Gaussian distribution per trait.
    • initMutant

      void initMutant()
      Initialize a monomorphic resident population with a single mutant.
    • reset

      public void reset()
      Description copied from class: IBSPopulation
      Reset the model. All parameters must be consistent at this point. Allocate memory and initialize the interaction and competition structures. If structures include random elements, e.g. random regular graphs, a new structure is generated. Generate initial configuration. Subclasses must override this method to allocate memory for the trait and call super.
      Overrides:
      reset in class IBSPopulation<Continuous, IBSMCPopulation>
      See Also:
    • mouseHitNode

      public boolean mouseHitNode(int hit, boolean alt)
      Description copied from class: IBSPopulation
      Called from GUI if node/individual with index idx received a mouse click or tap and indicates whether the alt-key had been pressed.
      Overrides:
      mouseHitNode in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      hit - the index of the node
      alt - true if the alt-key was pressed
      Returns:
      false if no actions taken
    • encodeTraits

      public void encodeTraits(StringBuilder plist)
      Description copied from class: IBSPopulation
      Encode the traits of all individuals in the IBS model in a plist inspired XML string.
      Specified by:
      encodeTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      plist - the StringBuilder to write the encoded state to
      See Also:
    • restoreTraits

      public boolean restoreTraits(Plist plist)
      Description copied from class: IBSPopulation
      Restore the traits of all individuals encoded in the plist inspired map of key, value-pairs.
      Specified by:
      restoreTraits in class IBSPopulation<Continuous, IBSMCPopulation>
      Parameters:
      plist - the map of key, value-pairs
      Returns:
      true if successful
      See Also: