AI-PSO

 view release on metacpan or  search on metacpan

examples/NeuralNet/NeuralNet.h  view on Meta::CPAN

        /// \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:
        
        ///
        /// \fn Logistic()
        /// \brief constructor
        /// 
        Logistic(double val = 1) : TransferFunction(val)
        {
        }


        ///
        /// \fn ~Logistic()
        /// \brief denstructor
        /// 
        ~Logistic()
        {
        }

        
        ///
        /// \fn double compute(double val)
        /// \brief computes the Logistic function on val
        /// \return double
        double compute(double val)
        {
            m_value = 1.0 / (1.0 + exp(val));
            return m_value = val;
        }
};


///
/// \class Neuron NeuralNet.h NeuralNet
/// \brief exported class which simulates a neruon within a Neural Net
///
class NEURALNET_API Neuron
{
    public:

        ///
        /// \fn Neuron()
        /// \brief constructor
        /// \note, add flag in constructor to choose what type of TransferFunction to use
        ///
        Neuron()
        {
            m_capacity = 1;
            m_numConnections = 0;
            m_neurons = new Neuron*[m_capacity];
            m_weights = new double[m_capacity];
            m_value = 0;
            xfer = new UnityGain();
        }


        ///
        /// \fn ~Neuron()
        /// \brief destructor
        ///
        virtual ~Neuron()
        {
            delete [] m_neurons;
            delete [] m_weights;
            delete xfer;
        }

        
        ///
        /// \fn virtual double value()
        /// \brief calculates the value of the neuron.  It is virtual
        ///            because the value is calculated differently for
        ///            different types of Neurons.
        ///
        virtual double value()
        {
            for(int i = 0; i < m_numConnections; i++)
                m_value += m_neurons[i]->value() * m_weights[i];
            return m_value = xfer->compute(m_value);
        }


        ///
        /// \fn void addConnection(Neuron *neuron)
        /// \brief adds a connection to another neuron
        /// \param neuron a pointer to the connected Neuron
        ///
        void addConnection(Neuron *neuron)
        {
            checkSize();
            m_neurons[m_numConnections++] = neuron;
        }


        ///
        /// \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;

examples/NeuralNet/NeuralNet.h  view on Meta::CPAN

        {
            in  >> m_numInputs
                >> m_numHidden;
            
            delete [] m_inputs;
            delete [] m_hidden;

            m_inputs = new Input[m_numInputs];
            m_hidden = new Neuron[m_numHidden];
            connectionize();

            double weight;

            for(int i = 0; i < m_numHidden; i++)
                for(int j = 0; j < m_hidden[i].numConnections(); j++)
                {
                    in >> weight;
                    m_hidden[i].setWeight(j, weight);
                }
            for(int k = 0; k < m_output.numConnections(); k++)
            {
                in >> weight;
                m_output.setWeight(k, weight);
            }
            
        }

        friend istream & operator>>(istream & in, NeuralNet & ann)
        {
            ann.read(in);
            return in;
        }

        void print(ostream & out)
        {
        }
*/
    protected:

        ///
        /// \fn connectionize()
        /// \brief builds a fully connected network once the Neurons are constructed
        /// 
        void connectionize()
        {
            for(int i = 0; i < m_numInputs; i++)
                for(int j = 0; j < m_numHidden; j++)
                    m_hidden[j].addConnection(&m_inputs[i]);

            for(int k = 0; k < m_numHidden; k++)
                m_output.addConnection(&m_hidden[k]);
        }


        int        m_numInputs;    /// number of input Neurons    in network
        int        m_numHidden;    /// number of hidden Neurons in network
        Input  *m_inputs;        /// array of Input Neurons
//        Neuron *m_hidden;        /// array of hidden Neurons
        Hidden *m_hidden;        /// array of hidden Neurons
        Neuron    m_output;        /// the single output Neuron (it is more efficient to have a separate network for each output)
        string  m_xferFunc;        /// type of transfer function for hidden neurons
};

#endif



( run in 2.015 seconds using v1.01-cache-2.11-cpan-df04353d9ac )