This example shows how to use signals to control the number of elements from a random lattice sequence to be examined by a minimization algorithm, and thus ignore candidate lattices rejected by filters.
#include "latbuilder/CoordUniformFigureOfMerit.h"
#include "latcommon/ProductWeights.h"
#include "latbuilder/Kernel/PAlpha.h"
#include "latbuilder/Accumulator.h"
#include "latbuilder/Storage.h"
#include "latbuilder/MeritFilterList.h"
#include "latbuilder/MeritFilter.h"
#include "latbuilder/Norm/Normalizer.h"
#include "latbuilder/Norm/PAlphaSL10.h"
#include "latbuilder/Functor/LowPass.h"
#include "latbuilder/MeritSeq/CoordUniformCBC.h"
#include "latbuilder/MeritSeq/CoordUniformInnerProd.h"
#include "latbuilder/GenSeq/CoprimeIntegers.h"
#include "latbuilder/GenSeq/Creator.h"
#include "latbuilder/Traversal.h"
#include "latbuilder/LFSR113.h"
#include "latbuilder/Functor/MinElement.h"
#include "latbuilder/TextStream.h"
#include <iostream>
#include <limits>
using TextStream::operator<<;
template <typename T, typename... ARGS>
std::unique_ptr<T> unique(ARGS&&... args)
{ return std::unique_ptr<T>(new T(std::forward<ARGS>(args)...)); }
template <LatType LAT>
class Observer {
public:
Observer(int maxCount) { m_maxCount = maxCount; m_count = m_totalCount = 0; }
void onStart() { m_count = m_totalCount = 0; }
bool onElementVisited(
const Real&) { ++m_totalCount;
return ++m_count < m_maxCount; }
void onReject(const LatDef&) { m_count--; }
int count() const { return m_count; }
int totalCount() const { return m_totalCount; }
private:
int m_maxCount;
int m_count;
int m_totalCount;
};
template <LatType L, Compress C>
{
auto weights = unique<LatCommon::ProductWeights>();
weights->setDefaultWeight(0.7);
std::cout << "figure of merit: " << figure << std::endl;
auto cbc = MeritSeq::cbc<MeritSeq::CoordUniformInnerProd>(storage, figure);
auto normalizer = unique<Norm::Normalizer<L, Norm::PAlphaSL10>>(
);
filters.add(std::move(normalizer));
filters.add(std::move(lowPass));
std::cout << "filters: " << filters << std::endl;
Observer<L> obs(samples);
minElement.
onStart().connect(boost::bind(&Observer<L>::onStart, &obs));
minElement.
onElementVisited().connect(boost::bind(&Observer<L>::onElementVisited, &obs, _1));
filters.template onReject<L>().connect(boost::bind(&Observer<L>::onReject, &obs, _1));
while (
cbc.baseLat().dimension() < dimension) {
std::cout << "CBC search for dimension: " << (baseDim + 1) << std::endl;
std::cout <<
" base lattice: " <<
cbc.baseLat() << std::endl;
std::cout <<
" base merit value: " <<
cbc.baseMerit() << std::endl;
auto meritSeq =
cbc.meritSeq(baseDim == 0 ? genSeq0 : genSeq);
auto filteredSeq = filters.apply(meritSeq);
auto best = minElement(filteredSeq.begin(), filteredSeq.end());
std::cout <<
"BEST LATTICE: " <<
cbc.baseLat() <<
" with merit value " << *best << std::endl;
std::cout << " " << obs.count() << " accepted / " << obs.totalCount() << " tried" << std::endl;;
genSeq.randomGenerator().jump();
}
}
int main()
{
int samples = 15;
return 0;
}