org.biojavax.ga.util
Class WeightedSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet
          extended by org.biojavax.ga.util.WeightedSet
All Implemented Interfaces:
Serializable, Iterable, Collection, Set

public class WeightedSet
extends AbstractSet
implements Serializable

Inspred by the BioJava Distribution objects the WeightedSet is a map from a Key to a Weight. Unlike Distributions the Keys do not have to be Symbols. In the GA package the WeightedMap is useful for sampling Organisms according to their fitness.

When Symbols are added or their weights are set then the weights are internally normalized to 1

Since:
1.5
Version:
1.0
Author:
Mark Schreiber
See Also:
Serialized Form

Constructor Summary
WeightedSet()
           
 
Method Summary
 boolean add(Object o)
          Adds a new Object with a weight of zero.
 boolean addAll(Collection c)
           
 Map asMap()
          Converts the Set to a map from key Objects to Double weights.
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection c)
           
protected  double getTotalWeight()
          The total weight that has been added to this Set.
 double getWeight(Object o)
          Determines the normalized weight for o
 boolean isEmpty()
           
 Iterator iterator()
          Returns an unmodifiable iterator over the keys of the set.
 boolean remove(Object o)
           
 boolean retainAll(Collection c)
           
 Object sample()
          Randomly samples an Object from the Set according to its weight.
 void setWeight(Object o, double w)
          Sets the weight of an Object.
 int size()
           
 Object[] toArray()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
toArray
 

Constructor Detail

WeightedSet

public WeightedSet()
Method Detail

asMap

public Map asMap()
Converts the Set to a map from key Objects to Double weights.

Returns:
a Map with all the key-weight mappings. Weights are not normalized in this map.

sample

public Object sample()
Randomly samples an Object from the Set according to its weight.

Returns:
the Object sampled.

getWeight

public double getWeight(Object o)
                 throws NoSuchElementException
Determines the normalized weight for o

Parameters:
o - the Object you want to know the weight of
Returns:
the normalized weight
Throws:
NoSuchElementException - if o is not found in this set

getTotalWeight

protected double getTotalWeight()
The total weight that has been added to this Set.

Returns:
the total weight (the value that can be used for normalizing)

setWeight

public void setWeight(Object o,
                      double w)
Sets the weight of an Object. If the Object is not in this Set then it is added.

Parameters:
o - the Object
w - the weight.
Throws:
IllegalArgumentException - if w is < 0.0

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection
Specified by:
contains in interface Set
Overrides:
contains in class AbstractCollection

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection
Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection
Specified by:
isEmpty in interface Set
Overrides:
isEmpty in class AbstractCollection

retainAll

public boolean retainAll(Collection c)
Specified by:
retainAll in interface Collection
Specified by:
retainAll in interface Set
Overrides:
retainAll in class AbstractCollection

add

public boolean add(Object o)
Adds a new Object with a weight of zero. Equivalent to setWeight(o, 0.0);

Specified by:
add in interface Collection
Specified by:
add in interface Set
Overrides:
add in class AbstractCollection
Parameters:
o - the object to add.
Returns:
true if this Object has not been added before.

size

public int size()
Specified by:
size in interface Collection
Specified by:
size in interface Set
Specified by:
size in class AbstractCollection

containsAll

public boolean containsAll(Collection c)
Specified by:
containsAll in interface Collection
Specified by:
containsAll in interface Set
Overrides:
containsAll in class AbstractCollection

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection

clear

public void clear()
Specified by:
clear in interface Collection
Specified by:
clear in interface Set
Overrides:
clear in class AbstractCollection

iterator

public Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface Set
Specified by:
iterator in class AbstractCollection
Returns:
an Iterator

addAll

public boolean addAll(Collection c)
Specified by:
addAll in interface Collection
Specified by:
addAll in interface Set
Overrides:
addAll in class AbstractCollection