#include #include #include "neuron.h" #include "connection.h" Connection::Connection() { #ifdef DEBUG std::cout << "Connection::Connection1" << std::endl; #endif index = -1; deltaWeight = 0; weight = 0; momentum = 0.4; Q = 0; R = -1; randomizeWeight(); } Connection::Connection(const pNeuronX& from, const pNeuronX& to) { #ifdef DEBUG std::cout << "Connection::Connection2" << std::endl; #endif index = -1; deltaWeight = 0; weight = 0; momentum = 0.4; Q = 0; R = -1; this->from = from; this->to = to; randomizeWeight(); } double Connection::getError(void) { #ifdef DEBUG std::cout << "Connection::getError" << std::endl; #endif return error; } void Connection::setError(const double& e) { #ifdef DEBUG std::cout << "Connection::setError" << std::endl; #endif error = e; } int Connection::getIndex(void) { #ifdef DEBUG std::cout << "Connection::getIndex" << std::endl; #endif return index; } void Connection::setIndex(const int& index) { #ifdef DEBUG std::cout << "Connection::setIndex" << std::endl; #endif this->index = index; } double Connection::getWeight(void) { #ifdef DEBUG std::cout << "Connection::getWeight" << std::endl; #endif return weight; } void Connection::setWeight(const double& w) { #ifdef DEBUG std::cout << "Connection::setWeight" << std::endl; #endif //deltaWeight = weight - w; weight = w; } double Connection::getDeltaWeight(void) { #ifdef DEBUG std::cout << "Connection::getDeltaWeight" << std::endl; #endif return deltaWeight; } void Connection::setDeltaWeight(const double& dw) { #ifdef DEBUG std::cout << "Connection::setDeltaWeight" << std::endl; #endif deltaWeight = dw; } // Controls how much the weights are changed during a weight update by factoring in previous weight updates. // It acts as a smoothing parameter that reduces oscillation and helps attain convergence. // This must be a real value between 0.0 and 1.0, a typical value for momentum is 0.9. double Connection::getMomentum(void) { #ifdef DEBUG std::cout << "Connection::getMomentum" << std::endl; #endif return momentum; } void Connection::setMomentum(const double& momentum) { #ifdef DEBUG std::cout << "Connection::setMomentum" << std::endl; #endif this->momentum = momentum; } double Connection::getQ(void) { return Q; } void Connection::setQ(const double& _Q) { Q = _Q; } double Connection::getR(void) { return R; } void Connection::setR(const double& _R) { R = _R; } pNeuronX& Connection::getFrom(void) { #ifdef DEBUG std::cout << "Connection::getFrom" << std::endl; #endif return from; } void Connection::setFrom(const pNeuronX& from) { #ifdef DEBUG std::cout << "Connection::setFrom" << std::endl; #endif this->from = from; } pNeuronX& Connection::getTo(void) { #ifdef DEBUG std::cout << "Connection::getTo" << std::endl; #endif return to; } void Connection::setTo(const pNeuronX& to) { #ifdef DEBUG std::cout << "Connection::setTo" << std::endl; #endif this->to = to; } double Connection::randomizeWeight(void) { #ifdef DEBUG std::cout << "Connection::randomizeWeight" << std::endl; #endif weight = rand() / double(RAND_MAX); //deltaWeight = weight; return weight; } void Connection::printOutput(void) { #ifdef DEBUG std::cout << "Connection::printOutput" << std::endl; #endif if (!from) return; if (!to) return; int f = from->getIndex(); int t = to->getIndex(); //std::cout << " Connection[" << index << "] w=" << weight << ", From=" << f << ", To=" << t << ", d=" << deltaWeight << std::endl; std::cout << " Connection[" << index << "] w=" << weight << ", From=" << f << ", To=" << t << ", d=" << deltaWeight << ", Q=" << Q << ", R=" << R << std::endl; } /* #include #include #include int random_number(int N) // random value in [0, N) { static std::random_device seed; static std::mt19937 eng(seed()); std::uniform_int_distribution<> dist(0, N - 1); return dist(eng); } std::vector random_sample(int first, int last, int n) { std::vector numbers; int remaining = last - first + 1; int m = std::min(n, remaining); while (m > 0) { if (random_number(remaining) < m) { numbers.push_back(first); --m; } --remaining; ++first; } return numbers; } int main() { auto numbers = random_sample(1, 100, 20); for (int value : numbers) { std::cout << value << " "; } std::cout << '\n'; } */ /* very simple random is 1+((power(r,x)-1) mod p) will be from 1 to p for values of x from 1 to p and will be random where r and p are prime numbers and r <> p. To shuffle an array a of n elements: for i from n ? 1 downto 1 do j ? random integer with 0 ? j ? i exchange a[j] and a[i] for (int i = cards.Length - 1; i > 0; i--) { int n = rand.Next(i + 1); Swap(ref cards[i], ref cards[n]); } */