OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
IndividualTest.cpp
Go to the documentation of this file.
1//
2// Test IndividualTest
3//
4// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
5// All rights reserved
6//
7// Implemented as part of the PhD thesis
8// "Toward massively parallel multi-objective optimization with application to
9// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
10//
11// This file is part of OPAL.
12//
13// OPAL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// You should have received a copy of the GNU General Public License
19// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20//
23#include "gtest/gtest.h"
24
25#include <string>
26
27namespace {
28
29 // The fixture for testing class Foo.
30 class IndividualTest : public ::testing::Test {
31 protected:
32
33 IndividualTest() {
34 // You can do set-up work for each test here.
35 lower_bound_ = -10.0;
36 upper_bound_ = 19.79;
37 }
38
39 virtual ~IndividualTest() {
40 // You can do clean-up work that doesn't throw exceptions here.
41 }
42
43 // If the constructor and destructor are not enough for setting up
44 // and cleaning up each test, you can define the following methods:
45
46 virtual void SetUp() {
47 // Code here will be called immediately after the constructor (right
48 // before each test).
49 }
50
51 virtual void TearDown() {
52 // Code here will be called immediately after each test (right
53 // before the destructor).
54 }
55
56 std::shared_ptr<Individual> createIndividual(size_t num_genes, std::string constraint="") {
57
60 Individual::constraints_t constraints;
61 for(size_t i=0; i < num_genes; i++) {
62 bounds.push_back(
63 std::pair<double, double>(lower_bound_, upper_bound_));
64 names.push_back("dvar"+std::to_string(i));
65 }
66 if (constraint.empty() == false)
67 constraints.insert(std::pair<std::string,Expressions::Expr_t*>
68 ("constraint0",new Expressions::Expr_t(constraint)));
69
70 std::shared_ptr<Individual> ind(new Individual(bounds,names,constraints));
71 return ind;
72 }
73
74 double lower_bound_;
75 double upper_bound_;
76 };
77
78 TEST_F(IndividualTest, IndividualRespectsBounds) {
79
80 size_t num_genes = 1;
81 std::shared_ptr<Individual> ind = createIndividual(num_genes);
82 double gene = ind->genes_m[0];
83
84 EXPECT_LE(lower_bound_, gene) << "gene should respect lower bound";
85 EXPECT_GE(upper_bound_, gene) << "gene should respect upper bound";
86
87 size_t my_size = ind->genes_m.size();
88 EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
89 << "individual should only have " << num_genes << " gene(s)";
90
91 }
92
93 TEST_F(IndividualTest, IndividualHasCorrectNumberOfGenes) {
94
95 size_t num_genes = 12;
96 std::shared_ptr<Individual> ind = createIndividual(num_genes);
97
98 size_t my_size = ind->genes_m.size();
99 EXPECT_EQ(static_cast<size_t>(num_genes), my_size)
100 << "individual should only have " << num_genes << " gene(s)";
101
102 }
103
104 TEST_F(IndividualTest, IndividualRandomGene) {
105
106 size_t num_genes = 1;
107 std::shared_ptr<Individual> ind = createIndividual(num_genes);
108 double gene = ind->genes_m[0];
109 double new_gene = ind->new_gene(0);
110
111 EXPECT_NE(gene, new_gene) << "new gene should be different";
112 }
113
114 TEST_F(IndividualTest, IndividualConstraint) {
115
116 size_t num_genes = 2;
117 double half = (lower_bound_ + upper_bound_) / 2.;
118 std::string constraint = "(dvar0 + dvar1)/2. <=" + std::to_string(half);
119
120 // create several individuals to test
121 for (int i=0; i<10; i++) {
122 std::shared_ptr<Individual> ind = createIndividual(num_genes,constraint);
123 double gene0 = ind->genes_m[0];
124 double gene1 = ind->genes_m[1];
125 EXPECT_LE((gene0+gene1)/2, half) << "constraint should be respected";
126 }
127 }
128}
129
130int main(int argc, char **argv) {
131 ::testing::InitGoogleTest(&argc, argv);
132 return RUN_ALL_TESTS();
133}
void bounds(const PETE_Expr< T1 > &expr, Vektor< T2, D > &minval, Vektor< T2, D > &maxval)
int main(int argc, char **argv)
Expression Expr_t
type of an expression
Definition Expression.h:63
std::vector< std::string > names_t
gene names
Definition Individual.h:43
std::vector< std::pair< double, double > > bounds_t
bounds on design variables
Definition Individual.h:47
Expressions::Named_t constraints_t
constraints
Definition Individual.h:49