PaGMO  1.1.5
hypercube.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #include <string>
26 
27 #include "../exceptions.h"
28 #include "base.h"
29 #include "hypercube.h"
30 
31 namespace pagmo { namespace topology {
32 
35 
37 {
38  return base_ptr(new hypercube(*this));
39 }
40 
56 {
57  // Store frequently-used variables.
59  pagmo_assert(t_size != 0);
60 
61  // This traces the size of the current hypercube. We start from dimension 1 and increase by one.
62  vertices_size_type cube_size = 2;
63  // This in turn traces the position offset between the "first" and "second" sub-hypercube. This is actually always equal to cube_size / 2
64  vertices_size_type jump_size = 1;
65 
66  // We consider all hypercubes of increasing sizes up to the biggest one possible to obtain with the current number of nodes
67  while(jump_size <= t_size) {
68  // Ordinal 0-based position of the new node in the cube with cube_size nodes (i.e. a cube of dimension lg(cube_size))
69  vertices_size_type index_in_cube = n % cube_size;
70 
71  // If the node belongs to the second "sub-hypercube"
72  if(index_in_cube >= jump_size) {
73  // Connect the new node with the corresponding node in the "first" sub-hypercube. Note: vertices are 0-based!
74  add_edge(n, t_size - jump_size - 1);
75  add_edge(t_size - jump_size - 1, n);
76  }
77 
78  // Proceed to the larger hypercube
79  cube_size <<= 1;
80  jump_size <<= 1;
81  }
82 }
83 
84 std::string hypercube::get_name() const
85 {
86  return "Hypercube";
87 }
88 
89 }} //namespaces
90 
91 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::topology::hypercube)
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base topology.
Definition: topology/base.h:47
std::string get_name() const
Get name of the topology.
Definition: hypercube.cpp:84
Base topology class.
Definition: topology/base.h:75
Hypercube topology.
Definition: hypercube.h:43
base_ptr clone() const
Clone method.
Definition: hypercube.cpp:36
vertices_size_type get_number_of_vertices() const
Get number of vertices.
graph_type::vertices_size_type vertices_size_type
Vertices size type.
hypercube()
Default constructor.
Definition: hypercube.cpp:34
void add_edge(const vertices_size_type &, const vertices_size_type &)
Add an edge.
void connect(const vertices_size_type &)
Definition: hypercube.cpp:55