* whitespace cleanup
This commit is contained in:
parent
56c6edab04
commit
70e60a50d2
195 changed files with 1763 additions and 1873 deletions
|
|
@ -18,15 +18,14 @@ LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
|
|||
SET (GPROP_SOURCES gprop.cpp)
|
||||
|
||||
# especially for Visual Studio
|
||||
IF(NOT WIN32 OR CYGWIN)
|
||||
ADD_EXECUTABLE(gprop ${GPROP_SOURCES})
|
||||
IF(NOT WIN32 OR CYGWIN)
|
||||
ADD_EXECUTABLE(gprop ${GPROP_SOURCES})
|
||||
ADD_DEPENDENCIES(gprop eo eoutils)
|
||||
|
||||
TARGET_LINK_LIBRARIES(gprop eo eoutils)
|
||||
|
||||
|
||||
TARGET_LINK_LIBRARIES(gprop eo eoutils)
|
||||
|
||||
SET(GPROP_VERSION ${GLOBAL_VERSION})
|
||||
SET_TARGET_PROPERTIES(gprop PROPERTIES VERSION "${GPROP_VERSION}")
|
||||
ENDIF(NOT WIN32 OR CYGWIN)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
|
|||
|
|
@ -207,12 +207,12 @@ int correct(const mlp::net& net, const mlp::set& set)
|
|||
unsigned partial = 0;
|
||||
|
||||
for (unsigned i = 0; i < s->output.size(); ++i)
|
||||
if ((s->output[i] < 0.5 && net(s->input)[i] < 0.5) ||
|
||||
(s->output[i] > 0.5 && net(s->input)[i] > 0.5))
|
||||
++partial;
|
||||
if ((s->output[i] < 0.5 && net(s->input)[i] < 0.5) ||
|
||||
(s->output[i] > 0.5 && net(s->input)[i] > 0.5))
|
||||
++partial;
|
||||
|
||||
if (partial == s->output.size())
|
||||
++sum;
|
||||
++sum;
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ namespace l2
|
|||
//---------------------------------------------------------------------------
|
||||
// error
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
real error(const mlp::net& net, const set& ts)
|
||||
{
|
||||
real error_ = 0.0;
|
||||
|
|
@ -37,12 +37,12 @@ namespace l2
|
|||
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
||||
{
|
||||
vector out = net(s->input);
|
||||
|
||||
|
||||
for (unsigned i = 0; i < out.size(); ++i)
|
||||
{
|
||||
real target = s->output[i];
|
||||
real value = out[i];
|
||||
error_ -= target * log(value + min_real) +
|
||||
error_ -= target * log(value + min_real) +
|
||||
(1.0 - target) * log(1.0 - value + min_real);
|
||||
}
|
||||
}
|
||||
|
|
@ -53,25 +53,25 @@ namespace l2
|
|||
//-------------------------------------------------------------------------
|
||||
// l2
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
|
||||
class net: public qp::net
|
||||
{
|
||||
public:
|
||||
net(mlp::net& n): qp::net(n) {}
|
||||
|
||||
|
||||
real error(const set& ts)
|
||||
{
|
||||
real error_ = 0;
|
||||
|
||||
|
||||
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
||||
{
|
||||
forward(s->input);
|
||||
error_ -= backward(s->input, s->output);
|
||||
}
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
real backward(const vector& input, const vector& output)
|
||||
{
|
||||
|
|
@ -84,7 +84,7 @@ namespace l2
|
|||
{
|
||||
neuron& n = (*current_layer)[j];
|
||||
real out = output[j];
|
||||
n.ndelta += n.delta = (out - n.out) /
|
||||
n.ndelta += n.delta = (out - n.out) /
|
||||
(n.out * (1.0 - n.out) + min_real) * n.out * (1.0 - n.out);
|
||||
|
||||
if (size() == 1) // monolayer
|
||||
|
|
@ -92,21 +92,21 @@ namespace l2
|
|||
else // multilayer
|
||||
for (unsigned k = 0; k < n.dxo.size(); ++k)
|
||||
n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
||||
|
||||
error_ += out * log(n.out + min_real) +
|
||||
|
||||
error_ += out * log(n.out + min_real) +
|
||||
(1.0 - out) * log(1.0 - n.out + min_real);
|
||||
}
|
||||
|
||||
|
||||
// hidden layers
|
||||
while (++current_layer != rend())
|
||||
{
|
||||
reverse_iterator forward_layer = current_layer - 1;
|
||||
reverse_iterator backward_layer = current_layer + 1;
|
||||
|
||||
reverse_iterator backward_layer = current_layer + 1;
|
||||
|
||||
for (unsigned j = 0; j < current_layer->size(); ++j)
|
||||
{
|
||||
neuron& n = (*current_layer)[j];
|
||||
real sum = 0;
|
||||
real sum = 0;
|
||||
for (unsigned k = 0; k < forward_layer->size(); ++k)
|
||||
{
|
||||
neuron& nf = (*forward_layer)[k];
|
||||
|
|
@ -114,7 +114,7 @@ namespace l2
|
|||
}
|
||||
n.delta = n.out * (1.0 - n.out) * sum;
|
||||
n.ndelta += n.delta;
|
||||
|
||||
|
||||
if (backward_layer == rend()) // first hidden layer
|
||||
n.dxo += n.delta * input;
|
||||
else // rest of hidden layers
|
||||
|
|
@ -122,19 +122,19 @@ namespace l2
|
|||
n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
} // namespace l2
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // l2_h
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace std {
|
|||
istream& operator>>(istream& is, mlp::vector& v)
|
||||
{
|
||||
for (mlp::vector::iterator vi = v.begin() ; vi != v.end() ; vi++) {
|
||||
is >> *vi;
|
||||
is >> *vi;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
|
@ -133,15 +133,15 @@ namespace mlp
|
|||
#ifdef HAVE_LIBYAML_CPP
|
||||
YAML_SERIALIZABLE_AUTO(neuron)
|
||||
void emit_yaml(YAML::Emitter&out) const {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "Class" << YAML::Value << "mlp::neuron";
|
||||
YAML_EMIT_MEMBER(out,bias);
|
||||
YAML_EMIT_MEMBER(out,weight);
|
||||
out << YAML::EndMap;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "Class" << YAML::Value << "mlp::neuron";
|
||||
YAML_EMIT_MEMBER(out,bias);
|
||||
YAML_EMIT_MEMBER(out,weight);
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
void load_yaml(const YAML::Node& node) {
|
||||
YAML_LOAD_MEMBER(node, bias);
|
||||
YAML_LOAD_MEMBER(node, weight);
|
||||
YAML_LOAD_MEMBER(node, bias);
|
||||
YAML_LOAD_MEMBER(node, weight);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
|
@ -213,17 +213,17 @@ namespace mlp {
|
|||
}
|
||||
#ifdef HAVE_LIBYAML_CPP
|
||||
friend ostream& operator<<(YAML::Emitter& e, const layer &l) {
|
||||
e << ((std::vector<neuron>)l);
|
||||
e << ((std::vector<neuron>)l);
|
||||
}
|
||||
|
||||
friend void operator>>(const YAML::Node& n, layer &l) {
|
||||
// These temporary variable shenanegins are necessary because
|
||||
// the compiler gets very confused about which template operator>>
|
||||
// function to use.
|
||||
// The following does not work: n >> l;
|
||||
// So we use a temporary variable thusly:
|
||||
std::vector<mlp::neuron> *obviously_a_vector = &l;
|
||||
n >> *obviously_a_vector;
|
||||
// These temporary variable shenanegins are necessary because
|
||||
// the compiler gets very confused about which template operator>>
|
||||
// function to use.
|
||||
// The following does not work: n >> l;
|
||||
// So we use a temporary variable thusly:
|
||||
std::vector<mlp::neuron> *obviously_a_vector = &l;
|
||||
n >> *obviously_a_vector;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -243,7 +243,7 @@ namespace std {
|
|||
istream& operator>>(istream& is, mlp::layer& l)
|
||||
{
|
||||
for (mlp::layer::iterator li = l.begin() ; li != l.end() ; li++) {
|
||||
is >> *li;
|
||||
is >> *li;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
|
@ -277,15 +277,15 @@ namespace mlp {
|
|||
#ifdef HAVE_LIBYAML_CPP
|
||||
YAML_SERIALIZABLE_AUTO(net)
|
||||
void emit_members(YAML::Emitter&out) const {
|
||||
const std::vector<layer>* me_as_layer_vector = this;
|
||||
out << YAML::Key << "layers" << YAML::Value << *me_as_layer_vector;
|
||||
const std::vector<layer>* me_as_layer_vector = this;
|
||||
out << YAML::Key << "layers" << YAML::Value << *me_as_layer_vector;
|
||||
}
|
||||
|
||||
void load_members(const YAML::Node& node) {
|
||||
std::vector<layer>* me_as_layer_vector = this;
|
||||
node["layers"] >> *me_as_layer_vector;
|
||||
std::vector<layer>* me_as_layer_vector = this;
|
||||
node["layers"] >> *me_as_layer_vector;
|
||||
}
|
||||
#endif // HAVE_LIBYAML_CPP
|
||||
#endif // HAVE_LIBYAML_CPP
|
||||
|
||||
/** Virtual destructor */
|
||||
virtual ~net() {};
|
||||
|
|
@ -303,14 +303,14 @@ namespace mlp {
|
|||
is >> layer_size;
|
||||
layer_sizes.push_back(layer_size);
|
||||
}
|
||||
unsigned check_outputs;
|
||||
unsigned check_outputs;
|
||||
is >> check_outputs;
|
||||
assert (check_outputs == num_outputs);
|
||||
init (num_inputs,num_outputs,layer_sizes);
|
||||
// skip forward to pass up opening '<' char
|
||||
char c=' ';
|
||||
while (c!='<' && !is.eof()) { is >> c;}
|
||||
for (iterator l =begin() ; l != end(); l++) {
|
||||
// skip forward to pass up opening '<' char
|
||||
char c=' ';
|
||||
while (c!='<' && !is.eof()) { is >> c;}
|
||||
for (iterator l =begin() ; l != end(); l++) {
|
||||
is >> *l;
|
||||
}
|
||||
do { is >> c; } while (c == ' ' && !is.eof());
|
||||
|
|
@ -351,15 +351,15 @@ namespace mlp {
|
|||
}
|
||||
|
||||
void save(ostream &os) const {
|
||||
// Save the number of inputs, number of outputs, and number of hidden layers
|
||||
// Save the number of inputs, number of outputs, and number of hidden layers
|
||||
os << num_inputs() << "\n" << num_outputs() << "\n" << num_hidden_layers() << "\n";
|
||||
for(const_iterator l = begin(); l != end(); ++l)
|
||||
for(const_iterator l = begin(); l != end(); ++l)
|
||||
os << l->size() << " ";
|
||||
os << "\n";
|
||||
os << "< ";
|
||||
for(const_iterator l = begin(); l != end(); ++l)
|
||||
os << "\n";
|
||||
os << "< ";
|
||||
for(const_iterator l = begin(); l != end(); ++l)
|
||||
os << *l << " ";
|
||||
os << ">\n";
|
||||
os << ">\n";
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -454,7 +454,7 @@ namespace mlp {
|
|||
|
||||
void load(istream &is) {
|
||||
unsigned input_size, output_size;
|
||||
is >> input_size >> output_size;
|
||||
is >> input_size >> output_size;
|
||||
sample samp(input_size, output_size);;
|
||||
while (is >> samp) { push_back(samp); }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace mse
|
|||
//---------------------------------------------------------------------------
|
||||
// error
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
real error(const mlp::net& net, const set& ts)
|
||||
{
|
||||
real error_ = 0.0;
|
||||
|
|
@ -36,7 +36,7 @@ namespace mse
|
|||
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
||||
{
|
||||
vector out = net(s->input);
|
||||
|
||||
|
||||
for (unsigned i = 0; i < out.size(); ++i)
|
||||
{
|
||||
real diff = s->output[i] - out[i];
|
||||
|
|
@ -49,26 +49,26 @@ namespace mse
|
|||
//-------------------------------------------------------------------------
|
||||
// mse
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
|
||||
class net: public qp::net
|
||||
{
|
||||
public:
|
||||
net(mlp::net& n): qp::net(n) {}
|
||||
|
||||
|
||||
real error(const set& ts)
|
||||
{
|
||||
real error_ = 0;
|
||||
|
||||
|
||||
for (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
||||
{
|
||||
forward(s->input);
|
||||
error_ += backward(s->input, s->output);
|
||||
}
|
||||
error_ /= ts.size();
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
real backward(const vector& input, const vector& output)
|
||||
{
|
||||
|
|
@ -89,22 +89,22 @@ namespace mse
|
|||
else // multilayer
|
||||
for (unsigned k = 0; k < n.dxo.size(); ++k)
|
||||
n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
||||
|
||||
|
||||
error_ += diff * diff;
|
||||
}
|
||||
|
||||
|
||||
// hidden layers
|
||||
while (++current_layer != rend())
|
||||
{
|
||||
reverse_iterator forward_layer = current_layer - 1;
|
||||
reverse_iterator backward_layer = current_layer + 1;
|
||||
|
||||
|
||||
for (unsigned j = 0; j < current_layer->size(); ++j)
|
||||
{
|
||||
|
||||
|
||||
neuron& n = (*current_layer)[j];
|
||||
real sum = 0;
|
||||
|
||||
|
||||
for (unsigned k = 0; k < forward_layer->size(); ++k)
|
||||
{
|
||||
neuron& nf = (*forward_layer)[k];
|
||||
|
|
@ -113,8 +113,8 @@ namespace mse
|
|||
|
||||
n.delta = n.out * (1.0 - n.out) * sum;
|
||||
n.ndelta += n.delta;
|
||||
|
||||
|
||||
|
||||
|
||||
if (backward_layer == rend()) // first hidden layer
|
||||
n.dxo += n.delta * input;
|
||||
else // rest of hidden layers
|
||||
|
|
@ -122,19 +122,19 @@ namespace mse
|
|||
n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
} // namespace mse
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // mse_h
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
|
|
|
|||
|
|
@ -41,55 +41,55 @@ namespace qp
|
|||
const real backtrack_step = 0.5;
|
||||
const real me_floor = 0.0001;
|
||||
const real mw_floor = 0.0001;
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// neuron
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
struct neuron
|
||||
{
|
||||
mlp::neuron* n;
|
||||
real out, delta, ndelta, dbias1, dbias2;
|
||||
vector dweight1, dweight2, dxo;
|
||||
|
||||
neuron(mlp::neuron& _n):
|
||||
n(&_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
|
||||
dweight1(n->weight.size(), 0),
|
||||
dweight2(n->weight.size(), 0),
|
||||
|
||||
neuron(mlp::neuron& _n):
|
||||
n(&_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
|
||||
dweight1(n->weight.size(), 0),
|
||||
dweight2(n->weight.size(), 0),
|
||||
dxo(n->weight.size(), 0) {}
|
||||
|
||||
|
||||
void reset()
|
||||
{
|
||||
// underlaying neuron
|
||||
n->reset();
|
||||
|
||||
|
||||
// addons
|
||||
out = delta = ndelta = dbias1 = dbias2 = 0;
|
||||
fill(dweight1.begin(), dweight1.end(), 0);
|
||||
fill(dweight2.begin(), dweight2.end(), 0);
|
||||
fill(dxo.begin(), dxo.end(), 0);
|
||||
}
|
||||
|
||||
|
||||
real operator()(const vector& input)
|
||||
{
|
||||
return out = mlp::sigmoid(n->bias + dbias1 +
|
||||
return out = mlp::sigmoid(n->bias + dbias1 +
|
||||
(n->weight + dweight1) * input);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const neuron& n)
|
||||
{
|
||||
return os << *n.n << " " << n.out << " " << n.delta << " "
|
||||
<< n.ndelta << " " << n.dbias1 << " " << n.dbias2 << " "
|
||||
return os << *n.n << " " << n.out << " " << n.delta << " "
|
||||
<< n.ndelta << " " << n.dbias1 << " " << n.dbias2 << " "
|
||||
<< n.dweight1 << " " << n.dweight2 << " " << n.dxo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// layer
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class layer: public std::vector<neuron>
|
||||
{
|
||||
public:
|
||||
|
|
@ -102,16 +102,16 @@ namespace qp
|
|||
void reset()
|
||||
{
|
||||
for(iterator n = begin(); n != end(); ++n)
|
||||
n->reset();
|
||||
n->reset();
|
||||
}
|
||||
|
||||
vector operator()(const vector& input)
|
||||
{
|
||||
vector output(size());
|
||||
|
||||
|
||||
for(unsigned i = 0; i < output.size(); ++i)
|
||||
output[i] = (*this)[i](input);
|
||||
|
||||
output[i] = (*this)[i](input);
|
||||
|
||||
return output;
|
||||
}
|
||||
};
|
||||
|
|
@ -120,10 +120,10 @@ namespace qp
|
|||
//---------------------------------------------------------------------------
|
||||
// net
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class net: public std::vector<layer>
|
||||
{
|
||||
public:
|
||||
public:
|
||||
net(mlp::net& n) //: std::vector<layer>(n.begin(), n.end()) { reset(); }
|
||||
{
|
||||
for (mlp::net::iterator l = n.begin(); l != n.end(); ++l)
|
||||
|
|
@ -135,27 +135,27 @@ namespace qp
|
|||
void reset()
|
||||
{
|
||||
for(iterator l = begin(); l != end(); ++l)
|
||||
l->reset();
|
||||
l->reset();
|
||||
}
|
||||
|
||||
real train(const set& ts,
|
||||
unsigned epochs,
|
||||
real target_error,
|
||||
|
||||
real train(const set& ts,
|
||||
unsigned epochs,
|
||||
real target_error,
|
||||
real tolerance,
|
||||
real eta = eta_default,
|
||||
real momentum = alpha_default,
|
||||
real eta = eta_default,
|
||||
real momentum = alpha_default,
|
||||
real lambda = lambda_default)
|
||||
{
|
||||
real error_ = max_real;
|
||||
|
||||
|
||||
while (epochs-- && error_ > target_error)
|
||||
{
|
||||
real last_error = error_;
|
||||
|
||||
|
||||
init_delta();
|
||||
|
||||
error_ = error(ts);
|
||||
|
||||
|
||||
if (error_ < last_error + tolerance)
|
||||
{
|
||||
coeff_adapt(eta, momentum, lambda);
|
||||
|
|
@ -170,10 +170,10 @@ namespace qp
|
|||
error_ = last_error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return error_;
|
||||
}
|
||||
|
||||
|
||||
virtual real error(const set& ts) = 0;
|
||||
|
||||
// protected:
|
||||
|
|
@ -185,7 +185,7 @@ namespace qp
|
|||
input.swap(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// private:
|
||||
void init_delta()
|
||||
{
|
||||
|
|
@ -193,11 +193,11 @@ namespace qp
|
|||
for (layer::iterator n = l->begin(); n != l->end(); ++n)
|
||||
fill(n->dxo.begin(), n->dxo.end(), n->ndelta = 0.0);
|
||||
}
|
||||
|
||||
|
||||
void coeff_adapt(real& eta, real& momentum, real& lambda)
|
||||
{
|
||||
real me = 0, mw = 0, ew = 0;
|
||||
|
||||
|
||||
for (iterator l = begin(); l != end(); ++l)
|
||||
for (layer::iterator n = l->begin(); n != l->end(); ++n)
|
||||
{
|
||||
|
|
@ -205,7 +205,7 @@ namespace qp
|
|||
mw += n->dweight1 * n->dweight1;
|
||||
ew += n->dxo * n->dweight1;
|
||||
}
|
||||
|
||||
|
||||
me = std::max(static_cast<real>(sqrt(me)), me_floor);
|
||||
mw = std::max(static_cast<real>(sqrt(mw)), mw_floor);
|
||||
eta *= (1.0 + 0.5 * ew / ( me * mw));
|
||||
|
|
@ -213,11 +213,11 @@ namespace qp
|
|||
lambda = lambda0 * me / mw;
|
||||
momentum = eta * lambda;
|
||||
#ifdef DEBUG
|
||||
std::cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
std::cout << me << " \t" << mw << " \t" << ew << " \t"
|
||||
<< eta << " \t" << momentum << " \t" << lambda << std::endl;
|
||||
#endif // DEBUG
|
||||
}
|
||||
|
||||
|
||||
void weight_update(unsigned size, bool fire, real eta, real momentum)
|
||||
{
|
||||
for (iterator l = begin(); l != end(); ++l)
|
||||
|
|
@ -239,13 +239,13 @@ namespace qp
|
|||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
} // namespace qp
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // qp_h
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
|
|
|
|||
|
|
@ -160,14 +160,14 @@ template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T
|
|||
{
|
||||
std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, " "));
|
||||
os << v.back();
|
||||
}
|
||||
}
|
||||
return os << '>';
|
||||
}
|
||||
|
||||
template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
|
||||
{
|
||||
v.clear();
|
||||
|
||||
|
||||
char c;
|
||||
is >> c;
|
||||
if (!is || c != '<')
|
||||
|
|
@ -186,7 +186,7 @@ template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
|
|||
}
|
||||
} while (is && c != '>');
|
||||
}
|
||||
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
|
|
@ -194,11 +194,11 @@ template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v)
|
|||
// euclidean_distance
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class T> T euclidean_distance(const std::vector<T>& v1,
|
||||
template<class T> T euclidean_distance(const std::vector<T>& v1,
|
||||
const std::vector<T>& v2)
|
||||
{
|
||||
T sum = 0, tmp;
|
||||
|
||||
|
||||
for (unsigned i = 0; i < v1.size(); ++i)
|
||||
{
|
||||
tmp = v1[i] - v2[i];
|
||||
|
|
@ -211,4 +211,3 @@ template<class T> T euclidean_distance(const std::vector<T>& v1,
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
|
|||
SET (GPSYMREG_SOURCES main.cpp)
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
ADD_EXECUTABLE(gpsymreg ${GPSYMREG_SOURCES})
|
||||
|
||||
ADD_EXECUTABLE(gpsymreg ${GPSYMREG_SOURCES})
|
||||
|
||||
ADD_DEPENDENCIES(gpsymreg eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
|
|
@ -36,4 +36,3 @@ SET_TARGET_PROPERTIES(gpsymreg PROPERTIES VERSION "${GPSYMREG_VERSION}")
|
|||
TARGET_LINK_LIBRARIES(gpsymreg eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
jeggermo@liacs.nl
|
||||
jeggermo@liacs.nl
|
||||
*/
|
||||
|
||||
#ifndef _FITNESS_FUNCTION_H
|
||||
|
|
@ -225,4 +225,3 @@ class RegFitness: public eoEvalFunc< eoParseTree<FitnessType, Node> >
|
|||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
|
||||
jeggermo@liacs.nl
|
||||
jeggermo@liacs.nl
|
||||
|
||||
*/
|
||||
|
||||
|
|
@ -146,25 +146,25 @@ int main(int argc, char *argv[])
|
|||
|
||||
// the parameters are passed on as well
|
||||
|
||||
RegFitness eval(generationCounter, initSequence, parameter);
|
||||
RegFitness eval(generationCounter, initSequence, parameter);
|
||||
|
||||
|
||||
|
||||
// Depth Initializor, set for Ramped Half and Half Initialization
|
||||
|
||||
eoParseTreeDepthInit<FitnessType, Node> initializer(parameter.InitMaxDepth, initSequence, true, true);
|
||||
eoParseTreeDepthInit<FitnessType, Node> initializer(parameter.InitMaxDepth, initSequence, true, true);
|
||||
|
||||
|
||||
|
||||
// create the initial population
|
||||
|
||||
Pop pop(parameter.population_size, initializer);
|
||||
Pop pop(parameter.population_size, initializer);
|
||||
|
||||
|
||||
|
||||
// and evaluate the individuals
|
||||
|
||||
apply<EoType>(eval, pop);
|
||||
apply<EoType>(eval, pop);
|
||||
|
||||
|
||||
|
||||
|
|
@ -335,10 +335,3 @@ int main(int argc, char *argv[])
|
|||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,18 +3,18 @@
|
|||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
jeggermo@liacs.nl
|
||||
jeggermo@liacs.nl
|
||||
*/
|
||||
|
||||
#ifndef _NODE_H
|
||||
|
|
@ -33,13 +33,13 @@ using namespace std;
|
|||
/* A new Operation and Node class for even more flexibility.
|
||||
|
||||
Improvements over the t-eoSymreg code are:
|
||||
|
||||
|
||||
* No hardcoded functions or operators. The Operation and Node class below
|
||||
allow you to specify your own unary and binary functions as well as
|
||||
binary operators (like +,-,*,/). Moreover you can detemine if you want
|
||||
to allow primitve subroutines with either one or two arguments.
|
||||
|
||||
If a Node has a subroutine Operation it will take evaluate the first
|
||||
|
||||
If a Node has a subroutine Operation it will take evaluate the first
|
||||
(and possible second) child branch and use them as input variables for
|
||||
the remaining second (or third) child branch.
|
||||
*/
|
||||
|
|
@ -47,20 +47,20 @@ using namespace std;
|
|||
|
||||
typedef enum {Variable, UFunction, BFunction, BOperator, Const} Type;
|
||||
|
||||
typedef double (*BinaryFunction)(const double,const double);
|
||||
typedef double (*BinaryFunction)(const double,const double);
|
||||
typedef double (*UnaryFunction)(const double);
|
||||
|
||||
struct Operation
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
typedef unsigned int VariableID;
|
||||
typedef string Label;
|
||||
|
||||
|
||||
|
||||
// if your compiler allows you to have nameless unions you can make this a
|
||||
// union by removing the //'s below
|
||||
|
||||
|
||||
//union
|
||||
//{
|
||||
UnaryFunction uFunction;
|
||||
|
|
@ -68,34 +68,34 @@ struct Operation
|
|||
VariableID id;
|
||||
double constant;
|
||||
//};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Label label;
|
||||
Type type;
|
||||
|
||||
|
||||
// the default constructor results in a constant with value 0
|
||||
Operation() : constant(0), label("0"), type(Const){};
|
||||
// two possible constructors for Unary Functions
|
||||
Operation(UnaryFunction _uf, Label _label): uFunction(_uf), label(_label), type(UFunction) {};
|
||||
Operation(Label _label, UnaryFunction _uf): uFunction(_uf), label(_label), type(UFunction) {};
|
||||
|
||||
|
||||
// Watch out there are two constructors using pointers two binary functions:
|
||||
// Binary Function (printed as label(subtree0,subtree1) (e.g. pow(x,y))
|
||||
// Binary Operator (printed as (subtree0 label subtree1) (e.g. x^y)
|
||||
// The difference is purely cosmetic.
|
||||
|
||||
|
||||
// If you specify the label before the function pointer -> Binary Function
|
||||
Operation(Label _label, BinaryFunction _bf): bFunction(_bf), label(_label), type(BFunction) {};
|
||||
// If you specify the function pointer before the label -> Binary Operator
|
||||
Operation(BinaryFunction _bf, Label _label): bFunction(_bf), label(_label), type(BOperator) {};
|
||||
|
||||
|
||||
// A constructor for variables
|
||||
Operation(VariableID _id, Label _label): id(_id), label(_label), type(Variable) {};
|
||||
// A constructor for constants
|
||||
Operation(double _constant, Label _label): constant(_constant), label(_label), type(Const) {};
|
||||
|
||||
|
||||
|
||||
|
||||
Operation(const Operation &_op)
|
||||
{
|
||||
switch(_op.type)
|
||||
|
|
@ -110,7 +110,7 @@ struct Operation
|
|||
label = _op.label;
|
||||
};
|
||||
virtual ~Operation(){};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -118,14 +118,14 @@ class Node
|
|||
{
|
||||
private:
|
||||
Operation op;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
Node(void): op(Operation()){};
|
||||
Node(Operation &_op) : op(_op){};
|
||||
virtual ~Node(void) {}
|
||||
|
||||
int arity(void) const
|
||||
|
||||
int arity(void) const
|
||||
{
|
||||
switch(op.type)
|
||||
{
|
||||
|
|
@ -134,53 +134,53 @@ class Node
|
|||
case BFunction: return 2;
|
||||
case BOperator: return 2;
|
||||
case Const: return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void randomize(void) {}
|
||||
|
||||
|
||||
template<class Children>
|
||||
void operator()(double& result, Children args, vector<double> &var) const
|
||||
{
|
||||
double result0;
|
||||
double result1;
|
||||
|
||||
|
||||
|
||||
|
||||
switch(op.type)
|
||||
{
|
||||
case Variable: result = var[op.id%var.size()]; //%var.size() used in the case of Subroutines and as a security measure
|
||||
break;
|
||||
case UFunction: args[0].apply(result0, var);
|
||||
result = op.uFunction(result0);
|
||||
result = op.uFunction(result0);
|
||||
break;
|
||||
case BFunction:
|
||||
case BFunction:
|
||||
case BOperator: args[0].apply(result0, var);
|
||||
args[1].apply(result1, var);
|
||||
args[1].apply(result1, var);
|
||||
result = op.bFunction(result0,result1);
|
||||
break;
|
||||
case Const: result = op.constant;
|
||||
break;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
template<class Children>
|
||||
void operator()(string& result, Children args) const
|
||||
{
|
||||
|
||||
|
||||
string subtree0;
|
||||
string subtree1;
|
||||
string subtree2;
|
||||
|
||||
|
||||
switch(op.type)
|
||||
{
|
||||
|
||||
case Variable:
|
||||
|
||||
case Variable:
|
||||
case Const: result += op.label;
|
||||
break;
|
||||
|
||||
|
||||
case UFunction: result += op.label;
|
||||
result += "(";
|
||||
args[0].apply(subtree0);
|
||||
|
|
@ -195,7 +195,7 @@ class Node
|
|||
args[1].apply(subtree1);
|
||||
result += subtree1;
|
||||
result += ")";
|
||||
break;
|
||||
break;
|
||||
case BOperator: result += "(";
|
||||
args[0].apply(subtree0);
|
||||
result += subtree0;
|
||||
|
|
@ -204,16 +204,16 @@ class Node
|
|||
result += subtree1;
|
||||
result += ")";
|
||||
break;
|
||||
default: result += "ERROR in Node::operator(string,...) \n"; break;
|
||||
default: result += "ERROR in Node::operator(string,...) \n"; break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Operation getOp(void) const {return op;}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -230,7 +230,7 @@ class Node
|
|||
std::ostream& operator<<(std::ostream& os, const Node& eot)
|
||||
{
|
||||
Operation op(eot.getOp());
|
||||
|
||||
|
||||
os << (eot.getOp()).label;
|
||||
return os;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,18 @@
|
|||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
jeggermo@liacs.nl
|
||||
jeggermo@liacs.nl
|
||||
*/
|
||||
|
||||
#ifndef _PARAMETERS_FUNCTION_H
|
||||
|
|
@ -28,7 +28,7 @@ using namespace std;
|
|||
|
||||
struct Parameters{
|
||||
unsigned int nGenerations; // -G
|
||||
unsigned population_size; // -P
|
||||
unsigned population_size; // -P
|
||||
unsigned offspring_size; // -O
|
||||
unsigned int MaxSize; // -S
|
||||
unsigned int InitMaxDepth; // -D
|
||||
|
|
@ -41,72 +41,72 @@ struct Parameters{
|
|||
Parameters(int argc, char **argv)
|
||||
{
|
||||
eoParser parser(argc,argv);
|
||||
|
||||
|
||||
// generations
|
||||
eoValueParam<unsigned int> paramGenerations(1, "generations", "Generations", 'G', false);
|
||||
parser.processParam( paramGenerations );
|
||||
nGenerations = paramGenerations.value();
|
||||
cerr << "nGenerations= " << nGenerations << endl;
|
||||
|
||||
|
||||
// populationsize
|
||||
eoValueParam<unsigned int> paramPopulationSize(10, "populationsize", "PopulationSize", 'P', false);
|
||||
parser.processParam( paramPopulationSize );
|
||||
population_size = paramPopulationSize.value();
|
||||
cerr << "population_size= " << population_size << endl;
|
||||
|
||||
|
||||
// offspringsize
|
||||
eoValueParam<unsigned int> paramOffspringSize(population_size, "offspringsize", "OffspringSize", 'O', false);
|
||||
parser.processParam( paramOffspringSize );
|
||||
offspring_size = paramOffspringSize.value();
|
||||
cerr << "offspring_size= " << offspring_size << endl;
|
||||
|
||||
|
||||
// maxsize
|
||||
eoValueParam<unsigned int> paramMaxSize(15, "maxsize", "MaxSize", 'S', false);
|
||||
parser.processParam( paramMaxSize );
|
||||
MaxSize = paramMaxSize.value();
|
||||
cerr << "MaxSize= " << MaxSize << endl;
|
||||
|
||||
|
||||
// initialmaxdepth
|
||||
eoValueParam<unsigned int> paramInitialMaxDepth(4, "initialmaxdepth", "InitialMaxDepth", 'D', false);
|
||||
parser.processParam( paramInitialMaxDepth );
|
||||
InitMaxDepth = paramInitialMaxDepth.value();
|
||||
cerr << "InitMaxDepth= " << InitMaxDepth << endl;
|
||||
|
||||
|
||||
// randomseed
|
||||
eoValueParam<unsigned int> paramRandomSeed(1, "randomseed", "Random Seed", 'R', false);
|
||||
parser.processParam( paramRandomSeed );
|
||||
randomseed = paramRandomSeed.value();
|
||||
cerr << "randomseed= " << randomseed << endl;
|
||||
|
||||
|
||||
|
||||
|
||||
// crossover-rate
|
||||
eoValueParam<double> paramXover(0.75, "crossoverrate", "crossover rate", 'x', false);
|
||||
parser.processParam(paramXover );
|
||||
xover_rate = paramXover.value();
|
||||
cerr << "xover_rate= " << xover_rate << endl;
|
||||
|
||||
|
||||
//mutation-rate
|
||||
eoValueParam<double> paramMutation(0.25, "mutationrate", "mutation rate", 'm', false);
|
||||
parser.processParam(paramMutation );
|
||||
mutation_rate = paramMutation.value();
|
||||
cerr << "mutation_rate= " << mutation_rate << endl;
|
||||
|
||||
|
||||
//tournament size
|
||||
eoValueParam<unsigned int > paramTournamentSize(5, "tournamentsize", "tournament size", 't', false);
|
||||
parser.processParam(paramTournamentSize );
|
||||
tournamentsize = paramTournamentSize.value();
|
||||
cerr << "Tournament Size= " << tournamentsize << endl;
|
||||
|
||||
|
||||
|
||||
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
~Parameters(){};
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
|
|||
SET (MASTERMIND_SOURCES mastermind.cpp)
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
ADD_EXECUTABLE(mastermind ${MASTERMIND_SOURCES})
|
||||
|
||||
ADD_EXECUTABLE(mastermind ${MASTERMIND_SOURCES})
|
||||
|
||||
ADD_DEPENDENCIES(mastermind eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
|
|
@ -36,4 +36,3 @@ SET_TARGET_PROPERTIES(mastermind PROPERTIES VERSION "${MASTERMIND_VERSION}")
|
|||
TARGET_LINK_LIBRARIES(mastermind eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
|
|||
|
|
@ -51,8 +51,8 @@ int main(int argc, char** argv)
|
|||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
cerr << argv[0] << ": " << e.what() << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
cerr << argv[0] << ": " << e.what() << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Reference in a new issue