9 #include "boost/smart_ptr.hpp"
16 #include "boost/property_tree/ptree.hpp"
17 #include "boost/property_tree/json_parser.hpp"
21 ,
template <
class>
class CrossoverOperator
22 ,
template <
class>
class MutationOperator
24 class Variator :
public CrossoverOperator<ind_t>,
25 public MutationOperator<ind_t>
37 for(
auto constraint : constraints) {
38 bool allDesignVariables =
true;
39 std::set<std::string> req_vars = constraint.second->getReqVars();
40 if (req_vars.empty()) allDesignVariables =
false;
41 for (std::string req_var : req_vars) {
44 allDesignVariables =
false;
48 if (allDesignVariables ==
true)
57 args->getArg<
double>(
"mutation-probability", 0.5);
60 args->getArg<
double>(
"recombination-probability", 0.5);
75 if ( fname.empty() ) {
76 for(
size_t i = 0; i < sizeInitial; i++) {
82 typedef boost::property_tree::ptree ptree_t;
85 boost::property_tree::read_json(fname, tree);
87 if ( tree.get<std::string>(
"name").compare(
"opt-pilot") ) {
89 "Json file not generated by opt-pilot.");
92 std::size_t nDVars = 0;
93 for(
auto& elem : tree.get_child(
"dvar-bounds")) {
97 "The design variable '" + elem.first +
"' is not in the list.");
104 "The number of design variables do not agree.");
107 boost::property_tree::ptree&
population = tree.get_child(
"population");
109 std::size_t size = 0;
111 for (
auto& ind : population ) {
113 if ( size > sizeInitial - 1 )
120 for (
auto& dvar : population.get_child(ind.first).get_child(
"dvar")) {
122 std::size_t idx = std::distance(
dNames_m.begin(), it);
124 dvars[idx] = dvar.second.get_value<
double>();
132 for (std::size_t i = size; i < sizeInitial; ++i) {
160 void variate(std::vector<unsigned int> parents) {
163 for(
unsigned int parent : parents) {
170 while(!tmp.empty()) {
173 unsigned int idx = tmp.front(); tmp.pop();
174 boost::shared_ptr<ind_t> a =
population_m->get_staging(idx);
180 boost::shared_ptr<ind_t> copyA(
new ind_t(a));
185 this->mutate(a,
args_);
187 if (a->viable())
break;
201 idx = tmp.front(); tmp.pop();
202 boost::shared_ptr<ind_t> b =
population_m->get_staging(idx);
207 boost::shared_ptr<ind_t> copyA(
new ind_t(a));
208 boost::shared_ptr<ind_t> copyB(
new ind_t(b));
218 this->crossover(a, b,
args_);
223 this->mutate(a,
args_);
224 this->mutate(b,
args_);
228 bool viableA = a->viable();
229 bool viableB = b->viable();
230 if (viableA ==
true && viableB ==
true) {
233 std::cout <<
"Individuals not viable, I try again: iter= " << iter <<
std::endl;
237 if (viableA ==
false) {
240 if (viableB ==
false) {
255 std::swap(ind->genes_m, dvars);
267 boost::shared_ptr<ind_t> return_ind(
new ind_t(ind));
301 return (range * (
double) rand() / (RAND_MAX + 1.0));
Expressions::Named_t constraints_m
constraints
void infeasible(boost::shared_ptr< ind_t > ind)
set an individual as infeasible: replace with a new individual
boost::shared_ptr< Population< ind_t > > population_m
population of individuals
boost::shared_ptr< Population< ind_t > > population()
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
CmdArguments_t args_
user specified command line arguments
boost::shared_ptr< CmdArguments > CmdArguments_t
std::vector< double > genes_t
representation of genes
void new_individual(boost::shared_ptr< ind_t > ind)
copy an individual
std::queue< unsigned int > individualsToEvaluate_m
keep a queue of individuals that have to be evaluated
std::vector< std::string > dNames_m
names of the design variables
void new_individual(Individual::genes_t &dvars)
create a new individual
Optimizer::bounds_t dVarBounds_m
bounds on design variables
void variate(std::vector< unsigned int > parents)
boost::shared_ptr< ind_t > popIndividualToEvaluate()
return next individual to evaluate
double recombinationProbability_m
probability of applying the recombination operator
const T * find(const T table[], const std::string &name)
Look up name.
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
double mutationProbability_m
probability of applying the mutation operator
void new_individual()
create a new individual
bool hasMoreIndividualsToEvaluate()
returns false if all individuals have been evaluated
Variator(std::vector< std::string > dNames, Optimizer::bounds_t dVarBounds, Expressions::Named_t constraints, CmdArguments_t args)
double drand(double range)
Inform & endl(Inform &inf)