23 #include "gtest/gtest.h"
25 #include "boost/smart_ptr.hpp"
30 class PopulationTest :
public ::testing::Test {
38 virtual ~PopulationTest() {
45 virtual void SetUp() {
50 virtual void TearDown() {
56 boost::shared_ptr<Individual> createIndividual(
size_t num_genes) {
61 for(
size_t i=0; i < num_genes; i++) {
62 bounds.push_back(std::pair<double, double>(0.0, 10.0));
63 names.push_back(
"dvar"+std::to_string(i));
71 boost::scoped_ptr< Population<Individual> > population_;
74 TEST_F(PopulationTest, AddOneIndividual) {
76 boost::shared_ptr<Individual> ind = createIndividual(1);
77 unsigned int id = population_->add_individual(ind);
78 double gene = ind->genes_m[0];
79 double obj = ind->objectives_m[0];
81 EXPECT_EQ(
static_cast<size_t>(0),
id) <<
"first individuals id should be 0";
83 boost::shared_ptr<Individual> tmp = population_->get_individual(
id);
84 EXPECT_EQ(0, tmp.get()) <<
"no committed individuals after insert";
86 tmp = population_->get_staging(
id);
87 EXPECT_EQ(gene, tmp->genes_m[0]) <<
"gene should have specified value";
88 EXPECT_EQ(obj, tmp->objectives_m[0]) <<
"objective should have specified value";
90 size_t my_size = population_->size();
91 EXPECT_EQ(
static_cast<size_t>(0), my_size)
92 <<
"no committed individuals, population should still be 0";
96 TEST_F(PopulationTest, CommitOneIndividual) {
98 boost::shared_ptr<Individual> ind = createIndividual(1);
99 unsigned int id = population_->add_individual(ind);
100 double gene = ind->genes_m[0];
101 double obj = ind->objectives_m[0];
103 EXPECT_EQ(
static_cast<size_t>(0),
id) <<
"first individuals id should be 0";
105 population_->commit_individuals();
107 boost::shared_ptr<Individual> tmp = population_->get_staging(
id);
108 EXPECT_EQ(0, tmp.get()) <<
"no staging individuals after commit";
110 tmp = population_->get_individual(
id);
111 EXPECT_EQ(gene, tmp->genes_m[0]);
112 EXPECT_EQ(obj, tmp->objectives_m[0]);
114 size_t my_size = population_->size();
115 EXPECT_EQ(
static_cast<size_t>(1), my_size);
119 TEST_F(PopulationTest, KeepIndividuals) {
121 boost::shared_ptr<Individual> ind1 = createIndividual(1);
122 boost::shared_ptr<Individual> ind2 = createIndividual(1);
123 boost::shared_ptr<Individual> ind3 = createIndividual(1);
124 boost::shared_ptr<Individual> ind4 = createIndividual(1);
126 size_t id0 = population_->add_individual(ind1);
127 EXPECT_EQ(
static_cast<size_t>(0), id0);
128 size_t id1 = population_->add_individual(ind2);
129 EXPECT_EQ(
static_cast<size_t>(1), id1);
130 size_t id2 = population_->add_individual(ind3);
131 EXPECT_EQ(
static_cast<size_t>(2), id2);
132 size_t id3 = population_->add_individual(ind4);
133 EXPECT_EQ(
static_cast<size_t>(3), id3);
135 population_->commit_individuals();
137 std::set<unsigned int> survivors;
138 survivors.insert(id1);
139 survivors.insert(id3);
141 population_->keepSurvivors(survivors);
143 size_t size = population_->size();
144 EXPECT_EQ(survivors.size(), size);
146 boost::shared_ptr<Individual> tmp = population_->get_individual(id1);
147 EXPECT_EQ(ind2->genes_m[0], tmp->genes_m[0]);
148 EXPECT_EQ(ind2->objectives_m[0], tmp->objectives_m[0]);
151 TEST_F(PopulationTest, IDsContinuous) {
153 boost::shared_ptr<Individual> ind0 = createIndividual(1);
154 boost::shared_ptr<Individual> ind1 = createIndividual(1);
155 boost::shared_ptr<Individual> ind2 = createIndividual(1);
156 boost::shared_ptr<Individual> ind3 = createIndividual(1);
158 size_t id0 = population_->add_individual(ind0);
159 EXPECT_EQ(
static_cast<size_t>(0), id0);
160 size_t id1 = population_->add_individual(ind1);
161 EXPECT_EQ(
static_cast<size_t>(1), id1);
162 size_t id2 = population_->add_individual(ind2);
163 EXPECT_EQ(
static_cast<size_t>(2), id2);
164 size_t id3 = population_->add_individual(ind3);
165 EXPECT_EQ(
static_cast<size_t>(3), id3);
167 unsigned int individual_to_be_removed_id = 1;
168 population_->remove_individual(ind1);
170 boost::shared_ptr<Individual> newind = createIndividual(1);
171 unsigned int id_new = population_->add_individual(newind);
172 EXPECT_EQ(individual_to_be_removed_id, id_new);
174 boost::shared_ptr<Individual> tmp = population_->get_staging(id_new);
175 EXPECT_EQ(newind->genes_m[0], tmp->genes_m[0]);
176 EXPECT_EQ(newind->objectives_m[0], tmp->objectives_m[0]);
179 TEST_F(PopulationTest, FindNonExistingStaging) {
181 boost::shared_ptr<Individual> tmp = population_->get_staging(124);
182 EXPECT_EQ(0, tmp.get());
185 TEST_F(PopulationTest, FindNonExistingIndividual) {
187 boost::shared_ptr<Individual> tmp = population_->get_individual(124);
188 EXPECT_EQ(0, tmp.get());
191 TEST_F(PopulationTest, RepresentedCheck) {
193 boost::shared_ptr<Individual> ind = createIndividual(1);
194 population_->add_individual(ind);
196 population_->add_individual(ind);
197 population_->commit_individuals();
199 bool represented = population_->isRepresentedInPopulation(ind->genes_m);
200 EXPECT_TRUE(represented);
202 std::vector<double> tmp_genes;
203 tmp_genes.push_back(12312);
204 represented = population_->isRepresentedInPopulation(tmp_genes);
205 EXPECT_FALSE(represented);
210 int main(
int argc,
char **argv) {
211 ::testing::InitGoogleTest(&argc, argv);
212 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