From 237c7741d2465fd9618058d781a8522209641655 Mon Sep 17 00:00:00 2001 From: Shin Date: Wed, 18 May 2016 22:57:06 +0200 Subject: [PATCH] cellular encoding --- .../CelularEncoding/Cell.h | 182 +++++++++++++ .../CelularEncoding/CelularEncoding.h | 182 +++++++++++++ .../CelularEncoding/Exception.h | 23 ++ .../CelularEncoding/Instruction/Bias.h | 143 ++++++++++ .../CelularEncoding/Instruction/Division.h | 101 ++++++++ .../CelularEncoding/Instruction/Finish.h | 58 +++++ .../CelularEncoding/Instruction/Instruction.h | 32 +++ .../CelularEncoding/Instruction/Link.h | 245 ++++++++++++++++++ .../CelularEncoding/Instruction/Other.h | 34 +++ .../CellularEncoding/Cell.cpp | 12 + .../CellularEncoding/CellularEncoding.cpp | 20 ++ 11 files changed, 1032 insertions(+) create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Cell.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Exception.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Bias.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Division.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Finish.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Instruction.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Link.h create mode 100644 include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Other.h create mode 100644 src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/Cell.cpp create mode 100644 src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/CellularEncoding.cpp diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Cell.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Cell.h new file mode 100644 index 0000000..03fa359 --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Cell.h @@ -0,0 +1,182 @@ +#pragma once + +#include +#include "./Instruction/Instruction.h" + +#include +#include + +namespace NeuralNetworks { +namespace ConstructiveAlgorithms { +namespace CelularEncoding { + class CelularEncoding; + + class Link { + public: + Link(bool status_, float value_, std::size_t neuron_) : status(status_), value(value_), neuron(neuron_) { + + } + + bool status=false; + float value=0.0; + std::size_t neuron; + }; + + class Cell { + friend Instruction::Instruction; + + public: + Cell(const Cell&)=delete; + Cell&operator=(const Cell&)=delete; + + Cell(Cell &&r): id(r.id), codePtr(r.codePtr), _isOutput(r._isOutput), _isInput(r._isInput), alive(r.alive), life(r.life), bias(r.bias), links(r.links) { + + } + + Cell(const std::size_t& id_, const EvolutionaryAlgorithm::GeneticPrograming::CodeTree* codePtr_): id(id_), codePtr(codePtr_) { + + } + + void step(CelularEncoding&); + + bool isAlive() const { + return alive; + }; + + void die() { + alive=false; + }; + + std::size_t getLife() const { + return life; + } + + void setLife(const std::size_t &newLife) { + life=newLife; + } + + float getBias() const { + return bias; + } + void setBias(const float &newBias) { + bias=newBias; + } + + std::size_t getLinkRegister() const { + return linkRegister; + } + + void setLinkRegister(const std::size_t &newLinkRegister) { + linkRegister=newLinkRegister; + } + + const std::vector& getLinks() const { + return links; + } + + void setLinks(const std::vector &links_) { + links=links_; + } + + void addLink(const Link& l) { + for(auto &link:links) { + if(link.neuron ==l.neuron) { + link.value=l.value; + link.status=l.status; + return; + } + } + links.push_back(l); + } + + bool getLinkStatus(const std::size_t index) const { + if(index < links.size()) { + return links[index].status; + } + return false; + } + + void setLinkStatus(const std::size_t index, const bool &status) { + if(index < links.size()) { + links[index].status=status; + } + } + + float getLinkValue(const std::size_t index) const { + if(index < links.size()) { + return links[index].value; + } + return 0.0; + } + + void setLinkValue(const std::size_t index, const float &value) { + if(index < links.size()) { + links[index].value = value; + } + } + + void setCodePointer(const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *c) { + codePtr=c; + } + + const EvolutionaryAlgorithm::GeneticPrograming::CodeTree* getCodePointer() const { + return codePtr; + } + + std::size_t getID() const { + return id; + } + + bool isOutput() const { + return _isOutput; + } + + void setOutput() { + _isOutput=true; + } + void disableOutput() { + _isOutput=false; + } + + bool isInput() const { + return _isInput; + } + + void setInput() { + _isInput=true; + } + void disableInput() { + _isInput=false; + } + + protected: + std::size_t id; + + const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *codePtr; + //const Code &code; + + bool _isOutput=false; + bool _isInput=false; + + bool alive=true; + std::size_t life=1; + float bias = 0.0; + + std::size_t linkRegister=0; + + std::vector links = std::vector(); +/* + std::size_t codePosition; + std::size_t linkRegister; + + std::size_t sigme; + std::size_t dyn; + std::size_t link; + std::size_t simplif; +*/ + // 150 of paper + private: + }; +} +} +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h new file mode 100644 index 0000000..7f38fab --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h @@ -0,0 +1,182 @@ +#pragma once + +#include "Exception.h" +#include "Cell.h" + +#include + +#include +#include + +namespace NeuralNetworks { + namespace ConstructiveAlgorithms { + namespace CelularEncoding { + class CelularEncoding { + public: + CelularEncoding(const CelularEncoding &) = delete; + + CelularEncoding &operator=(const CelularEncoding &) = delete; + + CelularEncoding() { + + } + + void setActivationFunction(const std::shared_ptr &fun) { + _activationFunction=fun; + } + + void setMaxSteps(std::size_t steps) { + _maxSteps=steps; + } + + NeuralNetwork::Recurrent::Network run() { + std::size_t cellsStep = 0; + std::size_t steps=0; + do { + cellsStep = step(); + steps++; + } + while(cellsStep > 0 && steps < _maxSteps); + + if(steps >= _maxSteps) { + throw Exception("Went over max steps"); + } + + if(cells.size() > _maxCells) { + throw Exception("Went over max cells"); + } + + std::size_t outputs = 0; + std::size_t inputs = 0; + + std::vector cells2Neurons; + cells2Neurons.resize(cells.size()); + + std::size_t indexOfNeuronTmp=1; + + for(std::size_t i = 0; i < cells.size(); i++) { + if(cells[i]->isInput()) { + cells2Neurons[i] = indexOfNeuronTmp++; + inputs++; + } + } + + for(std::size_t i = 0; i < cells.size(); i++) { + if(cells[i]->isOutput()) { + if(!cells[i]->isInput()) { + cells2Neurons[i] = indexOfNeuronTmp++; + } + outputs++; + } + } + + for(std::size_t i = 0; i < cells.size(); i++) { + if(!cells[i]->isOutput() && !cells[i]->isInput()) { + cells2Neurons[i] = indexOfNeuronTmp++; + } + } + + std::size_t hiddenNeurons = static_cast(cells.size()) - static_cast(inputs) - static_cast(outputs) < 0 ? 0 : cells.size() - inputs - outputs; + + NeuralNetwork::Recurrent::Network netw(inputs, outputs, hiddenNeurons); + for(std::size_t i = 0; i < cells.size(); i++) { + + const auto &cell = cells[i]; + + std::size_t indexOfNeuron = cells2Neurons[i]; + auto& neuron = netw[indexOfNeuron]; + if(cells2Neurons[i] > inputs) { + neuron.setActivationFunction(*_activationFunction); + } + + neuron.weight(0)=cell->getBias(); + + for(auto &link: cells[i]->getLinks()) { + if(link.status == true) { + neuron.weight(cells2Neurons[link.neuron]) = link.value; + } else { + neuron.weight(cells2Neurons[link.neuron]) = 0.0; + } + } + } + + return netw; + } + + Cell &addCell(const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *c) { + cells.push_back(std::make_shared(cells.size(), c)); + + return (*cells.back()); + } + + void setAcyclicTopology() { + cells.clear(); +/* + for(std::size_t i = 0; i < inputSize; i++) { + addCell(code).die(); + } +*/ + Cell &cell = addCell(code); + cell.setLife(_initialLife); + _processingOrder.push_back(cell.getID()); + + cell.setOutput(); + cell.setInput(); +/* + for(std::size_t i = 0; i < inputSize; i++) { + Link l(true, 1.0, i); + cell.addLink(l); + } +*/ + } + + void setCyclicTopology() { + setAcyclicTopology(); + // Acyclic + reccurent link + Link l(true, 1.0, cells.back()->getID()); + cells.back()->addLink(l); + } + + void setCode(const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *code_) { + code = code_; + } + + const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *getCodeStart() const { + return code; + } + + std::vector> &getCells() { + return cells; + } + + void addCellToProcessingOrder(std::size_t id) { + auto position = std::find(_processingOrder.begin(),_processingOrder.end(),currentID); + if(position == _processingOrder.end()) { + _processingOrder.push_back(id); + } else { + _processingOrder.insert(position+1,id); + } + } + + void setInitiaLife(std::size_t life) { + _initialLife=life; + } + + protected: + std::size_t step(); + + private: + std::size_t _maxCells= 15; + std::size_t _maxSteps = std::numeric_limits::max(); + std::size_t _initialLife = 2.0; + + std::shared_ptr _activationFunction = std::make_shared(); + std::vector _processingOrder = {}; + std::size_t currentID = 0; + const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *code = nullptr; + std::vector> cells = {}; + }; + + } + } +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Exception.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Exception.h new file mode 100644 index 0000000..9830a23 --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Exception.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace NeuralNetworks { + namespace ConstructiveAlgorithms { + namespace CelularEncoding { + class Exception : public std::exception { + public: + Exception (const std::string &e) : _what(e) { + + } + + virtual const char* what() const noexcept override { + return _what.c_str(); + } + protected: + std::string _what; + }; + } + } +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Bias.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Bias.h new file mode 100644 index 0000000..c040140 --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Bias.h @@ -0,0 +1,143 @@ +#pragma once + +#include "./Instruction.h" +#include "../Cell.h" +#include "../CelularEncoding.h" + +namespace NeuralNetworks { +namespace ConstructiveAlgorithms { +namespace CelularEncoding { +namespace Instruction{ + class IncBias : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(cell.getBias()+1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "IncBias"; + } + }; + + class DecBias : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(cell.getBias()-1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "DecBias"; + } + + }; + class MulBias : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(cell.getBias()*2.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "MulBias"; + } + }; + + class DivBias : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(cell.getBias()*0.5); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "DivBias"; + } + }; + + class SetBiasZero : public Instruction{ + public: + SetBiasZero(bool terminal = true) : _nodes(terminal ? 0 : 1) { + + } + + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(0.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return _nodes; + } + + virtual std::string toString() const override { + return "SetBiasZero"; + } + protected: + std::size_t _nodes; + }; + + class SetBiasOne : public Instruction{ + public: + SetBiasOne(bool terminal = true) : _nodes(terminal ? 0 : 1) { + + } + + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return _nodes; + } + + virtual std::string toString() const override { + return "SetBiasZero"; + } + protected: + std::size_t _nodes; + }; + + class SetBiasMinusOne : public Instruction{ + public: + SetBiasMinusOne(bool terminal = true) : _nodes(terminal ? 0 : 1) { + + } + + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setBias(-1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return _nodes; + } + + virtual std::string toString() const override { + return "SetBiasZero"; + } + protected: + std::size_t _nodes; + }; +} +} +} +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Division.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Division.h new file mode 100644 index 0000000..5e30fff --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Division.h @@ -0,0 +1,101 @@ +#pragma once + +#include "./Instruction.h" +#include "../Cell.h" +#include "../CelularEncoding.h" + +namespace NeuralNetworks { +namespace ConstructiveAlgorithms { +namespace CelularEncoding { +namespace Instruction{ + class Par : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &encoding, const std::vector &) override { + Cell &newCell = encoding.addCell(cell.getCodePointer()->c2); + + if(cell.isOutput()) { + newCell.setOutput(); + } + + if(cell.isInput()) { + newCell.setInput(); + } + + newCell.setLife(cell.getLife()); + newCell.setBias(cell.getBias()); + + newCell.setLinks(cell.getLinks()); + + for(auto &cellI:encoding.getCells()) { + for( const Link &link: cellI->getLinks()) { + if(link.neuron == cell.getID()) { + Link newLink (link); + newLink.neuron=newCell.getID(); + cellI->addLink(newLink); + break; + } + } + } + encoding.addCellToProcessingOrder(newCell.getID()); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 2; + } + + virtual std::string toString() const override { + return "Par"; + } + + protected: + private: + }; + + class Seq : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &encoding, const std::vector &) override { + Cell &newCell = encoding.addCell(cell.getCodePointer()->c2); + + Link l(true, 1.0, cell.getID()); + + if(cell.isOutput()) { + newCell.setOutput(); + cell.disableOutput(); + } + + newCell.addLink(l); + + for(auto& cellIter:encoding.getCells()) { + if(cellIter->getID() != newCell.getID()) { + auto links = cellIter->getLinks(); + for(auto& link: links) { + if(link.neuron==cell.getID()) { + link.neuron=newCell.getID(); + } + } + cellIter->setLinks(links); + } + } + + newCell.setLife(cell.getLife()); + encoding.addCellToProcessingOrder(newCell.getID()); + cell.setCodePointer(cell.getCodePointer()->c1); + //TODO: copy registers + } + + virtual std::size_t numberOfNodes() const override { + return 2; + } + + virtual std::string toString() const override { + return "Seq"; + } + + protected: + private: + }; +} +} +} +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Finish.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Finish.h new file mode 100644 index 0000000..89d4bbd --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Finish.h @@ -0,0 +1,58 @@ +#pragma once + +#include "./Instruction.h" +#include "../Cell.h" +#include "../CelularEncoding.h" + +namespace NeuralNetworks { +namespace ConstructiveAlgorithms { +namespace CelularEncoding { +namespace Instruction{ + class End : public Instruction{ + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.die(); + } + + virtual std::size_t numberOfNodes() const override { + return 0; + } + + virtual std::string toString() const override { + return "End"; + } + protected: + private: + }; + + class Rec : public Instruction{ + public: + Rec(bool terminal = true) : _nodes(terminal ? 0 : 1) { + + } + + virtual void run(Cell &cell, CelularEncoding &encoding, const std::vector &) override { + if(cell.getLife()<=1) { + cell.setCodePointer(cell.getCodePointer()->c1); + cell.die(); + }else { + cell.setLife(cell.getLife()-1); + cell.setCodePointer(encoding.getCodeStart()); + } + } + + virtual std::size_t numberOfNodes() const override { + return _nodes; + } + + virtual std::string toString() const override { + return "Rec"; + } + protected: + std::size_t _nodes; + private: + }; +} +} +} +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Instruction.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Instruction.h new file mode 100644 index 0000000..8d785cb --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Instruction.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include + +namespace NeuralNetworks { +namespace ConstructiveAlgorithms { +namespace CelularEncoding { + + class Cell; + class CelularEncoding; + + namespace Instruction { + class Instruction : public EvolutionaryAlgorithm::GeneticPrograming::Instruction { + public: + virtual ~Instruction() { + + } + + // todo what implement?? + void operator()() {} + + virtual void run(Cell &, CelularEncoding &, const std::vector &) = 0; + + protected: + private: + }; + } +} +} +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Link.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Link.h new file mode 100644 index 0000000..b52860b --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Link.h @@ -0,0 +1,245 @@ +#pragma once + +#include "./Instruction.h" +#include "../Cell.h" +#include "../CelularEncoding.h" + +namespace NeuralNetworks { + namespace ConstructiveAlgorithms { + namespace CelularEncoding { + namespace Instruction { + class Inclr : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + if(cell.getLinkRegister() == cell.getLinks().size() - 1) { + cell.setLinkRegister(0); + } else { + cell.setLinkRegister(cell.getLinkRegister() + 1); + } + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "IncLr"; + } + + protected: + private: + }; + + class Declr : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + if(cell.getLinkRegister() == 0) { + if(cell.getLinks().size() > 1) { + cell.setLinkRegister(cell.getLinks().size() - 1); + } + } else { + cell.setLinkRegister(cell.getLinkRegister() - 1); + } + + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Declr"; + } + + protected: + private: + }; + + class ValPlus : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), 1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Val+"; + } + + protected: + private: + }; + + class ValMinus : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), -1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Val-"; + } + + protected: + private: + }; + + class Inc : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), cell.getLinkValue(cell.getLinkRegister()) + 1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Inc"; + } + + protected: + private: + }; + + class Dec : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), cell.getLinkValue(cell.getLinkRegister()) - 1.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Dec"; + } + + protected: + private: + }; + + class Mult : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), cell.getLinkValue(cell.getLinkRegister()) * 2.0); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Mult"; + } + + protected: + private: + }; + + class Div : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkValue(cell.getLinkRegister(), cell.getLinkValue(cell.getLinkRegister()) * 0.5); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Div"; + } + + protected: + private: + }; + + class On : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkStatus(cell.getLinkRegister(), true); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "On"; + } + + protected: + private: + }; + + class Cyc : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + bool exists = false; + for(std::size_t i = 0; i < cell.getLinks().size(); i++) { + auto &link = cell.getLinks()[i]; + if(link.neuron == cell.getID()) { + exists = true; + cell.setLinkStatus(i,true); + } + } + + if(!exists) { + cell.addLink(Link(true, 1.0, cell.getID())); + } + + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Cyc"; + } + + protected: + private: + }; + + class Off : public Instruction { + public: + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setLinkStatus(cell.getLinkRegister(), false); + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return 1; + } + + virtual std::string toString() const override { + return "Off"; + } + + protected: + private: + }; + } + } + } +} \ No newline at end of file diff --git a/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Other.h b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Other.h new file mode 100644 index 0000000..e4c0a1f --- /dev/null +++ b/include/NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Instruction/Other.h @@ -0,0 +1,34 @@ +#pragma once + +#include "./Instruction.h" +#include "../Cell.h" +#include "../CelularEncoding.h" + +namespace NeuralNetworks { + namespace ConstructiveAlgorithms { + namespace CelularEncoding { + namespace Instruction{ + class Wait : public Instruction{ + public: + Wait(bool terminal = false) : _numberOfNodes(terminal? 0 : 1) { + + } + + virtual void run(Cell &cell, CelularEncoding &, const std::vector &) override { + cell.setCodePointer(cell.getCodePointer()->c1); + } + + virtual std::size_t numberOfNodes() const override { + return _numberOfNodes; + } + + virtual std::string toString() const override { + return "Wait"; + } + protected: + std::size_t _numberOfNodes; + }; + } + } + } +} \ No newline at end of file diff --git a/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/Cell.cpp b/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/Cell.cpp new file mode 100644 index 0000000..f5e1aed --- /dev/null +++ b/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/Cell.cpp @@ -0,0 +1,12 @@ +#include +#include + +void NeuralNetworks::ConstructiveAlgorithms::CelularEncoding::Cell::step(CelularEncoding &c) { + if(isAlive()) { + reinterpret_cast(codePtr->instruction.lock().get())->run(*this, c, codePtr->parameters); + } + + if(codePtr == nullptr) { + die(); + } +} \ No newline at end of file diff --git a/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/CellularEncoding.cpp b/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/CellularEncoding.cpp new file mode 100644 index 0000000..ae62066 --- /dev/null +++ b/src/NeuralNetwork/ConstructiveAlgorithms/CellularEncoding/CellularEncoding.cpp @@ -0,0 +1,20 @@ +#include + +std::size_t NeuralNetworks::ConstructiveAlgorithms::CelularEncoding::CelularEncoding::step() { + std::size_t activeCellCount = 0; + std::vector processingOrder(_processingOrder); + for(std::size_t i = 0; i < processingOrder.size(); i++) { + std::size_t cell = processingOrder[i]; + if(cells[cell]->isAlive()) { + currentID = cells[cell]->getID(); + cells[cell]->step(*this); + activeCellCount++; + } else { + auto iter = std::find(_processingOrder.begin(), _processingOrder.end(), cell); + if(iter != _processingOrder.end()) { + _processingOrder.erase(iter); + } + } + } + return activeCellCount; +} \ No newline at end of file