Problem and spaces¶
The five space builders and their composition into mixed spaces via
Space(*blocks).
spaces ¶
Pythonic search-space builders (M4-1 Task 1).
Light dataclasses mirroring Block (crates/core/src/space.rs:13-19) 1:1,
one per block kind:
Float(lo, hi, n) -- continuous block, n coordinates in [lo, hi]
Int(lo, hi, n) -- integer block, n coordinates in [lo, hi]
Categorical(k, n) -- n categorical genes, each in 0..k
Binary(n) -- n bits
Permutation(n) -- one permutation of range(n)
Space(*blocks) composes one or more blocks, in the given order, into a
search space. A bare block is accepted wherever a Space is expected (see
as_space) -- sezgi.Problem.space() may return either.
These builders are pure Python: Space._to_json() serializes to Block's
own JSON wire shape (#[serde(tag = "type", rename_all = "snake_case")]),
consumed directly by _sezgi.from_callable_spaced(...)
(py-sezgi/src/lib.rs) -- no new Rust-side space-parsing code, reusing
Block's existing Deserialize impl.
Float ¶
Float(lo: float, hi: float, n: int)
A continuous block: n coordinates, each in [lo, hi].
Accepted bare wherever a sezgi.Space is expected -- sezgi.Problem.
space() may return a Float(...) directly instead of wrapping it in
Space(Float(...)) (see as_space). Converts to list[float] when it
becomes (all or part of) evaluate's x argument (see
sezgi.problem's own conversion table).
Float(-5.0, 5.0, 10) # 10 coordinates, each in [-5.0, 5.0]
__annotations__
class-attribute
¶
__annotations__ = {'lo': <class 'float'>, 'hi': <class 'float'>, 'n': <class 'int'>}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__dataclass_fields__
class-attribute
¶
__dataclass_fields__ = {'lo': Field(name='lo',type=<class 'float'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'hi': Field(name='hi',type=<class 'float'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'n': Field(name='n',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__doc__
class-attribute
¶
__doc__ = "A continuous block: `n` coordinates, each in `[lo, hi]`.\n\n Accepted bare wherever a `sezgi.Space` is expected -- `sezgi.Problem.\n space()` may return a `Float(...)` directly instead of wrapping it in\n `Space(Float(...))` (see `as_space`). Converts to `list[float]` when it\n becomes (all or part of) `evaluate`'s `x` argument (see\n `sezgi.problem`'s own conversion table).\n\n Float(-5.0, 5.0, 10) # 10 coordinates, each in [-5.0, 5.0]\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__match_args__
class-attribute
¶
__match_args__ = ('lo', 'hi', 'n')
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
Int ¶
Int(lo: int, hi: int, n: int)
An integer block: n coordinates, each in [lo, hi] (inclusive).
Accepted bare wherever a sezgi.Space is expected -- see Float's own
docstring. Converts to list[int] (see sezgi.problem's conversion
table).
Int(0, 9, 5) # 5 coordinates, each an integer in [0, 9]
__annotations__
class-attribute
¶
__annotations__ = {'lo': <class 'int'>, 'hi': <class 'int'>, 'n': <class 'int'>}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__dataclass_fields__
class-attribute
¶
__dataclass_fields__ = {'lo': Field(name='lo',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'hi': Field(name='hi',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'n': Field(name='n',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__doc__
class-attribute
¶
__doc__ = "An integer block: `n` coordinates, each in `[lo, hi]` (inclusive).\n\n Accepted bare wherever a `sezgi.Space` is expected -- see `Float`'s own\n docstring. Converts to `list[int]` (see `sezgi.problem`'s conversion\n table).\n\n Int(0, 9, 5) # 5 coordinates, each an integer in [0, 9]\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__match_args__
class-attribute
¶
__match_args__ = ('lo', 'hi', 'n')
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
Categorical ¶
Categorical(k: int, n: int)
A categorical block: n genes, each a category index in 0..k.
Accepted bare wherever a sezgi.Space is expected -- see Float's own
docstring. Converts to list[int] -- category INDICES (0..k), not
labels (see sezgi.problem's conversion table).
Categorical(k=4, n=3) # 3 genes, each an index in {0, 1, 2, 3}
__annotations__
class-attribute
¶
__annotations__ = {'k': <class 'int'>, 'n': <class 'int'>}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__dataclass_fields__
class-attribute
¶
__dataclass_fields__ = {'k': Field(name='k',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'n': Field(name='n',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__doc__
class-attribute
¶
__doc__ = "A categorical block: `n` genes, each a category index in `0..k`.\n\n Accepted bare wherever a `sezgi.Space` is expected -- see `Float`'s own\n docstring. Converts to `list[int]` -- category INDICES (`0..k`), not\n labels (see `sezgi.problem`'s conversion table).\n\n Categorical(k=4, n=3) # 3 genes, each an index in {0, 1, 2, 3}\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__match_args__
class-attribute
¶
__match_args__ = ('k', 'n')
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
Binary ¶
Binary(n: int)
A binary block: n bits.
Accepted bare wherever a sezgi.Space is expected -- see Float's own
docstring. Converts to list[bool] (see sezgi.problem's conversion
table).
Binary(8) # 8 bits
__annotations__
class-attribute
¶
__annotations__ = {'n': <class 'int'>}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__dataclass_fields__
class-attribute
¶
__dataclass_fields__ = {'n': Field(name='n',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__doc__
class-attribute
¶
__doc__ = "A binary block: `n` bits.\n\n Accepted bare wherever a `sezgi.Space` is expected -- see `Float`'s own\n docstring. Converts to `list[bool]` (see `sezgi.problem`'s conversion\n table).\n\n Binary(8) # 8 bits\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__match_args__
class-attribute
¶
__match_args__ = ('n',)
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
Permutation ¶
Permutation(n: int)
A permutation block: one permutation of range(n).
Accepted bare wherever a sezgi.Space is expected -- see Float's own
docstring. Converts to list[int], a 0-based permutation of range(n)
(see sezgi.problem's conversion table).
Permutation(10) # one permutation of range(10), e.g. a TSP tour
__annotations__
class-attribute
¶
__annotations__ = {'n': <class 'int'>}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__dataclass_fields__
class-attribute
¶
__dataclass_fields__ = {'n': Field(name='n',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,default_factory=<dataclasses._MISSING_TYPE object at 0x7fa5130c6de0>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
__doc__
class-attribute
¶
__doc__ = "A permutation block: one permutation of `range(n)`.\n\n Accepted bare wherever a `sezgi.Space` is expected -- see `Float`'s own\n docstring. Converts to `list[int]`, a 0-based permutation of `range(n)`\n (see `sezgi.problem`'s conversion table).\n\n Permutation(10) # one permutation of range(10), e.g. a TSP tour\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__match_args__
class-attribute
¶
__match_args__ = ('n',)
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
Space ¶
Space(*blocks)
Composes one or more blocks into a search space, in the given order.
A single-block space's evaluate(x) receives that block's own
converted value, passed bare; a multi-block space's x is a tuple of
per-block values, in the order the blocks were given here -- see
sezgi.Problem's own docstring for the exact conversion table.
Composes blocks, in the given order, into one search space.
blocks: one or more Float/Int/Categorical/Binary/
Permutation instances, in the order evaluate's x tuple
should present them (see the class docstring).
Raises ValueError if called with no blocks, TypeError if any
argument is not one of the five block types.
__doc__
class-attribute
¶
__doc__ = "Composes one or more blocks into a search space, in the given order.\n\n A single-block space's `evaluate(x)` receives that block's own\n converted value, passed bare; a multi-block space's `x` is a tuple of\n per-block values, in the order the blocks were given here -- see\n `sezgi.Problem`'s own docstring for the exact conversion table.\n "
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__module__
class-attribute
¶
__module__ = 'sezgi.spaces'
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__hash__ ¶
__hash__()
Consistent with __eq__: two Spaces with equal blocks tuples
hash equal (final-review INFO fix -- defining __eq__ without
__hash__ makes Python set __hash__ = None, so Space was
unhashable while the five frozen block dataclasses it composes are
all hashable on their own). blocks is a tuple of frozen
dataclasses, so it is itself hashable whenever every block in it
is.