Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::glop::DynamicMaximum< Index > Class Template Reference

#include <pricing.h>

Public Member Functions

 DynamicMaximum (absl::BitGenRef random)
 To simplify the APIs, we take a random number generator at construction.
 
void ClearAndResize (Index n)
 
Index GetMaximum ()
 
void Remove (Index position)
 Removes the given index from the set of candidates.
 
void AddOrUpdate (Index position, Fractional value)
 
void StartDenseUpdates ()
 
void DenseAddOrUpdate (Index position, Fractional value)
 
void Clear ()
 Returns the current size n that was used in the last ClearAndResize().
 
Index Size () const
 
std::string StatString () const
 Returns some stats about this class if they are enabled.
 

Detailed Description

template<typename Index>
class operations_research::glop::DynamicMaximum< Index >

Maintains a set of elements in [0, n), each with an associated value and allows to query the element of maximum value efficiently.

This is optimized for use in the pricing step of the simplex algorithm. Basically at each simplex iterations, you want to:

1/ Get the candidate with the maximum value. The number of candidates can be close to n, or really small. You also want some randomization if several elements have an equivalent (maximum) value.

2/ Update the set of candidate and their values, where the number of update is usually a lot smaller than n. Note that in some corner cases, there are two "updates" phases, so a position can be updated twice.

The idea is to be faster than O(num_candidates) per GetMaximum(), most of the time. All updates should be in O(1) with as little overhead as possible. The algorithm here dynamically maintain the top-k (for k=32) with best effort and use it instead of doing a O(num_candidates) scan when possible.

Note
when O(num_updates) << n, this can have a huge effect. A basic O(1) per update, O(num_candidates) per maximum query was taking around 60% of the total time on graph40-80-1rand.pb.gz ! with the top-32 algo coded here, it is around 3%, and the number of "fast" GetMaximum() that hit the top-k heap on the first 120s of that problem was 250757 / 255659. Note that n was 282624 in this case, which is not even the biggest size we can tackle.

Note(user): This could be moved to util/ as a general class if someone wants to reuse it, it is however tuned for use in Glop pricing step and might becomes even more specific in the future.

Definition at line 61 of file pricing.h.

Constructor & Destructor Documentation

◆ DynamicMaximum()

template<typename Index >
operations_research::glop::DynamicMaximum< Index >::DynamicMaximum ( absl::BitGenRef random)
inlineexplicit

To simplify the APIs, we take a random number generator at construction.

Definition at line 64 of file pricing.h.

Member Function Documentation

◆ AddOrUpdate()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::AddOrUpdate ( Index position,
Fractional value )
inline

Adds an element to the set of candidate and sets its value. If the element is already present, this updates its value. The value must be finite.

Definition at line 190 of file pricing.h.

◆ Clear()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::Clear ( )
inline

Returns the current size n that was used in the last ClearAndResize().

Definition at line 94 of file pricing.h.

◆ ClearAndResize()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::ClearAndResize ( Index n)
inline

Prepares the class to hold up to n candidates with indices in [0, n). Initially no indices is a candidate.

Definition at line 161 of file pricing.h.

◆ DenseAddOrUpdate()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::DenseAddOrUpdate ( Index position,
Fractional value )
inline

Definition at line 181 of file pricing.h.

◆ GetMaximum()

template<typename Index >
Index operations_research::glop::DynamicMaximum< Index >::GetMaximum ( )
inline

Returns the index with the maximum value or Index(-1) if the set is empty and there is no possible candidate. If there are more than one candidate with the same maximum value, this will return a random one (not always uniformly if there is a large number of ties).

Optimized version if the maximum is in tops_ already.

We do two things here: 1/ Filter tops_ to only contain valid entries. This is because we never remove element, so the value of one of the element in tops might have decreased now. Note that we leave threshold_ untouched, so it can actually be lower than the minimum of the element in tops. 2/ Get the maximum of the valid elements.

The two possible sources of "invalidity".

We need to iterate over all the candidates.

Todo
(user): Add a mode when we do not maintain the TopK for small sizes (like n < 1000) ? The gain might not be worth the extra code though.

Definition at line 209 of file pricing.h.

◆ Remove()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::Remove ( Index position)
inline

Removes the given index from the set of candidates.

Definition at line 169 of file pricing.h.

◆ Size()

template<typename Index >
Index operations_research::glop::DynamicMaximum< Index >::Size ( ) const
inline

Definition at line 95 of file pricing.h.

◆ StartDenseUpdates()

template<typename Index >
void operations_research::glop::DynamicMaximum< Index >::StartDenseUpdates ( )
inline

Optimized version of AddOrUpdate() for the dense case. If one knows that there will be O(n) updates, it is possible to call StartDenseUpdates() and then use DenseAddOrUpdate() instead of AddOrUpdate() which is slighlty faster.

Note
calling AddOrUpdate() will still works fine, but will cause an extra test per call.

This disable tops_ until the next GetMaximum().

Definition at line 174 of file pricing.h.

◆ StatString()

template<typename Index >
std::string operations_research::glop::DynamicMaximum< Index >::StatString ( ) const
inline

Returns some stats about this class if they are enabled.

Definition at line 98 of file pricing.h.


The documentation for this class was generated from the following file: