Parameters

The parameters are used to make the macros dynamic. For example you can build a parameter of type microgp.parameter.categorical.Categorical passing some values (alternatives); the macro that contains this kind of parameter will take the given values and the resulting text of the macro will depend on the values taken by the parameters assigned. The method microgp.parameter.helpers.make_parameter allows to build a parameter providing the parameter class type and the needed attributes.

Examples:

>>> registers = ugp.make_parameter(ugp.parameter.Categorical, alternatives=['ax', 'bx', 'cx', 'dx'])
>>> int256 = ugp.make_parameter(ugp.parameter.Integer, min=0, max=256)
>>> add = ugp.Macro("    add {reg}, 0{num:x}h ; ie. {reg} += {num}", {'reg': registers, 'num': int256})

There are several types of parameters:

Parameter

Parameters inheritance hierarchies:

Structure of a node

microgp.parameter.Parameter

microgp.parameter.Structural

microgp.parameter.Special

Integer

microgp.parameter.integer

class microgp.parameter.integer.Integer(name: str, *args, **kwargs)

Integer parameter in a given range.

Example:

>>> int256 = ugp4.make_parameter(ugp4.parameter.Integer, min=0, max=256)
Parameters:
  • min (int) – minimum value included.
  • max (int) – maximum value not included.
is_valid(value)

Check if the passed value is in range min, max.

mutate(strength: float = 0.5)

Mutate current value according to strength (ie. strength).

run_paranoia_checks() → bool

Checks the internal consistency of a “paranoid” object.

The function should be overridden by the sub-classes to implement the required, specific checks. It always returns True, but throws an exception whenever an inconsistency is detected.

Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when -O is used for compiling). Their typical usage is: assert foo.run_paranoia_checks()

Returns:True (always)
Raise:
AssertionError if some internal data structure is incoherent

Bitstring

microgp.parameter.bitstring

class microgp.parameter.bitstring.Bitstring(*args, **kwargs)

Fixed-length bitstring parameter.

Example:

>>> word8 = ugp4.make_parameter(ugp4.parameter.Bitstring, len_=8)
Parameters:len_ (int > 0) – length of the bit string
is_valid(value)

Check whether the given value is valid for the parameters

mutate(strength: float = 0.5)

Mutate current value according to strength (ie. strength).

value

Get current value of the parameter (type str)

Categorical, CategoricalSorted

microgp.parameter.categorical

class microgp.parameter.categorical.Categorical(name: str, *args, **kwargs)

Categorical parameter. It can take values in ‘alternatives’.

Example:

>>> registers = ugp4.make_parameter(ugp4.parameter.Categorical, alternatives=['ax', 'bx', 'cx', 'dx'])
Parameters:alternatives (list) – list of possible values
is_valid(value)

Check whether the given value is valid for the parameters

mutate(strength: float = 0.5)

Mutate current value according to strength (ie. strength).

run_paranoia_checks() → bool

Checks the internal consistency of a “paranoid” object.

The function should be overridden by the sub-classes to implement the required, specific checks. It always returns True, but throws an exception whenever an inconsistency is detected.

Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when -O is used for compiling). Their typical usage is: assert foo.run_paranoia_checks()

Returns:True (always)
Raise:
AssertionError if some internal data structure is incoherent
class microgp.parameter.categorical.CategoricalSorted(name: str, *args, **kwargs)

CategoricalSorted parameter. It can take values in ‘alternatives’. It behaves differently during the mutation phase.

Example:

>>> cat_sor = ugp4.make_parameter(ugp4.parameter.CategoricalSorted, alternatives=['e', 'f', 'g', 'h', 'i', 'l'])
Parameters:alternatives (list) – sorted list of possible values
mutate(strength: float = 0.5)

Mutate current value according to strength (ie. strength).

run_paranoia_checks() → bool

Checks the internal consistency of a “paranoid” object.

The function should be overridden by the sub-classes to implement the required, specific checks. It always returns True, but throws an exception whenever an inconsistency is detected.

Notez bien: Sanity checks may be computationally intensive, paranoia checks are not supposed to be used in production environments (i.e., when -O is used for compiling). Their typical usage is: assert foo.run_paranoia_checks()

Returns:True (always)
Raise:
AssertionError if some internal data structure is incoherent

LocalReference, ExternalReference

microgp.parameter.reference

class microgp.parameter.reference.Reference(individual: Individual, node: NodeID, **kwargs)

Base class for references. Inherits from Structural

class microgp.parameter.reference.LocalReference(individual: Individual, node: NodeID, **kwargs)

A reference to a Node connected through (undirected) “next” edges

Examples:

>>> ref_fwd = ugp4.make_parameter(ugp4.parameter.LocalReference,
>>>                              allow_self=False,
>>>                              allow_forward=True,
>>>                              allow_backward=False,
>>>                              frames_up=1)
>>> ref_bcw = ugp4.make_parameter(ugp4.parameter.LocalReference,
>>>                              allow_self=False,
>>>                              allow_forward=False,
>>>                              allow_backward=True,
>>>                              frames_up=1)
Parameters:
  • allow_self (bool) – The referenced node may be the node itself;
  • allow_forward (bool) – The referenced node may be a successors;
  • allow_backward (bool) – The referenced node may be a predecessors;
  • frames_up (int) – How many frame up the reference must be within (optional, default: 0, i.e., only the current frame)
class microgp.parameter.reference.ExternalReference(individual: Individual, node: NodeID, **kwargs)

A reference to a NodeID non connected through (undirected) “next” edges.

>>> proc1 = ugp4.make_parameter(ugp4.parameter.ExternalReference, section_name='proc1', min=5, max=5)
Parameters:section_name (str) – name of the new target section.

Information

microgp.parameter.special.Information

Helpers

microgp.parameter.helpers

microgp.parameter.helpers.make_parameter(base_class: Type[microgp.parameter.abstract.Parameter], **attributes) → Type[CT_co]

Binds a Base parameter class, fixing some of its internal attributes.

Parameters:
  • base_class (Parameter) – Base class for binding parameter.
  • **attributes (dict) – Attributes.
Returns:

Bound parameter.

Examples:

>>> register = ugp4.make_parameter(ugp4.parameter.Categorical, alternatives=['ax', 'bx', 'cx', 'dx'])
>>> int8 = ugp4.make_parameter(ugp4.parameter.Integer, min_=-128, max_=128)
>>> p1, p2 = register(), int8()