-
Notifications
You must be signed in to change notification settings - Fork 5
Optimize subset solution and neighbourhoods #30
Description
Consider the following changes:
-
Avoid separate storage of set with all IDs. -
Provide private inner class
RandomRetrievalSetinSubsetSolutionwhich combines aMapandArrayListso 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 toSubsetSolutionfor 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
ArrayListinstead ofSetso that a random non-fixed ID can then be selected in constant time (after the copy). -
Generalize
SetUtilitiesto deal with any kind of collection. Retain but deprecateSetUtilitiesby delegating to a more general utility classCollectionUtils. Provide fast implementations for random access lists (implementing bothListandRandomAccess) 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.