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 boolean
add(Object o)
Adds a newObject
with a weight of zero.boolean
addAll(Collection c)
Map
asMap()
Converts the Set to a map from keyObjects
toDouble
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 foro
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 anObject
from theSet
according to its weight.void
setWeight(Object o, double w)
Sets the weight of anObject
.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.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 keyObjects
toDouble
weights.- Returns:
- a Map with all the key-weight mappings. Weights are not normalized in this map.
-
sample
public Object sample()
Randomly samples anObject
from theSet
according to its weight.- Returns:
- the Object sampled.
-
getWeight
public double getWeight(Object o) throws NoSuchElementException
Determines the normalized weight foro
- Parameters:
o
- theObject
you want to know the weight of- Returns:
- the normalized weight
- Throws:
NoSuchElementException
- ifo
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 anObject
. If theObject
is not in thisSet
then it is added.- Parameters:
o
- theObject
w
- the weight.- Throws:
IllegalArgumentException
- ifw
is < 0.0
-
contains
public boolean contains(Object o)
- Specified by:
contains
in interfaceCollection
- Specified by:
contains
in interfaceSet
- Overrides:
contains
in classAbstractCollection
-
remove
public boolean remove(Object o)
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceSet
- Overrides:
remove
in classAbstractCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection
- Specified by:
isEmpty
in interfaceSet
- Overrides:
isEmpty
in classAbstractCollection
-
retainAll
public boolean retainAll(Collection c)
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceSet
- Overrides:
retainAll
in classAbstractCollection
-
add
public boolean add(Object o)
Adds a newObject
with a weight of zero. Equivalent to setWeight(o, 0.0);- Specified by:
add
in interfaceCollection
- Specified by:
add
in interfaceSet
- Overrides:
add
in classAbstractCollection
- Parameters:
o
- the object to add.- Returns:
- true if this Object has not been added before.
-
size
public int size()
- Specified by:
size
in interfaceCollection
- Specified by:
size
in interfaceSet
- Specified by:
size
in classAbstractCollection
-
containsAll
public boolean containsAll(Collection c)
- Specified by:
containsAll
in interfaceCollection
- Specified by:
containsAll
in interfaceSet
- Overrides:
containsAll
in classAbstractCollection
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollection
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in interfaceSet
- Overrides:
clear
in classAbstractCollection
-
iterator
public Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Specified by:
iterator
in interfaceSet
- Specified by:
iterator
in classAbstractCollection
- Returns:
- an Iterator
-
addAll
public boolean addAll(Collection c)
- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceSet
- Overrides:
addAll
in classAbstractCollection
-
-