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:
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
Constructors Constructor Description WeightedSet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(Object o)Adds a newObjectwith a weight of zero.booleanaddAll(Collection c)MapasMap()Converts the Set to a map from keyObjectstoDoubleweights.voidclear()booleancontains(Object o)booleancontainsAll(Collection c)protected doublegetTotalWeight()The total weight that has been added to this Set.doublegetWeight(Object o)Determines the normalized weight forobooleanisEmpty()Iteratoriterator()Returns an unmodifiable iterator over the keys of the set.booleanremove(Object o)booleanretainAll(Collection c)Objectsample()Randomly samples anObjectfrom theSetaccording to its weight.voidsetWeight(Object o, double w)Sets the weight of anObject.intsize()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.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
spliterator, toArray
-
-
-
-
Constructor Detail
-
WeightedSet
public WeightedSet()
-
-
Method Detail
-
asMap
public 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 Object sample()
Randomly samples anObjectfrom theSetaccording to its weight.- Returns:
- the Object sampled.
-
getWeight
public double getWeight(Object o) throws NoSuchElementException
Determines the normalized weight foro- Parameters:
o- theObjectyou want to know the weight of- Returns:
- the normalized weight
- Throws:
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(Object o, double w)
Sets the weight of anObject. If theObjectis not in thisSetthen it is added.- Parameters:
o- theObjectw- the weight.- Throws:
IllegalArgumentException- ifwis < 0.0
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceCollection- Specified by:
containsin interfaceSet- Overrides:
containsin classAbstractCollection
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceCollection- Specified by:
removein interfaceSet- Overrides:
removein classAbstractCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfaceSet- Overrides:
isEmptyin classAbstractCollection
-
retainAll
public boolean retainAll(Collection c)
- Specified by:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceSet- Overrides:
retainAllin classAbstractCollection
-
add
public boolean add(Object o)
Adds a newObjectwith a weight of zero. Equivalent to setWeight(o, 0.0);- Specified by:
addin interfaceCollection- Specified by:
addin interfaceSet- Overrides:
addin classAbstractCollection- Parameters:
o- the object to add.- Returns:
- true if this Object has not been added before.
-
size
public int size()
- Specified by:
sizein interfaceCollection- Specified by:
sizein interfaceSet- Specified by:
sizein classAbstractCollection
-
containsAll
public boolean containsAll(Collection c)
- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceSet- Overrides:
containsAllin classAbstractCollection
-
toArray
public Object[] toArray()
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceSet- Overrides:
toArrayin classAbstractCollection
-
clear
public void clear()
- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceSet- Overrides:
clearin classAbstractCollection
-
iterator
public Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.- Specified by:
iteratorin interfaceCollection- Specified by:
iteratorin interfaceIterable- Specified by:
iteratorin interfaceSet- Specified by:
iteratorin classAbstractCollection- Returns:
- an Iterator
-
addAll
public boolean addAll(Collection c)
- Specified by:
addAllin interfaceCollection- Specified by:
addAllin interfaceSet- Overrides:
addAllin classAbstractCollection
-
-