This example shows how to use signals in order to interrupt the computation of a weighted figure of merit when its value has become too large.
#include "latbuilder/WeightedFigureOfMerit.h"
#include "latcommon/ProductWeights.h"
#include "latcommon/CoordinateSets.h"
#include "latbuilder/ProjDepMerit/Spectral.h"
#include "latcommon/NormaBestLat.h"
#include "latbuilder/Accumulator.h"
#include "latbuilder/Storage.h"
#include "latbuilder/Functor/binary.h"
#include "latbuilder/LatSeq/Korobov.h"
#include "latbuilder/GenSeq/CoprimeIntegers.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)...)); }
class Observer {
public:
Observer() { reset(); }
void reset() { m_bestMerit = std::numeric_limits<Real>::infinity(); }
const LatDef& bestLat() { return m_bestLat; }
const Real bestMerit() {
return m_bestMerit; }
void observe(
const LatDef& lat,
Real merit)
{
std::cout << lat << "\t:\t" << merit;
if (merit < m_bestMerit) {
std::cout << " <-- best";
m_bestMerit = merit;
m_bestLat = lat;
}
std::cout << std::endl;
}
bool onProgress(
Real merit)
const
{ return merit < m_bestMerit; }
void onAbort(const LatDef& lat) const
{ std::cout << "rejected " << lat << std::endl; }
private:
LatDef m_bestLat;
};
template <LatType L, Compress C>
{
auto weights = unique<LatCommon::ProductWeights>();
weights->setDefaultWeight(0.7);
std::cout << "figure of merit: " << figure << std::endl;
storage.sizeParam(),
Coprime(storage.sizeParam().numPoints()),
dimension
);
1, latSeq.latDimension(),
0, latSeq.latDimension() - 1
);
std::cout << "projections: " << allProjections << std::endl;
auto initialMerit = storage.createMeritValue(0.0);
auto eval = figure.evaluator(storage);
Observer obs;
eval.onProgress().connect(boost::bind(&Observer::onProgress, &obs, _1));
eval.onAbort().connect(boost::bind(&Observer::onAbort, &obs, _1));
for (const auto& lat : latSeq) {
auto merit =
eval(lat, allProjections, initialMerit);
obs.observe(lat, merit);
}
std::cout << "BEST LATTICE: " << obs.bestLat() << " with merit value " << obs.bestMerit() << std::endl;
}
int main()
{
return 0;
}