Class WeightedObjectChooser

java.lang.Object
com.softsynth.jmsl.util.WeightedObjectChooser

public class WeightedObjectChooser
extends java.lang.Object
Holds objects and associated weights. Weights are used to govern the likelihood of an object being chosen with next().

Every call to next() first sums the weights, and an object is chosen by comparing a randomly chosen threshold against a running total as objects are enumerated.

For example, the following adds two objects where one is twice as likely to be chosen as the other
 WeightedObjectChooser chooser = new WeightedObjectChooser();
 chooser.addWeightedObject(new Object(), 1.0);
 chooser.addWeightedObject(new Object(), 2.0);
 Object obj = chooser.next();
 
This class was requested by Larry Polansky.
Author:
Nick Didkovsky, email: nick@didkovsky.com, (c) 2004 Nick Didkovsky, all rights reserved.
  • Constructor Summary

    Constructors
    Constructor Description
    WeightedObjectChooser()  
  • Method Summary

    Modifier and Type Method Description
    void addWeightedObject​(java.lang.Object object, double weight)
    Add an Object and associate it with specified weight
    boolean contains​(java.lang.Object object)  
    java.util.Enumeration elements()  
    double getWeight​(java.lang.Object object)  
    static void main​(java.lang.String[] args)
    source:
    java.lang.Object next()  
    void remove​(java.lang.Object object)
    remove specified object from objects under consideration
    void setWeight​(java.lang.Object object, double weight)
    Set weight of an Object.
    int size()  
    java.lang.String toString()  
    double totalWeights()  

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • WeightedObjectChooser

      public WeightedObjectChooser()
  • Method Details

    • next

      public java.lang.Object next()
      Returns:
      randomly chosen Object, biased by weight
    • addWeightedObject

      public void addWeightedObject​(java.lang.Object object, double weight)
      Add an Object and associate it with specified weight
    • setWeight

      public void setWeight​(java.lang.Object object, double weight)
      Set weight of an Object. Adds Object if not already included
    • getWeight

      public double getWeight​(java.lang.Object object)
      Returns:
      weight for specified object. If in doubt use contains() to check first if object is in the chooser
    • remove

      public void remove​(java.lang.Object object)
      remove specified object from objects under consideration
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
      Returns:
      a String listing all objects, their weights, and the percentage of their weights compared to the total weight
    • totalWeights

      public double totalWeights()
      Returns:
      sum of all weights for all objects in this WeightedObjectChooser
    • elements

      public java.util.Enumeration elements()
      Returns:
      Enumeration of Objects added to this WeightedObjectChooser
    • size

      public int size()
      Returns:
      number of objects
    • contains

      public boolean contains​(java.lang.Object object)
    • main

      public static void main​(java.lang.String[] args)
      source:
       public static void main(String args[]) {
           JMSLRandom.randomize();
           WeightedObjectChooser chooser = new WeightedObjectChooser();
           Object gaga1 = new Object();
           Object gaga2 = new Object();
           chooser.addWeightedObject(gaga1, 1.0);
           chooser.addWeightedObject(gaga2, 1.0);
           System.out.println(chooser.toString());
           int gaga1kount = 0;
           int gaga2kount = 0;
           for (int i = 0; i < 100000; i++) {
               Object obj = chooser.next();
               if (obj.equals(gaga1))
                   gaga1kount++;
               if (obj.equals(gaga2))
                   gaga2kount++;
           }
           System.out.println("gaga1 count=" + gaga1kount + ", gaga2 count=" + gaga2kount);
       }