00001
00011 #include "RandomConnections.h"
00012
00013 RandomConnections::RandomConnections( const double conn_prob ) :
00014 conn_prob(conn_prob)
00015 {
00016 if (conn_prob <= 0 || conn_prob > 1)
00017 throw PCSIM::Exception( "RandomConnections::RandomConnections" , "Connection probability must lie in interval (0,1]" );
00018 if (conn_prob != 1)
00019 geom_rnd = new GeometricDistribution(1 - conn_prob);
00020 }
00021
00022
00023 RandomConnections::RandomConnections( const double conn_prob, MPI::Intracomm const& ) :
00024 conn_prob(conn_prob)
00025 {
00026 if (conn_prob <= 0 || conn_prob > 1)
00027 throw PCSIM::Exception( "RandomConnections::RandomConnections" , "Connection probability must lie in interval (0,1]" );
00028 if (conn_prob != 1)
00029 geom_rnd = new GeometricDistribution(1 - conn_prob );
00030 }
00031
00032
00033 RandomConnections::~RandomConnections()
00034 {
00035 if (conn_prob != 1)
00036 delete geom_rnd;
00037 }
00038
00039 void RandomConnections::init(const SimObjectPopulation &srcPopulation, const SimObjectPopulation &destPopulation)
00040 {
00041
00042 src_popul = &srcPopulation;
00043 dest_popul = &destPopulation;
00044 }
00045
00046 size_t RandomConnections::estimate()
00047 {
00048 return 0;
00049 }
00050
00051 void RandomConnections::reset(SimObject::ID::SortedVector::const_iterator src_begin_it,
00052 SimObject::ID::SortedVector::const_iterator src_end_it,
00053 SimObject::ID::SortedVector::const_iterator dest_begin_it,
00054 SimObject::ID::SortedVector::const_iterator dest_end_it)
00055 {
00056 curr_src_idx = 0;
00057 curr_dest_idx = -1;
00058
00059
00060 from_begin_it = src_begin_it;
00061 to_begin_it = dest_begin_it;
00062 src_max_idx = src_end_it - src_begin_it;
00063 dest_max_idx = dest_end_it - dest_begin_it;
00064 }
00065
00066 bool RandomConnections:: next( pair<SimObject::ID, SimObject::ID> &conn_pair )
00067 {
00068 last_conn_valid = false;
00069
00070 size_t step;
00071 if (conn_prob == 1)
00072 step = 1;
00073 else
00074 step = size_t( (*geom_rnd)( *m_rnd_eng ) );
00075
00076
00077 while (true) {
00078 curr_dest_idx += step;
00079
00080 if (curr_dest_idx >= dest_max_idx) {
00081 curr_src_idx += size_t(curr_dest_idx / dest_max_idx );
00082 curr_dest_idx %= dest_max_idx;
00083 }
00084
00085 if (curr_src_idx < src_max_idx)
00086 {
00087 last_conn_idx.first = *(from_begin_it + curr_src_idx);
00088 conn_pair.first = (*src_popul).getID(last_conn_idx.first);
00089
00090 last_conn_idx.second = *(to_begin_it + curr_dest_idx);
00091 conn_pair.second = (*dest_popul).getID(last_conn_idx.second);
00092 }
00093 else
00094 return false;
00095
00096 if (conn_pair.first == conn_pair.second)
00097 if (conn_prob == 1)
00098 step = 1;
00099 else
00100 step = size_t( (*geom_rnd)( *m_rnd_eng ) );
00101 else
00102 break;
00103 }
00104
00105 last_conn_valid = true;
00106
00107 return true;
00108 }
00109
00110