⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplenet.h

📁 amygdata的神经网络算法源代码
💻 H
字号:
/***************************************************************************                          simplenet.h  -  description                             -------------------    begin                : Fri Nov 21 2003    copyright            : (C) 2003 by Rdiger Koch    email                : rkoch@rkoch.org ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   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.                                   * *                                                                         * ***************************************************************************/#ifndef SIMPLENET_H#define SIMPLENET_H#include <amygdala/genomehandler.h>#include <amygdala/factory.h>#include <amygdala/topology.h>#include <vector>#include <map>#include <string>namespace Amygdala {class Network;class Neuron;class Genome;/** @brief A morphogenesis model using a fixed arrangement of neurons - a hollow sphere.  *  * Axons growth is guided by attractor and repellant emitting cells. Since repellants  * are simply negative attractors they're dealt with in the same way and are only called  * attractors. Sensitivities determine  * which attractor a neuron is sensitive to. A neuron can be sensitive to several  * of the 8 possible attractors. Neurons belonging only to non existant  * attractors are dead. Neurons belonging to no attractor arborize in their neighborhood  * only. The axon growth follows the attractor gradient. The axon forks in case  * of a sensitivity to several attractors.  * Once it reaches a strong enough concentration it connects to dendritic trees at that location.  * The axon continues to grow until it reaches the maximal concentration of attractor  * <p>  * Sensitivity and attractor emitters are located on the outer radius. Their number  * is variable but can only be of one of 8 types. The concentration of the substance  * they emmit decreases S=S_0/r^2 where r is the distance from the emitter.  * <p>  * The input and output are provided by neurons that should be designed and  * set to "fixed" in the genome server. This is to make sure that we always have  * appropriate input and output neurons. After the morphogenesis is complete it is  * suggested to input some engineering knowledge and set a few connections between  * I and O neurons manually to push the  * network into the desired direction right from the beginning.  * <p>  * This class can be used in 2 modes:  * <ul>  *    <li>Creation of a genome  *    <li>Parsing a genome and creation of a network  * </ul>  * @see Genome::SetHandler  * @author Rudiger Koch  */class SimpleNet : public GenomeHandler  {public:    typedef struct {        unsigned int type;  // 0-7 for eight attractors        float strength;          // -1.0 to +1.0        float x, y, z;    } Emitter;     typedef std::vector <Emitter>::const_iterator emitter_iterator;    emitter_iterator sensitivities_begin() { return sensitivities.begin(); }       emitter_iterator sensitivities_end() { return sensitivities.end(); }       emitter_iterator attractors_begin() { return attractors.begin(); }       emitter_iterator attractors_end() { return attractors.end(); }       /** Encapsulate the axon growth */    class GrowNeuron {    public:        typedef struct { float x; float y; float z;}  Location;                // typedefs used only for visualization         typedef std::vector <Location>::const_iterator axon_iterator;        typedef struct {Location location; Neuron * neuron; } SynLoc;        typedef std::vector <SynLoc>::const_iterator synloc_iterator;    private:        typedef Location AxonTip;        unsigned char axonType;         bool growing;        Neuron *neuron;        AxonTip axonTip;        // these vectors are only for visualization        std::vector <SynLoc> synLocs;        std::vector <Location> axon;        const float sensitivityThreshold;        const int radius;        const SimpleNet * simplenet;        Amygdala::Network * net;                /** Summation of all influences from sensitivities          */        void CalculateType(const std::vector <Emitter> & sensitivities);        /** This function makes a synapse to the neuron located            at the current axontip unless it exists already. */        void TryMkSynapse();        void TryMkOutputSynapses();        bool AlreadyConnected(Neuron * postNeuron);    public:        /** provided for visualization */        axon_iterator axon_begin() { return axon.begin(); }        /** provided for visualization */        axon_iterator axon_end() { return axon.end(); }        /** provided for visualization */        synloc_iterator synlocs_begin() { return synLocs.begin(); }        /** provided for visualization */        synloc_iterator synlocs_end() { return synLocs.end(); }        /** grow the axon            @return true if the axon can still grow, false otherwise */        bool GrowAxon(const std::vector <Emitter> & attractors);        void InitAxon(const std::vector <Emitter> & sensitivities, Neuron * n);        void Growing(bool g) { growing = g; }        Neuron * GetNeuron() const { return neuron; }        GrowNeuron(SimpleNet * sn, const unsigned int _radius, Amygdala::Network * _net);        ~GrowNeuron();    };   // END of GrowNeuron        /** iterate through the cube. This iterator returns the neurons in no      * particular order      */    class cube_iterator {        GrowNeuron **** cube;        int size, x, y, z;    public:        static bool end;        cube_iterator(SimpleNet * sn, bool end = false){             cube = sn->cube;            size = 2 * sn->oR + 1;            if(end) {                x = y = z = size;                return;            }            x = y = z = 0;            if(cube [x][y][z] == NULL) (*this)++;        }                cube_iterator & operator++(int){            do {                if ( ++x >= size){                    x = 0;                    if(++y >= size){                        y = 0;                        if(++z >= size){                            x = y = z = size;                            return *this;                        }                    }                }            } while (cube [x][y][z] == NULL);            return *this;           }                GrowNeuron * operator * () { return cube [x][y][z]; } const                bool operator==(const cube_iterator & right) const        {            if(x==right.x && y == right.y && z == right.z) return true;            else return false;        }        bool operator!=(const cube_iterator & right) const        {            if(x==right.x && y == right.y && z == right.z) return false;            else return true;        }    };        cube_iterator begin() { cube_iterator it(this); return it;}    cube_iterator end() { cube_iterator it(this, cube_iterator::end); return it;}        typedef std::vector <GrowNeuron *>::const_iterator neuron_iterator;    neuron_iterator input_begin() { return inputNeurons.begin(); }    neuron_iterator input_end() { return inputNeurons.end(); }    neuron_iterator output_begin() { return outputNeurons.begin(); }    neuron_iterator output_end() { return outputNeurons.end(); }                SimpleNet(std::string nType, float _oR, float _iR);    ~SimpleNet();/** Add one input neuron. Neuron IDs are given in the order of the call  * first for input, then for output neurons  * @see AddOutputNeuron() */    void AddInputNeuron(float phi, float psi, AmIdInt id);/** Add one output neuron. Neuron IDs are given in the order of the call  * first for input, then for output neurons  * @see AddInputNeuron() */    void AddOutputNeuron(float phi, float psi, AmIdInt id);/** When done with adding input / output neurons call this function to generate  * a string representation of the genome and upload to the server.  * @param uri the uri in the form http://server:port  * @see AddInputNeuron()  * @see AddOutputNeuron() */    void Upload(std::string uri);/** iterate through all neurons and grow the axons a little  * @return true if at least one axon grew a significant amount, false otherwise  */    bool Step();    void FinishParsing();    void Gene(const std::string & gene, AmIdInt chromosomeId);        std::vector < AmIdInt > GetInputIDs();    std::vector < AmIdInt > GetOutputIDs();private:    void MakeEmitter(Emitter & emitter, const std::string & gene);    void DestroyCube();    void MakeCube();    void MakeIOChromosome (std::vector<std::string> & neurons, Genome & g);    void MakeIONeuron(const std::string & gene, int type);    void MakeInhibitories();    private: // member vars    NFactory * neuronFactory;    Network * net;    float outerRadius, innerRadius;    int oR;    AmIdInt maxId;    Amygdala::AmIdInt chromosome;    GrowNeuron **** cube;    std::vector <GrowNeuron *> inputNeurons;    std::vector <GrowNeuron *> outputNeurons;    std::vector <std::string> inputChromosome;    std::vector <std::string> outputChromosome;    Topology *top;    /** determines the type of neuron */    std::vector <Emitter> sensitivities;    /** guides the axon growth */    std::vector <Emitter> attractors;        const float FACTOR;};} // namespace Amygdala#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -