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
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 TypeMethodDescriptionbooleanAdds a newObjectwith a weight of zero.booleanaddAll(Collection c) asMap()Converts the Set to a map from keyObjectstoDoubleweights.voidclear()booleanbooleanprotected doubleThe total weight that has been added to this Set.doubleDetermines the normalized weight forobooleanisEmpty()iterator()Returns an unmodifiable iterator over the keys of the set.booleanbooleansample()Randomly samples anObjectfrom theSetaccording to its weight.voidSets the weight of anObject.intsize()Object[]toArray()Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
spliterator, toArray
-
Constructor Details
-
WeightedSet
public WeightedSet()
-
-
Method Details
-
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
Randomly samples anObjectfrom theSetaccording to its weight.- Returns:
- the Object sampled.
-
getWeight
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
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 theObjectis not in thisSetthen it is added.- Parameters:
o- theObjectw- the weight.- Throws:
IllegalArgumentException- ifwis < 0.0
-
contains
- Specified by:
containsin interfaceCollection- Specified by:
containsin interfaceSet- Overrides:
containsin classAbstractCollection
-
remove
- Specified by:
removein interfaceCollection- Specified by:
removein interfaceSet- Overrides:
removein classAbstractCollection
-
isEmpty
- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfaceSet- Overrides:
isEmptyin classAbstractCollection
-
retainAll
- Specified by:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceSet- Overrides:
retainAllin classAbstractCollection
-
add
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
- Specified by:
sizein interfaceCollection- Specified by:
sizein interfaceSet- Specified by:
sizein classAbstractCollection
-
containsAll
- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceSet- Overrides:
containsAllin classAbstractCollection
-
toArray
- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceSet- Overrides:
toArrayin classAbstractCollection
-
clear
- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceSet- Overrides:
clearin classAbstractCollection
-
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
- Specified by:
addAllin interfaceCollection- Specified by:
addAllin interfaceSet- Overrides:
addAllin classAbstractCollection
-