signalgp_utils.hpp

Helper functions for working with SignalGP virtual hardware/programs.

Todo:

Mutator class

tests

Functions

template<size_t TAG_WIDTH>
BitSet<TAG_WIDTH> GenRandSignalGPTag(Random &rnd, const vector<BitSet<TAG_WIDTH>> &unique_from = vector<BitSet<TAG_WIDTH>>())

Generate one random SignalGP tag (BitSet<TAG_WIDTH>). Given a vector of other tags (unique_from), this function will guarantee the tag generated is unique with respect to those tags.

Parameters:
  • rnd – - Random number generator to use when generating a random tag.

  • unique_from – - Other tags that the tag being generated should be unique with respect to.

template<size_t TAG_WIDTH>
vector<BitSet<TAG_WIDTH>> GenRandSignalGPTags(Random &rnd, size_t count, bool guarantee_unique = false, const vector<BitSet<TAG_WIDTH>> &unique_from = vector<BitSet<TAG_WIDTH>>())

Generate ‘count’ number of random SignalGP tags (BitSet<TAG_WIDTH>). Given a vector of other tags (unique_from), this function will guarantee the tags generated are unique with respect to those tags.

Parameters:
  • rnd – - Random number generator to use when generating a random tag.

  • count – - How many tags should be generated?

  • guarantee_unique – - Should generated tags be guaranteed to be unique from each other and from tags in ‘unique_from’?

  • unique_from – - Other tags that the tag being generated should be unique with respect to. Only used if ‘guarantee_unique’ is true.

template<size_t TAG_WIDTH, typename TRAIT_T = double, typename MATCHBIN_T = MatchBin<size_t, HammingMetric<TAG_WIDTH>, RankedSelector<std::ratio<1, 2>>, MultiplicativeCountdownRegulator<>>>
EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>::Instruction GenRandSignalGPInst(Random &rnd, const InstLib<EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>> &inst_lib, int min_arg_val = 0, int max_arg_val = 15)

Generate a random SignalGP instruction (templated off of tag width).

Parameters:
  • rnd – - Random number generator to use when generating a random tag.

  • inst_lib – - Instruction library used to generate the instruction (instruction will be valid within instruction library)

  • min_arg_val – - Mininum value for an instruction argument.

  • max_arg_val – - Maximum value for an instruction argument.

template<size_t TAG_WIDTH, typename TRAIT_T = double, typename MATCHBIN_T = MatchBin<size_t, HammingMetric<TAG_WIDTH>, RankedSelector<std::ratio<1, 2>>, MultiplicativeCountdownRegulator<>>>
EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>::Function GenRandSignalGPFunction(Random &rnd, const InstLib<EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>> &inst_lib, size_t min_inst_cnt = 1, size_t max_inst_cnt = 32, int min_arg_val = 0, int max_arg_val = 15)

Generate a random SignalGP function (templated off of tag width).

Parameters:
  • rnd – - Random number generator to use when generating a random tag.

  • inst_lib – - Instruction library used to generate the function (instruction will be valid within instruction library)

  • min_inst_cnt – - Minimum number of instructions in generated function.

  • max_inst_cnt – - Maximum number of instructions in generated function.

  • min_arg_val – - Mininum value for an instruction argument.

  • max_arg_val – - Maximum value for an instruction argument.

template<size_t TAG_WIDTH, typename TRAIT_T = double, typename MATCHBIN_T = MatchBin<size_t, HammingMetric<TAG_WIDTH>, RankedSelector<std::ratio<1, 2>>, MultiplicativeCountdownRegulator<>>>
EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>::Program GenRandSignalGPProgram(Random &rnd, const InstLib<EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>> &inst_lib, size_t min_func_cnt = 1, size_t max_func_cnt = 16, size_t min_fun_len = 1, size_t max_fun_len = 32, int min_arg_val = 0, int max_arg_val = 15)

Generate a random SignalGP program (templated off of tag width).

Parameters:
  • rnd – - Random number generator to use when generating a random tag.

  • inst_lib – - Instruction library used to generate the program.

  • min_func_cnt – - Mininum number of functions in generated program.

  • max_func_cnt – - Maximum number of functions in generated program.

  • min_fun_len – - Minimum number of instructions per function.

  • max_fun_len – - Maximum number of instructions per function.

  • min_arg_val – - Mininum value for an instruction argument.

  • max_arg_val – - Maximum value for an instruction argument.

template<size_t TAG_WIDTH, typename TRAIT_T = double, typename MATCHBIN_T = MatchBin<size_t, HammingMetric<TAG_WIDTH>, RankedSelector<std::ratio<1, 2>>, MultiplicativeCountdownRegulator<>>>
class SignalGPMutator
#include <signalgp_utils.hpp>

The SignalGPMutator class provides a manager for applying (and minimally tracking) mutations for SignalGP programs.

  • A SignalGPMutator object has two major components:

    • A set of mutators

    • And, a set of generic mutator parameters

  • Additionally, a SignalGPMutator object keeps track of a standard set of SignalGP-specific program constraints to be used by mutators when modifying a program.

  • Mutators associated a name and a description with a function that mutates a SignalGP program.

  • Parameters associate a name, a parameter ID, and a description with a parameter value.

  • When a SignalGPMutator object is ‘applied’ (ApplyMutators) to a SignalGP program, it applies all mutators to the program in the order they were added to the SignalGPMutator object.

  • Each mutator reports the number of mutations caused by that mutator; this is tracked and can be extracted using the GetLastMutationCnt function.

  • By default a SignalGPMutator object has the capacity for the following mutations:

    • Whole-function duplication and deletion (each applied per-function)

    • Slip mutations (applied per-function)

    • Function-tag bit-flips (applied per-bit)

    • Single-instruction substitution (applied per-instruction)

    • Single-instruction insertions and deletions (each applied per-instruction)

    • Instruction-tag bit-flips (applied per-bit)

    • Instruction-argument substitutions (applied per-argument) NOTE: could use some feedback on this!

    • Not loving the inconsistency between rates and constraints at the moment.

Public Types

enum TagType

Tags can belong to two types of programmatic entities in SignalGP: functions and instructions.

Values:

enumerator FUNCTION
enumerator INSTRUCTION
using hardware_t = EventDrivenGP_AW<TAG_WIDTH, TRAIT_T, MATCHBIN_T>

SignalGP hardware type.

using program_t = typename hardware_t::program_t

SignalGP program type.

using tag_t = typename hardware_t::affinity_t

SignalGP tag type.

using inst_lib_t = typename hardware_t::inst_lib_t

SignalGP instruction library type.

using inst_t = typename hardware_t::inst_t

SignalGP program instruction type.

using fun_t = typename hardware_t::Function

SignalGP program function type.

using mutator_fun_t = std::function<size_t(program_t&, Random&)>

Mutator function type.

Public Functions

inline SignalGPMutator(size_t _PROG_MIN_FUNC_CNT = 1, size_t _PROG_MAX_FUNC_CNT = 8, size_t _PROG_MIN_FUNC_LEN = 1, size_t _PROG_MAX_FUNC_LEN = 32, size_t _PROG_MAX_TOTAL_LEN = 256, int _PROG_MIN_ARG_VAL = 0, int _PROG_MAX_ARG_VAL = 15)
SignalGPMutator(const SignalGPMutator&) = default
SignalGPMutator(SignalGPMutator&&) = default
inline ~SignalGPMutator()
SignalGPMutator &operator=(const SignalGPMutator&) = default

Copy operator.

SignalGPMutator &operator=(SignalGPMutator&&) = default

Move operator.

inline size_t ApplyMutations(program_t &p, Random &r)

ApplyMutations applies all added mutators to the given program and updates the last mutation count for each mutator.

Parameters:
  • p – - program to be mutated

  • r – - random number generator to use

inline size_t ApplyMutator(const std::string &name, program_t &p, Random &r)

ApplyMutator applies a single mutator (given by name argument) to the given program and updates the last mutation count for that mutator.

inline size_t GetProgMinFuncCnt() const
inline size_t GetProgMaxFuncCnt() const
inline size_t GetProgMinFuncLen() const
inline size_t GetProgMaxFuncLen() const
inline size_t GetProgMaxTotalLen() const
inline int GetProgMinArgVal() const
inline int GetProgMaxArgVal() const
inline void SetProgMinFuncCnt(size_t val)
inline void SetProgMaxFuncCnt(size_t val)
inline void SetProgMinFuncLen(size_t val)
inline void SetProgMaxFuncLen(size_t val)
inline void SetProgMaxTotalLen(size_t val)
inline void SetProgMinArgVal(int val)
inline void SetProgMaxArgVal(int val)
inline double ARG_SUB__PER_ARG() const
inline double INST_SUB__PER_INST() const
inline double INST_INS__PER_INST() const
inline double INST_DEL__PER_INST() const
inline double SLIP__PER_FUNC() const
inline double FUNC_DUP__PER_FUNC() const
inline double FUNC_DEL__PER_FUNC() const
inline double TAG_BIT_FLIP__PER_BIT() const
inline void ARG_SUB__PER_ARG(double val)
inline void INST_SUB__PER_INST(double val)
inline void INST_INS__PER_INST(double val)
inline void INST_DEL__PER_INST(double val)
inline void SLIP__PER_FUNC(double val)
inline void FUNC_DUP__PER_FUNC(double val)
inline void FUNC_DEL__PER_FUNC(double val)
inline void TAG_BIT_FLIP__PER_BIT(double val)
inline size_t GetParamCnt() const

Return the number of registered parameters.

inline size_t GetParamID(const std::string &name) const

Return the ID of the mutation rate type that has the specified name. Return (size_t)-1 if nothing found.

inline double GetParam(size_t id) const

Return the rate associated with the specified mutation rate ID.

inline double GetParam(const std::string &param_name) const

Return the rate associated with the specified mutation rate name.

inline const std::string &GetParamDesc(size_t id) const

Return the description associated with the parameter specified by id.

inline const std::string &GetParamDesc(const std::string &name)

Return the description associated with the parameter specified by name.

inline const std::string &GetParamName(size_t id) const

Return the parameter name for the parameter with the given ID.

inline size_t AddParam(const std::string &name, double param, const std::string &desc = "")

Add a paramter to the parameter set. Return ID of that parameter, which can be used to access the parameter value more quickly than with its name.

inline void SetParam(const std::string &name, double param)

Modify existing parameter value (by name).

inline void SetParam(size_t id, double param)

Modify existing paramter value (by ID).

inline size_t GetMutatorCnt() const

Return the number of mutators in this SignalGPMutator object.

inline void AddMutator(const std::string &name, const mutator_fun_t &mut_fun, const std::string &desc = "")

Add a mutator to the mutator set.

inline void RemoveMutator(const std::string &name)

Remove a mutator from the mutator set.

inline void ClearMutators()

Remove all mutators from the mutator set.

inline void ResetMutators()

Reset mutator set back to the defaults.

inline const std::string &GetMutatorDesc(const std::string &name) const

Return the description associated with the mutator associated with name.

inline size_t GetLastMutationCnt(const std::string &name) const

Return the last mutation count from the mutator associated with name.

inline size_t DefaultMutator_FuncDup(program_t &program, Random &rnd)

Mutator: Default implementation for whole-function duplication mutations. Respects constraints (e.g., max total program length, etc.). Duplications occur at a per-function rate specified by the “FUNC_DUP__PER_FUNC” parameter value.

inline size_t DefaultMutator_FuncDel(program_t &program, Random &rnd)

Mutator: Default implementation for whole-function deletion mutations. Respects constraints (e.g., max total program length, etc.). Deletions occur at a per-function rate specified by the “FUNC_DEL__PER_FUNC” parameter value.

inline size_t DefaultMutator_FuncTag(program_t &program, Random &rnd)

Mutator: Default implementation for function tag mutations. Respects constraints (e.g., max total program length, etc.). Function tag bit-flips occur at a per-bit rate specified by the “TAG_BIT_FLIP__PER_BIT” parameter value.

inline size_t DefaultMutator_Slip(program_t &program, Random &rnd)

Mutator: Default implementation for slip mutations, which result in the duplication or deletion of sequences of instructions within a function. Respects constraints (e.g., max total program length, etc.). Slip-mutations occur at a per-function rate specified by the “SLIP__PER_FUNC” parameter value.

inline size_t DefaultMutator_Subs(program_t &program, Random &rnd)

Mutator: Default implementation for instruction substitution mutations, including instruction-operation substitutions, instruction-argument substitutions, and instruction-tag bit-flips. Respects constraints (e.g., max total program length, etc.). Instruction substitions occur at a per-instruction rate specified by “INST_SUB__PER_INST”. Instruction argument substitutions occur at a per-argument rate specified by “ARG_SUB__PER_ARG”. Instruction tag mutations occur at a per-bit rate specified by “TAG_BIT_FLIP__PER_BIT”.

inline size_t DefaultMutator_InstInDels(program_t &program, Random &rnd)

Mutator: Default implementation for single-instruction insertion and deletion mutations. Respects constraints (e.g., max total program length, etc.). Single-instruction insertions occur at a per-instruction rate specified by “INST_INS__PER_INST”. Single-instruction deletions occur at a per-instruction rate specified by “INST_DEL__PER_INST”.

inline void Print(std::ostream &os = std::cout)

Output (via the given std::ostream, std::cout by default) report of parameters and mutators associated with this SignalGPMutator object.

inline bool VerifyProgram(program_t &prog)

Verify that the given program (prog) is within the constraints associated with this SignalGPMutator object. Useful for mutator testing.

Protected Functions

inline MutatorDef &GetMutator(size_t id)

Get the mutator specified by id.

inline const MutatorDef &GetConstMutator(size_t id) const

Get a const reference to the mutator specified by id.

inline size_t GetMutatorID(const std::string &name) const

Get the ID (location in mutator_lib) of mutator specified by name argument.

Protected Attributes

vector<MutatorParamDef> param_lib

To hold added parameters.

std::map<std::string, size_t> param_name_map

Associate parameter names with location in param_lib.

vector<MutatorDef> mutator_lib

To hold added mutators.

std::map<std::string, size_t> mutator_name_map

Associate mutator name with location in mutator_lib.

size_t prog_min_func_cnt

Minimum number of functions mutations are allowed to reduce a SignalGP program to.

size_t prog_max_func_cnt

Maximum number of functions a mutated SignalGP program can grow to.

size_t prog_min_func_len

Minimum number of instructions a SignalGP function can shrink to.

size_t prog_max_func_len

Maximum number of instructions a SignalGP function can grow to.

size_t prog_max_total_len

Maximum number of total instructions a SignalGP program can grow to.

int prog_min_arg_val

Minimum argument value a SignalGP instruction can mutate to.

int prog_max_arg_val

Maximum argument value a SignalGP instruction can mutate to.

size_t arg_sub__per_arg__id

Rate ID for: Rate to apply substitutions to instruction arguments.

size_t inst_sub__per_inst__id

Rate ID for: Per-instruction rate to apply instruction substitutions.

size_t inst_ins__per_inst__id

Rate ID for: Per-instruction rate to apply instruction insertions.

size_t inst_del__per_inst__id

Rate ID for: Per-instruction rate to apply instruction deletions.

size_t slip__per_func__id

Rate ID for: Per-function rate to apply slip-mutations.

size_t func_dup__per_func__id

Rate ID for: Per-function rate to apply function duplications.

size_t func_del__per_func__id

Rate ID for: Per-function rate to apply function deletions.

size_t tag_bit_flip__per_bit__id

Rate ID for: Per-bit rate to apply tag bit flips.

struct MutatorDef
#include <signalgp_utils.hpp>

Struct used to define a mutation operator.

Public Functions

inline MutatorDef(const std::string &_n, const mutator_fun_t &_fun, const std::string &_d)

Public Members

std::string name

Name of this mutator.

mutator_fun_t mutator

Mutate function associated with this mutator.

std::string desc

Description of this mutator.

size_t last_mut_cnt

Number of mutations caused by this mutator on the last application of this mutator.

struct MutatorParamDef
#include <signalgp_utils.hpp>

Struct used to define a mutation rate.

Public Functions

inline MutatorParamDef(const std::string &_n, double _p, const std::string &_d)
MutatorParamDef(const MutatorParamDef&) = default

Public Members

std::string name

Name of this parameter.

double param

Value of this parameter.

std::string desc

Description of this parameter.

template<typename Hardware>
class SignalGPMutatorFacade : public SignalGPMutator<Hardware::affinity_width, Hardware::trait_t, Hardware::matchbin_t>
#include <signalgp_utils.hpp>