23 #include "gtest/gtest.h"
25 #include "boost/smart_ptr.hpp"
32 class IndividualTest :
public ::testing::Test {
41 virtual ~IndividualTest() {
48 virtual void SetUp() {
53 virtual void TearDown() {
58 boost::shared_ptr<Individual> createIndividual(
size_t num_genes, std::string constraint=
"") {
63 for(
size_t i=0; i < num_genes; i++) {
65 std::pair<double, double>(lower_bound_, upper_bound_));
66 names.push_back(
"dvar"+std::to_string(i));
68 if (constraint.empty() ==
false)
69 constraints.insert(std::pair<std::string,Expressions::Expr_t*>
80 TEST_F(IndividualTest, IndividualRespectsBounds) {
83 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
84 double gene = ind->genes_m[0];
86 EXPECT_LE(lower_bound_, gene) <<
"gene should respect lower bound";
87 EXPECT_GE(upper_bound_, gene) <<
"gene should respect upper bound";
89 size_t my_size = ind->genes_m.size();
90 EXPECT_EQ(
static_cast<size_t>(num_genes), my_size)
91 <<
"individual should only have " << num_genes <<
" gene(s)";
95 TEST_F(IndividualTest, IndividualHasCorrectNumberOfGenes) {
97 size_t num_genes = 12;
98 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
100 size_t my_size = ind->genes_m.size();
101 EXPECT_EQ(
static_cast<size_t>(num_genes), my_size)
102 <<
"individual should only have " << num_genes <<
" gene(s)";
106 TEST_F(IndividualTest, IndividualRandomGene) {
108 size_t num_genes = 1;
109 boost::shared_ptr<Individual> ind = createIndividual(num_genes);
110 double gene = ind->genes_m[0];
111 double new_gene = ind->new_gene(0);
113 EXPECT_NE(gene, new_gene) <<
"new gene should be different";
116 TEST_F(IndividualTest, IndividualConstraint) {
118 size_t num_genes = 2;
119 double half = (lower_bound_ + upper_bound_) / 2.;
120 std::string constraint =
"(dvar0 + dvar1)/2. <=" + std::to_string(half);
123 for (
int i=0; i<10; i++) {
124 boost::shared_ptr<Individual> ind = createIndividual(num_genes,constraint);
125 double gene0 = ind->genes_m[0];
126 double gene1 = ind->genes_m[1];
127 EXPECT_LE((gene0+gene1)/2, half) <<
"constraint should be respected";
132 int main(
int argc,
char **argv) {
133 ::testing::InitGoogleTest(&argc, argv);
134 return RUN_ALL_TESTS();
void bounds(const PETE_Expr< T1 > &expr, Vektor< T2, D > &minval, Vektor< T2, D > &maxval)
int main(int argc, char **argv)
std::vector< std::string > names_t
gene names
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Expressions::Named_t constraints_t
constraints