Open hdbeukel opened 8 years ago
Document the fact that specifying fixed IDs in the updated SingleSwapNeighbourhood
and SinglePerturbationNeighbourhood
will increase the complexity for generated random moves from O(1)
to O(n)
in the worst case, where n
is the total number of items from which a subset is selected.
Consider the following changes:
Avoid separate storage of set with all IDs.Provide private inner class
RandomRetrievalSet
inSubsetSolution
which combines aMap
andArrayList
so that a random item can be picked in constant (instead of linear) time. The map maps contained items to the index at which they occur in the list and its key set serves as the modelled set. Items occur in the list in no particular order and the order may change when manipulating the collection. The map and list are kept in sync upon modifications. Constant time removals are preserved by swapping the removed and last item in the list after which the last item is removed. This avoids shifting the remaining items and is allowed as no order is to be maintained in the list (the list purely serves as a mechanism for fast random sampling). Use this utility class to store the selected/unselected IDs and add methods toSubsetSolution
for picking a random ID from one of these two groups (in constant time).Update neighbourhoods to benefit from the constant time random selection of IDs to add/remove/swap. Can only be used if no IDs are fixed in the set of selected/unselected IDs.
Neighbourhoods: avoid copy of selected/unselected IDs if there are fixed IDs but one of both sets does not contain any fixed IDs while only the other one does. If a copy is needed, consider creating an
ArrayList
instead ofSet
so that a random non-fixed ID can then be selected in constant time (after the copy).Generalize
SetUtilities
to deal with any kind of collection. Retain but deprecateSetUtilities
by delegating to a more general utility classCollectionUtils
. Provide fast implementations for random access lists (implementing bothList
andRandomAccess
) in addition to the slower, general fallback mechanism for other collections as currently implemented inSetUtilities
. These extended utilities can be used in the subset neighbourhoods.