pg.manyof#

Accessible via pg.manyof, pg.sublist_of, pg.hyper.manyof, pg.hyper.sublist_of.

manyof(k, candidates, distinct=True, sorted=False, *, name=None, hints=None, **kwargs)[source]#

N choose K.

Example:

@pg.members([
  ('x', pg.typing.Int())
])
class A(pg.Object):
  pass

# Chooses 2 distinct candidates.
v = pg.manyof(2, [1, 2, 3])

# Chooses 2 non-distinct candidates.
v = pg.manyof(2, [1, 2, 3], distinct=False)

# Chooses 2 distinct candidates sorted by their indices.
v = pg.manyof(2, [1, 2, 3], sorted=True)

# A complex type as candidate.
v1 = pg.manyof(2, ['a', {'x': 1}, A(1)])

# A hierarchical categorical choice:
v2 = pg.manyof(2, [
    'foo',
    'bar',
    A(pg.oneof([1, 2, 3]))
])
Return type:

Any

Note

Under symbolic mode (by default), pg.manyof returns a pg.hyper.ManyOf object. Under dynamic evaluation mode, which is called under the context of pg.hyper.DynamicEvaluationContext or pg.hyper.DynamicEvaluationContext, it evaluates to a concrete candidate value.

To use conditional search space in dynamic evaluate mode, the candidate should be wrapped with a lambda function, which is not necessary under symbolic mode. For example:

pg.manyof(2, [
   lambda: pg.oneof([0, 1], name='sub_a'),
   lambda: pg.floatv(0.0, 1.0, name='sub_b'),
   lambda: pg.manyof(2, ['a', 'b', 'c'], name='sub_c')
], name='root')
Parameters:
  • k – number of choices to make. Should be no larger than the length of candidates unless choice_distinct is set to False,

  • candidates – Candidates to select from. Items of candidate can be any type, therefore it can have nested hyper primitives, which forms a hierarchical search space.

  • distinct – If True, each choice needs to be unique.

  • sorted – If True, choices are sorted by their indices in the candidates.

  • name – A name that can be used to identify a decision point in the search space. This is needed when the code to instantiate the same hyper primitive may be called multiple times under a pg.DynamicEvaluationContext.collect context or a pg.DynamicEvaluationContext.apply context.

  • hints – An optional value which acts as a hint for the controller.

  • **kwargs – Keyword arguments for backward compatibility. choices_distinct: Old name for distinct. choices_sorted: Old name for sorted.

Returns:

In symbolic mode, this function returns a Choices. In dynamic evaluate mode, this function returns a list of items in candidates. If evaluated under a pg.DynamicEvaluationContext.apply scope, this function will return a list of selected candidates. If evaluated under a pg.DynamicEvaluationContext.collect scope, it will return a list of the first valid combination from the candidates. For example:

# Evaluates to [0, 1, 2].
manyof(3, range(5))

# Evaluates to [0, 0, 0].
manyof(3, range(5), distinct=False)