cellular encoding
This commit is contained in:
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user