1 /*
2  *  cDemeTopologyNetwork.h
3  *  Avida
4  *
5  *  Copyright 1999-2011 Michigan State University. All rights reserved.
6  *
7  *
8  *  This file is part of Avida.
9  *
10  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
11  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
12  *
13  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
17  *  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef cDemeTopologyNetwork_h
22 #define cDemeTopologyNetwork_h
23 
24 /* THIS HEADER REQUIRES BOOST */
25 #include <boost/graph/adjacency_list.hpp>
26 #include <map>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "cDemeNetwork.h"
32 
33 class cDeme;
34 class cWorld;
35 class cPopulationCell;
36 
37 /*!
38  */
39 class cDemeTopologyNetwork : public cDemeNetwork {
40 public:
41 	enum FitnessType {
42 		MIN_CPL=0,
43 		MAX_CC=1,
44 		MIN_CC=2,
45 		TGT_CC=3,
46 		LENGTH_SUM=4,
47 	};
48 
49 	//! Internal vertex properties.
50 	struct vertex_properties {
vertex_propertiesvertex_properties51 		vertex_properties() { }
vertex_propertiesvertex_properties52 		vertex_properties(std::pair<int,int> pos, int cell_id) : _x(pos.first), _y(pos.second), _cell_id(cell_id), _active_edge(-1) { }
locationvertex_properties53 		std::pair<double,double> location() const { return std::make_pair(static_cast<double>(_x),static_cast<double>(_y)); }
54 		int _x, _y, _cell_id; // coordinates and cell id of this vertex, used to relate it back to the population.
55 		int _active_edge; // edge that will be used for unicast/rotate/select operations.
56 	};
57 
58 	//! Internal edge properties
59 	struct edge_properties {
edge_propertiesedge_properties60 		edge_properties() { }
edge_propertiesedge_properties61 		edge_properties(int t) : _t(t) { }
62 		int _t; //!< Time (update) at which this edge was (last!) added to the network.
63 	};
64 
65   //! An ease-of-use typedef to support the distributed construction of a network.
66   typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, vertex_properties, edge_properties> Network;
67 
68   //! A map of cell IDs to vertex descriptors.
69   typedef std::map<int, Network::vertex_descriptor> CellVertexMap;
70 
71   //! Map of cell IDs to counts.
72   typedef std::map<int, unsigned int> CellCountMap;
73 
74   //! Function object to remove edges if they've decayed.
75 	struct edge_decayed {
edge_decayededge_decayed76 		edge_decayed(int n, int d, Network& net) : _now(n), _decay(d), _network(net) { }
77 		bool operator()(Network::edge_descriptor e);
78 		int _now, _decay;
79 		Network& _network;
80 	};
81 
82 	//! Constructor.
83 	cDemeTopologyNetwork(cWorld* world, cDeme& deme);
84 
85 	//! Destructor.
~cDemeTopologyNetwork()86 	virtual ~cDemeTopologyNetwork() { }
87 
88 	//! Called at the end of every update.
89 	virtual void ProcessUpdate();
90 
91 	//! Connect u->v with weight w.
92 	virtual void Connect(cPopulationCell& u, cPopulationCell& v, double w=1.0);
93 
94 	//! Broadcast a message to connected cells.
95 	virtual void BroadcastToNeighbors(cPopulationCell& s, cOrgMessage& msg, cPopulationInterface* pop_interface);
96 
97 	//! Unicast a message to the currently selected neighbor.
98 	virtual void Unicast(cPopulationCell& s, cOrgMessage& msg, cPopulationInterface* pop_interface);
99 
100 	//! Rotate the selected link from among the current neighbors.
101 	virtual void Rotate(cPopulationCell& s, int x);
102 
103 	//! Select the current link from among the neighbors.
104 	virtual void Select(cPopulationCell& s, int x);
105 
106 	//! Called when the organism living in cell u dies.
107 	virtual void OrganismDeath(cPopulationCell& u);
108 
109 	//! Returns a network-defined fitness.
110 	virtual double Fitness(bool record_stats=true) const;
111 
112 	//! Measure statistics of this network.
113 	virtual cStats::network_stats_t Measure() const;
114 
115 	//! Print this network's topology.
116 	virtual void PrintTopology(cDataFile& df) const;
117 
118 protected:
119 	//! Ensure that the active edge of the given vertex is valid.
120 	bool ActivateEdge(Network::vertex_descriptor u);
121 
122 	Network m_network; //!< Underlying network model.
123 	CellVertexMap m_cv; //!< Map of cell ids to vertex descriptors.
124 	double m_link_length_sum; //!< Sum of all link lengths, at connection.
125 
126 private:
127 	cDemeTopologyNetwork();
128 	cDemeTopologyNetwork(const cDemeTopologyNetwork&);
129 	cDemeTopologyNetwork& operator=(const cDemeTopologyNetwork&);
130 };
131 
132 #endif
133