31 #ifndef __TASMANIAN_ADDONS_CMANAGER_HPP 
   32 #define __TASMANIAN_ADDONS_CMANAGER_HPP 
   76     template<
typename IntTypeDim, 
typename IntTypeBatch>
 
   77     CandidateManager(IntTypeDim dimensions, IntTypeBatch batch_size) : num_dimensions(static_cast<size_t>(dimensions)),
 
   78         num_batch(batch_size), num_candidates(0), num_running(0), num_done(0){}
 
   89     void operator=(std::vector<double> &&new_candidates){
 
   90         candidates = std::move(new_candidates);
 
   91         num_candidates = candidates.size() / num_dimensions;
 
   93         if (num_candidates == 0) 
return;
 
   96         status.resize(num_candidates);
 
   97         std::fill(status.begin(), status.end(), free);
 
   98         for(
auto const &p : running_jobs){
 
   99             size_t i = 
find(p.data());
 
  100             if (i < num_candidates) status[sorted[i]] = running;
 
  106         size_t num_complete = p.size() / num_dimensions;
 
  107         num_done += num_complete;
 
  108         num_running -= num_complete;
 
  110         for(
auto ip = p.begin(); ip != p.end(); std::advance(ip, num_dimensions)){
 
  115             if (i < num_candidates) status[sorted[i]] = done;
 
  119         auto inext = [](std::forward_list<std::vector<double>>::iterator ib)->
 
  120             std::forward_list<std::vector<double>>::iterator{
 
  124         for(
size_t i=0; i<num_complete; i++){
 
  125             auto ib = running_jobs.before_begin();
 
  126             while(not 
match(&p[i * num_dimensions], inext(ib)->data())) ib++;
 
  127             running_jobs.erase_after(ib);
 
  132     std::vector<double> 
next(
size_t remaining_budget){
 
  133         size_t this_batch = std::min(remaining_budget, num_batch);
 
  135         while((i < num_candidates) && (status[i] != free)) i++;
 
  136         if (i == num_candidates) 
return std::vector<double>();
 
  139         std::vector<double> result(&candidates[i*num_dimensions], &candidates[i*num_dimensions] + num_dimensions);
 
  140         running_jobs.push_front(result);
 
  143         while((i < num_candidates) && (num_next < this_batch)){
 
  144             while((i < num_candidates) && (status[i] != free)) i++;
 
  145             if (i < num_candidates){
 
  146                 running_jobs.push_front(std::vector<double>(&candidates[i*num_dimensions], &candidates[i*num_dimensions] + num_dimensions));
 
  147                 result.insert(result.end(), &candidates[i*num_dimensions], &candidates[i*num_dimensions] + num_dimensions);
 
  150                 status[i++] = running;
 
  167     bool match(
double const a[], 
double const b[])
 const{
 
  168         for(
size_t i=0; i<num_dimensions; i++)
 
  174     bool compare(
double const a[], 
double const b[])
 const{
 
  175         for(
size_t i=0; i<num_dimensions; i++){
 
  184         sorted.resize(num_candidates);
 
  185         std::iota(sorted.begin(), sorted.end(), 0);
 
  186         std::sort(sorted.begin(), sorted.end(), [&](
size_t a, 
size_t b)->bool{ return compare(&candidates[a*num_dimensions], &candidates[b*num_dimensions]); });
 
  190     size_t find(
double const point[])
 const{
 
  193         int sstart = 0, send = (int) num_candidates - 1, current = (sstart + send) / 2;
 
  194         while (sstart <= send){
 
  195             const double *cp = &candidates[sorted[current]*num_dimensions];
 
  197                 sstart = current + 1;
 
  201                 return (
size_t) current;
 
  203             current = (sstart + send) / 2;
 
  205         return num_candidates;
 
  209     size_t const num_dimensions, num_batch;
 
  210     size_t num_candidates, num_running, num_done;
 
  211     std::vector<double> candidates;
 
  212     std::vector<size_t> sorted;
 
  213     std::vector<TypeStatus> status;
 
  214     std::forward_list<std::vector<double>> running_jobs;
 
  230     template<
typename IntType>
 
  237         IO::writeNumbers<mode_binary, IO::pad_auto>(os, points.size(), values.size());
 
  238         IO::writeVector<mode_binary, IO::pad_auto>(points, os);
 
  239         IO::writeVector<mode_binary, IO::pad_auto>(values, os);
 
  244         points.resize(IO::readNumber<IO::mode_binary_type, size_t>(is));
 
  245         values.resize(IO::readNumber<IO::mode_binary_type, size_t>(is));
 
  246         IO::readVector<IO::mode_binary_type>(is, points);
 
  247         IO::readVector<IO::mode_binary_type>(is, values);
 
  251     void add(std::vector<double> 
const &x, std::vector<double> 
const &y){
 
  252         points.insert(points.end(), x.begin(), x.end());
 
  253         values.insert(values.end(), y.begin(), y.end());
 
  258         if (points.empty()) 
return;
 
  268     size_t const num_dimensions;
 
  269     std::vector<double> points, values;
 
Manages candidate points.
Definition: tsgCandidateManager.hpp:70
size_t getNumDone() const
Returns the number of complete jobs.
Definition: tsgCandidateManager.hpp:160
CandidateManager(IntTypeDim dimensions, IntTypeBatch batch_size)
Constructor, accepts number of dimensions as a constant parameter.
Definition: tsgCandidateManager.hpp:77
~CandidateManager()
Default destructor.
Definition: tsgCandidateManager.hpp:80
void operator=(std::vector< double > &&new_candidates)
Assign a new set of candidate points.
Definition: tsgCandidateManager.hpp:89
size_t getNumRunning() const
Returns the number of running jobs.
Definition: tsgCandidateManager.hpp:157
TypeStatus
Jobs are divided into already checked-out (running), checked-in (done), and available (free).
Definition: tsgCandidateManager.hpp:73
size_t getNumCandidates() const
Returns the number of all candidate jobs.
Definition: tsgCandidateManager.hpp:163
void sort_candidates()
Creates a sorted list of all candidates.
Definition: tsgCandidateManager.hpp:183
void complete(std::vector< double > const &p)
Mark a point as "complete".
Definition: tsgCandidateManager.hpp:105
std::vector< double > next(size_t remaining_budget)
Returns the next best point to compute, returns empty vector if no points are available.
Definition: tsgCandidateManager.hpp:132
size_t find(double const point[]) const
Find the index of the point within the canidates vector, returns num_candidates if not found.
Definition: tsgCandidateManager.hpp:190
bool match(double const a[], double const b[]) const
Returns true if entries in a and b match to Maths::num_tol, assumes sizes match already.
Definition: tsgCandidateManager.hpp:167
bool compare(double const a[], double const b[]) const
Returns true if the lexicographical order of a is before b.
Definition: tsgCandidateManager.hpp:174
Stores complete set of points before adding to the sparse grid.
Definition: tsgCandidateManager.hpp:227
void add(std::vector< double > const &x, std::vector< double > const &y)
Add a point to the stored list.
Definition: tsgCandidateManager.hpp:251
void read(std::istream &is)
Read the stored samples from the stream.
Definition: tsgCandidateManager.hpp:243
size_t getNumStored() const
Returns the number of stored points.
Definition: tsgCandidateManager.hpp:265
~CompleteStorage()
Default destructor.
Definition: tsgCandidateManager.hpp:233
CompleteStorage(IntType dimensions)
Constructor, accepts number of dimensions as a constant parameter.
Definition: tsgCandidateManager.hpp:231
void write(std::ostream &os) const
Write the stored samples to a stream.
Definition: tsgCandidateManager.hpp:236
void load(TasmanianSparseGrid &grid)
Move the stored points into the grid.
Definition: tsgCandidateManager.hpp:257
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
void loadConstructedPoints(std::vector< double > const &x, std::vector< double > const &y)
Add pairs of points with associated model values.
constexpr double num_tol
Numerical tolerance for various algorithms.
Definition: tsgMathUtils.hpp:132
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Common includes and methods for all addons.