public class WeightedSet
extends java.util.AbstractSet
implements java.io.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
| Constructor and Description |
|---|
WeightedSet() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(java.lang.Object o)
Adds a new
Object with a weight of zero. |
boolean |
addAll(java.util.Collection c) |
java.util.Map |
asMap()
Converts the Set to a map from key
Objects to Double
weights. |
void |
clear() |
boolean |
contains(java.lang.Object o) |
boolean |
containsAll(java.util.Collection c) |
protected double |
getTotalWeight()
The total weight that has been added to this Set.
|
double |
getWeight(java.lang.Object o)
Determines the normalized weight for
o |
boolean |
isEmpty() |
java.util.Iterator |
iterator()
Returns an unmodifiable iterator over the keys of the set.
|
boolean |
remove(java.lang.Object o) |
boolean |
retainAll(java.util.Collection c) |
java.lang.Object |
sample()
Randomly samples an
Object from the Set according
to its weight. |
void |
setWeight(java.lang.Object o,
double w)
Sets the weight of an
Object. |
int |
size() |
java.lang.Object[] |
toArray() |
public java.util.Map asMap()
Objects to Double
weights.public java.lang.Object sample()
Object from the Set according
to its weight.public double getWeight(java.lang.Object o)
throws java.util.NoSuchElementException
oo - the Object you want to know the weight ofjava.util.NoSuchElementException - if o is not found in this setprotected double getTotalWeight()
public void setWeight(java.lang.Object o,
double w)
Object. If the Object is
not in this Set then it is added.o - the Objectw - the weight.java.lang.IllegalArgumentException - if w is < 0.0public boolean contains(java.lang.Object o)
contains in interface java.util.Collectioncontains in interface java.util.Setcontains in class java.util.AbstractCollectionpublic boolean remove(java.lang.Object o)
remove in interface java.util.Collectionremove in interface java.util.Setremove in class java.util.AbstractCollectionpublic boolean isEmpty()
isEmpty in interface java.util.CollectionisEmpty in interface java.util.SetisEmpty in class java.util.AbstractCollectionpublic boolean retainAll(java.util.Collection c)
retainAll in interface java.util.CollectionretainAll in interface java.util.SetretainAll in class java.util.AbstractCollectionpublic boolean add(java.lang.Object o)
Object with a weight of zero. Equivalent to
setWeight(o, 0.0);add in interface java.util.Collectionadd in interface java.util.Setadd in class java.util.AbstractCollectiono - the object to add.public int size()
size in interface java.util.Collectionsize in interface java.util.Setsize in class java.util.AbstractCollectionpublic boolean containsAll(java.util.Collection c)
containsAll in interface java.util.CollectioncontainsAll in interface java.util.SetcontainsAll in class java.util.AbstractCollectionpublic java.lang.Object[] toArray()
toArray in interface java.util.CollectiontoArray in interface java.util.SettoArray in class java.util.AbstractCollectionpublic void clear()
clear in interface java.util.Collectionclear in interface java.util.Setclear in class java.util.AbstractCollectionpublic java.util.Iterator iterator()
iterator in interface java.lang.Iterableiterator in interface java.util.Collectioniterator in interface java.util.Setiterator in class java.util.AbstractCollectionpublic boolean addAll(java.util.Collection c)
addAll in interface java.util.CollectionaddAll in interface java.util.SetaddAll in class java.util.AbstractCollection