cellular encoding
This commit is contained in:
@@ -0,0 +1,182 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <EvolutionaryAlgorithms/GeneticPrograming/CodeTree.h>
|
||||||
|
#include "./Instruction/Instruction.h"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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<Link>& getLinks() const {
|
||||||
|
return links;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setLinks(const std::vector<Link> &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<Link> links = std::vector<Link>();
|
||||||
|
/*
|
||||||
|
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:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Exception.h"
|
||||||
|
#include "Cell.h"
|
||||||
|
|
||||||
|
#include <NeuralNetwork/Recurrent/Network.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
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<NeuralNetwork::ActivationFunction::ActivationFunction> &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<std::size_t> 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<int>(cells.size()) - static_cast<int>(inputs) - static_cast<int>(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<Cell>(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<std::shared_ptr<Cell>> &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<std::size_t>::max();
|
||||||
|
std::size_t _initialLife = 2.0;
|
||||||
|
|
||||||
|
std::shared_ptr<NeuralNetwork::ActivationFunction::ActivationFunction> _activationFunction = std::make_shared<NeuralNetwork::ActivationFunction::Sigmoid>();
|
||||||
|
std::vector<std::size_t> _processingOrder = {};
|
||||||
|
std::size_t currentID = 0;
|
||||||
|
const EvolutionaryAlgorithm::GeneticPrograming::CodeTree *code = nullptr;
|
||||||
|
std::vector<std::shared_ptr<Cell>> cells = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<double> &) 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<double> &) 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:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<double> &) 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<double> &) 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:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <EvolutionaryAlgorithms/GeneticPrograming/Instruction.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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<double> &) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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<double> &) 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:
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<double> &) 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#include <NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/Cell.h>
|
||||||
|
#include <NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h>
|
||||||
|
|
||||||
|
void NeuralNetworks::ConstructiveAlgorithms::CelularEncoding::Cell::step(CelularEncoding &c) {
|
||||||
|
if(isAlive()) {
|
||||||
|
reinterpret_cast<Instruction::Instruction *>(codePtr->instruction.lock().get())->run(*this, c, codePtr->parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(codePtr == nullptr) {
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <NeuralNetwork/ConstructiveAlgorithms/CelularEncoding/CelularEncoding.h>
|
||||||
|
|
||||||
|
std::size_t NeuralNetworks::ConstructiveAlgorithms::CelularEncoding::CelularEncoding::step() {
|
||||||
|
std::size_t activeCellCount = 0;
|
||||||
|
std::vector<std::size_t> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user