5 #include <boost/asio/spawn.hpp> 6 #include <boost/interprocess/sync/interprocess_semaphore.hpp> 27 template <
typename Res,
typename Params>
37 boost::interprocess::interprocess_semaphore
ready;
38 boost::interprocess::interprocess_semaphore
processed;
68 std::vector<Params>
get_parameters(boost::asio::yield_context yield);
78 boost::asio::yield_context yield);
89 void run(
int i,
int nb_iterations,
90 const std::vector<Params>& population,
94 template <
typename Res,
typename Params>
96 : accumulate(accumulate),
97 ready(0), processed(0), last(false)
101 template <
typename Res,
typename Params>
107 template <
typename Res,
typename Params>
114 template <
typename Res,
typename Params>
116 boost::asio::yield_context)
120 if (last) stop =
true;
123 template <
typename Res,
typename Params>
125 const std::vector<Params>& population,
126 std::vector<Res>& res)
129 for (
const auto& p : population)
131 params.emplace_back(p);
134 if (i == nb_iterations) last =
true;
136 for (
unsigned int i = 0; i < results.size(); ++i)
138 res[i] = accumulate(results[i]);
142 template <
typename Res,
typename Params>
153 std::function<Res(
const std::list<Res>&)> accumulate,
154 int nb_eval_by_parameter = 1,
int nb_eval_by_slave = 1)
156 auto fitness = std::make_shared<distributed_fitness<Res, Params>>(accumulate);
169 template <
typename T>
179 return std::accumulate(l.begin(), l.end(), T{}, std::plus<T>()) / (
double)l.size();
std::vector< std::list< Res > > results
Definition: fitness.hpp:36
void set_results(std::vector< std::list< Res >> res, boost::asio::yield_context yield)
Increments processed, set the results and stop the algorithm if last is true.
Definition: fitness.hpp:115
boost::interprocess::interprocess_semaphore processed
Definition: fitness.hpp:38
void run(Client client)
Start a coroutine handling incoming slave connection and a coroutine handling the tests.
Definition: master.hpp:219
std::vector< Params > params
Definition: fitness.hpp:35
Handle score computing and flow control of the algorithm.
Handle flow control of the algorithm.
Definition: fitness.hpp:33
std::atomic< bool > last
Definition: fitness.hpp:40
bool finished()
Get the value of stop, i.e. if the algorithm is finished or not.
Definition: fitness.hpp:102
std::function< Res(const std::list< Res > &)> accumulate
Definition: fitness.hpp:34
bool stop
Definition: fitness.hpp:39
boost::interprocess::interprocess_semaphore ready
Definition: fitness.hpp:37
double mean_score(const std::list< T > &l)
Compute the mean of a list of values.
Definition: fitness.hpp:177
std::vector< Params > get_parameters(boost::asio::yield_context yield)
Decrements ready and get the parameters of the genetic algorithm.
Definition: fitness.hpp:108
void run(int i, int nb_iterations, const std::vector< Params > &population, std::vector< Res > &results)
Manage a batch test of agents.
Definition: fitness.hpp:124
Handle serializable representations of agents.
Master part of the algorithm, handle server-side connection and redistribute evenly the tests to the ...
Definition: master.hpp:26
std::shared_ptr< distributed_fitness< Res, Params > > make_distributed_fitness(int port, std::function< Res(const std::list< Res > &)> accumulate, int nb_eval_by_parameter=1, int nb_eval_by_slave=1)
Creates a fitness tool and launches a master in a separate thread.
Definition: fitness.hpp:152
distributed_fitness(std::function< Res(const std::list< Res > &)> accumulate)
Standard Constructor. Initialize the accumulate function, set last to false and set ready and process...
Definition: fitness.hpp:95