AI-PSO
view release on metacpan or search on metacpan
examples/NeuralNet/NeuralNet.h view on Meta::CPAN
/// \file NeuralNet.h
/// \brief Header file for simple 3 layer feed forward NeuralNet classes / DLL
///
/// \author Kyle Schlansker
/// \date August 2004
///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#ifdef WIN32
# ifdef NEURALNET_EXPORTS
# define NEURALNET_API __declspec(dllexport)
# else
# define NEURALNET_API __declspec(dllimport)
# endif
#else
# define NEURALNET_API
#endif
#ifndef NEURAL_NET
#define NEURAL_NET
///
/// \class TransferFunction NeuralNet.h NeuralNet
/// \brief defines a transfer function object
///
class NEURALNET_API TransferFunction
{
public:
///
/// \fn TransferFunction(double val)
/// \brief constructor
/// \param val a double
///
TransferFunction(double val = 1)
{
}
///
/// \fn ~TransferFunction()
/// \brief destructor
///
virtual ~TransferFunction()
{
}
///
/// \fn virtual double compute()
/// \brief computes the transfer function and returns result
/// \param val a double
/// \return double
///
virtual double compute(double val) = 0;
protected:
double m_value; /// value on which to compute the transfer function
};
///
/// \class UnityGain NeuralNet.h NeuralNet
/// \brief defines a transfer function that passes its output as its input (good for input neurons)
///
class NEURALNET_API UnityGain : public TransferFunction
{
public:
///
/// \fn UnityGain(double val)
/// \brief constructor
/// \param val a double
///
UnityGain(double val = 1) : TransferFunction(val)
{
}
///
/// \fn ~UnityGain()
/// \brief destructor
///
~UnityGain()
{
}
///
/// \fn compute(double val)
/// \brief computes the transfer function by returning the input
/// \return double
///
double compute(double val)
{
return m_value = val;
}
};
///
/// \class Logistic NeuralNet.h NeuralNet
/// \brief defines the Logistic transfer function
///
class NEURALNET_API Logistic : public TransferFunction
{
public:
examples/NeuralNet/NeuralNet.h view on Meta::CPAN
///
/// \fn void setWeight(int index, double weight)
/// \brief sets the connection weight of connection at
/// index to weight
/// \param index an int
/// \param weight a double
///
void setWeight(int index, double weight)
{
if(index >= 0 && index <= m_numConnections)
m_weights[index] = weight;
}
///
/// \fn int numConnections()
/// \brief returns the number of connections this Neuron has
/// \return int
///
int numConnections()
{
return m_numConnections;
}
protected:
///
/// \fn void checkSize()
/// \brief checks the size of the connection array for this Neuron.
/// if a connection needs to be added past the capacity, then
/// new connection array space is allocated.
///
void checkSize()
{
if( m_numConnections >= m_capacity )
{
m_capacity *= 2;
Neuron **newNeuronArr = new Neuron*[m_capacity];
double *newWeightArr = new double[m_capacity];
for(int i = 0; i < m_numConnections; i++)
{
newNeuronArr[i] = m_neurons[i];
newWeightArr[i] = m_weights[i];
}
delete [] m_neurons;
delete [] m_weights;
m_neurons = newNeuronArr;
m_weights = newWeightArr;
}
}
///
/// \fn double transferFunction(double val)
/// \brief applies a transfer function to val and returns the result
/// \param val a double
/// \return double
///
double transferFunc(double val)
{
return val;
}
int m_numConnections; /// number of connections to other Neurons
int m_capacity; /// capacity of connection array
Neuron **m_neurons; /// connection array of pointers to other Neurons
double *m_weights; /// weight array of connections
double m_value; /// value of this Neuron
TransferFunction *xfer;
};
///
/// \class Input NeuralNet.h NeuralNet
/// \brief Simulates an input neuron in a Neural net. This class extends Neuron
/// but allows for its value to be set directly and it also overrides
/// the virtual value function so that it returns its value directly
/// rather than passing though a transfer function.
///
class NEURALNET_API Input : public Neuron
{
public:
///
/// \fn Input(double value)
/// \brief constructor
///
Input(double value = 0) : Neuron()
{
m_value = value;
}
///
/// \fn ~Input()
/// \brief destructor
///
virtual ~Input()
{
}
///
/// \fn void setValue(double value)
/// \brief sets the value of this input Neuron to value
/// \param value a double
///
void setValue(double value)
{
m_value = value;
}
( run in 0.352 second using v1.01-cache-2.11-cpan-99c4e6809bf )