Package org.biojavax.ga.util
Class WeightedSet
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
org.biojavax.ga.util.WeightedSet
- All Implemented Interfaces:
Serializable
,Iterable
,Collection
,Set
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a newObject
with a weight of zero.boolean
addAll
(Collection c) asMap()
Converts the Set to a map from keyObjects
toDouble
weights.void
clear()
boolean
boolean
protected double
The total weight that has been added to this Set.double
Determines the normalized weight foro
boolean
isEmpty()
iterator()
Returns an unmodifiable iterator over the keys of the set.boolean
boolean
sample()
Randomly samples anObject
from theSet
according to its weight.void
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 Details
-
WeightedSet
public WeightedSet()
-
-
Method Details
-
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
Randomly samples anObject
from theSet
according to its weight.- Returns:
- the Object sampled.
-
getWeight
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
The total weight that has been added to this Set.- Returns:
- the total weight (the value that can be used for normalizing)
-
setWeight
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 invalid input: '<' 0.0
-
contains
- Specified by:
contains
in interfaceCollection
- Specified by:
contains
in interfaceSet
- Overrides:
contains
in classAbstractCollection
-
remove
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceSet
- Overrides:
remove
in classAbstractCollection
-
isEmpty
- Specified by:
isEmpty
in interfaceCollection
- Specified by:
isEmpty
in interfaceSet
- Overrides:
isEmpty
in classAbstractCollection
-
retainAll
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceSet
- Overrides:
retainAll
in classAbstractCollection
-
add
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
- Specified by:
size
in interfaceCollection
- Specified by:
size
in interfaceSet
- Specified by:
size
in classAbstractCollection
-
containsAll
- Specified by:
containsAll
in interfaceCollection
- Specified by:
containsAll
in interfaceSet
- Overrides:
containsAll
in classAbstractCollection
-
toArray
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollection
-
clear
- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in interfaceSet
- Overrides:
clear
in classAbstractCollection
-
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
- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceSet
- Overrides:
addAll
in classAbstractCollection
-