AvidaGP.hpp

This is a simple, efficient CPU for and applied version of Avida.

Todo:

Should we save a copy of the original genome? (or create a new “memory” member)

We should clean up how we handle scope; the root scope is zero, so the arg-based scopes are 1-16 (or however many). Right now we increment the value in various places and should be more consistent.

How should Avida-GP organisms take an action? Options include sending ALL outputs and picking the maximum field; sending a single output and using its value; having specialized commands…

template<typename HARDWARE>
class AvidaCPU_Base
#include <AvidaGP.hpp>

Public Types

using this_t = AvidaCPU_Base<HARDWARE>
using hardware_t = HARDWARE
using inst_t = Instruction
using arg_t = size_t
using inst_lib_t = AvidaCPU_InstLib<hardware_t, arg_t, INST_ARGS>
using genome_t = Genome<Instruction, inst_lib_t>
using stack_t = vector<double>
using arg_set_t = array<arg_t, INST_ARGS>

Public Functions

inline void ExitScope()
inline bool UpdateScope(size_t new_scope, ScopeType type = ScopeType::BASIC)
inline void BypassScope(size_t scope)
inline AvidaCPU_Base(const genome_t &in_genome)

Create a new AvidaCPU seeding it with a genome.

inline AvidaCPU_Base()

Create a default AvidaCPU (no genome sequence, default instruction set)

inline AvidaCPU_Base(Ptr<const inst_lib_t> inst_lib)

Create an AvidaCPU with a specified instruction set (but no genome sequence)

inline AvidaCPU_Base(const inst_lib_t &inst_lib)
AvidaCPU_Base(const AvidaCPU_Base&) = default

Copy constructor.

AvidaCPU_Base(AvidaCPU_Base&&) = default

Move constructor.

inline virtual ~AvidaCPU_Base()

Destructor.

inline bool operator<(const this_t &other) const
inline bool operator!=(const this_t &other) const
inline void Reset()

Reset the entire CPU to a starting state, without a genome.

inline virtual void ResetHardware()

Reset just the CPU hardware, but keep the genome and traits.

inline void ResetIP()

Reset the instruction pointer to the beginning of the genome AND reset scope.

inline Ptr<const inst_lib_t> GetInstLib() const
inline inst_t GetInst(size_t pos) const
inline inst_t &operator[](size_t pos)
inline const genome_t &GetGenome() const
inline size_t GetSize() const
inline size_t size() const
inline double GetReg(size_t id) const
inline double GetInput(int id) const
inline const std::unordered_map<int, double> &GetInputs() const
inline size_t GetNumInputs() const
inline double GetOutput(int id) const
inline const std::unordered_map<int, double> &GetOutputs() const
inline size_t GetNumOutputs() const
inline const stack_t &GetStack(size_t id) const
inline int GetFunStart(size_t id) const
inline size_t GetIP() const
inline vector<ScopeInfo> GetScopeStack() const
inline size_t CurScope() const
inline ScopeType CurScopeType() const
inline ScopeType GetScopeType(size_t id)
inline vector<RegBackup> GetRegStack() const
inline vector<size_t> GetCallStack() const
inline size_t GetNumErrors() const
inline double GetTrait(size_t id) const
inline const vector<double> &GetTraits()
inline size_t GetNumTraits() const
inline void SetInst(size_t pos, const inst_t &inst)
inline void SetInst(size_t pos, size_t id, size_t a0 = 0, size_t a1 = 0, size_t a2 = 0)
inline void SetGenome(const genome_t &g)
inline void SetReg(size_t id, double val)
inline void SetInput(int input_id, double value)
inline void SetInputs(const std::unordered_map<int, double> &vals)
inline void SetInputs(std::unordered_map<int, double> &&vals)
inline void SetInputs(const vector<double> &vals)
inline void SetOutput(int output_id, double value)
inline void SetOutputs(const std::unordered_map<int, double> &vals)
inline void SetOutputs(std::unordered_map<int, double> &&vals)
inline double PopStack(size_t id)
inline void PushStack(size_t id, double value)
inline void SetFunStart(size_t id, int value)
inline void SetIP(size_t pos)
inline void PushRegInfo(size_t scope_id, size_t reg_id)
inline void PushCallInfo(size_t pos)
inline void IncErrors()
inline void SetTrait(size_t id, double val)
inline void PushTrait(double val)
inline inst_t GetRandomInst(Random &rand)
inline void RandomizeInst(size_t pos, Random &rand)
inline void PushInst(size_t id, size_t a0 = 0, size_t a1 = 0, size_t a2 = 0)

Add a new instruction to the end of the genome, by ID and args.

inline void PushInst(const std::string &name, size_t a0 = 0, size_t a1 = 0, size_t a2 = 0)

Add a new instruction to the end of the genome, by NAME and args.

inline void PushInst(const Instruction &inst)

Add a specified new instruction to the end of the genome.

inline void PushInst(const Instruction &inst, size_t count)

Add multiple copies of a specified instruction to the end of the genome.

inline void PushDefaultInst(size_t count = 1)

Add one or more default instructions to the end of the genome.

inline void PushInstString(std::string info)
inline void PushRandom(Random &random, const size_t count = 1)
bool Load(std::istream &input)
inline bool Load(const std::string &filename)
inline void ProcessInst(const inst_t &inst)

Process a specified instruction, provided by the caller.

size_t InstScope(const inst_t &inst) const

Determine the scope associated with a particular instruction.

inline void SingleProcess()

Process the NEXT instruction pointed to be the instruction pointer.

inline void Process(size_t num_inst)

Process the next SERIES of instructions, directed by the instruction pointer.

void PrintInst(const inst_t &inst, std::ostream &os = std::cout) const

Print out a single instruction, with its arguments.

void PrintGenome(std::ostream &os = std::cout) const

Print out this program.

void PrintGenome(const std::string &filename) const
void PrintSymbols(std::ostream &os = std::cout) const

Print out a short version of the genome as a single string.

inline std::string ToString() const

Convert the current state to a string.

size_t PredictNextInst() const

Figure out which instruction is going to actually be run next SingleProcess()

void PrintState(std::ostream &os = std::cout) const

Print out the state of the virtual CPU.

inline void Trace(size_t num_inst, std::ostream &os = std::cout)

Trace the instructions being exectured, with full CPU details.

inline void Trace(size_t num_inst, const std::string &filename)

Public Members

genome_t genome
array<double, CPU_SIZE> regs
std::unordered_map<int, double> inputs
std::unordered_map<int, double> outputs
array<stack_t, CPU_SIZE> stacks
array<int, CPU_SIZE> fun_starts
size_t inst_ptr
vector<ScopeInfo> scope_stack
vector<RegBackup> reg_stack
vector<size_t> call_stack
size_t errors
vector<double> traits

Public Static Attributes

static constexpr size_t CPU_SIZE = 16
static constexpr size_t INST_ARGS = 3
static constexpr size_t STACK_CAP = 16
struct Instruction : public InstructionBase
#include <AvidaGP.hpp>

Public Functions

inline Instruction(size_t _id = 0, size_t a0 = 0, size_t a1 = 0, size_t a2 = 0)
Instruction(const Instruction&) = default
Instruction(Instruction&&) = default
Instruction &operator=(const Instruction&) = default
Instruction &operator=(Instruction&&) = default
inline bool operator<(const Instruction &in) const
inline bool operator==(const Instruction &in) const
inline bool operator!=(const Instruction &in) const
inline bool operator>(const Instruction &in) const
inline bool operator>=(const Instruction &in) const
inline bool operator<=(const Instruction &in) const
inline void Set(size_t _id, size_t _a0 = 0, size_t _a1 = 0, size_t _a2 = 0)
inline size_t GetIndex() const override

Public Members

size_t id
arg_set_t args
struct RegBackup
#include <AvidaGP.hpp>

Public Functions

inline RegBackup()
inline RegBackup(size_t _s, size_t _r, double _v)

Public Members

size_t scope
size_t reg_id
double value
struct ScopeInfo
#include <AvidaGP.hpp>

Public Functions

inline ScopeInfo()
inline ScopeInfo(size_t _s, ScopeType _t, size_t _p)

Public Members

size_t scope
ScopeType type
size_t start_pos
class AvidaGP : public AvidaCPU_Base<AvidaGP>
#include <AvidaGP.hpp>

Public Types

using base_t = AvidaCPU_Base<AvidaGP>

Public Functions

inline AvidaGP(const genome_t &in_genome)
inline AvidaGP(Ptr<const inst_lib_t> inst_lib)
inline AvidaGP(const inst_lib_t &inst_lib)
AvidaGP() = default
AvidaGP(const AvidaGP&) = default
AvidaGP(AvidaGP&&) = default
inline virtual ~AvidaGP()