21 #ifndef __VARIATOR_H__
22 #define __VARIATOR_H__
34 #include "boost/property_tree/ptree.hpp"
35 #include "boost/property_tree/json_parser.hpp"
39 ,
template <
class>
class CrossoverOperator
40 ,
template <
class>
class MutationOperator
42 class Variator :
public CrossoverOperator<ind_t>,
43 public MutationOperator<ind_t>
55 for(
auto constraint : constraints) {
56 bool allDesignVariables =
true;
57 std::set<std::string> req_vars = constraint.second->getReqVars();
58 if (req_vars.empty()) allDesignVariables =
false;
59 for (std::string req_var : req_vars) {
62 allDesignVariables =
false;
66 if (allDesignVariables ==
true)
75 args->getArg<
double>(
"mutation-probability", 0.5);
78 args->getArg<
double>(
"recombination-probability", 0.5);
93 if ( fname.empty() ) {
94 for(
size_t i = 0; i < sizeInitial; i++) {
100 typedef boost::property_tree::ptree ptree_t;
103 boost::property_tree::read_json(fname, tree);
105 if ( tree.get<std::string>(
"name").compare(
"opt-pilot") ) {
107 "Json file not generated by opt-pilot.");
110 std::size_t nDVars = 0;
111 for(
auto& elem : tree.get_child(
"dvar-bounds")) {
115 "The design variable '" + elem.first +
"' is not in the list.");
122 "The number of design variables do not agree.");
125 boost::property_tree::ptree&
population = tree.get_child(
"population");
127 std::size_t size = 0;
129 for (
auto& ind : population ) {
131 if ( size > sizeInitial - 1 )
138 for (
auto& dvar : population.get_child(ind.first).get_child(
"dvar")) {
140 std::size_t idx = std::distance(
dNames_m.begin(),
it);
142 dvars[idx] = dvar.second.get_value<
double>();
150 for (std::size_t i = size; i < sizeInitial; ++i) {
178 void variate(std::vector<unsigned int> parents) {
181 for(
unsigned int parent : parents) {
188 while(!tmp.empty()) {
191 unsigned int idx = tmp.front(); tmp.pop();
192 std::shared_ptr<ind_t> a =
population_m->get_staging(idx);
198 std::shared_ptr<ind_t> copyA(
new ind_t(a));
203 this->mutate(a,
args_);
205 if (a->viable())
break;
219 idx = tmp.front(); tmp.pop();
220 std::shared_ptr<ind_t> b =
population_m->get_staging(idx);
225 std::shared_ptr<ind_t> copyA(
new ind_t(a));
226 std::shared_ptr<ind_t> copyB(
new ind_t(b));
236 this->crossover(a, b,
args_);
241 this->mutate(a,
args_);
242 this->mutate(b,
args_);
246 bool viableA = a->viable();
247 bool viableB = b->viable();
248 if (viableA ==
true && viableB ==
true) {
251 std::cout <<
"Individuals not viable, I try again: iter= " << iter <<
std::endl;
255 if (viableA ==
false) {
258 if (viableB ==
false) {
273 std::swap(ind->genes_m, dvars);
285 std::shared_ptr<ind_t> return_ind(
new ind_t(ind));
319 return (range * (
double) rand() / (RAND_MAX + 1.0));
std::shared_ptr< Population< ind_t > > population_m
population of individuals
void new_individual(std::shared_ptr< ind_t > ind)
copy an individual
void new_individual()
create a new individual
std::vector< double > genes_t
representation of genes
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
double mutationProbability_m
probability of applying the mutation operator
Optimizer::bounds_t dVarBounds_m
bounds on design variables
void new_individual(Individual::genes_t &dvars)
create a new individual
void initial_population(size_t sizeInitial, std::string fname)
create an initial population
Expressions::Named_t constraints_m
constraints
void variate(std::vector< unsigned int > parents)
double drand(double range)
Inform & endl(Inform &inf)
std::shared_ptr< Population< ind_t > > population()
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Variator(std::vector< std::string > dNames, Optimizer::bounds_t dVarBounds, Expressions::Named_t constraints, CmdArguments_t args)
const T * find(const T table[], const std::string &name)
Look up name.
std::vector< std::pair< double, double > > bounds_t
type of bounds for design variables
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
std::shared_ptr< CmdArguments > CmdArguments_t
double recombinationProbability_m
probability of applying the recombination operator
std::shared_ptr< ind_t > popIndividualToEvaluate()
return next individual to evaluate
CmdArguments_t args_
user specified command line arguments
bool hasMoreIndividualsToEvaluate()
returns false if all individuals have been evaluated
void infeasible(std::shared_ptr< ind_t > ind)
set an individual as infeasible: replace with a new individual