Skip to content

Commit 5d0cd80

Browse files
committed
pyclasset added
1 parent 03aaf70 commit 5d0cd80

7 files changed

Lines changed: 64 additions & 18 deletions

File tree

setup.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def getExtensions():
5858

5959
extra = ['-march=native',
6060
'-mtune=native',
61-
'-g'
61+
'-g',
6262
]
6363

6464
if platform == 'darwin':
@@ -121,14 +121,17 @@ def getExtensions():
121121
# 'libboost_serialization-mgw48-mt-1_58'],
122122
# include_dirs = ['C:/MinGW/include', 'C:/Users/Peter/Desktop/boost_1_58_0'],
123123
# library_dirs = ['C:/MinGW/lib', 'C:/Users/Peter/Desktop/boost_1_58_0/stage/lib'],
124-
extra.extend(['-DUSE_BOOST_PYTHON', '-DUSE_BOOST_RANDOM',
124+
extra.extend(['-DUSE_BOOST_PYTHON', '-DUSE_BOOST_RANDOM', #'-O0',
125125
#'-DVDEBUG',
126126
])
127-
extensionsList.append(Extension('MultiNEAT._MultiNEAT',
128-
sources,
129-
libraries=libs,
130-
extra_compile_args=extra)
131-
)
127+
exx = Extension('MultiNEAT._MultiNEAT',
128+
sources,
129+
libraries=libs,
130+
extra_compile_args=extra)
131+
print(dir(exx))
132+
print(exx)
133+
print(exx.extra_compile_args)
134+
extensionsList.append(exx)
132135
else:
133136
raise AttributeError('Unknown tool: {}'.format(build_sys))
134137

src/Genes.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ namespace NEAT
173173
py::object itp = bs::get<py::object>(it->second.m_Details);
174174
t = itp(); // details is a function that returns a random instance of the trait
175175
}
176+
177+
if (it->second.type == "pyclassset")
178+
{
179+
// this time m_Details is a (list, probs) tuple
180+
// the list is a list of classes that get instantiated
181+
py::object tup = bs::get<py::object>(it->second.m_Details);
182+
py::list classlist = py::extract<py::list>(tup[0]);
183+
py::list probs = py::extract<py::list>(tup[1]);
184+
std::vector<double> dprobs;
185+
186+
// get the probs
187+
int ln = py::len(probs);
188+
if ((ln == 0) || (py::len(classlist) == 0))
189+
{
190+
throw std::runtime_error("Empty class or probs list");
191+
}
192+
193+
for(int i=0; i<ln; i++)
194+
{
195+
dprobs.push_back(py::extract<double>(probs[i]));
196+
}
197+
198+
// instantiate random class
199+
int idx = a_RNG.Roulette(dprobs);
200+
py::object itp = py::extract<py::object>(classlist[idx]);
201+
t = itp();
202+
}
176203
#endif
177204

178205
Trait tr;
@@ -399,7 +426,7 @@ namespace NEAT
399426
did_mutate = true;
400427
}
401428
#ifdef USE_BOOST_PYTHON
402-
else if (it->second.type == "pyobject")
429+
else if ((it->second.type == "pyobject") || (it->second.type == "pyclassset"))
403430
{
404431
m_Traits[it->first].value = bs::get<py::object>(m_Traits[it->first].value).attr("mutate")();
405432
did_mutate = true;
@@ -562,7 +589,11 @@ namespace NEAT
562589
////////////////
563590
LinkGene()
564591
{
565-
592+
m_FromNeuronID = 0;
593+
m_ToNeuronID = 0;
594+
m_InnovationID = 0;
595+
m_Weight = 0;
596+
m_IsRecurrent = false;
566597
}
567598

568599
LinkGene(int a_InID, int a_OutID, int a_InnovID, double a_Wgt, bool a_Recurrent = false)

src/Genome.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ namespace NEAT
129129
}
130130

131131
// New constructor that creates a fully-connected CTRNN
132+
/*
132133
Genome::Genome(int a_ID,
133134
int a_NumInputs,
134135
int a_NumHidden, // ignored for seed type == 0, specifies number of hidden units if seed type == 1
@@ -242,13 +243,14 @@ namespace NEAT
242243
243244
m_initial_num_neurons = NumNeurons();
244245
m_initial_num_links = NumLinks();
245-
}
246+
}*/
246247

247248
Genome::Genome(int a_ID,
248249
int a_NumInputs,
249250
int a_NumHidden, // ignored for seed_type == 0, specifies number of hidden units if seed_type == 1
250251
int a_NumOutputs,
251-
bool a_FS_NEAT, ActivationFunction a_OutputActType,
252+
bool a_FS_NEAT,
253+
ActivationFunction a_OutputActType,
252254
ActivationFunction a_HiddenActType,
253255
int a_SeedType,
254256
const Parameters &a_Parameters,
@@ -506,6 +508,11 @@ namespace NEAT
506508
}
507509
}
508510
}
511+
512+
if (a_FS_NEAT && (a_FS_NEAT_links==1))
513+
{
514+
throw std::runtime_error("Known bug - don't use FS-NEAT with just 1 link and 1/1/1 genome");
515+
}
509516

510517
// Also initialize the Genome's traits
511518
m_GenomeGene.InitTraits(a_Parameters.GenomeTraits, t_RNG);

src/Genome.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ namespace NEAT
186186
Genome(std::ifstream &a_DataFile);
187187

188188
// This creates a CTRNN fully-connected genome
189-
Genome(int a_ID, int a_NumInputs, int a_NumHidden, int a_NumOutputs,
190-
ActivationFunction a_OutputActType, ActivationFunction a_HiddenActType, const Parameters &a_Parameters);
189+
//Genome(int a_ID, int a_NumInputs, int a_NumHidden, int a_NumOutputs,
190+
// ActivationFunction a_OutputActType, ActivationFunction a_HiddenActType, const Parameters &a_Parameters);
191191

192192
// This creates a standart minimal genome - perceptron-like structure
193193
Genome(int a_ID,

src/Parameters.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ class Parameters
654654
py::object itp = py::extract<py::object>(trait_params["details"]);
655655
t.m_Details = itp;
656656
}
657+
else if (t.type == "pyclassset")
658+
{
659+
py::object itp = py::extract<py::object>(trait_params["details"]);
660+
t.m_Details = itp;
661+
}
657662

658663
return t;
659664
}
@@ -740,6 +745,11 @@ class Parameters
740745
t["type"] = "pyobject";
741746
dt = bs::get<py::object>(pms.m_Details);
742747
}
748+
if (pms.type == "pyclassset")
749+
{
750+
t["type"] = "pyclassset";
751+
dt = bs::get<py::object>(pms.m_Details);
752+
}
743753

744754
t["details"] = dt;
745755

src/PythonBindings.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,6 @@ BOOST_PYTHON_MODULE(_MultiNEAT)
250250
class_<Genome, Genome*>("Genome", init<>())
251251

252252
.def(init<char*>())
253-
.def(init<unsigned int, unsigned int, unsigned int, unsigned int,
254-
ActivationFunction, ActivationFunction, Parameters>())
255-
//.def(init<unsigned int, unsigned int, unsigned int, unsigned int,
256-
// bool, ActivationFunction, ActivationFunction, int, Parameters, unsigned int>())
257253
.def(init<unsigned int, unsigned int, unsigned int, unsigned int,
258254
bool, ActivationFunction, ActivationFunction, int, Parameters, unsigned int, unsigned int>())
259255

src/Traits.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ namespace NEAT
183183
}
184184
};
185185

186-
187186
class TraitParameters
188187
{
189188
public:

0 commit comments

Comments
 (0)