Space
A Space is used by a StateElement to associate the value to a domain. The interface for Space is explained in the API Reference.
Defining spaces
Spaces can be either Numeric or CatSet. Continuous spaces are only representable in the former, while discrete spaces can be represented in both objects. Specifically, CatSet is designed to hold categorical data, for which no ordering of values is possible.
Defining a space is done via the Space interface. To define Numeric spaces, you have to specify lower and upper bounds, while for CatSet you have to specify an array which holds all possible values.
Note
Performance for
CatSetmay be poor for large sets.Some shortcuts also exist to make defining Spaces less cumbersome. Below are a few ways of defining a
Space
1# Continuous space
2cont_space = Space(
3 low=-numpy.ones((2, 2)),
4 high=numpy.ones((2, 2)),
5)
6# Shortcut
7cont_space = box_space(numpy.ones((2, 2)))
8
9
10# Discrete set
11discr_set = Space(array=numpy.array([0, 1, 2, 3]))
12# Shortcut
13discr_set = integer_set(4)
14
15# # Other shortcuts
16# currently unavailable
17# space = lin_space(-5, 5, num=11, dtype=numpy.int16)
18# space = lin_space(-5, 5, num=22)
19space = integer_space(10, dtype=numpy.int16)
20space = box_space(low=-numpy.ones((2, 2)), high=numpy.ones((2, 2)))
Mechanisms
Space comes with several mechanisms:
check whether a value is contained in a space,
sample a value from a space,
Perform the cartesian product of spaces.
Values in space
You can check if a value belongs to a space with Python’s in operator:
1s = Space(array=numpy.array([1, 2, 3], dtype=numpy.int16))
2assert 1 in s
3assert [1] in s
4assert [[1]] in s
5assert numpy.array(1) in s
6assert numpy.array([1]) in s
7assert numpy.array([[1]]) in s
8assert 4 not in s
9assert -1 not in s
10
Essentially, CoopIHC delegates this operation to Numpy.
Sampling from spaces
You can sample values from spaces using the sample() method:
1s = Space(array=numpy.arange(1000), seed=123)
2q = Space(array=numpy.arange(1000), seed=123)
3r = Space(array=numpy.arange(1000), seed=12)
4_s, _q, _r = s.sample(), q.sample(), r.sample()
5assert _s in s
6assert _q in q
7assert _r in r
8assert _s == _q
9assert _s != _r
You can provide a given seed for each Space via its seed keyword argument, for repeatable random experiments.
Cartesian Product
You can compute the cartesian product of several spaces. For continuous spaces, a None value is used.
1s = Space(array=numpy.array([1, 2, 3], dtype=numpy.int16))
2q = Space(array=numpy.array([-3, -2, -1], dtype=numpy.int16))
3cp, shape = Space.cartesian_product(s, q)