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.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(java.lang.Object o)
Adds a newObject
with a weight of zero.boolean
addAll(java.util.Collection c)
java.util.Map
asMap()
Converts the Set to a map from keyObjects
toDouble
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 foro
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 anObject
from theSet
according to its weight.void
setWeight(java.lang.Object o, double w)
Sets the weight of anObject
.int
size()
java.lang.Object[]
toArray()
-
-
-
Constructor Detail
-
WeightedSet
public WeightedSet()
-
-
Method Detail
-
asMap
public java.util.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 java.lang.Object sample()
Randomly samples anObject
from theSet
according to its weight.- Returns:
- the Object sampled.
-
getWeight
public double getWeight(java.lang.Object o) throws java.util.NoSuchElementException
Determines the normalized weight foro
- Parameters:
o
- theObject
you want to know the weight of- Returns:
- the normalized weight
- Throws:
java.util.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(java.lang.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:
java.lang.IllegalArgumentException
- ifw
is < 0.0
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
contains
in interfacejava.util.Collection
- Specified by:
contains
in interfacejava.util.Set
- Overrides:
contains
in classjava.util.AbstractCollection
-
remove
public boolean remove(java.lang.Object o)
- Specified by:
remove
in interfacejava.util.Collection
- Specified by:
remove
in interfacejava.util.Set
- Overrides:
remove
in classjava.util.AbstractCollection
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Collection
- Specified by:
isEmpty
in interfacejava.util.Set
- Overrides:
isEmpty
in classjava.util.AbstractCollection
-
retainAll
public boolean retainAll(java.util.Collection c)
- Specified by:
retainAll
in interfacejava.util.Collection
- Specified by:
retainAll
in interfacejava.util.Set
- Overrides:
retainAll
in classjava.util.AbstractCollection
-
add
public boolean add(java.lang.Object o)
Adds a newObject
with a weight of zero. Equivalent to setWeight(o, 0.0);- Specified by:
add
in interfacejava.util.Collection
- Specified by:
add
in interfacejava.util.Set
- Overrides:
add
in 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:
size
in interfacejava.util.Collection
- Specified by:
size
in interfacejava.util.Set
- Specified by:
size
in classjava.util.AbstractCollection
-
containsAll
public boolean containsAll(java.util.Collection c)
- Specified by:
containsAll
in interfacejava.util.Collection
- Specified by:
containsAll
in interfacejava.util.Set
- Overrides:
containsAll
in classjava.util.AbstractCollection
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArray
in interfacejava.util.Collection
- Specified by:
toArray
in interfacejava.util.Set
- Overrides:
toArray
in classjava.util.AbstractCollection
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection
- Specified by:
clear
in interfacejava.util.Set
- Overrides:
clear
in classjava.util.AbstractCollection
-
iterator
public java.util.Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.- Specified by:
iterator
in interfacejava.util.Collection
- Specified by:
iterator
in interfacejava.lang.Iterable
- Specified by:
iterator
in interfacejava.util.Set
- Specified by:
iterator
in classjava.util.AbstractCollection
- Returns:
- an Iterator
-
addAll
public boolean addAll(java.util.Collection c)
- Specified by:
addAll
in interfacejava.util.Collection
- Specified by:
addAll
in interfacejava.util.Set
- Overrides:
addAll
in classjava.util.AbstractCollection
-
-