Skip to content

Commit

Permalink
Removed the dependency of boost::random. Now Boost is not needed to u…
Browse files Browse the repository at this point in the history
…se the library.
  • Loading branch information
peter-ch committed Oct 1, 2014
1 parent 0f451d4 commit a74a8a8
Show file tree
Hide file tree
Showing 29 changed files with 131 additions and 79 deletions.
13 changes: 7 additions & 6 deletions DEPENDS
@@ -1,8 +1,9 @@
* Boost 1.49 and above with Boost.Python and Boost.Serialization
* Python 2.7 (with headers and libs)
* ProgressBar (Python package)
* NumPy (Python package)
* Matplotlib (Python package)
* OpenCV 2.3 and above (with Python bindings)
* Boost 1.49 and above with Boost.Python and Boost.Serialization (optional)
* Python 2.7 (with headers and libs) (optional)
* ProgressBar (Python package) (optional)
* NumPy (Python package) (optional)
* Matplotlib (Python package) (optional)
* OpenCV 2.3 and above (with Python bindings) (optional)
* Cython (if you want Python bindings)


50 changes: 25 additions & 25 deletions MultiNEAT.project
Expand Up @@ -29,31 +29,31 @@
<Description/>
<Dependencies/>
<VirtualDirectory Name="src">
<File Name="lib/Assert.h"/>
<File Name="lib/Genes.h"/>
<File Name="lib/Genome.cpp"/>
<File Name="lib/Genome.h"/>
<File Name="lib/Innovation.cpp"/>
<File Name="lib/Innovation.h"/>
<File Name="lib/Main.cpp"/>
<File Name="lib/NeuralNetwork.cpp"/>
<File Name="lib/NeuralNetwork.h"/>
<File Name="lib/Parameters.cpp"/>
<File Name="lib/Parameters.h"/>
<File Name="lib/PhenotypeBehavior.cpp"/>
<File Name="lib/PhenotypeBehavior.h"/>
<File Name="lib/Population.cpp"/>
<File Name="lib/Population.h"/>
<File Name="lib/PythonBindings.cpp"/>
<File Name="lib/PythonBindings.h"/>
<File Name="lib/Random.cpp"/>
<File Name="lib/Random.h"/>
<File Name="lib/Species.cpp"/>
<File Name="lib/Species.h"/>
<File Name="lib/Substrate.cpp"/>
<File Name="lib/Substrate.h"/>
<File Name="lib/Utils.cpp"/>
<File Name="lib/Utils.h"/>
<File Name="src/Assert.h"/>
<File Name="src/Genes.h"/>
<File Name="src/Genome.cpp"/>
<File Name="src/Genome.h"/>
<File Name="src/Innovation.cpp"/>
<File Name="src/Innovation.h"/>
<File Name="src/Main.cpp"/>
<File Name="src/NeuralNetwork.cpp"/>
<File Name="src/NeuralNetwork.h"/>
<File Name="src/Parameters.cpp"/>
<File Name="src/Parameters.h"/>
<File Name="src/PhenotypeBehavior.cpp"/>
<File Name="src/PhenotypeBehavior.h"/>
<File Name="src/Population.cpp"/>
<File Name="src/Population.h"/>
<File Name="src/PythonBindings.cpp"/>
<File Name="src/PythonBindings.h"/>
<File Name="src/Random.cpp"/>
<File Name="src/Random.h"/>
<File Name="src/Species.cpp"/>
<File Name="src/Species.h"/>
<File Name="src/Substrate.cpp"/>
<File Name="src/Substrate.h"/>
<File Name="src/Utils.cpp"/>
<File Name="src/Utils.h"/>
</VirtualDirectory>
<Settings Type="Executable">
<GlobalSettings>
Expand Down
12 changes: 12 additions & 0 deletions MultiNEAT.workspace
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<CodeLite_Workspace Name="MultiNEAT" Database="">
<Project Name="MultiNEAT" Path="MultiNEAT.project" Active="Yes"/>
<BuildMatrix>
<WorkspaceConfiguration Name="Debug" Selected="yes">
<Project Name="MultiNEAT" ConfigName="Debug"/>
</WorkspaceConfiguration>
<WorkspaceConfiguration Name="Release" Selected="no">
<Project Name="MultiNEAT" ConfigName="Release"/>
</WorkspaceConfiguration>
</BuildMatrix>
</CodeLite_Workspace>
24 changes: 12 additions & 12 deletions setup.py
Expand Up @@ -5,18 +5,18 @@
setup(name='MultiNEAT',
version='0.1',
py_modules=['MultiNEAT'],
ext_modules=[Extension('_MultiNEAT', ['lib/Genome.cpp',
'lib/Innovation.cpp',
'lib/NeuralNetwork.cpp',
'lib/Parameters.cpp',
'lib/PhenotypeBehavior.cpp',
'lib/Population.cpp',
'lib/PythonBindings.cpp',
'lib/Random.cpp',
'lib/Species.cpp',
'lib/Substrate.cpp',
'lib/Utils.cpp'],
ext_modules=[Extension('_MultiNEAT', ['src/Genome.cpp',
'src/Innovation.cpp',
'src/NeuralNetwork.cpp',
'src/Parameters.cpp',
'src/PhenotypeBehavior.cpp',
'src/Population.cpp',
'src/PythonBindings.cpp',
'src/Random.cpp',
'src/Species.cpp',
'src/Substrate.cpp',
'src/Utils.cpp'],
libraries=['boost_python',
'boost_serialization'],
'boost_serialization'],
extra_compile_args=['-O3', '-march=native'])
])
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/Main.cpp → src/Main.cpp
Expand Up @@ -18,6 +18,9 @@ double test(Genome& g)

int main()
{
srand(time(0));
std::cout << (double)(rand() % 10000000) / 10000000.0 << "\n";
/*
Parameters params;
params.PopulationSize = 1000;
RNG rng;
Expand All @@ -41,6 +44,7 @@ int main()
pop.Tick(g);
}
//pop.Epoch();
*/

/*for(int i=0; i < pop.m_Species.size(); i++)
for(int j=0; j < pop.m_Species[i].m_Individuals.size(); j++)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
87 changes: 65 additions & 22 deletions lib/Random.cpp → src/Random.cpp
Expand Up @@ -41,84 +41,127 @@ namespace NEAT
// Seeds the random number generator with this value
void RNG::Seed(int a_Seed)
{
#ifdef USE_BOOST_RANDOM
gen.seed(a_Seed);
#else
srand(a_Seed);
#endif
}

void RNG::TimeSeed()
{
gen.seed(time(0));
Seed(time(0));
}

// Returns randomly either 1 or -1
int RNG::RandPosNeg()
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_int_distribution<> dist(0, 1);
#elif USE_CPP11_RANDOM
std::uniform_int_distribution<int> dist(0,1);
#endif
int choice = dist(gen);
#else
int choice = rand() % 2;
#endif
if (choice == 0)
return -1;
else
return 1;
}

// Returns a random integer between X and Y
// in case of ( 0 .. 1 ) returns 0
int RNG::RandInt(int aX, int aY)
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_int_distribution<> dist(aX, aY);
#elif USE_CPP11_RANDOM
std::uniform_int_distribution<int> dist(0,1);
#endif
return dist(gen);
#else
return aX + (rand() % (aY - aX + 1));
#endif

}

// Returns a random number from a uniform distribution in the range of [0 .. 1]
double RNG::RandFloat()
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_01<> dist;
#elif USE_CPP11_RANDOM
std::uniform_real_distribution<double> dist(0.0, 1.0);
#endif
return dist(gen);
#else
return (double)(rand() % 10000000) / 10000000.0;
#endif
}

// Returns a random number from a uniform distribution in the range of [-1 .. 1]
double RNG::RandFloatClamped()
{
#ifdef USE_BOOST_RANDOM
return (RandFloat() - RandFloat());
#elif USE_CPP11_RANDOM
std::uniform_real_distribution<double> dist(-1.0, 1.0);
return dist(gen);
#endif
}

// Returns a random number from a gaussian (normal) distribution in the range of [-1 .. 1]
double RNG::RandGaussClamped()
{
#ifdef USE_BOOST_RANDOM
boost::random::normal_distribution<> dist;
#elif USE_CPP11_RANDOM
std::normal_distribution<double> dist(0.0, 1.0);
#endif
double pick = dist(gen);
Clamp(pick, -1, 1);
return pick;
#else
static int t_iset=0;
static double t_gset;
double t_fac,t_rsq,t_v1,t_v2;

if (t_iset==0)
{
do
{
t_v1=2.0f*(RandFloat())-1.0f;
t_v2=2.0f*(RandFloat())-1.0f;
t_rsq=t_v1*t_v1+t_v2*t_v2;
}
while (t_rsq>=1.0f || t_rsq==0.0f);

t_fac=sqrt(-2.0f*log(t_rsq)/t_rsq);
t_gset=t_v1*t_fac;
t_iset=1;

double t_tmp = t_v2*t_fac;

Clamp(t_tmp, -1.0, 1.0);
return t_tmp;
}
else
{
t_iset=0;
double t_tmp = t_gset;
Clamp(t_tmp, -1.0, 1.0);
return t_tmp;
}
#endif
}

int RNG::Roulette(std::vector<double>& a_probs)
{
#ifdef USE_BOOST_RANDOM
boost::random::discrete_distribution<> d_dist(a_probs);
#elif USE_CPP11_RANDOM
std::discrete_distribution<int> dist(a_probs);
#endif
return d_dist(gen);
#else
double t_marble = 0, t_spin = 0, t_total_score = 0;
for(unsigned int i=0; i<a_probs.size(); i++)
{
t_total_score += a_probs[i];
}
t_marble = RandFloat() * t_total_score;

int t_chosen = 0;
t_spin = a_probs[t_chosen];
while(t_spin < t_marble)
{
t_chosen++;
t_spin += a_probs[t_chosen];
}

return t_chosen;
#endif
}


Expand Down
20 changes: 6 additions & 14 deletions lib/Random.h → src/Random.h
Expand Up @@ -30,31 +30,23 @@
// Description: Declarations for a class dealing with random numbers.
///////////////////////////////////////////////////////////////////////////////

#define USE_BOOST_RANDOM
#ifdef USE_BOOST_RANDOM

#include <boost/random.hpp>

#elif USE_CPP11_RANDOM

#include <random>

#include <boost/random.hpp>
#else
#include <stdlib.h>
#endif

#include <vector>
#include <limits>

namespace NEAT
{

class RNG
{

#ifdef USE_BOOST_RANDOM

boost::random::mt19937 gen;

#elif USE_CPP11_RANDOM

std::default_random_engine gen;

#endif

public:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a74a8a8

Please sign in to comment.