Skip to content

Commit

Permalink
Replace the random sampling function with C++17 equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
mertyildiran committed Oct 29, 2019
1 parent 397679a commit 0cecefc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions .ale_config
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
-fPIC
-I/usr/include/python3.6m
-w
-std=c++17
1 change: 1 addition & 0 deletions plexus/.ycm_extra_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ def Settings(**kwargs):
'-fPIC',
'-I/usr/include/python3.6m',
'-w',
'-std=c++17',
],
}
27 changes: 14 additions & 13 deletions plexus/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <thread>
#include <vector>
#include <unordered_map>
#include <random>
#include <algorithm>

class Neuron;

Expand Down Expand Up @@ -81,17 +83,16 @@ class Network
void load(std::vector<double> input_arr, std::vector<double> output_arr);
};

// Function to get a random sampling from a vector
// using Fisher-Yates shuffle method
template<class BidiIter >
BidiIter random_unique(BidiIter begin, BidiIter end, size_t num_random) {
size_t left = std::distance(begin, end);
while (num_random--) {
BidiIter r = begin;
std::advance(r, rand()%left);
std::swap(*begin, *r);
++begin;
--left;
}
return begin;

// Requires -std=c++17 compilation flag
template<class RandomSampling>
RandomSampling random_sample(
RandomSampling neurons,
unsigned int sample_length
)
{
RandomSampling sample;
std::sample(neurons.begin(), neurons.end(), std::back_inserter(sample),
sample_length, std::mt19937{std::random_device{}()});
return sample;
}
15 changes: 9 additions & 6 deletions plexus/plexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Neuron::Neuron(Network& network)
void Neuron::partially_subscribe()
{
if (this->subscriptions.size() == 0) {
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
unsigned seed = std::chrono::system_clock::now()
.time_since_epoch().count();
std::default_random_engine generator (seed);
std::normal_distribution<double> distribution(
this->network->get_connectivity(),
Expand All @@ -34,9 +35,12 @@ void Neuron::partially_subscribe()
sample_length = this->network->nonmotor_neurons.size();
if (sample_length <= 0)
sample_length = 0;
std::vector<Neuron*> elected = random_sample(
this->network->nonmotor_neurons,
sample_length
);
this->subscriptions.reserve(sample_length);
for (unsigned int i = 0; i < sample_length; i++) {
Neuron* target_neuron = this->network->nonmotor_neurons[i];
for (auto target_neuron: elected) {
if (target_neuron != this) {
this->subscriptions.insert(
std::pair<Neuron*, double>(
Expand Down Expand Up @@ -203,9 +207,8 @@ void Network::_ignite(Network* network)
std::vector<Neuron*> ban_list;
while (network->freezer == false) {
if (network->randomly_fire) {
Neuron* neuron = random_unique(
network->nonsensory_neurons.begin(),
network->nonsensory_neurons.end(),
Neuron* neuron = random_sample(
network->nonsensory_neurons,
1
)[0];
if (neuron->type == MOTOR_NEURON) {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
name='cplexus',
sources=['plexus/plexus.cpp', 'plexus/wrapper.cpp'],
language='c++',
extra_compile_args=['-w'],
extra_compile_args=['-w', '-std=c++17'],
)
]
)

0 comments on commit 0cecefc

Please sign in to comment.