Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::sat::SharedSolutionRepository< ValueType > Class Template Referenceabstract

#include <synchronization.h>

Classes

struct  Solution
 The solution format used by this class. More...
 

Public Member Functions

 SharedSolutionRepository (int num_solutions_to_keep, absl::string_view name="")
 
int NumSolutions () const
 
Solution GetSolution (int index) const
 Returns the solution i where i must be smaller than NumSolutions().
 
int64_t GetBestRank () const
 
ValueType GetVariableValueInSolution (int var_index, int solution_index) const
 
Solution GetRandomBiasedSolution (absl::BitGenRef random) const
 Returns a random solution biased towards good solutions.
 
void Add (const Solution &solution)
 
void Synchronize ()
 
std::vector< std::string > TableLineStats () const
 

Protected Member Functions

int64_t num_added_ ABSL_GUARDED_BY (mutex_)=0
 
int64_t num_queried_ ABSL_GUARDED_BY (mutex_)=0
 
int64_t num_synchronization_ ABSL_GUARDED_BY (mutex_)=0
 
std::vector< int > tmp_indices_ ABSL_GUARDED_BY (mutex_)
 
std::vector< Solution > solutions_ ABSL_GUARDED_BY (mutex_)
 
std::vector< Solution > new_solutions_ ABSL_GUARDED_BY (mutex_)
 

Protected Attributes

const std::string name_
 
const int num_solutions_to_keep_
 
absl::Mutex mutex_
 

Detailed Description

template<typename ValueType>
class operations_research::sat::SharedSolutionRepository< ValueType >

Thread-safe. Keeps a set of n unique best solution found so far.

Todo
(user): Maybe add some criteria to only keep solution with an objective really close to the best solution.

Definition at line 58 of file synchronization.h.

Constructor & Destructor Documentation

◆ SharedSolutionRepository()

template<typename ValueType >
operations_research::sat::SharedSolutionRepository< ValueType >::SharedSolutionRepository ( int num_solutions_to_keep,
absl::string_view name = "" )
inlineexplicit

Definition at line 60 of file synchronization.h.

Member Function Documentation

◆ ABSL_GUARDED_BY() [1/6]

template<typename ValueType >
std::vector< int > tmp_indices_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
mutableprotected

Our two solutions pools, the current one and the new one that will be merged into the current one on each Synchronize() calls.

◆ ABSL_GUARDED_BY() [2/6]

template<typename ValueType >
std::vector< Solution > solutions_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
protected

◆ ABSL_GUARDED_BY() [3/6]

template<typename ValueType >
std::vector< Solution > new_solutions_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
protected

◆ ABSL_GUARDED_BY() [4/6]

template<typename ValueType >
int64_t num_added_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
protectedpure virtual

◆ ABSL_GUARDED_BY() [5/6]

template<typename ValueType >
int64_t num_queried_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
mutableprotectedpure virtual

◆ ABSL_GUARDED_BY() [6/6]

template<typename ValueType >
int64_t num_synchronization_ operations_research::sat::SharedSolutionRepository< ValueType >::ABSL_GUARDED_BY ( mutex_ )
protectedpure virtual

◆ Add()

template<typename ValueType >
void operations_research::sat::SharedSolutionRepository< ValueType >::Add ( const Solution & solution)

Add a new solution. Note that it will not be added to the pool of solution right away. One must call Synchronize for this to happen. In order to be deterministic, this will keep all solutions until Synchronize() is called, so we need to be careful not to generate too many solutions at once.

Definition at line 837 of file synchronization.h.

◆ GetBestRank()

template<typename ValueType >
int64_t operations_research::sat::SharedSolutionRepository< ValueType >::GetBestRank ( ) const

Returns the rank of the best known solution. You shouldn't call this if NumSolutions() is zero.

Definition at line 785 of file synchronization.h.

◆ GetRandomBiasedSolution()

template<typename ValueType >
SharedSolutionRepository< ValueType >::Solution operations_research::sat::SharedSolutionRepository< ValueType >::GetRandomBiasedSolution ( absl::BitGenRef random) const

Returns a random solution biased towards good solutions.

Todo
(user): Experiments on the best distribution.

As long as we have solution with the best objective that haven't been explored too much, we select one uniformly. Otherwise, we select a solution from the pool uniformly.

Note(user): Because of the increase of num_selected, this is dependent on the order of call. It should be fine for "determinism" because we do generate the task of a batch always in the same order.

Select all the best solution with a low enough selection count.

Definition at line 801 of file synchronization.h.

◆ GetSolution()

template<typename ValueType >
SharedSolutionRepository< ValueType >::Solution operations_research::sat::SharedSolutionRepository< ValueType >::GetSolution ( int index) const

Returns the solution i where i must be smaller than NumSolutions().

Definition at line 778 of file synchronization.h.

◆ GetVariableValueInSolution()

template<typename ValueType >
ValueType operations_research::sat::SharedSolutionRepository< ValueType >::GetVariableValueInSolution ( int var_index,
int solution_index ) const

Returns the variable value of variable 'var_index' from solution 'solution_index' where solution_index must be smaller than NumSolutions() and 'var_index' must be smaller than number of variables.

Definition at line 792 of file synchronization.h.

◆ NumSolutions()

template<typename ValueType >
int operations_research::sat::SharedSolutionRepository< ValueType >::NumSolutions ( ) const

Returns the number of current solution in the pool. This will never decrease.

Definition at line 771 of file synchronization.h.

◆ Synchronize()

template<typename ValueType >
void operations_research::sat::SharedSolutionRepository< ValueType >::Synchronize ( )

Updates the current pool of solution with the one recently added. Note that we use a stable ordering of solutions, so the final pool will be independent on the order of the calls to AddSolution() provided that the set of added solutions is the same.

Works in O(num_solutions_to_keep_).

We use a stable sort to keep the num_selected count for the already existing solutions.

Todo
(user): Introduce a notion of orthogonality to diversify the pool?

Definition at line 845 of file synchronization.h.

◆ TableLineStats()

template<typename ValueType >
std::vector< std::string > operations_research::sat::SharedSolutionRepository< ValueType >::TableLineStats ( ) const
inline

Definition at line 128 of file synchronization.h.

Member Data Documentation

◆ mutex_

template<typename ValueType >
absl::Mutex operations_research::sat::SharedSolutionRepository< ValueType >::mutex_
mutableprotected

Definition at line 138 of file synchronization.h.

◆ name_

template<typename ValueType >
const std::string operations_research::sat::SharedSolutionRepository< ValueType >::name_
protected

Definition at line 135 of file synchronization.h.

◆ num_solutions_to_keep_

template<typename ValueType >
const int operations_research::sat::SharedSolutionRepository< ValueType >::num_solutions_to_keep_
protected

Definition at line 136 of file synchronization.h.


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