29#include "boost/smart_ptr.hpp"
36#include "boost/property_tree/ptree.hpp"
37#include "boost/property_tree/json_parser.hpp"
41 ,
template <
class>
class CrossoverOperator
42 ,
template <
class>
class MutationOperator
44class Variator :
public CrossoverOperator<ind_t>,
45 public MutationOperator<ind_t>
57 for(
auto constraint : constraints) {
58 bool allDesignVariables =
true;
59 std::set<std::string> req_vars = constraint.second->getReqVars();
60 if (req_vars.empty()) allDesignVariables =
false;
61 for (std::string req_var : req_vars) {
64 allDesignVariables =
false;
68 if (allDesignVariables ==
true)
77 args->getArg<
double>(
"mutation-probability", 0.5);
80 args->getArg<
double>(
"recombination-probability", 0.5);
95 if ( fname.empty() ) {
96 for(
size_t i = 0; i < sizeInitial; i++) {
102 typedef boost::property_tree::ptree ptree_t;
105 boost::property_tree::read_json(fname, tree);
107 if ( tree.get<std::string>(
"name").compare(
"opt-pilot") ) {
109 "Json file not generated by opt-pilot.");
112 std::size_t nDVars = 0;
113 for(
auto& elem : tree.get_child(
"dvar-bounds")) {
117 "The design variable '" + elem.first +
"' is not in the list.");
124 "The number of design variables do not agree.");
127 boost::property_tree::ptree&
population = tree.get_child(
"population");
129 std::size_t size = 0;
133 if ( size > sizeInitial - 1 )
140 for (
auto& dvar :
population.get_child(ind.first).get_child(
"dvar")) {
142 std::size_t idx = std::distance(
dNames_m.begin(), it);
144 dvars[idx] = dvar.second.get_value<
double>();
152 for (std::size_t i = size; i < sizeInitial; ++i) {
180 void variate(std::vector<unsigned int> parents) {
183 for(
unsigned int parent : parents) {
190 while(!tmp.empty()) {
193 unsigned int idx = tmp.front(); tmp.pop();
200 boost::shared_ptr<ind_t> copyA(
new ind_t(
a));
207 if (
a->viable())
break;
221 idx = tmp.front(); tmp.pop();
222 boost::shared_ptr<ind_t> b =
population_m->get_staging(idx);
227 boost::shared_ptr<ind_t> copyA(
new ind_t(
a));
228 boost::shared_ptr<ind_t> copyB(
new ind_t(b));
238 this->crossover(
a, b,
args_);
244 this->mutate(b,
args_);
248 bool viableA =
a->viable();
249 bool viableB = b->viable();
250 if (viableA ==
true && viableB ==
true) {
253 std::cout <<
"Individuals not viable, I try again: iter= " << iter <<
std::endl;
257 if (viableA ==
false) {
260 if (viableB ==
false) {
275 std::swap(ind->genes_m, dvars);
287 boost::shared_ptr<ind_t> return_ind(
new ind_t(ind));
321 return (range * (
double) rand() / (RAND_MAX + 1.0));
Inform & endl(Inform &inf)
boost::shared_ptr< CmdArguments > CmdArguments_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
const T * find(const T table[], const std::string &name)
Look up name.
std::vector< double > genes_t
representation of genes
CmdArguments_t args_
user specified command line arguments
boost::shared_ptr< Population< ind_t > > population_m
population of individuals
void new_individual()
create a new individual
Optimizer::bounds_t dVarBounds_m
bounds on design variables
boost::shared_ptr< Population< ind_t > > population()
double drand(double range)
double mutationProbability_m
probability of applying the mutation operator
void infeasible(boost::shared_ptr< ind_t > ind)
set an individual as infeasible: replace with a new individual
void new_individual(boost::shared_ptr< ind_t > ind)
copy an individual
Expressions::Named_t constraints_m
constraints
void new_individual(Individual::genes_t &dvars)
create a new individual
void variate(std::vector< unsigned int > parents)
boost::shared_ptr< ind_t > popIndividualToEvaluate()
return next individual to evaluate
Variator(std::vector< std::string > dNames, Optimizer::bounds_t dVarBounds, Expressions::Named_t constraints, CmdArguments_t args)
bool hasMoreIndividualsToEvaluate()
returns false if all individuals have been evaluated
std::vector< std::string > dNames_m
names of the design variables
std::queue< unsigned int > individualsToEvaluate_m
keep a queue of individuals that have to be evaluated
double recombinationProbability_m
probability of applying the recombination operator
void initial_population(size_t sizeInitial, std::string fname)
create an initial population
std::vector< std::pair< double, double > > bounds_t
type of bounds for design variables