pg.template

Accessible via pg.template, pg.hyper.template.

template(value, where=None)[source]

Creates an object template from the input.

Example:

d = pg.Dict(x=pg.oneof(['a', 'b', 'c'], y=pg.manyof(2, range(4))))
t = pg.template(d)

assert t.dna_spec() == pg.geno.space([
    pg.geno.oneof([
        pg.geno.constant(),
        pg.geno.constant(),
        pg.geno.constant(),
    ], location='x'),
    pg.geno.manyof([
        pg.geno.constant(),
        pg.geno.constant(),
        pg.geno.constant(),
        pg.geno.constant(),
    ], location='y')
])

assert t.encode(pg.Dict(x='a', y=0)) == pg.DNA([0, 0])
assert t.decode(pg.DNA([0, 0])) == pg.Dict(x='a', y=0)

t = pg.template(d, where=lambda x: isinstance(x, pg.hyper.ManyOf))
 assert t.dna_spec() == pg.geno.space([
    pg.geno.manyof([
        pg.geno.constant(),
        pg.geno.constant(),
        pg.geno.constant(),
        pg.geno.constant(),
    ], location='y')
])
assert t.encode(pg.Dict(x=pg.oneof(['a', 'b', 'c']), y=0)) == pg.DNA(0)
assert t.decode(pg.DNA(0)) == pg.Dict(x=pg.oneof(['a', 'b', 'c']), y=0)
Return type:

pg.hyper.ObjectTemplate

Parameters:
  • value – A value based on which the template is created.

  • where – Function to filter hyper values. If None, all hyper primitives from value will be included in the encoding/decoding process. Otherwise only the hyper values on which ‘where’ returns True will be included. where can be useful to partition a search space into separate optimization processes. Please see ‘ObjectTemplate’ docstr for details.

Returns:

A template object.