2016-12-15 18:26:39 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
2016-12-15 18:26:39 +00:00
|
|
|
Source file: extras/Hadrons/GeneticScheduler.hpp
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2017-12-26 13:16:47 +00:00
|
|
|
Copyright (C) 2015-2018
|
2016-05-09 14:49:06 +01:00
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2016-12-15 18:26:39 +00:00
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
2016-12-15 18:21:52 +00:00
|
|
|
/* END LEGAL */
|
2016-05-09 14:49:06 +01:00
|
|
|
|
|
|
|
#ifndef Hadrons_GeneticScheduler_hpp_
|
|
|
|
#define Hadrons_GeneticScheduler_hpp_
|
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
#include <Grid/Hadrons/Global.hpp>
|
|
|
|
#include <Grid/Hadrons/Graph.hpp>
|
2016-05-09 14:49:06 +01:00
|
|
|
|
|
|
|
BEGIN_HADRONS_NAMESPACE
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Scheduler based on a genetic algorithm *
|
|
|
|
******************************************************************************/
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
2016-05-09 14:49:06 +01:00
|
|
|
class GeneticScheduler
|
|
|
|
{
|
|
|
|
public:
|
2017-12-13 16:36:15 +00:00
|
|
|
typedef std::vector<T> Gene;
|
|
|
|
typedef std::pair<Gene *, Gene *> GenePair;
|
|
|
|
typedef std::function<V(const Gene &)> ObjFunc;
|
2016-05-09 14:49:06 +01:00
|
|
|
struct Parameters
|
|
|
|
{
|
|
|
|
double mutationRate;
|
|
|
|
unsigned int popSize, seed;
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
GeneticScheduler(Graph<T> &graph, const ObjFunc &func,
|
|
|
|
const Parameters &par);
|
|
|
|
// destructor
|
|
|
|
virtual ~GeneticScheduler(void) = default;
|
|
|
|
// access
|
2016-12-10 20:14:13 +00:00
|
|
|
const Gene & getMinSchedule(void);
|
2018-04-06 15:41:31 +01:00
|
|
|
V getMinValue(void);
|
2016-05-09 14:49:06 +01:00
|
|
|
// breed a new generation
|
|
|
|
void nextGeneration(void);
|
2016-12-13 19:02:32 +00:00
|
|
|
// heuristic benchmarks
|
|
|
|
void benchmarkCrossover(const unsigned int nIt);
|
2016-05-09 14:49:06 +01:00
|
|
|
// print population
|
|
|
|
friend std::ostream & operator<<(std::ostream &out,
|
2017-12-13 16:36:15 +00:00
|
|
|
const GeneticScheduler<V, T> &s)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-07 00:00:45 +00:00
|
|
|
out << "[";
|
2016-05-09 14:49:06 +01:00
|
|
|
for (auto &p: s.population_)
|
|
|
|
{
|
2016-12-07 00:00:45 +00:00
|
|
|
out << p.first << ", ";
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
2016-12-07 00:00:45 +00:00
|
|
|
out << "\b\b]";
|
2016-05-09 14:49:06 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
private:
|
2016-12-10 20:14:13 +00:00
|
|
|
// evolution steps
|
2016-05-09 14:49:06 +01:00
|
|
|
void initPopulation(void);
|
2016-12-10 20:14:13 +00:00
|
|
|
void doCrossover(void);
|
|
|
|
void doMutation(void);
|
2016-05-09 14:49:06 +01:00
|
|
|
// genetic operators
|
2016-12-10 20:14:13 +00:00
|
|
|
GenePair selectPair(void);
|
2016-12-13 19:02:05 +00:00
|
|
|
void crossover(Gene &c1, Gene &c2, const Gene &p1, const Gene &p2);
|
2016-12-10 20:14:13 +00:00
|
|
|
void mutation(Gene &m, const Gene &c);
|
2016-12-13 19:02:05 +00:00
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
private:
|
2017-12-13 16:36:15 +00:00
|
|
|
Graph<T> &graph_;
|
|
|
|
const ObjFunc &func_;
|
|
|
|
const Parameters par_;
|
|
|
|
std::multimap<V, Gene> population_;
|
|
|
|
std::mt19937 gen_;
|
2016-05-09 14:49:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* template implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
GeneticScheduler<V, T>::GeneticScheduler(Graph<T> &graph, const ObjFunc &func,
|
2016-05-09 14:49:06 +01:00
|
|
|
const Parameters &par)
|
|
|
|
: graph_(graph)
|
|
|
|
, func_(func)
|
|
|
|
, par_(par)
|
|
|
|
{
|
|
|
|
gen_.seed(par_.seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
const typename GeneticScheduler<V, T>::Gene &
|
|
|
|
GeneticScheduler<V, T>::getMinSchedule(void)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
|
|
|
return population_.begin()->second;
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
2018-04-06 15:41:31 +01:00
|
|
|
V GeneticScheduler<V, T>::getMinValue(void)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
|
|
|
return population_.begin()->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// breed a new generation //////////////////////////////////////////////////////
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::nextGeneration(void)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
|
|
|
// random initialization of the population if necessary
|
|
|
|
if (population_.size() != par_.popSize)
|
|
|
|
{
|
|
|
|
initPopulation();
|
|
|
|
}
|
2018-03-02 19:20:01 +00:00
|
|
|
//LOG(Debug) << "Starting population:\n" << *this << std::endl;
|
2016-12-07 00:00:45 +00:00
|
|
|
|
|
|
|
// random mutations
|
2016-12-21 23:26:30 +00:00
|
|
|
//PARALLEL_FOR_LOOP
|
2016-12-07 00:00:45 +00:00
|
|
|
for (unsigned int i = 0; i < par_.popSize; ++i)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
doMutation();
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
2018-03-02 19:20:01 +00:00
|
|
|
//LOG(Debug) << "After mutations:\n" << *this << std::endl;
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2016-12-07 00:00:45 +00:00
|
|
|
// mating
|
2016-12-21 23:26:30 +00:00
|
|
|
//PARALLEL_FOR_LOOP
|
2016-12-07 00:00:45 +00:00
|
|
|
for (unsigned int i = 0; i < par_.popSize/2; ++i)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
doCrossover();
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
2018-03-02 19:20:01 +00:00
|
|
|
//LOG(Debug) << "After mating:\n" << *this << std::endl;
|
2016-05-09 14:49:06 +01:00
|
|
|
|
|
|
|
// grim reaper
|
|
|
|
auto it = population_.begin();
|
|
|
|
|
|
|
|
std::advance(it, par_.popSize);
|
|
|
|
population_.erase(it, population_.end());
|
2018-03-02 19:20:01 +00:00
|
|
|
//LOG(Debug) << "After grim reaper:\n" << *this << std::endl;
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
|
|
|
|
2016-12-10 20:14:13 +00:00
|
|
|
// evolution steps /////////////////////////////////////////////////////////////
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::initPopulation(void)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
|
|
|
population_.clear();
|
|
|
|
for (unsigned int i = 0; i < par_.popSize; ++i)
|
|
|
|
{
|
|
|
|
auto p = graph_.topoSort(gen_);
|
|
|
|
|
2017-01-19 22:08:22 +00:00
|
|
|
population_.insert(std::make_pair(func_(p), p));
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::doCrossover(void)
|
2016-12-07 00:00:45 +00:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
auto p = selectPair();
|
2016-12-13 19:02:05 +00:00
|
|
|
Gene &p1 = *(p.first), &p2 = *(p.second);
|
2016-12-10 20:14:13 +00:00
|
|
|
Gene c1, c2;
|
2016-12-07 00:00:45 +00:00
|
|
|
|
2016-12-13 19:02:05 +00:00
|
|
|
crossover(c1, c2, p1, p2);
|
2016-12-10 20:14:13 +00:00
|
|
|
PARALLEL_CRITICAL
|
|
|
|
{
|
2017-01-19 22:08:22 +00:00
|
|
|
population_.insert(std::make_pair(func_(c1), c1));
|
|
|
|
population_.insert(std::make_pair(func_(c2), c2));
|
2016-12-10 20:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::doMutation(void)
|
2016-12-10 20:14:13 +00:00
|
|
|
{
|
|
|
|
std::uniform_real_distribution<double> mdis(0., 1.);
|
|
|
|
std::uniform_int_distribution<unsigned int> pdis(0, population_.size() - 1);
|
2016-12-07 00:00:45 +00:00
|
|
|
|
2016-12-10 20:14:13 +00:00
|
|
|
if (mdis(gen_) < par_.mutationRate)
|
|
|
|
{
|
|
|
|
Gene m;
|
|
|
|
auto it = population_.begin();
|
|
|
|
|
|
|
|
std::advance(it, pdis(gen_));
|
|
|
|
mutation(m, it->second);
|
|
|
|
PARALLEL_CRITICAL
|
|
|
|
{
|
2017-01-19 22:08:22 +00:00
|
|
|
population_.insert(std::make_pair(func_(m), m));
|
2016-12-10 20:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-07 00:00:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 20:14:13 +00:00
|
|
|
// genetic operators ///////////////////////////////////////////////////////////
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
typename GeneticScheduler<V, T>::GenePair GeneticScheduler<V, T>::selectPair(void)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
|
|
|
std::vector<double> prob;
|
2016-12-07 00:00:45 +00:00
|
|
|
unsigned int ind;
|
2016-12-10 20:14:13 +00:00
|
|
|
Gene *p1, *p2;
|
2017-12-03 18:47:40 +00:00
|
|
|
const double max = population_.rbegin()->first;
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2017-12-03 18:47:40 +00:00
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
for (auto &c: population_)
|
|
|
|
{
|
2017-12-03 18:47:40 +00:00
|
|
|
prob.push_back(std::exp((c.first-1.)/max));
|
|
|
|
}
|
|
|
|
std::discrete_distribution<unsigned int> dis1(prob.begin(), prob.end());
|
|
|
|
auto rIt = population_.begin();
|
|
|
|
ind = dis1(gen_);
|
|
|
|
std::advance(rIt, ind);
|
|
|
|
p1 = &(rIt->second);
|
|
|
|
prob[ind] = 0.;
|
|
|
|
std::discrete_distribution<unsigned int> dis2(prob.begin(), prob.end());
|
|
|
|
rIt = population_.begin();
|
|
|
|
std::advance(rIt, dis2(gen_));
|
|
|
|
p2 = &(rIt->second);
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2016-12-07 00:00:45 +00:00
|
|
|
return std::make_pair(p1, p2);
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::crossover(Gene &c1, Gene &c2, const Gene &p1,
|
2016-12-13 19:02:05 +00:00
|
|
|
const Gene &p2)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
Gene buf;
|
|
|
|
std::uniform_int_distribution<unsigned int> dis(0, p1.size() - 1);
|
|
|
|
unsigned int cut = dis(gen_);
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2016-12-13 19:02:05 +00:00
|
|
|
c1.clear();
|
|
|
|
buf = p2;
|
2016-12-10 20:14:13 +00:00
|
|
|
for (unsigned int i = 0; i < cut; ++i)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-13 19:02:05 +00:00
|
|
|
c1.push_back(p1[i]);
|
|
|
|
buf.erase(std::find(buf.begin(), buf.end(), p1[i]));
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < buf.size(); ++i)
|
|
|
|
{
|
|
|
|
c1.push_back(buf[i]);
|
|
|
|
}
|
|
|
|
c2.clear();
|
|
|
|
buf = p2;
|
|
|
|
for (unsigned int i = cut; i < p1.size(); ++i)
|
|
|
|
{
|
|
|
|
buf.erase(std::find(buf.begin(), buf.end(), p1[i]));
|
2016-12-10 20:14:13 +00:00
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < buf.size(); ++i)
|
2016-12-07 00:00:45 +00:00
|
|
|
{
|
2016-12-13 19:02:05 +00:00
|
|
|
c2.push_back(buf[i]);
|
|
|
|
}
|
|
|
|
for (unsigned int i = cut; i < p1.size(); ++i)
|
|
|
|
{
|
|
|
|
c2.push_back(p1[i]);
|
2016-12-07 00:00:45 +00:00
|
|
|
}
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::mutation(Gene &m, const Gene &c)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
Gene buf;
|
|
|
|
std::uniform_int_distribution<unsigned int> dis(0, c.size() - 1);
|
|
|
|
unsigned int cut = dis(gen_);
|
|
|
|
Graph<T> g1 = graph_, g2 = graph_;
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2016-12-10 20:14:13 +00:00
|
|
|
for (unsigned int i = 0; i < cut; ++i)
|
2016-05-09 14:49:06 +01:00
|
|
|
{
|
2016-12-10 20:14:13 +00:00
|
|
|
g1.removeVertex(c[i]);
|
|
|
|
}
|
|
|
|
for (unsigned int i = cut; i < c.size(); ++i)
|
|
|
|
{
|
|
|
|
g2.removeVertex(c[i]);
|
|
|
|
}
|
|
|
|
if (g1.size() > 0)
|
|
|
|
{
|
|
|
|
buf = g1.topoSort(gen_);
|
|
|
|
}
|
|
|
|
if (g2.size() > 0)
|
|
|
|
{
|
|
|
|
m = g2.topoSort(gen_);
|
|
|
|
}
|
|
|
|
for (unsigned int i = cut; i < c.size(); ++i)
|
|
|
|
{
|
|
|
|
m.push_back(buf[i - cut]);
|
2016-05-09 14:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:15 +00:00
|
|
|
template <typename V, typename T>
|
|
|
|
void GeneticScheduler<V, T>::benchmarkCrossover(const unsigned int nIt)
|
2016-12-13 19:02:32 +00:00
|
|
|
{
|
|
|
|
Gene p1, p2, c1, c2;
|
|
|
|
double neg = 0., eq = 0., pos = 0., total;
|
|
|
|
int improvement;
|
|
|
|
|
|
|
|
LOG(Message) << "Benchmarking crossover..." << std::endl;
|
|
|
|
for (unsigned int i = 0; i < nIt; ++i)
|
|
|
|
{
|
|
|
|
p1 = graph_.topoSort(gen_);
|
|
|
|
p2 = graph_.topoSort(gen_);
|
|
|
|
crossover(c1, c2, p1, p2);
|
|
|
|
improvement = (func_(c1) + func_(c2) - func_(p1) - func_(p2))/2;
|
|
|
|
if (improvement < 0) neg++; else if (improvement == 0) eq++; else pos++;
|
|
|
|
}
|
|
|
|
total = neg + eq + pos;
|
|
|
|
LOG(Message) << " -: " << neg/total << " =: " << eq/total
|
|
|
|
<< " +: " << pos/total << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
END_HADRONS_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Hadrons_GeneticScheduler_hpp_
|