Package org.biojavax.ga.util
Class WeightedSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet
-
- org.biojavax.ga.util.WeightedSet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable,java.util.Collection,java.util.Set
public class WeightedSet extends java.util.AbstractSet implements java.io.SerializableInspred 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
Constructors Constructor Description WeightedSet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(java.lang.Object o)Adds a newObjectwith a weight of zero.booleanaddAll(java.util.Collection c)java.util.MapasMap()Converts the Set to a map from keyObjectstoDoubleweights.voidclear()booleancontains(java.lang.Object o)booleancontainsAll(java.util.Collection c)protected doublegetTotalWeight()The total weight that has been added to this Set.doublegetWeight(java.lang.Object o)Determines the normalized weight forobooleanisEmpty()java.util.Iteratoriterator()Returns an unmodifiable iterator over the keys of the set.booleanremove(java.lang.Object o)booleanretainAll(java.util.Collection c)java.lang.Objectsample()Randomly samples anObjectfrom theSetaccording to its weight.voidsetWeight(java.lang.Object o, double w)Sets the weight of anObject.intsize()java.lang.Object[]toArray()
-
-
-
Method Detail
-
asMap
public java.util.Map asMap()
Converts the Set to a map from keyObjectstoDoubleweights.- Returns:
- a Map with all the key-weight mappings. Weights are not normalized in this map.
-
sample
public java.lang.Object sample()
Randomly samples anObjectfrom theSetaccording to its weight.- Returns:
- the Object sampled.
-
getWeight
public double getWeight(java.lang.Object o) throws java.util.NoSuchElementExceptionDetermines the normalized weight foro- Parameters:
o- theObjectyou want to know the weight of- Returns:
- the normalized weight
- Throws:
java.util.NoSuchElementException- ifois 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(java.lang.Object o, double w)Sets the weight of anObject. If theObjectis not in thisSetthen it is added.- Parameters:
o- theObjectw- the weight.- Throws:
java.lang.IllegalArgumentException- ifwis < 0.0
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
containsin interfacejava.util.Collection- Specified by:
containsin interfacejava.util.Set- Overrides:
containsin classjava.util.AbstractCollection
-
remove
public boolean remove(java.lang.Object o)
- Specified by:
removein interfacejava.util.Collection- Specified by:
removein interfacejava.util.Set- Overrides:
removein classjava.util.AbstractCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Collection- Specified by:
isEmptyin interfacejava.util.Set- Overrides:
isEmptyin classjava.util.AbstractCollection
-
retainAll
public boolean retainAll(java.util.Collection c)
- Specified by:
retainAllin interfacejava.util.Collection- Specified by:
retainAllin interfacejava.util.Set- Overrides:
retainAllin classjava.util.AbstractCollection
-
add
public boolean add(java.lang.Object o)
Adds a newObjectwith a weight of zero. Equivalent to setWeight(o, 0.0);- Specified by:
addin interfacejava.util.Collection- Specified by:
addin interfacejava.util.Set- Overrides:
addin classjava.util.AbstractCollection- Parameters:
o- the object to add.- Returns:
- true if this Object has not been added before.
-
size
public int size()
- Specified by:
sizein interfacejava.util.Collection- Specified by:
sizein interfacejava.util.Set- Specified by:
sizein classjava.util.AbstractCollection
-
containsAll
public boolean containsAll(java.util.Collection c)
- Specified by:
containsAllin interfacejava.util.Collection- Specified by:
containsAllin interfacejava.util.Set- Overrides:
containsAllin classjava.util.AbstractCollection
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.Set- Overrides:
toArrayin classjava.util.AbstractCollection
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection- Specified by:
clearin interfacejava.util.Set- Overrides:
clearin classjava.util.AbstractCollection
-
iterator
public java.util.Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Specified by:
iteratorin interfacejava.util.Set- Specified by:
iteratorin classjava.util.AbstractCollection- Returns:
- an Iterator
-
addAll
public boolean addAll(java.util.Collection c)
- Specified by:
addAllin interfacejava.util.Collection- Specified by:
addAllin interfacejava.util.Set- Overrides:
addAllin classjava.util.AbstractCollection
-
-