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

📄 factory.h

📁 amygdata的神经网络算法源代码
💻 H
字号:
/***************************************************************************                          factory.h  -  description                             -------------------    copyright            : (C) 2004 by Matt Grover    email                : mgrover@amygdala.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 FACTORY_H#define FACTORY_H #include <amygdala/amygdalaclass.h>#include <amygdala/network.h>#include <amygdala/outputmanager.h>#include <amygdala/trainer.h>#include <amygdala/spikingneuron.h>#include <string>#include <map>namespace Amygdala {class Topology;class FactoryBase;class Neuron;/** @class FactoryRegistry factory.h amygdala/factory.h * @brief A singleton container for factories that can be retrieved by name * * FactoryRegistry provides a method for storing factories * and retrieving them again with the name of the class produced * by the factory. * @see Factory  * @author Matt Grover <mgrover@amygdala.org> * @author Rudiger Koch <rkoch@rkoch.org> */class FactoryRegistry: public AmygdalaClass {public:	/** @return Ptr to a factory that builds className */	FactoryBase* GetFactory(std::string className);	/** Add a factory to the registry	 * @param className The class that the factory build	 * @param factory Ptr to a factory that builds className */	void AddFactory(std::string className, FactoryBase* factory);private:    /** Register all Amygdala classes that need a resolver */	FactoryRegistry();	~FactoryRegistry(){};	std::map<std::string, FactoryBase*> factories; 	// className, ptr to factory	friend class FactoryBase;};/** @class FactoryBase factory.h amygdala/factory.h * @brief A class that does nothing other than creating other classes * * A Factory is a class that constructs another class of a specific type. * This is a base class for all other factory classes.  * @author Matt Grover <mgrover@amygdala.org> * @author Rudiger Koch <rkoch@rkoch.org> */class FactoryBase: public AmygdalaClass {public:    virtual ~FactoryBase() {}    /** @return An instance of the class */    virtual AmygdalaClass* MakeObject() = 0;    /** @return The name of the class that the factory builds */    /** @return Reference to the FactoryRegistry singleton */    static FactoryRegistry& GetRegistry() { return registry; }protected:    FactoryBase() {}    static FactoryRegistry registry;};/** @class NFactory factory.h amygdala/factory.h * @brief Base class for NeuronFactory * * @see NeuronFactory, FactoryBase  * @author Matt Grover <mgrover@amygdala.org> * @author Rudiger Koch <rkoch@rkoch.org> */class NFactory: public FactoryBase {public:    virtual ~NFactory() {}    virtual AmygdalaClass* MakeObject() = 0;        /** @return Ptr to a new Neuron that has been added to     * the Topology top */    virtual Neuron* MakeNeuron(AmIdInt neuronId,                        NeuronProperties* neuronProps,                        Topology* top) = 0;    /** @return Ptr to a NeuronProperties object that matches     * the specific type of Neuron that is built by the factory */    virtual NeuronProperties* MakeNeuronProperties(bool initPhysProps) = 0;protected:    NFactory():FactoryBase() {}    friend class Topology;};/** @class NeuronFactory factory.h amygdala/factory.h  * create Neuron instances of class &lt;neuron&gt;. When users want to write their  * own neuron types they can instantiate a NeuronFactory for it and register  * it.  * @see Factory, NFactory  * @author Rudiger Koch  * @author Matt Grover  */template<class neuron, class neuronProperties>class NeuronFactory : public NFactory {public:    virtual ~NeuronFactory(){}    virtual neuron * MakeObject(){        return 0;    }    /** The () operator allows this factory to be used as a functor.     * @return Ptr to a new Neuron */    neuron* operator()(AmIdInt neuronId, const neuronProperties& neuronProps, Topology* top);    /** The () operator allows this factory to be used as a functor.     * @return Ptr to a new Neuron */    neuron* operator()(const neuronProperties& neuronProps, Topology* top);    /** @return Ptr to a new Neuron */    neuron* MakeNeuron(AmIdInt neuronId,                            const neuronProperties& neuronProps, Topology* top);    /** @return Ptr to a new Neuron */    neuron* MakeNeuron(const neuronProperties& neuronProps, Topology* top);    NeuronFactory():NFactory() {}    //NeuronFactory(Topology* _topology):NFactory(), topology(_topology) {}    /** Version for NetLoader */    virtual Neuron* MakeNeuron(AmIdInt neuronId,                        NeuronProperties* neuronProps,                        Topology* top);    /** For NetLoader */                            virtual neuronProperties* MakeNeuronProperties(bool initPhysProps);private:    friend class Topology;    friend class NetLoader;};/** @class SIFactory factory.h amygdala/factory.h  * @brief a virtual base class for the template class SpikeInputFactory  * @author Matt Grover <mgrover@amygdala.org>  * @author Rudiger Koch <rkoch@rkoch.org>  */class SIFactory : public FactoryBase {public:    virtual ~SIFactory(){}    virtual SpikeInput * MakeSpikeInput(const std::string& name) = 0;protected:    SIFactory() : FactoryBase() {}};/** @class SpikeInputFactory factory.h amygdala/factory.h * @brief A factory for constructing SpikeInput objects. * * @see FactoryBase * @see SIFactory * @author Matt Grover <mgrover@amygdala.org> * @author Rudiger Koch <rkoch@rkoch.org> */template<class spikeinput>class SpikeInputFactory: public SIFactory {public:    spikeinput* operator()(const std::string& name) { return MakeSpikeInput(name); }        /** @return Ptr to a new spikeinput */    virtual spikeinput * MakeSpikeInput(const std::string& name)    {        spikeinput * si = new spikeinput(name);        Network::GetNetworkRef()->AddSpikeInput(si);        return si;    }    virtual spikeinput * MakeObject() {        return 0;    }};/** @class SOFactory factory.h amygdala/factory.h  * @brief a virtual base class for the template class SpikeOutputFactory  * @author Matt Grover <mgrover@amygdala.org>  * @author Rudiger Koch <rkoch@rkoch.org>  */class SOFactory : public FactoryBase {public:    virtual ~SOFactory(){}    virtual SpikeOutput* MakeSpikeOutput(std::string name) = 0;protected:    SOFactory() : FactoryBase() {}};/** @class SpikeOutputFactory factory.h amygdala/factory.h  * @brief A factory for constructing SpikeOuput objects.  *  * @see FactoryBase  * @see SOFactory  * @author Matt Grover <mgrover@amygdala.org>  * @author Rudiger Koch <rkoch@rkoch.org>  */template<class spikeoutput>class SpikeOutputFactory: public SOFactory {public:    spikeoutput* operator()(std::string name) { return MakeSpikeOutput(name); }    /** @return Ptr to a new spikeinput */    virtual spikeoutput* MakeSpikeOutput(std::string name)    {        spikeoutput * so = new spikeoutput();        OutputManager::AddSpikeOutput(so);        return so;    }        virtual spikeoutput * MakeObject() {        return 0;    }};/** @class TFactory factory.h amygdala/factory.h  * @brief a virtual base class for the template class TopologyFactory  * @author Matt Grover <mgrover@amygdala.org>  * @author Rudiger Koch <rkoch@rkoch.org>  */class TFactory : public FactoryBase {public:    virtual ~TFactory(){}        virtual Topology * MakeTopology(std::string name) = 0;        virtual AmygdalaClass* MakeObject() = 0;protected:    TFactory() : FactoryBase() {}    void AddTopologyToNet(Topology* top) const { Network::GetNetworkRef()->AddTopology(top); }};/** @class TopologyFactory factory.h amygdala/factory.h * @brief A factory for constructing Topology objects. * * @see FactoryBase */template<class topology>class TopologyFactory: public TFactory {public:    TopologyFactory():TFactory() {}    virtual ~TopologyFactory() {}    virtual Topology* MakeObject() {	    return 0;    }    /** @return Ptr to a new Topology */    virtual topology* MakeTopology(std::string name)    {        topology* top = new topology(name);        AddTopologyToNet(top);        return top;    }    /** Operator () allows TopologyFactory to act as a functor     * @return Ptr to a new Topology */    topology* operator()(std::string name) { return MakeTopology(name); }private:    friend class Network;    friend class FactoryRegistry;};/** @class TrFactory factory.h amygdala/factory.h * @brief Abstract base class for TrainerFactory * * @see TrainerFactory, FactoryBase */class TrFactory: public FactoryBase {public:    virtual ~TrFactory() {}    virtual AmygdalaClass* MakeObject() = 0;    /** @return Ptr to a new trainer for Neuron nrn */    virtual Trainer* MakeTrainer(TrainerProperties* props, SpikingNeuron* nrn, std::string name) = 0;    virtual Trainer* MakeTrainer(TrainerProperties* props, Topology* top, std::string name) = 0;    /** @return Ptr to a TrainerProperties object that matches     * the specific type of Trainer that is built by the factory */    virtual TrainerProperties* MakeTrainerProperties() = 0;protected:    TrFactory():FactoryBase() {}};/** @class TrainerFactory factory.h amygdala/factory.h  * create Trainer instances of class &lt;trainer&gt;. When users want to write their  * own trainer types they can instantiate a TrainerFactory for it and register  * it.  * @see Factory, TrFactory  * @author Matt Grover  */template<class trainer, class trainerProperties>class TrainerFactory : public TrFactory {public:    virtual ~TrainerFactory(){}    virtual trainer * MakeObject(){        return 0;    }    /** The () operator allows this factory to be used as a functor.     * @return Ptr to a new Neuron */    trainer* operator()(trainerProperties& props, SpikingNeuron* nrn, std::string name) {        return MakeTrainer(props, nrn, name);    }    trainer* operator()(trainerProperties& props, Topology* top, std::string name) {        return MakeTrainer(props, top, name);    }    /** @return Ptr to a new Neuron */    trainer* MakeTrainer(trainerProperties& props, SpikingNeuron* nrn, std::string name) {        trainer* t = new trainer(props, name);        Network::GetNetworkRef()->AddTrainer(name, t);        nrn->SetTrainer(t);        return t;    }    trainer* MakeTrainer(trainerProperties& props, Topology* top, std::string name) {        trainer* t = new trainer(props, name);        Network::GetNetworkRef()->AddTrainer(name, t);        for (NeuronGroup::iterator itr=top->begin(); itr!=top->end(); itr++) {            SpikingNeuron* sn = dynamic_cast<SpikingNeuron*>(*itr);            if (sn) {                sn->SetTrainer(t);            }            else {                throw std::string("MakeTrainer() can only add Trainers to SpikingNeurons");            }        }        return t;    }        TrainerFactory():TrFactory() {}    /** Version for NetLoader */    virtual Trainer* MakeTrainer(TrainerProperties* props, SpikingNeuron* nrn, std::string name) {        trainer* t = new trainer(dynamic_cast<trainerProperties&>(*props), name);        Network::GetNetworkRef()->AddTrainer(name, t);        nrn->SetTrainer(t);        return t;    }    virtual Trainer* MakeTrainer(TrainerProperties* props, Topology* top, std::string name) {        trainer* t = new trainer(dynamic_cast<trainerProperties&>(*props), name);        Network::GetNetworkRef()->AddTrainer(name, t);        for (NeuronGroup::iterator itr=top->begin(); itr!=top->end(); itr++) {            SpikingNeuron* sn = dynamic_cast<SpikingNeuron*>(*itr);            if (sn) {                sn->SetTrainer(t);            }            else {                throw std::string("MakeTrainer() can only add Trainers to SpikingNeurons");            }        }        return t;    }        /** For NetLoader */    virtual trainerProperties* MakeTrainerProperties() {        trainerProperties* tp = new trainerProperties();        return tp;    }private:};// Template function definitionstemplate<class neuron, class neuronProperties>neuron* NeuronFactory<neuron,neuronProperties>::MakeNeuron(AmIdInt neuronId,                        const neuronProperties& neuronProps, Topology* top){    neuron* nrn = new neuron(neuronId, neuronProps);    if (top) top->AddNeuron(nrn);    return nrn;}template<class neuron, class neuronProperties>neuron* NeuronFactory<neuron, neuronProperties>::MakeNeuron(const neuronProperties& neuronProps, Topology* top){    AmIdInt nrnId = Network::GetNewNeuronId();    neuron* nrn = new neuron(nrnId, neuronProps);    if (top) top->AddNeuron(nrn);    return nrn;}template<class neuron, class neuronProperties>neuron* NeuronFactory<neuron, neuronProperties>::operator()(AmIdInt neuronId, const neuronProperties& neuronProps, Topology* top){    return MakeNeuron(neuronId, neuronProps, top);}template<class neuron, class neuronProperties>neuron* NeuronFactory<neuron, neuronProperties>::operator()(const neuronProperties& neuronProps, Topology* top){    return MakeNeuron(neuronProps, top);}template<class neuron, class neuronProperties>Neuron* NeuronFactory<neuron, neuronProperties>::MakeNeuron(AmIdInt neuronId,                        NeuronProperties* neuronProps,                        Topology* top){    neuron* nrn = new neuron(neuronId, dynamic_cast<neuronProperties&>(*neuronProps));    if (top) top->AddNeuron(nrn);    return nrn;}template<class neuron, class neuronProperties>neuronProperties* NeuronFactory<neuron, neuronProperties>::MakeNeuronProperties(bool initPhysProps){    return new neuronProperties(initPhysProps);}} // namespace Amygdala#endif

⌨️ 快捷键说明

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