Skip to content

Structures

This namespace contains data structures whose underlying storages are contiguous and therefore vectorization-friendly.

CBag

Bases: Structure

An integer bag from which one can do sampling without replacement.

Let us imagine that we wish to create a bag whose maximum length (i.e. whose maximum number of contained elements) is 5. For this, we can do:

bag = CBag(max_length=5)

which gives us an empty bag (i.e. a bag in which all pre-allocated slots are empty):

 _________________________________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Given that the maximum length for this bag is 5, the default set of acceptable values for this bag is 0, 1, 2, 3, 4. Let us put three values into our bag:

bag.push_(torch.tensor(1))
bag.push_(torch.tensor(3))
bag.push_(torch.tensor(4))

After these push operations, our bag can be visualized like this:

 _________________________________________________
|         |         |         |         |         |
|   1     |   3     |   4     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Let us now sample an element from this bag:

sampled1 = bag.pop_()

Because this is the first time we are sampling from this bag, the elements will be first shuffled. Let us assume that the shuffling resulted in:

 _________________________________________________
|         |         |         |         |         |
|   3     |   1     |   4     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Given this shuffed state, our call to pop_(...) will pop the leftmost element (3 in this case). Therefore, the value of sampled1 will be 3 (as a scalar PyTorch tensor), and the state of the bag after the pop operation will be:

 _________________________________________________
|         |         |         |         |         |
|   1     |   4     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Let us keep sampling until the bag is empty:

sampled2 = bag.pop_()
sampled3 = bag.pop_()

The value of sampled2 becomes 1, and the value of sampled3 becomes 4.

This class can also represent a contiguous batch of bags. As an example, let us create 4 bags, each of length 5:

bag_batch = CBag(batch_size=4, max_length=5)

After this instantiation, bag_batch can be visualized like this:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

We can add values to our batch like this:

bag_batch.push_(torch.tensor([3, 2, 3, 1]))
bag_batch.push_(torch.tensor([3, 1, 1, 4]))

which would result in:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
|   3     |   3     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
|   2     |   1     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
|   3     |   1     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
|   1     |   4     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

We can also add values only to some of the bags within the batch:

bag_batch.push_(
    torch.tensor([0, 2, 1, 0]),
    where=torch.tensor([True, True, False, False])),
)

which would result in:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
|   3     |   3     |   0     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
|   2     |   1     |   2     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
|   3     |   1     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
|   1     |   4     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Notice that the batch items 2 and 3 were not affected, because their corresponding values in the where tensor were given as False.

Let us now assume that we wish to obtain a sample from each bag. We can do:

sample_batch1 = bag_batch.pop_()

Since this is the first sampling operation on this bag batch, each bag will first be shuffled. Let us assume that the shuffling resulted in:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
|   0     |   3     |   3     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
|   1     |   2     |   2     | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
|   3     |   1     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
|   4     |   1     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Given this shuffled state, the pop operation takes the leftmost element from each bag. Therefore, the value of sample_batch1 becomes a 1-dimensional tensor containing [0, 1, 3, 4]. Once the pop operation is completed, the state of the batch of bags becomes:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
|   3     |   3     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
|   2     |   2     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
|   1     | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
|   1     | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Now, if we wish to pop only from some of the bags, we can do:

sample_batch2 = bag_batch.pop_(
    where=torch.tensor([True, False, True, False]),
)

which makes the value of sample_batch2 a 1-dimensional tensor containing [3, 2, 1, 1] (the leftmost element for each bag). The state of our batch of bags will become:

 __[ batch item 0 ]_______________________________
|         |         |         |         |         |
|   3     | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 1 ]_______________________________
|         |         |         |         |         |
|   2     |   2     | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 2 ]_______________________________
|         |         |         |         |         |
| <empty> | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

 __[ batch item 3 ]_______________________________
|         |         |         |         |         |
|   1     | <empty> | <empty> | <empty> | <empty> |
|_________|_________|_________|_________|_________|

Notice that the batch items 1 and 3 were not modified, because their corresponding values in the where argument were given as False.

Source code in evotorch/tools/structures.py
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
class CBag(Structure):
    """
    An integer bag from which one can do sampling without replacement.

    Let us imagine that we wish to create a bag whose maximum length (i.e.
    whose maximum number of contained elements) is 5. For this, we can do:

    ```python
    bag = CBag(max_length=5)
    ```

    which gives us an empty bag (i.e. a bag in which all pre-allocated slots
    are empty):

    ```
     _________________________________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Given that the maximum length for this bag is 5, the default set of
    acceptable values for this bag is 0, 1, 2, 3, 4. Let us put three values
    into our bag:

    ```
    bag.push_(torch.tensor(1))
    bag.push_(torch.tensor(3))
    bag.push_(torch.tensor(4))
    ```

    After these push operations, our bag can be visualized like this:

    ```
     _________________________________________________
    |         |         |         |         |         |
    |   1     |   3     |   4     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Let us now sample an element from this bag:

    ```python
    sampled1 = bag.pop_()
    ```

    Because this is the first time we are sampling from this bag, the elements
    will be first shuffled. Let us assume that the shuffling resulted in:

    ```
     _________________________________________________
    |         |         |         |         |         |
    |   3     |   1     |   4     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Given this shuffed state, our call to `pop_(...)` will pop the leftmost
    element (3 in this case). Therefore, the value of `sampled1` will be 3
    (as a scalar PyTorch tensor), and the state of the bag after the pop
    operation will be:

    ```
     _________________________________________________
    |         |         |         |         |         |
    |   1     |   4     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Let us keep sampling until the bag is empty:

    ```python
    sampled2 = bag.pop_()
    sampled3 = bag.pop_()
    ```

    The value of `sampled2` becomes 1, and the value of `sampled3` becomes 4.

    This class can also represent a contiguous batch of bags. As an example,
    let us create 4 bags, each of length 5:

    ```python
    bag_batch = CBag(batch_size=4, max_length=5)
    ```

    After this instantiation, `bag_batch` can be visualized like this:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    We can add values to our batch like this:

    ```python
    bag_batch.push_(torch.tensor([3, 2, 3, 1]))
    bag_batch.push_(torch.tensor([3, 1, 1, 4]))
    ```

    which would result in:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    |   3     |   3     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    |   2     |   1     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    |   3     |   1     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    |   1     |   4     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    We can also add values only to some of the bags within the batch:

    ```
    bag_batch.push_(
        torch.tensor([0, 2, 1, 0]),
        where=torch.tensor([True, True, False, False])),
    )
    ```

    which would result in:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    |   3     |   3     |   0     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    |   2     |   1     |   2     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    |   3     |   1     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    |   1     |   4     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Notice that the batch items 2 and 3 were not affected, because their
    corresponding values in the `where` tensor were given as False.

    Let us now assume that we wish to obtain a sample from each bag. We can do:

    ```python
    sample_batch1 = bag_batch.pop_()
    ```

    Since this is the first sampling operation on this bag batch, each bag
    will first be shuffled. Let us assume that the shuffling resulted in:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    |   0     |   3     |   3     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    |   1     |   2     |   2     | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    |   3     |   1     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    |   4     |   1     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Given this shuffled state, the pop operation takes the leftmost element
    from each bag. Therefore, the value of `sample_batch1` becomes a
    1-dimensional tensor containing `[0, 1, 3, 4]`. Once the pop operation
    is completed, the state of the batch of bags becomes:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    |   3     |   3     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    |   2     |   2     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    |   1     | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    |   1     | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Now, if we wish to pop only from some of the bags, we can do:

    ```python
    sample_batch2 = bag_batch.pop_(
        where=torch.tensor([True, False, True, False]),
    )
    ```

    which makes the value of `sample_batch2` a 1-dimensional tensor containing
    `[3, 2, 1, 1]` (the leftmost element for each bag). The state of our batch
    of bags will become:

    ```
     __[ batch item 0 ]_______________________________
    |         |         |         |         |         |
    |   3     | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 1 ]_______________________________
    |         |         |         |         |         |
    |   2     |   2     | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 2 ]_______________________________
    |         |         |         |         |         |
    | <empty> | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|

     __[ batch item 3 ]_______________________________
    |         |         |         |         |         |
    |   1     | <empty> | <empty> | <empty> | <empty> |
    |_________|_________|_________|_________|_________|
    ```

    Notice that the batch items 1 and 3 were not modified, because their
    corresponding values in the `where` argument were given as False.
    """

    def __init__(
        self,
        *,
        max_length: int,
        value_range: Optional[tuple] = None,
        batch_size: Optional[Union[int, tuple, list]] = None,
        batch_shape: Optional[Union[int, tuple, list]] = None,
        generator: Any = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        verify: bool = True,
    ):
        """
        Initialize the CBag.

        Args:
            max_length: Maximum length (i.e. maximum capacity for storing
                elements).
            value_range: Optionally expected as a tuple of integers in the
                form `(a, b)` where `a` is the lower bound and `b` is the
                exclusive upper bound for the range of acceptable integer
                values. If this argument is omitted, the range will be
                `(0, n)` where `n` is `max_length`.
            batch_size: Optionally an integer or a size tuple, for when
                one wishes to create not just a single bag, but a batch
                of bags.
            batch_shape: Alias for the argument `batch_size`.
            generator: Optionally an instance of `torch.Generator` or any
                object with an attribute (or a property) named `generator`
                (in which case it will be expected that this attribute will
                provide the actual `torch.Generator` instance). If this
                argument is provided, then the shuffling operation will use
                this generator. Otherwise, the global generator of PyTorch
                will be used.
            dtype: dtype for the values contained by the bag(s).
                By default, the dtype is `torch.int64`.
            device: The device on which the bag(s) will be stored.
                By default, the device is `torch.device("cpu")`.
            verify: Whether or not to do explicit checks for the correctness
                of the operations (against popping from an empty bag or
                pushing into a full bag). By default, this is True.
                If you are sure that such errors will not occur, you might
                turn this to False for getting a performance gain.
        """

        if dtype is None:
            dtype = torch.int64
        else:
            dtype = to_torch_dtype(dtype)
            if dtype not in (torch.int16, torch.int32, torch.int64):
                raise RuntimeError(
                    f"CBag currently supports only torch.int16, torch.int32, and torch.int64."
                    f" This dtype is not supported: {repr(dtype)}."
                )

        self._gen_kwargs = {}
        if generator is not None:
            if isinstance(generator, torch.Generator):
                self._gen_kwargs["generator"] = generator
            else:
                generator = generator.generator
                if generator is not None:
                    self._gen_kwargs["generator"] = generator

        max_length = int(max_length)

        self._data = CList(
            max_length=max_length,
            batch_size=batch_size,
            batch_shape=batch_shape,
            dtype=dtype,
            device=device,
            verify=verify,
        )

        if value_range is None:
            a = 0
            b = max_length
        else:
            a, b = value_range

        self._low_item = int(a)
        self._high_item = int(b)  # upper bound is exclusive
        self._choice_count = self._high_item - self._low_item
        self._bignum = self._choice_count + 1

        if self._low_item < 1:
            self._shift = 1 - self._low_item
        else:
            self._shift = 0

        self._empty = self._low_item - 1
        self._data.data[:] = self._empty
        self._sampling_phase: bool = False

    def push_(self, value: Numbers, where: Optional[Numbers] = None):
        """
        Push new value(s) into the bag(s).

        Args:
            value: The value(s) to be pushed into the bag(s).
            where: Optionally a boolean tensor. If this is given, then only
                the bags with their corresponding boolean flags set as True
                will be affected.
        """
        if self._sampling_phase:
            raise RuntimeError("Cannot put a new element into the CBag after calling `sample_(...)`")
        self._data.push_(value, where)

    def _shuffle(self):
        dtype = self._data.dtype
        device = self._data.device
        nrows, ncols = self._data.data.shape

        try:
            gaussian_noise = torch.randn(nrows, ncols, dtype=torch.float32, device=device, **(self._gen_kwargs))
            noise = gaussian_noise.argsort().to(dtype=dtype) * self._bignum
            self._data.data[:] += torch.where(
                self._data.data != self._empty, self._shift + noise, torch.tensor(0, dtype=dtype, device=device)
            )
            self._data.data[:] = self._data.data.sort(dim=-1, descending=True, stable=False).values
        finally:
            self._data.data[:] %= self._bignum
            self._data.data[:] -= self._shift

    def pop_(self, where: Optional[Numbers] = None) -> torch.Tensor:
        """
        Sample value(s) from the bag(s).

        Upon being called for the first time, this method will cause the
        contained elements to be shuffled.

        Args:
            where: Optionally a boolean tensor. If this is given, then only
                the bags with their corresponding boolean flags set as True
                will be affected.
        """
        if not self._sampling_phase:
            self._shuffle()
            self._sampling_phase = True
        return self._data.pop_(where)

    def clear(self):
        """
        Clear the bag(s).
        """
        self._data.data[:] = self._empty
        self._data.clear()
        self._sampling_phase = False

    @property
    def length(self) -> torch.Tensor:
        """
        The length(s) of the bag(s)
        """
        return self._data.length

    @property
    def data(self) -> torch.Tensor:
        """
        The underlying data tensor
        """
        return self._data.data

data property

The underlying data tensor

length property

The length(s) of the bag(s)

__init__(*, max_length, value_range=None, batch_size=None, batch_shape=None, generator=None, dtype=None, device=None, verify=True)

Initialize the CBag.

Parameters:

Name Type Description Default
max_length int

Maximum length (i.e. maximum capacity for storing elements).

required
value_range Optional[tuple]

Optionally expected as a tuple of integers in the form (a, b) where a is the lower bound and b is the exclusive upper bound for the range of acceptable integer values. If this argument is omitted, the range will be (0, n) where n is max_length.

None
batch_size Optional[Union[int, tuple, list]]

Optionally an integer or a size tuple, for when one wishes to create not just a single bag, but a batch of bags.

None
batch_shape Optional[Union[int, tuple, list]]

Alias for the argument batch_size.

None
generator Any

Optionally an instance of torch.Generator or any object with an attribute (or a property) named generator (in which case it will be expected that this attribute will provide the actual torch.Generator instance). If this argument is provided, then the shuffling operation will use this generator. Otherwise, the global generator of PyTorch will be used.

None
dtype Optional[DType]

dtype for the values contained by the bag(s). By default, the dtype is torch.int64.

None
device Optional[Device]

The device on which the bag(s) will be stored. By default, the device is torch.device("cpu").

None
verify bool

Whether or not to do explicit checks for the correctness of the operations (against popping from an empty bag or pushing into a full bag). By default, this is True. If you are sure that such errors will not occur, you might turn this to False for getting a performance gain.

True
Source code in evotorch/tools/structures.py
def __init__(
    self,
    *,
    max_length: int,
    value_range: Optional[tuple] = None,
    batch_size: Optional[Union[int, tuple, list]] = None,
    batch_shape: Optional[Union[int, tuple, list]] = None,
    generator: Any = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    verify: bool = True,
):
    """
    Initialize the CBag.

    Args:
        max_length: Maximum length (i.e. maximum capacity for storing
            elements).
        value_range: Optionally expected as a tuple of integers in the
            form `(a, b)` where `a` is the lower bound and `b` is the
            exclusive upper bound for the range of acceptable integer
            values. If this argument is omitted, the range will be
            `(0, n)` where `n` is `max_length`.
        batch_size: Optionally an integer or a size tuple, for when
            one wishes to create not just a single bag, but a batch
            of bags.
        batch_shape: Alias for the argument `batch_size`.
        generator: Optionally an instance of `torch.Generator` or any
            object with an attribute (or a property) named `generator`
            (in which case it will be expected that this attribute will
            provide the actual `torch.Generator` instance). If this
            argument is provided, then the shuffling operation will use
            this generator. Otherwise, the global generator of PyTorch
            will be used.
        dtype: dtype for the values contained by the bag(s).
            By default, the dtype is `torch.int64`.
        device: The device on which the bag(s) will be stored.
            By default, the device is `torch.device("cpu")`.
        verify: Whether or not to do explicit checks for the correctness
            of the operations (against popping from an empty bag or
            pushing into a full bag). By default, this is True.
            If you are sure that such errors will not occur, you might
            turn this to False for getting a performance gain.
    """

    if dtype is None:
        dtype = torch.int64
    else:
        dtype = to_torch_dtype(dtype)
        if dtype not in (torch.int16, torch.int32, torch.int64):
            raise RuntimeError(
                f"CBag currently supports only torch.int16, torch.int32, and torch.int64."
                f" This dtype is not supported: {repr(dtype)}."
            )

    self._gen_kwargs = {}
    if generator is not None:
        if isinstance(generator, torch.Generator):
            self._gen_kwargs["generator"] = generator
        else:
            generator = generator.generator
            if generator is not None:
                self._gen_kwargs["generator"] = generator

    max_length = int(max_length)

    self._data = CList(
        max_length=max_length,
        batch_size=batch_size,
        batch_shape=batch_shape,
        dtype=dtype,
        device=device,
        verify=verify,
    )

    if value_range is None:
        a = 0
        b = max_length
    else:
        a, b = value_range

    self._low_item = int(a)
    self._high_item = int(b)  # upper bound is exclusive
    self._choice_count = self._high_item - self._low_item
    self._bignum = self._choice_count + 1

    if self._low_item < 1:
        self._shift = 1 - self._low_item
    else:
        self._shift = 0

    self._empty = self._low_item - 1
    self._data.data[:] = self._empty
    self._sampling_phase: bool = False

clear()

Clear the bag(s).

Source code in evotorch/tools/structures.py
def clear(self):
    """
    Clear the bag(s).
    """
    self._data.data[:] = self._empty
    self._data.clear()
    self._sampling_phase = False

pop_(where=None)

Sample value(s) from the bag(s).

Upon being called for the first time, this method will cause the contained elements to be shuffled.

Parameters:

Name Type Description Default
where Optional[Numbers]

Optionally a boolean tensor. If this is given, then only the bags with their corresponding boolean flags set as True will be affected.

None
Source code in evotorch/tools/structures.py
def pop_(self, where: Optional[Numbers] = None) -> torch.Tensor:
    """
    Sample value(s) from the bag(s).

    Upon being called for the first time, this method will cause the
    contained elements to be shuffled.

    Args:
        where: Optionally a boolean tensor. If this is given, then only
            the bags with their corresponding boolean flags set as True
            will be affected.
    """
    if not self._sampling_phase:
        self._shuffle()
        self._sampling_phase = True
    return self._data.pop_(where)

push_(value, where=None)

Push new value(s) into the bag(s).

Parameters:

Name Type Description Default
value Numbers

The value(s) to be pushed into the bag(s).

required
where Optional[Numbers]

Optionally a boolean tensor. If this is given, then only the bags with their corresponding boolean flags set as True will be affected.

None
Source code in evotorch/tools/structures.py
def push_(self, value: Numbers, where: Optional[Numbers] = None):
    """
    Push new value(s) into the bag(s).

    Args:
        value: The value(s) to be pushed into the bag(s).
        where: Optionally a boolean tensor. If this is given, then only
            the bags with their corresponding boolean flags set as True
            will be affected.
    """
    if self._sampling_phase:
        raise RuntimeError("Cannot put a new element into the CBag after calling `sample_(...)`")
    self._data.push_(value, where)

CDict

Bases: Structure

Representation of a batchable dictionary.

This structure is very similar to a CMemory, but with the additional behavior of separately keeping track of which keys exist and which keys do not exist.

Let us consider an example where we have 5 keys, and each key is associated with a tensor of length 7. Such a dictionary could be allocated like this:

dictnry = CDict(7, num_keys=5)

Our allocated dictionary can be visualized as follows:

 _______________________________________
| key 0 -> ( missing )                  |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

Let us now sample a Gaussian noise and put it into the 0-th slot:

dictnry[0] = torch.randn(7)  # or: dictnry[torch.tensor(0)] = torch.randn(7)

which results in:

 _________________________________________
| key 0 -> [ Gaussian noise of length 7 ] |
| key 1 -> ( missing )                    |
| key 2 -> ( missing )                    |
| key 3 -> ( missing )                    |
| key 4 -> ( missing )                    |
|_________________________________________|

Let us now consider another example where we deal with not a single dictionary but with a dictionary batch. For the sake of this example, let us say that our desired batch size is 3. The allocation of such a batch would be as follows:

dict_batch = CDict(7, num_keys=5, batch_size=3)

Our dictionary batch can be visualized like this:

 __[ batch item 0 ]_____________________
| key 0 -> ( missing )                  |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> ( missing )                  |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> ( missing )                  |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

If we wish to set the 0-th element of each batch item, we could do:

dict_batch[0] = torch.tensor(
    [
        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
        [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
    ],
)

and the result would be:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

Continuing from the same example, if we wish to set the slot with key 1 in the 0th batch item, slot with key 2 in the 1st batch item, and slot with key 3 in the 2nd batch item, all in one go, we could do:

# Longer version: dict_batch[torch.tensor([1, 2, 3])] = ...

dict_batch[[1, 2, 3]] = torch.tensor(
    [
        [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
        [6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0],
        [7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0],
    ],
)

Our updated dictionary batch would then look like this:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> ( missing )                  |
| key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
| key 3 -> ( missing )                  |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
| key 4 -> ( missing )                  |
|_______________________________________|

Conditional modifications via boolean masks is also supported. For example, the following update on our dict_batch:

dict_batch.set_(
    [4, 3, 1],
    torch.tensor(
        [
            [8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0],
            [9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0],
            [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
        ]
    ),
    where=[True, True, False],  # or: where=torch.tensor([True,True,False]),
)

would result in:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
| key 2 -> ( missing )                  |
| key 3 -> ( missing )                  |
| key 4 -> [ 8. 8. 8. 8. 8. 8. 8.     ] |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> ( missing )                  |
| key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
| key 3 -> [ 9. 9. 9. 9. 9. 9. 9.     ] |
| key 4 -> ( missing )                  |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> ( missing )                  |
| key 2 -> ( missing )                  |
| key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
| key 4 -> ( missing )                  |
|_______________________________________|

Please notice above that the slot with key 1 of the batch item 2 was not modified because its corresponding mask value was given as False.

After all these modifications, querying whether or not an element with key 0 would give us the following output:

>>> dict_batch.contains(0)
torch.tensor([True, True, True], dtype=torch.bool)

which means that, for each dictionary within the batch, an element with key 0 exists. The same query for the key 3 would give us:

>>> dict_batch.contains(3)
torch.tensor([False, True, True], dtype=torch.bool)

which means that the 0-th dictionary within the batch does not have an element with key 3, but the dictionaries 1 and 2 do have their elements with that key.

Source code in evotorch/tools/structures.py
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
class CDict(Structure):
    """
    Representation of a batchable dictionary.

    This structure is very similar to a `CMemory`, but with the additional
    behavior of separately keeping track of which keys exist and which keys
    do not exist.

    Let us consider an example where we have 5 keys, and each key is associated
    with a tensor of length 7. Such a dictionary could be allocated like this:

    ```python
    dictnry = CDict(7, num_keys=5)
    ```

    Our allocated dictionary can be visualized as follows:

    ```text
     _______________________________________
    | key 0 -> ( missing )                  |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|
    ```

    Let us now sample a Gaussian noise and put it into the 0-th slot:

    ```python
    dictnry[0] = torch.randn(7)  # or: dictnry[torch.tensor(0)] = torch.randn(7)
    ```

    which results in:

    ```text
     _________________________________________
    | key 0 -> [ Gaussian noise of length 7 ] |
    | key 1 -> ( missing )                    |
    | key 2 -> ( missing )                    |
    | key 3 -> ( missing )                    |
    | key 4 -> ( missing )                    |
    |_________________________________________|
    ```

    Let us now consider another example where we deal with not a single
    dictionary but with a dictionary batch. For the sake of this example, let
    us say that our desired batch size is 3. The allocation of such a batch
    would be as follows:

    ```python
    dict_batch = CDict(7, num_keys=5, batch_size=3)
    ```

    Our dictionary batch can be visualized like this:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> ( missing )                  |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> ( missing )                  |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> ( missing )                  |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|
    ```

    If we wish to set the 0-th element of each batch item, we could do:

    ```python
    dict_batch[0] = torch.tensor(
        [
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
        ],
    )
    ```

    and the result would be:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|
    ```

    Continuing from the same example, if we wish to set the slot with key 1
    in the 0th batch item, slot with key 2 in the 1st batch item, and
    slot with key 3 in the 2nd batch item, all in one go, we could do:

    ```python
    # Longer version: dict_batch[torch.tensor([1, 2, 3])] = ...

    dict_batch[[1, 2, 3]] = torch.tensor(
        [
            [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
            [6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0],
            [7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0],
        ],
    )
    ```

    Our updated dictionary batch would then look like this:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
    | key 3 -> ( missing )                  |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
    | key 4 -> ( missing )                  |
    |_______________________________________|
    ```

    Conditional modifications via boolean masks is also supported.
    For example, the following update on our `dict_batch`:

    ```python
    dict_batch.set_(
        [4, 3, 1],
        torch.tensor(
            [
                [8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0],
                [9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0],
                [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
            ]
        ),
        where=[True, True, False],  # or: where=torch.tensor([True,True,False]),
    )
    ```

    would result in:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
    | key 2 -> ( missing )                  |
    | key 3 -> ( missing )                  |
    | key 4 -> [ 8. 8. 8. 8. 8. 8. 8.     ] |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
    | key 3 -> [ 9. 9. 9. 9. 9. 9. 9.     ] |
    | key 4 -> ( missing )                  |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> ( missing )                  |
    | key 2 -> ( missing )                  |
    | key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
    | key 4 -> ( missing )                  |
    |_______________________________________|
    ```

    Please notice above that the slot with key 1 of the batch item 2 was not
    modified because its corresponding mask value was given as False.

    After all these modifications, querying whether or not an element with
    key 0 would give us the following output:

    ```text
    >>> dict_batch.contains(0)
    torch.tensor([True, True, True], dtype=torch.bool)
    ```

    which means that, for each dictionary within the batch, an element with
    key 0 exists. The same query for the key 3 would give us:

    ```text
    >>> dict_batch.contains(3)
    torch.tensor([False, True, True], dtype=torch.bool)
    ```

    which means that the 0-th dictionary within the batch does not have an
    element with key 3, but the dictionaries 1 and 2 do have their elements
    with that key.
    """

    def __init__(
        self,
        *size: Union[int, tuple, list],
        num_keys: Union[int, tuple, list],
        key_offset: Optional[Union[int, tuple, list]] = None,
        batch_size: Optional[Union[int, tuple, list]] = None,
        batch_shape: Optional[Union[int, tuple, list]] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        verify: bool = True,
    ):
        """
        `__init__(...)`: Initialize the CDict.

        Args:
            size: Size of a tensor associated with a key, expected as an
                integer, or as multiple positional arguments (each positional
                argument being an integer), or as a tuple of integers.
            num_keys: How many keys (and therefore how many slots) can the
                dictionary have. If given as an integer `n`, then there will be
                `n` slots available in the dictionary, and to access a slot one
                will need to use an integer key `k` (where, by default, the
                minimum acceptable `k` is 0 and the maximum acceptable `k` is
                `n-1`). If given as a tuple of integers, then the number of slots
                available in the dictionary will be computed as the product of
                all the integers in the tuple, and a key will be expected as a
                tuple. For example, when `num_keys` is `(3, 5)`, there will be
                15 slots available in the dictionary (where, by default, the
                minimum acceptable key will be `(0, 0)` and the maximum
                acceptable key will be `(2, 4)`.
            key_offset: Optionally can be used to shift the integer values of
                the keys. For example, if `num_keys` is 10, then, by default,
                the minimum key is 0 and the maximum key is 9. But together
                with `num_keys=10`, if `key_offset` is given as 1, then the
                minimum key will be 1 and the maximum key will be 10.
                This argument can also be used together with a tuple-valued
                `num_keys`. For example, with `num_keys` set as `(3, 5)`,
                if `key_offset` is given as 1, then the minimum key value
                will be `(1, 1)` (instead of `(0, 0)`) and the maximum key
                value will be `(3, 5)` (instead of `(2, 4)`).
                Also, with a tuple-valued `num_keys`, `key_offset` can be
                given as a tuple, to shift the key values differently for each
                item in the tuple.
            batch_size: If given as None, then this dictionary will not be
                batched. If given as an integer `n`, then this object will
                represent a contiguous batch containing `n` dictionary blocks.
                If given as a tuple `(size0, size1, ...)`, then this object
                will represent a contiguous batch of dictionary, shape of this
                batch being determined by the given tuple.
            batch_shape: Alias for the argument `batch_size`.
            fill_with: Optionally a numeric value using which the values will
                be initialized. If no initialization is needed, then this
                argument can be left as None.
            dtype: The `dtype` of the values stored by this CDict.
            device: The device on which the dictionary will be allocated.
            verify: If True, then explicit checks will be done to verify
                that there are no indexing errors. Can be set as False for
                performance.
        """
        self._data = CMemory(
            *size,
            num_keys=num_keys,
            key_offset=key_offset,
            batch_size=batch_size,
            batch_shape=batch_shape,
            dtype=dtype,
            device=device,
            verify=verify,
        )

        self._exist = CMemory(
            num_keys=num_keys,
            key_offset=key_offset,
            batch_size=batch_size,
            batch_shape=batch_shape,
            dtype=torch.bool,
            device=device,
            verify=verify,
        )

    def get(self, key: Numbers, default: Optional[Numbers] = None) -> torch.Tensor:
        """
        Get the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            default: Optionally can be specified as the fallback value for when
                the element(s) with the given key(s) do not exist.
        Returns:
            The value(s) associated with the given key(s).
        """
        if default is None:
            return self._data[key]
        else:
            exist = self._exist[key]
            default = self._get_value(default)
            return do_where(exist, self._data[key], default)

    def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Set the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The new value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self._data.set_(key, value, where)
        self._exist.set_(key, True, where)

    def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Add value(s) onto the existing values of slots with the given key(s).

        Note that this operation does not change the existence flags of the
        keys. In other words, if element(s) with `key` do not exist, then
        they will still be flagged as non-existent after this operation.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be added onto the existing value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self._data.add_(key, value, where)

    def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Subtract value(s) from existing values of slots with the given key(s).

        Note that this operation does not change the existence flags of the
        keys. In other words, if element(s) with `key` do not exist, then
        they will still be flagged as non-existent after this operation.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be subtracted from existing value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self._data.subtract_(key, value, where)

    def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Divide the existing values of slots with the given key(s).

        Note that this operation does not change the existence flags of the
        keys. In other words, if element(s) with `key` do not exist, then
        they will still be flagged as non-existent after this operation.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be used as divisor(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self._data.divide_(key, value, where)

    def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Multiply the existing values of slots with the given key(s).

        Note that this operation does not change the existence flags of the
        keys. In other words, if element(s) with `key` do not exist, then
        they will still be flagged as non-existent after this operation.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be used as the multiplier(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self._data.multiply_(key, value, where)

    def contains(self, key: Numbers) -> torch.Tensor:
        """
        Query whether or not the element(s) with the given key(s) exist.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
        Returns:
            A boolean tensor indicating whether or not the element(s) with the
            specified key(s) exist.
        """
        return self._exist[key]

    def __getitem__(self, key: Numbers) -> torch.Tensor:
        """
        Get the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
        Returns:
            The value(s) associated with the given key(s).
        """
        return self.get(key)

    def __setitem__(self, key: Numbers, value: Numbers):
        """
        Set the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The new value(s).
        """
        self.set_(key, value)

    def clear(self, where: Optional[torch.Tensor] = None):
        """
        Clear the dictionaries.

        In the context of this data structure, to "clear" means to set the
        status for each key to non-existent.

        Args:
            where: Optionally a boolean tensor, specifying which dictionaries
                within the batch should be cleared. If this argument is omitted
                (i.e. left as None), then all dictionaries will be cleared.
        """
        if where is None:
            self._exist.data[:] = False
        else:
            where = self._get_where(where)
            all_false = torch.tensor(False, dtype=torch.bool, device=self._exist.device).expand(self._exist.shape)
            self._exist.data[:] = do_where(where, all_false, self._exist.data[:])

    @property
    def data(self) -> torch.Tensor:
        """
        The entire value tensor
        """
        return self._data.data

data property

The entire value tensor

__getitem__(key)

Get the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
Source code in evotorch/tools/structures.py
def __getitem__(self, key: Numbers) -> torch.Tensor:
    """
    Get the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
    Returns:
        The value(s) associated with the given key(s).
    """
    return self.get(key)

__init__(*size, num_keys, key_offset=None, batch_size=None, batch_shape=None, dtype=None, device=None, verify=True)

__init__(...): Initialize the CDict.

Parameters:

Name Type Description Default
size Union[int, tuple, list]

Size of a tensor associated with a key, expected as an integer, or as multiple positional arguments (each positional argument being an integer), or as a tuple of integers.

()
num_keys Union[int, tuple, list]

How many keys (and therefore how many slots) can the dictionary have. If given as an integer n, then there will be n slots available in the dictionary, and to access a slot one will need to use an integer key k (where, by default, the minimum acceptable k is 0 and the maximum acceptable k is n-1). If given as a tuple of integers, then the number of slots available in the dictionary will be computed as the product of all the integers in the tuple, and a key will be expected as a tuple. For example, when num_keys is (3, 5), there will be 15 slots available in the dictionary (where, by default, the minimum acceptable key will be (0, 0) and the maximum acceptable key will be (2, 4).

required
key_offset Optional[Union[int, tuple, list]]

Optionally can be used to shift the integer values of the keys. For example, if num_keys is 10, then, by default, the minimum key is 0 and the maximum key is 9. But together with num_keys=10, if key_offset is given as 1, then the minimum key will be 1 and the maximum key will be 10. This argument can also be used together with a tuple-valued num_keys. For example, with num_keys set as (3, 5), if key_offset is given as 1, then the minimum key value will be (1, 1) (instead of (0, 0)) and the maximum key value will be (3, 5) (instead of (2, 4)). Also, with a tuple-valued num_keys, key_offset can be given as a tuple, to shift the key values differently for each item in the tuple.

None
batch_size Optional[Union[int, tuple, list]]

If given as None, then this dictionary will not be batched. If given as an integer n, then this object will represent a contiguous batch containing n dictionary blocks. If given as a tuple (size0, size1, ...), then this object will represent a contiguous batch of dictionary, shape of this batch being determined by the given tuple.

None
batch_shape Optional[Union[int, tuple, list]]

Alias for the argument batch_size.

None
fill_with

Optionally a numeric value using which the values will be initialized. If no initialization is needed, then this argument can be left as None.

required
dtype Optional[DType]

The dtype of the values stored by this CDict.

None
device Optional[Device]

The device on which the dictionary will be allocated.

None
verify bool

If True, then explicit checks will be done to verify that there are no indexing errors. Can be set as False for performance.

True
Source code in evotorch/tools/structures.py
def __init__(
    self,
    *size: Union[int, tuple, list],
    num_keys: Union[int, tuple, list],
    key_offset: Optional[Union[int, tuple, list]] = None,
    batch_size: Optional[Union[int, tuple, list]] = None,
    batch_shape: Optional[Union[int, tuple, list]] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    verify: bool = True,
):
    """
    `__init__(...)`: Initialize the CDict.

    Args:
        size: Size of a tensor associated with a key, expected as an
            integer, or as multiple positional arguments (each positional
            argument being an integer), or as a tuple of integers.
        num_keys: How many keys (and therefore how many slots) can the
            dictionary have. If given as an integer `n`, then there will be
            `n` slots available in the dictionary, and to access a slot one
            will need to use an integer key `k` (where, by default, the
            minimum acceptable `k` is 0 and the maximum acceptable `k` is
            `n-1`). If given as a tuple of integers, then the number of slots
            available in the dictionary will be computed as the product of
            all the integers in the tuple, and a key will be expected as a
            tuple. For example, when `num_keys` is `(3, 5)`, there will be
            15 slots available in the dictionary (where, by default, the
            minimum acceptable key will be `(0, 0)` and the maximum
            acceptable key will be `(2, 4)`.
        key_offset: Optionally can be used to shift the integer values of
            the keys. For example, if `num_keys` is 10, then, by default,
            the minimum key is 0 and the maximum key is 9. But together
            with `num_keys=10`, if `key_offset` is given as 1, then the
            minimum key will be 1 and the maximum key will be 10.
            This argument can also be used together with a tuple-valued
            `num_keys`. For example, with `num_keys` set as `(3, 5)`,
            if `key_offset` is given as 1, then the minimum key value
            will be `(1, 1)` (instead of `(0, 0)`) and the maximum key
            value will be `(3, 5)` (instead of `(2, 4)`).
            Also, with a tuple-valued `num_keys`, `key_offset` can be
            given as a tuple, to shift the key values differently for each
            item in the tuple.
        batch_size: If given as None, then this dictionary will not be
            batched. If given as an integer `n`, then this object will
            represent a contiguous batch containing `n` dictionary blocks.
            If given as a tuple `(size0, size1, ...)`, then this object
            will represent a contiguous batch of dictionary, shape of this
            batch being determined by the given tuple.
        batch_shape: Alias for the argument `batch_size`.
        fill_with: Optionally a numeric value using which the values will
            be initialized. If no initialization is needed, then this
            argument can be left as None.
        dtype: The `dtype` of the values stored by this CDict.
        device: The device on which the dictionary will be allocated.
        verify: If True, then explicit checks will be done to verify
            that there are no indexing errors. Can be set as False for
            performance.
    """
    self._data = CMemory(
        *size,
        num_keys=num_keys,
        key_offset=key_offset,
        batch_size=batch_size,
        batch_shape=batch_shape,
        dtype=dtype,
        device=device,
        verify=verify,
    )

    self._exist = CMemory(
        num_keys=num_keys,
        key_offset=key_offset,
        batch_size=batch_size,
        batch_shape=batch_shape,
        dtype=torch.bool,
        device=device,
        verify=verify,
    )

__setitem__(key, value)

Set the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The new value(s).

required
Source code in evotorch/tools/structures.py
def __setitem__(self, key: Numbers, value: Numbers):
    """
    Set the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The new value(s).
    """
    self.set_(key, value)

add_(key, value, where=None)

Add value(s) onto the existing values of slots with the given key(s).

Note that this operation does not change the existence flags of the keys. In other words, if element(s) with key do not exist, then they will still be flagged as non-existent after this operation.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be added onto the existing value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Add value(s) onto the existing values of slots with the given key(s).

    Note that this operation does not change the existence flags of the
    keys. In other words, if element(s) with `key` do not exist, then
    they will still be flagged as non-existent after this operation.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be added onto the existing value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self._data.add_(key, value, where)

clear(where=None)

Clear the dictionaries.

In the context of this data structure, to "clear" means to set the status for each key to non-existent.

Parameters:

Name Type Description Default
where Optional[Tensor]

Optionally a boolean tensor, specifying which dictionaries within the batch should be cleared. If this argument is omitted (i.e. left as None), then all dictionaries will be cleared.

None
Source code in evotorch/tools/structures.py
def clear(self, where: Optional[torch.Tensor] = None):
    """
    Clear the dictionaries.

    In the context of this data structure, to "clear" means to set the
    status for each key to non-existent.

    Args:
        where: Optionally a boolean tensor, specifying which dictionaries
            within the batch should be cleared. If this argument is omitted
            (i.e. left as None), then all dictionaries will be cleared.
    """
    if where is None:
        self._exist.data[:] = False
    else:
        where = self._get_where(where)
        all_false = torch.tensor(False, dtype=torch.bool, device=self._exist.device).expand(self._exist.shape)
        self._exist.data[:] = do_where(where, all_false, self._exist.data[:])

contains(key)

Query whether or not the element(s) with the given key(s) exist.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
Source code in evotorch/tools/structures.py
def contains(self, key: Numbers) -> torch.Tensor:
    """
    Query whether or not the element(s) with the given key(s) exist.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
    Returns:
        A boolean tensor indicating whether or not the element(s) with the
        specified key(s) exist.
    """
    return self._exist[key]

divide_(key, value, where=None)

Divide the existing values of slots with the given key(s).

Note that this operation does not change the existence flags of the keys. In other words, if element(s) with key do not exist, then they will still be flagged as non-existent after this operation.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be used as divisor(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Divide the existing values of slots with the given key(s).

    Note that this operation does not change the existence flags of the
    keys. In other words, if element(s) with `key` do not exist, then
    they will still be flagged as non-existent after this operation.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be used as divisor(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self._data.divide_(key, value, where)

get(key, default=None)

Get the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
default Optional[Numbers]

Optionally can be specified as the fallback value for when the element(s) with the given key(s) do not exist.

None
Source code in evotorch/tools/structures.py
def get(self, key: Numbers, default: Optional[Numbers] = None) -> torch.Tensor:
    """
    Get the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        default: Optionally can be specified as the fallback value for when
            the element(s) with the given key(s) do not exist.
    Returns:
        The value(s) associated with the given key(s).
    """
    if default is None:
        return self._data[key]
    else:
        exist = self._exist[key]
        default = self._get_value(default)
        return do_where(exist, self._data[key], default)

multiply_(key, value, where=None)

Multiply the existing values of slots with the given key(s).

Note that this operation does not change the existence flags of the keys. In other words, if element(s) with key do not exist, then they will still be flagged as non-existent after this operation.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be used as the multiplier(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Multiply the existing values of slots with the given key(s).

    Note that this operation does not change the existence flags of the
    keys. In other words, if element(s) with `key` do not exist, then
    they will still be flagged as non-existent after this operation.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be used as the multiplier(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self._data.multiply_(key, value, where)

set_(key, value, where=None)

Set the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The new value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Set the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The new value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self._data.set_(key, value, where)
    self._exist.set_(key, True, where)

subtract_(key, value, where=None)

Subtract value(s) from existing values of slots with the given key(s).

Note that this operation does not change the existence flags of the keys. In other words, if element(s) with key do not exist, then they will still be flagged as non-existent after this operation.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be subtracted from existing value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Subtract value(s) from existing values of slots with the given key(s).

    Note that this operation does not change the existence flags of the
    keys. In other words, if element(s) with `key` do not exist, then
    they will still be flagged as non-existent after this operation.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be subtracted from existing value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self._data.subtract_(key, value, where)

CList

Bases: Structure

Representation of a batchable, contiguous, variable-length list structure.

This CList structure works with a pre-allocated contiguous block of memory with a separately stored length. In the batched case, each batch item has its own length.

This structure supports negative indexing (meaning that -1 refers to the last item, -2 refers to the second last item, etc.).

Let us imagine that we need a list where each element has a shape (3,), and our maximum length is 5. Such a list could be instantiated via:

lst = CList(3, max_length=5)

In its initial state, the list is empty, which can be visualized like:

 _______________________________________________________________
| index  |    0     |    1     |    2     |    3     |    4     |
| values | <unused> | <unused> | <unused> | <unused> | <unused> |
|________|__________|__________|__________|__________|__________|

We can add elements into our list like this:

lst.append_(torch.tensor([1.0, 2.0, 3.0]))
lst.append_(torch.tensor([4.0, 5.0, 6.0]))

After these two push operations, our list looks like this:

 __________________________________________________________________
| index  |      0     |     1     |    2     |    3     |    4     |
| values | [1. 2. 3.] | [4. 5. 6] | <unused> | <unused> | <unused> |
|________|____________|___________|__________|__________|__________|

Here, lst[0] returns [1. 2. 3.] and lst[1] returns [4. 5. 6.]. A CList also supports negative indices, allowing lst[-1] to return [4. 5. 6.] (the last element) and lst[-2] to return [1. 2. 3.] (the second last element).

One can also create a batch of lists. Let us imagine that we wish to create a batch of lists such that the batch size is 4, length of an element is 3, and the maximum length is 5. Such a batch can be created as follows:

list_batch = CList(3, max_length=5, batch_size=4)

Our batch can be visualized like this:

 __[ batch item 0 ]_____________________________________________
| index  |    0     |    1     |    2     |    3     |    4     |
| values | <unused> | <unused> | <unused> | <unused> | <unused> |
|________|__________|__________|__________|__________|__________|

 __[ batch item 1 ]_____________________________________________
| index  |    0     |    1     |    2     |    3     |    4     |
| values | <unused> | <unused> | <unused> | <unused> | <unused> |
|________|__________|__________|__________|__________|__________|

 __[ batch item 2 ]_____________________________________________
| index  |    0     |    1     |    2     |    3     |    4     |
| values | <unused> | <unused> | <unused> | <unused> | <unused> |
|________|__________|__________|__________|__________|__________|

 __[ batch item 3 ]_____________________________________________
| index  |    0     |    1     |    2     |    3     |    4     |
| values | <unused> | <unused> | <unused> | <unused> | <unused> |
|________|__________|__________|__________|__________|__________|

Let us now add [1. 1. 1.] to the batch item 0, [2. 2. 2.] to the batch item 1, and so on:

list_batch.append_(
    torch.tensor(
        [
            [1.0, 1.0, 1.0],
            [2.0, 2.0, 2.0],
            [3.0, 3.0, 3.0],
            [4.0, 4.0, 4.0],
        ]
    )
)

After these operations, list_batch looks like this:

 __[ batch item 0 ]_______________________________________________
| index  |    0       |    1     |    2     |    3     |    4     |
| values | [1. 1. 1.] | <unused> | <unused> | <unused> | <unused> |
|________|____________|__________|__________|__________|__________|

 __[ batch item 1 ]_______________________________________________
| index  |    0       |    1     |    2     |    3     |    4     |
| values | [2. 2. 2.] | <unused> | <unused> | <unused> | <unused> |
|________|____________|__________|__________|__________|__________|

 __[ batch item 2 ]_______________________________________________
| index  |    0       |    1     |    2     |    3     |    4     |
| values | [3. 3. 3.] | <unused> | <unused> | <unused> | <unused> |
|________|____________|__________|__________|__________|__________|

 __[ batch item 3 ]_______________________________________________
| index  |    0       |    1     |    2     |    3     |    4     |
| values | [4. 4. 4.] | <unused> | <unused> | <unused> | <unused> |
|________|____________|__________|__________|__________|__________|

We can also use a boolean mask to add to only some of the lists within the batch:

list_batch.append_(
    torch.tensor(
        [
            [5.0, 5.0, 5.0],
            [6.0, 6.0, 6.0],
            [7.0, 7.0, 7.0],
            [8.0, 8.0, 8.0],
        ]
    ),
    where=torch.tensor([True, False, False, True]),
)

which would update our batch of lists like this:

 __[ batch item 0 ]_________________________________________________
| index  |    0       |    1       |    2     |    3     |    4     |
| values | [1. 1. 1.] | [5. 5. 5.] | <unused> | <unused> | <unused> |
|________|____________|____________|__________|__________|__________|

 __[ batch item 1 ]_________________________________________________
| index  |    0       |    1       |    2     |    3     |    4     |
| values | [2. 2. 2.] |  <unused>  | <unused> | <unused> | <unused> |
|________|____________|____________|__________|__________|__________|

 __[ batch item 2 ]_________________________________________________
| index  |    0       |    1       |    2     |    3     |    4     |
| values | [3. 3. 3.] |  <unused>  | <unused> | <unused> | <unused> |
|________|____________|____________|__________|__________|__________|

 __[ batch item 3 ]_________________________________________________
| index  |    0       |    1       |    2     |    3     |    4     |
| values | [4. 4. 4.] | [8. 8. 8.] | <unused> | <unused> | <unused> |
|________|____________|____________|__________|__________|__________|

Please notice above how the batch items 1 and 2 were not modified because their corresponding boolean values in the where tensor were given as False.

After all these modifications we would get the following results:

>>> list_batch[0]
torch.tensor(
    [[1. 1. 1.],
     [2. 2. 2.],
     [3. 3. 3.],
     [4. 4. 4.]]
)

>>> list_batch[[1, 0, 0, 1]]
torch.tensor(
    [[5. 5. 5.],
     [2. 2. 2.],
     [3. 3. 3.],
     [8. 8. 8.]]
)

>>> list_batch[[-1, -1, -1, -1]]
torch.tensor(
    [[5. 5. 5.],
     [2. 2. 2.],
     [3. 3. 3.],
     [8. 8. 8.]]
)

Note that this CList structure also supports the ability to insert to the beginning, or to remove from the beginning. These operations internally shift the addresses for the beginning of the data within the underlying memory, and therefore, they are not any more costly than adding to or removing from the end of the list.

Source code in evotorch/tools/structures.py
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
class CList(Structure):
    """
    Representation of a batchable, contiguous, variable-length list structure.

    This CList structure works with a pre-allocated contiguous block of memory
    with a separately stored length. In the batched case, each batch item
    has its own length.

    This structure supports negative indexing (meaning that -1 refers to the
    last item, -2 refers to the second last item, etc.).

    Let us imagine that we need a list where each element has a shape `(3,)`,
    and our maximum length is 5.
    Such a list could be instantiated via:

    ```python
    lst = CList(3, max_length=5)
    ```

    In its initial state, the list is empty, which can be visualized like:

    ```text
     _______________________________________________________________
    | index  |    0     |    1     |    2     |    3     |    4     |
    | values | <unused> | <unused> | <unused> | <unused> | <unused> |
    |________|__________|__________|__________|__________|__________|
    ```

    We can add elements into our list like this:

    ```python
    lst.append_(torch.tensor([1.0, 2.0, 3.0]))
    lst.append_(torch.tensor([4.0, 5.0, 6.0]))
    ```

    After these two push operations, our list looks like this:

    ```text
     __________________________________________________________________
    | index  |      0     |     1     |    2     |    3     |    4     |
    | values | [1. 2. 3.] | [4. 5. 6] | <unused> | <unused> | <unused> |
    |________|____________|___________|__________|__________|__________|
    ```

    Here, `lst[0]` returns `[1. 2. 3.]` and `lst[1]` returns `[4. 5. 6.]`.
    A `CList` also supports negative indices, allowing `lst[-1]` to return
    `[4. 5. 6.]` (the last element) and `lst[-2]` to return `[1. 2. 3.]`
    (the second last element).

    One can also create a batch of lists. Let us imagine that we wish to
    create a batch of lists such that the batch size is 4, length of an
    element is 3, and the maximum length is 5. Such a batch can be created
    as follows:

    ```python
    list_batch = CList(3, max_length=5, batch_size=4)
    ```

    Our batch can be visualized like this:

    ```text
     __[ batch item 0 ]_____________________________________________
    | index  |    0     |    1     |    2     |    3     |    4     |
    | values | <unused> | <unused> | <unused> | <unused> | <unused> |
    |________|__________|__________|__________|__________|__________|

     __[ batch item 1 ]_____________________________________________
    | index  |    0     |    1     |    2     |    3     |    4     |
    | values | <unused> | <unused> | <unused> | <unused> | <unused> |
    |________|__________|__________|__________|__________|__________|

     __[ batch item 2 ]_____________________________________________
    | index  |    0     |    1     |    2     |    3     |    4     |
    | values | <unused> | <unused> | <unused> | <unused> | <unused> |
    |________|__________|__________|__________|__________|__________|

     __[ batch item 3 ]_____________________________________________
    | index  |    0     |    1     |    2     |    3     |    4     |
    | values | <unused> | <unused> | <unused> | <unused> | <unused> |
    |________|__________|__________|__________|__________|__________|
    ```

    Let us now add `[1. 1. 1.]` to the batch item 0, `[2. 2. 2.]` to the batch
    item 1, and so on:

    ```python
    list_batch.append_(
        torch.tensor(
            [
                [1.0, 1.0, 1.0],
                [2.0, 2.0, 2.0],
                [3.0, 3.0, 3.0],
                [4.0, 4.0, 4.0],
            ]
        )
    )
    ```

    After these operations, `list_batch` looks like this:

    ```text
     __[ batch item 0 ]_______________________________________________
    | index  |    0       |    1     |    2     |    3     |    4     |
    | values | [1. 1. 1.] | <unused> | <unused> | <unused> | <unused> |
    |________|____________|__________|__________|__________|__________|

     __[ batch item 1 ]_______________________________________________
    | index  |    0       |    1     |    2     |    3     |    4     |
    | values | [2. 2. 2.] | <unused> | <unused> | <unused> | <unused> |
    |________|____________|__________|__________|__________|__________|

     __[ batch item 2 ]_______________________________________________
    | index  |    0       |    1     |    2     |    3     |    4     |
    | values | [3. 3. 3.] | <unused> | <unused> | <unused> | <unused> |
    |________|____________|__________|__________|__________|__________|

     __[ batch item 3 ]_______________________________________________
    | index  |    0       |    1     |    2     |    3     |    4     |
    | values | [4. 4. 4.] | <unused> | <unused> | <unused> | <unused> |
    |________|____________|__________|__________|__________|__________|
    ```

    We can also use a boolean mask to add to only some of the lists within
    the batch:

    ```python
    list_batch.append_(
        torch.tensor(
            [
                [5.0, 5.0, 5.0],
                [6.0, 6.0, 6.0],
                [7.0, 7.0, 7.0],
                [8.0, 8.0, 8.0],
            ]
        ),
        where=torch.tensor([True, False, False, True]),
    )
    ```

    which would update our batch of lists like this:

    ```text
     __[ batch item 0 ]_________________________________________________
    | index  |    0       |    1       |    2     |    3     |    4     |
    | values | [1. 1. 1.] | [5. 5. 5.] | <unused> | <unused> | <unused> |
    |________|____________|____________|__________|__________|__________|

     __[ batch item 1 ]_________________________________________________
    | index  |    0       |    1       |    2     |    3     |    4     |
    | values | [2. 2. 2.] |  <unused>  | <unused> | <unused> | <unused> |
    |________|____________|____________|__________|__________|__________|

     __[ batch item 2 ]_________________________________________________
    | index  |    0       |    1       |    2     |    3     |    4     |
    | values | [3. 3. 3.] |  <unused>  | <unused> | <unused> | <unused> |
    |________|____________|____________|__________|__________|__________|

     __[ batch item 3 ]_________________________________________________
    | index  |    0       |    1       |    2     |    3     |    4     |
    | values | [4. 4. 4.] | [8. 8. 8.] | <unused> | <unused> | <unused> |
    |________|____________|____________|__________|__________|__________|
    ```

    Please notice above how the batch items 1 and 2 were not modified because
    their corresponding boolean values in the `where` tensor were given as
    `False`.

    After all these modifications we would get the following results:

    ```text
    >>> list_batch[0]
    torch.tensor(
        [[1. 1. 1.],
         [2. 2. 2.],
         [3. 3. 3.],
         [4. 4. 4.]]
    )

    >>> list_batch[[1, 0, 0, 1]]
    torch.tensor(
        [[5. 5. 5.],
         [2. 2. 2.],
         [3. 3. 3.],
         [8. 8. 8.]]
    )

    >>> list_batch[[-1, -1, -1, -1]]
    torch.tensor(
        [[5. 5. 5.],
         [2. 2. 2.],
         [3. 3. 3.],
         [8. 8. 8.]]
    )
    ```

    Note that this CList structure also supports the ability to insert to the
    beginning, or to remove from the beginning. These operations internally
    shift the addresses for the beginning of the data within the underlying
    memory, and therefore, they are not any more costly than adding to or
    removing from the end of the list.
    """

    def __init__(
        self,
        *size: Union[int, list, tuple],
        max_length: int,
        batch_size: Optional[Union[int, tuple, list]] = None,
        batch_shape: Optional[Union[int, tuple, list]] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        verify: bool = True,
    ):
        self._verify = bool(verify)
        self._max_length = int(max_length)

        self._data = CMemory(
            *size,
            num_keys=self._max_length,
            batch_size=batch_size,
            batch_shape=batch_shape,
            dtype=dtype,
            device=device,
            verify=False,
        )

        self._begin, self._end = [
            CMemory(
                num_keys=1,
                batch_size=batch_size,
                batch_shape=batch_shape,
                dtype=torch.int64,
                device=device,
                verify=False,
                fill_with=-1,
            )
            for _ in range(2)
        ]

        if "float" in str(self._data.dtype):
            self._pop_fallback = float("nan")
        else:
            self._pop_fallback = 0

        if self._begin.batch_ndim == 0:
            self._all_zeros = torch.tensor(0, dtype=torch.int64, device=self._begin.device)
        else:
            self._all_zeros = torch.zeros(1, dtype=torch.int64, device=self._begin.device).expand(
                self._begin.batch_shape
            )

    def _is_empty(self) -> torch.Tensor:
        # return (self._begin[self._all_zeros] == -1) & (self._end[self._all_zeros] == -1)
        return self._begin[self._all_zeros] == -1

    def _has_one_element(self) -> torch.Tensor:
        begin = self._begin[self._all_zeros]
        end = self._end[self._all_zeros]
        return (begin == end) & (begin >= 0)

    def _is_full(self) -> torch.Tensor:
        begin = self._begin[self._all_zeros]
        end = self._end[self._all_zeros]
        return ((end - begin) % self._max_length) == (self._max_length - 1)

    @staticmethod
    def _considering_where(other_mask: torch.Tensor, where: Optional[torch.Tensor]) -> torch.Tensor:
        return other_mask if where is None else other_mask & where

    def _get_info_for_adding_element(self, where: Optional[torch.Tensor]) -> _InfoForAddingElement:
        is_empty = self._is_empty()
        is_full = self._is_full()
        to_be_declared_non_empty = self._considering_where(is_empty, where)
        if self._verify:
            invalid_move = self._considering_where(is_full, where)
            if torch.any(invalid_move):
                raise IndexError("Some of the queues are full, and therefore elements cannot be added to them")
        valid_move = self._considering_where((~is_empty) & (~is_full), where)
        return _InfoForAddingElement(valid_move=valid_move, to_be_declared_non_empty=to_be_declared_non_empty)

    def _get_info_for_removing_element(self, where: Optional[torch.Tensor]) -> _InfoForRemovingElement:
        is_empty = self._is_empty()
        has_one_element = self._has_one_element()
        if self._verify:
            invalid_move = self._considering_where(is_empty, where)
            if torch.any(invalid_move):
                raise IndexError(
                    "Some of the queues are already empty, and therefore elements cannot be removed from them"
                )
        to_be_declared_empty = self._considering_where(has_one_element, where)
        valid_move = self._considering_where((~is_empty) & (~has_one_element), where)
        return _InfoForRemovingElement(valid_move=valid_move, to_be_declared_empty=to_be_declared_empty)

    def _move_begin_forward(self, where: Optional[torch.Tensor]):
        valid_move, to_be_declared_empty = self._get_info_for_removing_element(where)
        self._begin.set_(self._all_zeros, -1, where=to_be_declared_empty)
        self._end.set_(self._all_zeros, -1, where=to_be_declared_empty)
        self._begin.add_circular_(self._all_zeros, 1, self._max_length, where=valid_move)

    def _move_end_forward(self, where: Optional[torch.Tensor]):
        valid_move, to_be_declared_non_empty = self._get_info_for_adding_element(where)
        self._begin.set_(self._all_zeros, 0, where=to_be_declared_non_empty)
        self._end.set_(self._all_zeros, 0, where=to_be_declared_non_empty)
        self._end.add_circular_(self._all_zeros, 1, self._max_length, where=valid_move)

    def _move_begin_backward(self, where: Optional[torch.Tensor]):
        valid_move, to_be_declared_non_empty = self._get_info_for_adding_element(where)
        self._begin.set_(self._all_zeros, 0, where=to_be_declared_non_empty)
        self._end.set_(self._all_zeros, 0, where=to_be_declared_non_empty)
        self._begin.add_circular_(self._all_zeros, -1, self._max_length, where=valid_move)

    def _move_end_backward(self, where: Optional[torch.Tensor]):
        valid_move, to_be_declared_empty = self._get_info_for_removing_element(where)
        self._begin.set_(self._all_zeros, -1, where=to_be_declared_empty)
        self._end.set_(self._all_zeros, -1, where=to_be_declared_empty)
        self._end.add_circular_(self._all_zeros, -1, self._max_length, where=valid_move)

    def _get_key(self, key: Numbers) -> torch.Tensor:
        key = torch.as_tensor(key, dtype=torch.int64, device=self._data.device)
        batch_shape = self._data.batch_shape
        if key.shape != batch_shape:
            if key.ndim == 0:
                key = key.expand(self._data.batch_shape)
            else:
                raise ValueError(
                    f"Expected the keys of shape {batch_shape}, but received them in this shape: {key.shape}"
                )
        return key

    def _is_underlying_key_valid(self, underlying_key: torch.Tensor) -> torch.Tensor:
        within_valid_range = (underlying_key >= 0) & (underlying_key < self._max_length)
        begin = self._begin[self._all_zeros]
        end = self._end[self._all_zeros]
        empty = self._is_empty()
        non_empty = ~empty
        larger_end = non_empty & (end > begin)
        smaller_end = non_empty & (end < begin)
        same_begin_end = (begin == end) & (~empty)
        valid = within_valid_range & (
            (same_begin_end & (underlying_key == begin))
            | (larger_end & (underlying_key >= begin) & (underlying_key <= end))
            | (smaller_end & ((underlying_key <= end) | (underlying_key >= begin)))
        )
        return valid

    def _mod_underlying_key(self, underlying_key: torch.Tensor, *, verify: Optional[bool] = None) -> torch.Tensor:
        verify = self._verify if verify is None else verify
        if self._verify:
            where_negative = underlying_key < 0
            where_too_large = underlying_key >= self._max_length
            underlying_key = underlying_key.clone()
            underlying_key[where_negative] += self._max_length
            underlying_key[where_too_large] -= self._max_length
        else:
            underlying_key = underlying_key % self._max_length

        return underlying_key

    def _get_underlying_key(
        self,
        key: Numbers,
        *,
        verify: Optional[bool] = None,
        return_validity: bool = False,
        where: Optional[torch.Tensor] = None,
    ) -> Union[torch.Tensor, tuple]:
        if where is not None:
            where = self._get_where(where)
        verify = self._verify if verify is None else verify
        key = self._get_key(key)
        underlying_key_for_pos_index = self._begin[self._all_zeros] + key
        underlying_key_for_neg_index = self._end[self._all_zeros] + key + 1
        underlying_key = torch.where(key >= 0, underlying_key_for_pos_index, underlying_key_for_neg_index)
        underlying_key = self._mod_underlying_key(underlying_key, verify=verify)

        if verify or return_validity:
            valid = self._is_underlying_key_valid(underlying_key)
        else:
            valid = None

        if verify:
            okay = valid if where is None else valid | (~where)
            if not torch.all(okay):
                raise IndexError("Encountered invalid index/indices")

        if return_validity:
            return underlying_key, valid
        else:
            return underlying_key

    def get(self, key: Numbers, default: Optional[Numbers] = None) -> torch.Tensor:
        """
        Get the value(s) from the specified element(s).

        Args:
            key: The index/indices pointing to the element(s) whose value(s)
                is/are queried.
            default: Default value(s) to be returned for when the specified
                index/indices are invalid and/or out of range.
        Returns:
            The value(s) stored by the element(s).
        """
        if default is None:
            underlying_key = self._get_underlying_key(key)
            return self._data[underlying_key]
        else:
            default = self._data._get_value(default)
            underlying_key, valid_key = self._get_underlying_key(key, verify=False, return_validity=True)
            return do_where(valid_key, self._data[underlying_key % self._max_length], default)

    def __getitem__(self, key: Numbers) -> torch.Tensor:
        """
        Get the value(s) from the specified element(s).

        Args:
            key: The index/indices pointing to the element(s) whose value(s)
                is/are queried.
        Returns:
            The value(s) stored by the element(s).
        """
        return self.get(key)

    def _apply_modification_method(
        self, method_name: str, key: Numbers, value: Numbers, where: Optional[Numbers] = None
    ):
        underlying_key = self._get_underlying_key(key, where=where)
        getattr(self._data, method_name)(underlying_key, value, where)

    def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Set the element(s) addressed to by the given key(s).

        Args:
            key: The index/indices tensor.
            value: The new value(s).
            where: Optionally a boolean mask. When provided, only the elements
                whose corresponding mask value(s) is/are True will be subject
                to modification.
        """
        self._apply_modification_method("set_", key, value, where)

    def __setitem__(self, key: Numbers, value: Numbers):
        """
        Set the element(s) addressed to by the given key(s).

        Args:
            key: The index/indices tensor.
            value: The new value(s).
        """
        self._apply_modification_method("set_", key, value)

    def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Add to the element(s) addressed to by the given key(s).

        Please note that the word "add" is used in the arithmetic sense
        (i.e. in the sense of performing addition). For putting a new
        element into this list, please see the method `append_(...)`.

        Args:
            key: The index/indices tensor.
            value: The value(s) that will be added onto the existing
                element(s).
            where: Optionally a boolean mask. When provided, only the elements
                whose corresponding mask value(s) is/are True will be subject
                to modification.
        """
        self._apply_modification_method("add_", key, value, where)

    def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Subtract from the element(s) addressed to by the given key(s).

        Args:
            key: The index/indices tensor.
            value: The value(s) that will be subtracted from the existing
                element(s).
            where: Optionally a boolean mask. When provided, only the elements
                whose corresponding mask value(s) is/are True will be subject
                to modification.
        """
        self._apply_modification_method("subtract_", key, value, where)

    def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Multiply the element(s) addressed to by the given key(s).

        Args:
            key: The index/indices tensor.
            value: The value(s) that will be used as the multiplier(s) on the
                existing element(s).
            where: Optionally a boolean mask. When provided, only the elements
                whose corresponding mask value(s) is/are True will be subject
                to modification.
        """
        self._apply_modification_method("multiply_", key, value, where)

    def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Divide the element(s) addressed to by the given key(s).

        Args:
            key: The index/indices tensor.
            value: The value(s) that will be used as the divisor(s) on the
                existing element(s).
            where: Optionally a boolean mask. When provided, only the elements
                whose corresponding mask value(s) is/are True will be subject
                to modification.
        """
        self._apply_modification_method("divide_", key, value, where)

    def append_(self, value: Numbers, where: Optional[Numbers] = None):
        """
        Add new item(s) to the end(s) of the list(s).

        The length(s) of the updated list(s) will increase by 1.

        Args:
            value: The element that will be added to the list.
                In the non-batched case, this element is expected as a tensor
                whose shape matches `value_shape`.
                In the batched case, this value is expected as a batch of
                elements with extra leftmost dimensions (those extra leftmost
                dimensions being expressed by `batch_shape`).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then additions will happen only
                on the lists whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        self._move_end_forward(where)
        self.set_(-1, value, where=where)

    def push_(self, value: Numbers, where: Optional[Numbers] = None):
        """
        Alias for the method `append_(...)`.
        We provide this alternative name so that users who wish to use this
        CList structure like a stack will be able to use familiar terminology.
        """
        return self.append_(value, where=where)

    def appendleft_(self, value: Numbers, where: Optional[Numbers] = None):
        """
        Add new item(s) to the beginning point(s) of the list(s).

        The length(s) of the updated list(s) will increase by 1.

        Args:
            value: The element that will be added to the list.
                In the non-batched case, this element is expected as a tensor
                whose shape matches `value_shape`.
                In the batched case, this value is expected as a batch of
                elements with extra leftmost dimensions (those extra leftmost
                dimensions being expressed by `batch_shape`).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then additions will happen only
                on the lists whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        self._move_begin_backward(where)
        self.set_(0, value, where=where)

    def pop_(self, where: Optional[Numbers] = None):
        """
        Pop the last item(s) from the ending point(s) list(s).

        The length(s) of the updated list(s) will decrease by 1.

        Args:
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then the pop operations will happen
                only on the lists whose corresponding mask values are True.
        Returns:
            The popped item(s).
        """
        where = None if where is None else self._get_where(where)
        result = self.get(-1, default=self._pop_fallback)
        self._move_end_backward(where)
        return result

    def popleft_(self, where: Optional[Numbers] = None):
        """
        Pop the last item(s) from the beginning point(s) list(s).

        The length(s) of the updated list(s) will decrease by 1.

        Args:
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then the pop operations will happen
                only on the lists whose corresponding mask values are True.
        Returns:
            The popped item(s).
        """
        where = None if where is None else self._get_where(where)
        result = self.get(0, default=self._pop_fallback)
        self._move_begin_forward(where)
        return result

    def clear(self, where: Optional[torch.Tensor] = None):
        """
        Clear the list(s).

        In the context of this data structure, to "clear" means to reduce their
        lengths to 0.

        Args:
            where: Optionally a boolean tensor, specifying which lists within
                the batch will be cleared. If this argument is omitted (i.e.
                left as None), then all of the lists will be cleared.
        """
        if where is None:
            self._begin.data[:] = -1
            self._end.data[:] = -1
        else:
            where = self._get_where(where)
            all_minus_ones = torch.tensor(-1, dtype=torch.int64, device=self._begin.device).expand(self._begin.shape)
            self._begin.data[:] = do_where(where, all_minus_ones, self._begin.data)
            self._end.data[:] = do_where(where, all_minus_ones, self._end.data)

    @property
    def data(self) -> torch.Tensor:
        """
        The underlying tensor which stores all the data
        """
        return self._data.data

    @property
    def length(self) -> torch.Tensor:
        """
        The length(s) of the list(s)
        """
        is_empty = self._is_empty()
        is_full = self._is_full()
        result = ((self._end[self._all_zeros] - self._begin[self._all_zeros]) % self._max_length) + 1
        result[is_empty] = 0
        result[is_full] = self._max_length
        return result

    @property
    def max_length(self) -> int:
        """
        Maximum length for the list(s)
        """
        return self._max_length

data property

The underlying tensor which stores all the data

length property

The length(s) of the list(s)

max_length property

Maximum length for the list(s)

__getitem__(key)

Get the value(s) from the specified element(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices pointing to the element(s) whose value(s) is/are queried.

required
Source code in evotorch/tools/structures.py
def __getitem__(self, key: Numbers) -> torch.Tensor:
    """
    Get the value(s) from the specified element(s).

    Args:
        key: The index/indices pointing to the element(s) whose value(s)
            is/are queried.
    Returns:
        The value(s) stored by the element(s).
    """
    return self.get(key)

__setitem__(key, value)

Set the element(s) addressed to by the given key(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The new value(s).

required
Source code in evotorch/tools/structures.py
def __setitem__(self, key: Numbers, value: Numbers):
    """
    Set the element(s) addressed to by the given key(s).

    Args:
        key: The index/indices tensor.
        value: The new value(s).
    """
    self._apply_modification_method("set_", key, value)

add_(key, value, where=None)

Add to the element(s) addressed to by the given key(s).

Please note that the word "add" is used in the arithmetic sense (i.e. in the sense of performing addition). For putting a new element into this list, please see the method append_(...).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The value(s) that will be added onto the existing element(s).

required
where Optional[Numbers]

Optionally a boolean mask. When provided, only the elements whose corresponding mask value(s) is/are True will be subject to modification.

None
Source code in evotorch/tools/structures.py
def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Add to the element(s) addressed to by the given key(s).

    Please note that the word "add" is used in the arithmetic sense
    (i.e. in the sense of performing addition). For putting a new
    element into this list, please see the method `append_(...)`.

    Args:
        key: The index/indices tensor.
        value: The value(s) that will be added onto the existing
            element(s).
        where: Optionally a boolean mask. When provided, only the elements
            whose corresponding mask value(s) is/are True will be subject
            to modification.
    """
    self._apply_modification_method("add_", key, value, where)

append_(value, where=None)

Add new item(s) to the end(s) of the list(s).

The length(s) of the updated list(s) will increase by 1.

Parameters:

Name Type Description Default
value Numbers

The element that will be added to the list. In the non-batched case, this element is expected as a tensor whose shape matches value_shape. In the batched case, this value is expected as a batch of elements with extra leftmost dimensions (those extra leftmost dimensions being expressed by batch_shape).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then additions will happen only on the lists whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def append_(self, value: Numbers, where: Optional[Numbers] = None):
    """
    Add new item(s) to the end(s) of the list(s).

    The length(s) of the updated list(s) will increase by 1.

    Args:
        value: The element that will be added to the list.
            In the non-batched case, this element is expected as a tensor
            whose shape matches `value_shape`.
            In the batched case, this value is expected as a batch of
            elements with extra leftmost dimensions (those extra leftmost
            dimensions being expressed by `batch_shape`).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then additions will happen only
            on the lists whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    self._move_end_forward(where)
    self.set_(-1, value, where=where)

appendleft_(value, where=None)

Add new item(s) to the beginning point(s) of the list(s).

The length(s) of the updated list(s) will increase by 1.

Parameters:

Name Type Description Default
value Numbers

The element that will be added to the list. In the non-batched case, this element is expected as a tensor whose shape matches value_shape. In the batched case, this value is expected as a batch of elements with extra leftmost dimensions (those extra leftmost dimensions being expressed by batch_shape).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then additions will happen only on the lists whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def appendleft_(self, value: Numbers, where: Optional[Numbers] = None):
    """
    Add new item(s) to the beginning point(s) of the list(s).

    The length(s) of the updated list(s) will increase by 1.

    Args:
        value: The element that will be added to the list.
            In the non-batched case, this element is expected as a tensor
            whose shape matches `value_shape`.
            In the batched case, this value is expected as a batch of
            elements with extra leftmost dimensions (those extra leftmost
            dimensions being expressed by `batch_shape`).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then additions will happen only
            on the lists whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    self._move_begin_backward(where)
    self.set_(0, value, where=where)

clear(where=None)

Clear the list(s).

In the context of this data structure, to "clear" means to reduce their lengths to 0.

Parameters:

Name Type Description Default
where Optional[Tensor]

Optionally a boolean tensor, specifying which lists within the batch will be cleared. If this argument is omitted (i.e. left as None), then all of the lists will be cleared.

None
Source code in evotorch/tools/structures.py
def clear(self, where: Optional[torch.Tensor] = None):
    """
    Clear the list(s).

    In the context of this data structure, to "clear" means to reduce their
    lengths to 0.

    Args:
        where: Optionally a boolean tensor, specifying which lists within
            the batch will be cleared. If this argument is omitted (i.e.
            left as None), then all of the lists will be cleared.
    """
    if where is None:
        self._begin.data[:] = -1
        self._end.data[:] = -1
    else:
        where = self._get_where(where)
        all_minus_ones = torch.tensor(-1, dtype=torch.int64, device=self._begin.device).expand(self._begin.shape)
        self._begin.data[:] = do_where(where, all_minus_ones, self._begin.data)
        self._end.data[:] = do_where(where, all_minus_ones, self._end.data)

divide_(key, value, where=None)

Divide the element(s) addressed to by the given key(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The value(s) that will be used as the divisor(s) on the existing element(s).

required
where Optional[Numbers]

Optionally a boolean mask. When provided, only the elements whose corresponding mask value(s) is/are True will be subject to modification.

None
Source code in evotorch/tools/structures.py
def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Divide the element(s) addressed to by the given key(s).

    Args:
        key: The index/indices tensor.
        value: The value(s) that will be used as the divisor(s) on the
            existing element(s).
        where: Optionally a boolean mask. When provided, only the elements
            whose corresponding mask value(s) is/are True will be subject
            to modification.
    """
    self._apply_modification_method("divide_", key, value, where)

get(key, default=None)

Get the value(s) from the specified element(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices pointing to the element(s) whose value(s) is/are queried.

required
default Optional[Numbers]

Default value(s) to be returned for when the specified index/indices are invalid and/or out of range.

None
Source code in evotorch/tools/structures.py
def get(self, key: Numbers, default: Optional[Numbers] = None) -> torch.Tensor:
    """
    Get the value(s) from the specified element(s).

    Args:
        key: The index/indices pointing to the element(s) whose value(s)
            is/are queried.
        default: Default value(s) to be returned for when the specified
            index/indices are invalid and/or out of range.
    Returns:
        The value(s) stored by the element(s).
    """
    if default is None:
        underlying_key = self._get_underlying_key(key)
        return self._data[underlying_key]
    else:
        default = self._data._get_value(default)
        underlying_key, valid_key = self._get_underlying_key(key, verify=False, return_validity=True)
        return do_where(valid_key, self._data[underlying_key % self._max_length], default)

multiply_(key, value, where=None)

Multiply the element(s) addressed to by the given key(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The value(s) that will be used as the multiplier(s) on the existing element(s).

required
where Optional[Numbers]

Optionally a boolean mask. When provided, only the elements whose corresponding mask value(s) is/are True will be subject to modification.

None
Source code in evotorch/tools/structures.py
def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Multiply the element(s) addressed to by the given key(s).

    Args:
        key: The index/indices tensor.
        value: The value(s) that will be used as the multiplier(s) on the
            existing element(s).
        where: Optionally a boolean mask. When provided, only the elements
            whose corresponding mask value(s) is/are True will be subject
            to modification.
    """
    self._apply_modification_method("multiply_", key, value, where)

pop_(where=None)

Pop the last item(s) from the ending point(s) list(s).

The length(s) of the updated list(s) will decrease by 1.

Parameters:

Name Type Description Default
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then the pop operations will happen only on the lists whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def pop_(self, where: Optional[Numbers] = None):
    """
    Pop the last item(s) from the ending point(s) list(s).

    The length(s) of the updated list(s) will decrease by 1.

    Args:
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then the pop operations will happen
            only on the lists whose corresponding mask values are True.
    Returns:
        The popped item(s).
    """
    where = None if where is None else self._get_where(where)
    result = self.get(-1, default=self._pop_fallback)
    self._move_end_backward(where)
    return result

popleft_(where=None)

Pop the last item(s) from the beginning point(s) list(s).

The length(s) of the updated list(s) will decrease by 1.

Parameters:

Name Type Description Default
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then the pop operations will happen only on the lists whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def popleft_(self, where: Optional[Numbers] = None):
    """
    Pop the last item(s) from the beginning point(s) list(s).

    The length(s) of the updated list(s) will decrease by 1.

    Args:
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then the pop operations will happen
            only on the lists whose corresponding mask values are True.
    Returns:
        The popped item(s).
    """
    where = None if where is None else self._get_where(where)
    result = self.get(0, default=self._pop_fallback)
    self._move_begin_forward(where)
    return result

push_(value, where=None)

Alias for the method append_(...). We provide this alternative name so that users who wish to use this CList structure like a stack will be able to use familiar terminology.

Source code in evotorch/tools/structures.py
def push_(self, value: Numbers, where: Optional[Numbers] = None):
    """
    Alias for the method `append_(...)`.
    We provide this alternative name so that users who wish to use this
    CList structure like a stack will be able to use familiar terminology.
    """
    return self.append_(value, where=where)

set_(key, value, where=None)

Set the element(s) addressed to by the given key(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The new value(s).

required
where Optional[Numbers]

Optionally a boolean mask. When provided, only the elements whose corresponding mask value(s) is/are True will be subject to modification.

None
Source code in evotorch/tools/structures.py
def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Set the element(s) addressed to by the given key(s).

    Args:
        key: The index/indices tensor.
        value: The new value(s).
        where: Optionally a boolean mask. When provided, only the elements
            whose corresponding mask value(s) is/are True will be subject
            to modification.
    """
    self._apply_modification_method("set_", key, value, where)

subtract_(key, value, where=None)

Subtract from the element(s) addressed to by the given key(s).

Parameters:

Name Type Description Default
key Numbers

The index/indices tensor.

required
value Numbers

The value(s) that will be subtracted from the existing element(s).

required
where Optional[Numbers]

Optionally a boolean mask. When provided, only the elements whose corresponding mask value(s) is/are True will be subject to modification.

None
Source code in evotorch/tools/structures.py
def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Subtract from the element(s) addressed to by the given key(s).

    Args:
        key: The index/indices tensor.
        value: The value(s) that will be subtracted from the existing
            element(s).
        where: Optionally a boolean mask. When provided, only the elements
            whose corresponding mask value(s) is/are True will be subject
            to modification.
    """
    self._apply_modification_method("subtract_", key, value, where)

CMemory

Representation of a batchable contiguous memory.

This container can be seen as a batchable primitive dictionary where the keys are allowed either as integers or as tuples of integers. Please also note that, a memory block for each key is already allocated, meaning that unlike a dictionary of Python, each key already exists and is associated with a tensor.

Let us consider an example where we have 5 keys, and each key is associated with a tensor of length 7. Such a memory could be allocated like this:

memory = CMemory(7, num_keys=5)

Our allocated memory can be visualized as follows:

 _______________________________________
| key 0 -> [ empty tensor of length 7 ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

Let us now sample a Gaussian noise and put it into the 0-th slot:

memory[0] = torch.randn(7)  # or: memory[torch.tensor(0)] = torch.randn(7)

which results in:

 _________________________________________
| key 0 -> [ Gaussian noise of length 7 ] |
| key 1 -> [ empty tensor of length 7   ] |
| key 2 -> [ empty tensor of length 7   ] |
| key 3 -> [ empty tensor of length 7   ] |
| key 4 -> [ empty tensor of length 7   ] |
|_________________________________________|

Let us now consider another example where we deal with not a single CMemory, but with a CMemory batch. For the sake of this example, let us say that our desired batch size is 3. The allocation of such a batch would be as follows:

memory_batch = CMemory(7, num_keys=5, batch_size=3)

Our memory batch can be visualized like this:

 __[ batch item 0 ]_____________________
| key 0 -> [ empty tensor of length 7 ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ empty tensor of length 7 ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ empty tensor of length 7 ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

If we wish to set the 0-th element of each batch item, we could do:

memory_batch[0] = torch.tensor(
    [
        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
        [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
    ],
)

and the result would be:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

Continuing from the same example, if we wish to set the slot with key 1 in the 0th batch item, slot with key 2 in the 1st batch item, and slot with key 3 in the 2nd batch item, all in one go, we could do:

# Longer version: memory_batch[torch.tensor([1, 2, 3])] = ...

memory_batch[[1, 2, 3]] = torch.tensor(
    [
        [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
        [6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0],
        [7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0],
    ],
)

Our updated memory batch would then look like this:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

Conditional modifications via boolean masks is also supported. For example, the following update on our memory_batch:

memory_batch.set_(
    [4, 3, 1],
    torch.tensor(
        [
            [8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0],
            [9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0],
            [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
        ]
    ),
    where=[True, True, False],  # or: where=torch.tensor([True,True,False]),
)

would result in:

 __[ batch item 0 ]_____________________
| key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
| key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ empty tensor of length 7 ] |
| key 4 -> [ 8. 8. 8. 8. 8. 8. 8.     ] |
|_______________________________________|

 __[ batch item 1 ]_____________________
| key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
| key 3 -> [ 9. 9. 9. 9. 9. 9. 9.     ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

 __[ batch item 2 ]_____________________
| key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
| key 1 -> [ empty tensor of length 7 ] |
| key 2 -> [ empty tensor of length 7 ] |
| key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
| key 4 -> [ empty tensor of length 7 ] |
|_______________________________________|

Please notice above that the slot with key 1 of the batch item 2 was not modified because its corresponding mask value was given as False.

Source code in evotorch/tools/structures.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
class CMemory:
    """
    Representation of a batchable contiguous memory.

    This container can be seen as a batchable primitive dictionary where the
    keys are allowed either as integers or as tuples of integers. Please also
    note that, a memory block for each key is already allocated, meaning that
    unlike a dictionary of Python, each key already exists and is associated
    with a tensor.

    Let us consider an example where we have 5 keys, and each key is associated
    with a tensor of length 7. Such a memory could be allocated like this:

    ```python
    memory = CMemory(7, num_keys=5)
    ```

    Our allocated memory can be visualized as follows:

    ```text
     _______________________________________
    | key 0 -> [ empty tensor of length 7 ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|
    ```

    Let us now sample a Gaussian noise and put it into the 0-th slot:

    ```python
    memory[0] = torch.randn(7)  # or: memory[torch.tensor(0)] = torch.randn(7)
    ```

    which results in:

    ```text
     _________________________________________
    | key 0 -> [ Gaussian noise of length 7 ] |
    | key 1 -> [ empty tensor of length 7   ] |
    | key 2 -> [ empty tensor of length 7   ] |
    | key 3 -> [ empty tensor of length 7   ] |
    | key 4 -> [ empty tensor of length 7   ] |
    |_________________________________________|
    ```

    Let us now consider another example where we deal with not a single CMemory,
    but with a CMemory batch. For the sake of this example, let us say that our
    desired batch size is 3. The allocation of such a batch would be as
    follows:

    ```python
    memory_batch = CMemory(7, num_keys=5, batch_size=3)
    ```

    Our memory batch can be visualized like this:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ empty tensor of length 7 ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ empty tensor of length 7 ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ empty tensor of length 7 ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|
    ```

    If we wish to set the 0-th element of each batch item, we could do:

    ```python
    memory_batch[0] = torch.tensor(
        [
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
        ],
    )
    ```

    and the result would be:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|
    ```

    Continuing from the same example, if we wish to set the slot with key 1
    in the 0th batch item, slot with key 2 in the 1st batch item, and
    slot with key 3 in the 2nd batch item, all in one go, we could do:

    ```python
    # Longer version: memory_batch[torch.tensor([1, 2, 3])] = ...

    memory_batch[[1, 2, 3]] = torch.tensor(
        [
            [5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
            [6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0],
            [7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0],
        ],
    )
    ```

    Our updated memory batch would then look like this:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|
    ```

    Conditional modifications via boolean masks is also supported.
    For example, the following update on our `memory_batch`:

    ```python
    memory_batch.set_(
        [4, 3, 1],
        torch.tensor(
            [
                [8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0],
                [9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0],
                [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
            ]
        ),
        where=[True, True, False],  # or: where=torch.tensor([True,True,False]),
    )
    ```

    would result in:

    ```text
     __[ batch item 0 ]_____________________
    | key 0 -> [ 0. 0. 0. 0. 0. 0. 0.     ] |
    | key 1 -> [ 5. 5. 5. 5. 5. 5. 5.     ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ empty tensor of length 7 ] |
    | key 4 -> [ 8. 8. 8. 8. 8. 8. 8.     ] |
    |_______________________________________|

     __[ batch item 1 ]_____________________
    | key 0 -> [ 1. 1. 1. 1. 1. 1. 1.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ 6. 6. 6. 6. 6. 6. 6.     ] |
    | key 3 -> [ 9. 9. 9. 9. 9. 9. 9.     ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|

     __[ batch item 2 ]_____________________
    | key 0 -> [ 2. 2. 2. 2. 2. 2. 2.     ] |
    | key 1 -> [ empty tensor of length 7 ] |
    | key 2 -> [ empty tensor of length 7 ] |
    | key 3 -> [ 7. 7. 7. 7. 7. 7. 7.     ] |
    | key 4 -> [ empty tensor of length 7 ] |
    |_______________________________________|
    ```

    Please notice above that the slot with key 1 of the batch item 2 was not
    modified because its corresponding mask value was given as False.
    """

    def __init__(
        self,
        *size: Union[int, tuple, list],
        num_keys: Union[int, tuple, list],
        key_offset: Optional[Union[int, tuple, list]] = None,
        batch_size: Optional[Union[int, tuple, list]] = None,
        batch_shape: Optional[Union[int, tuple, list]] = None,
        fill_with: Optional[Numbers] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        verify: bool = True,
    ):
        """
        `__init__(...)`: Initialize the CMemory.

        Args:
            size: Size of a tensor associated with a key, expected as an
                integer, or as multiple positional arguments (each positional
                argument being an integer), or as a tuple of integers.
            num_keys: How many keys (and therefore how many slots) will the
                memory have. If given as an integer `n`, then there will be `n`
                slots in the memory, and to access a slot one will need to use
                an integer key `k` (where, by default, the minimum acceptable
                `k` is 0 and the maximum acceptable `k` is `n-1`).
                If given as a tuple of integers, then the number of slots in
                the memory will be computed as the product of all the integers
                in the tuple, and a key will be expected as a tuple.
                For example, when `num_keys` is `(3, 5)`, there will be 15
                slots in the memory (where, by default, the minimum acceptable
                key will be `(0, 0)` and the maximum acceptable key will be
                `(2, 4)`.
            key_offset: Optionally can be used to shift the integer values of
                the keys. For example, if `num_keys` is 10, then, by default,
                the minimum key is 0 and the maximum key is 9. But together
                with `num_keys=10`, if `key_offset` is given as 1, then the
                minimum key will be 1 and the maximum key will be 10.
                This argument can also be used together with a tuple-valued
                `num_keys`. For example, with `num_keys` set as `(3, 5)`,
                if `key_offset` is given as 1, then the minimum key value
                will be `(1, 1)` (instead of `(0, 0)`) and the maximum key
                value will be `(3, 5)` (instead of `(2, 4)`).
                Also, with a tuple-valued `num_keys`, `key_offset` can be
                given as a tuple, to shift the key values differently for each
                item in the tuple.
            batch_size: If given as None, then this memory will not be batched.
                If given as an integer `n`, then this object will represent
                a contiguous batch containing `n` memory blocks.
                If given as a tuple `(size0, size1, ...)`, then this object
                will represent a contiguous batch of memory, shape of this
                batch being determined by the given tuple.
            batch_shape: Alias for the argument `batch_size`.
            fill_with: Optionally a numeric value using which the values will
                be initialized. If no initialization is needed, then this
                argument can be left as None.
            dtype: The `dtype` of the memory tensor.
            device: The device on which the memory will be allocated.
            verify: If True, then explicit checks will be done to verify
                that there are no indexing errors. Can be set as False for
                performance.
        """
        self._dtype = torch.float32 if dtype is None else to_torch_dtype(dtype)
        self._device = torch.device("cpu") if device is None else torch.device(device)
        self._verify = bool(verify)

        if isinstance(num_keys, (list, tuple)):
            if len(num_keys) < 2:
                raise RuntimeError(
                    f"When expressed via a list or a tuple, the length of `num_keys` must be at least 2."
                    f" However, the encountered `num_keys` is {repr(num_keys)}, whose length is {len(num_keys)}."
                )
            self._multi_key = True
            self._num_keys = tuple((int(n) for n in num_keys))
            self._internal_key_shape = torch.Size(self._num_keys)
        else:
            self._multi_key = False
            self._num_keys = int(num_keys)
            self._internal_key_shape = torch.Size([self._num_keys])
        self._internal_key_ndim = len(self._internal_key_shape)

        if key_offset is None:
            self._key_offset = None
        else:
            if self._multi_key:
                if isinstance(key_offset, (list, tuple)):
                    key_offset = [int(n) for n in key_offset]
                    if len(key_offset) != len(self._num_keys):
                        raise RuntimeError("The length of `key_offset` does not match the length of `num_keys`")
                else:
                    key_offset = [int(key_offset) for _ in range(len(self._num_keys))]
                self._key_offset = torch.as_tensor(key_offset, dtype=torch.int64, device=self._device)
            else:
                if isinstance(key_offset, (list, tuple)):
                    raise RuntimeError("`key_offset` cannot be a sequence of integers when `num_keys` is a scalar")
                else:
                    self._key_offset = torch.as_tensor(int(key_offset), dtype=torch.int64, device=self._device)

        if self._verify:
            if self._multi_key:
                self._min_key = torch.zeros(len(self._num_keys), dtype=torch.int64, device=self._device)
                self._max_key = torch.tensor(list(self._num_keys), dtype=torch.int64, device=self._device) - 1
            else:
                self._min_key = torch.tensor(0, dtype=torch.int64, device=self._device)
                self._max_key = torch.tensor(self._num_keys - 1, dtype=torch.int64, device=self._device)
            if self._key_offset is not None:
                self._min_key += self._key_offset
                self._max_key += self._key_offset
        else:
            self._min_key = None
            self._max_key = None

        nsize = len(size)
        if nsize == 0:
            self._value_shape = torch.Size([])
        elif nsize == 1:
            if isinstance(size[0], (tuple, list)):
                self._value_shape = torch.Size((int(n) for n in size[0]))
            else:
                self._value_shape = torch.Size([int(size[0])])
        else:
            self._value_shape = torch.Size((int(n) for n in size))
        self._value_ndim = len(self._value_shape)

        if (batch_size is None) and (batch_shape is None):
            batch_size = None
        elif (batch_size is not None) and (batch_shape is None):
            pass
        elif (batch_size is None) and (batch_shape is not None):
            batch_size = batch_shape
        else:
            raise RuntimeError(
                "Encountered both `batch_shape` and `batch_size` at the same time."
                " None of them or one of them can be accepted, but not both of them at the same time."
            )

        if batch_size is None:
            self._batch_shape = torch.Size([])
        elif isinstance(batch_size, (tuple, list)):
            self._batch_shape = torch.Size((int(n) for n in batch_size))
        else:
            self._batch_shape = torch.Size([int(batch_size)])
        self._batch_ndim = len(self._batch_shape)

        self._for_all_batches = tuple(
            (
                torch.arange(self._batch_shape[i], dtype=torch.int64, device=self._device)
                for i in range(self._batch_ndim)
            )
        )

        self._data = torch.empty(
            self._batch_shape + self._internal_key_shape + self._value_shape,
            dtype=(self._dtype),
            device=(self._device),
        )

        if fill_with is not None:
            self._data[:] = fill_with

    @property
    def _is_dtype_bool(self) -> bool:
        return self._data.dtype is torch.bool

    def _key_must_be_valid(self, key: torch.Tensor) -> torch.Tensor:
        lb_satisfied = key >= self._min_key
        ub_satisfied = key <= self._max_key
        all_satisfied = lb_satisfied & ub_satisfied
        if not torch.all(all_satisfied):
            raise KeyError("Encountered invalid key(s)")

    def _get_key(self, key: Numbers, where: Optional[torch.Tensor] = None) -> torch.Tensor:
        key = torch.as_tensor(key, dtype=torch.int64, device=self._data.device)
        expected_shape = self.batch_shape + self.key_shape
        if key.shape == expected_shape:
            result = key
        elif key.shape == self.key_shape:
            result = key.expand(expected_shape)
        else:
            raise RuntimeError(f"The key tensor has an incompatible shape: {key.shape}")
        if where is not None:
            min_key = (
                torch.tensor(0, dtype=torch.int64, device=self._data.device) if self._min_key is None else self._min_key
            )
            key = do_where(where, key, min_key.expand(expected_shape))
        if self._verify:
            self._key_must_be_valid(key)

        return result

    def _get_value(self, value: Numbers) -> torch.Tensor:
        value = torch.as_tensor(value, dtype=self._data.dtype, device=self._data.device)
        expected_shape = self.batch_shape + self.value_shape
        if value.shape == expected_shape:
            return value
        elif (value.ndim == 0) or (value.shape == self.value_shape):
            return value.expand(expected_shape)
        else:
            raise RuntimeError(f"The value tensor has an incompatible shape: {value.shape}")
        return value

    def _get_where(self, where: Numbers) -> torch.Tensor:
        where = torch.as_tensor(where, dtype=torch.bool, device=self._data.device)
        if where.shape != self.batch_shape:
            raise RuntimeError(
                f"The boolean mask `where` has an incompatible shape: {where.shape}."
                f" Acceptable shape is: {self.batch_shape}"
            )
        return where

    def prepare_key_tensor(self, key: Numbers) -> torch.Tensor:
        """
        Return the tensor-counterpart of a key.

        Args:
            key: A key which can be a sequence of integers or a PyTorch tensor
                with an integer dtype.
                The shape of the given key must conform with the `key_shape`
                of this memory object.
                To address to a different key in each batch item, the shape of
                the given key can also have extra leftmost dimensions expressed
                by `batch_shape`.
        Returns:
            A copy of the key that is converted to PyTorch tensor.
        """
        return self._get_key(key)

    def prepare_value_tensor(self, value: Numbers) -> torch.Tensor:
        """
        Return the tensor-counterpart of a value.

        Args:
            value: A value that can be a numeric sequence or a PyTorch tensor.
                The shape of the given value must conform with the
                `value_shape` of this memory object.
                To express a different value for each batch item, the shape of
                the given value can also have extra leftmost dimensions
                expressed by `value_shape`.
        Returns:
            A copy of the given value(s), converted to PyTorch tensor.
        """
        return self._get_value(value)

    def prepare_where_tensor(self, where: Numbers) -> torch.Tensor:
        """
        Return the tensor-counterpart of a boolean mask.

        Args:
            where: A boolean mask expressed as a sequence of bools or as a
                boolean PyTorch tensor.
                The shape of the given mask must conform with the batch shape
                that is expressed by the property `batch_shape`.
        Returns:
            A copy of the boolean mask, converted to PyTorch tensor.
        """
        return self._get_where(where)

    def _get_address(self, key: Numbers, where: Optional[torch.Tensor] = None) -> tuple:
        key = self._get_key(key, where=where)
        if self._key_offset is not None:
            key = key - self._key_offset
        if self._multi_key:
            keys = tuple((key[..., j] for j in range(self._internal_key_ndim)))
            return self._for_all_batches + keys
        else:
            return self._for_all_batches + (key,)

    def get(self, key: Numbers) -> torch.Tensor:
        """
        Get the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
        Returns:
            The value(s) associated with the given key(s).
        """
        address = self._get_address(key)
        return self._data[address]

    def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Set the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The new value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        address = self._get_address(key, where=where)
        value = self._get_value(value)

        if where is None:
            self._data[address] = value
        else:
            old_value = self._data[address]
            new_value = value
            self._data[address] = do_where(where, new_value, old_value)

    def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Add value(s) onto the existing values of slots with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be added onto the existing value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        address = self._get_address(key, where=where)
        value = self._get_value(value)

        if where is None:
            if self._is_dtype_bool:
                self._data[address] |= value
            else:
                self._data[address] += value
        else:
            if self._is_dtype_bool:
                mask_shape = self._batch_shape + tuple((1 for _ in range(self._value_ndim)))
                self._data[address] |= value & where.reshape(mask_shape)
            else:
                self._data[address] += do_where(where, value, torch.tensor(0, dtype=value.dtype, device=value.device))

    def add_circular_(self, key: Numbers, value: Numbers, mod: Numbers, where: Optional[Numbers] = None):
        """
        Increase the values of the specified slots in a circular manner.

        This operation combines the add and modulo operations.
        Circularly adding `value` onto `x` with a modulo `mod` means:
        `x = (x + value) % mod`.

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be added onto the existing value(s).
            mod: After the raw adding operation, the modulos according to this
                `mod` argument will be computed and placed.
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        address = self._get_address(key, where=where)
        value = self._get_value(value)
        mod = self._get_value(mod)

        if self._is_dtype_bool:
            raise ValueError("Circular addition is not supported for dtype `torch.bool`")

        if where is None:
            self._data[address] = (self._data[address] + value) % mod
        else:
            old_value = self._data[address]
            new_value = (old_value + value) % mod
            self._data[address] = do_where(where, new_value, old_value)

    def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Multiply the existing values of slots with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be used as the multiplier(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        where = None if where is None else self._get_where(where)
        address = self._get_address(key, where=where)
        value = self._get_value(value)

        if where is None:
            if self._is_dtype_bool:
                self._data[address] &= value
            else:
                self._data[address] += value
        else:
            if self._is_dtype_bool:
                self._data[address] &= do_where(
                    where, value, torch.tensor(True, dtype=value.dtype, device=value.device)
                )
            else:
                self._data[address] *= do_where(where, value, torch.tensor(1, dtype=value.dtype, device=value.device))

    def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Subtract value(s) from existing values of slots with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be subtracted from existing value(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self.add_(key, -value, where)

    def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
        """
        Divide the existing values of slots with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The value(s) that will be used as divisor(s).
            where: Optionally a boolean mask whose shape matches `batch_shape`.
                If a `where` mask is given, then modifications will happen only
                on the memory slots whose corresponding mask values are True.
        """
        self.multiply_(key, 1 / value, where)

    def __getitem__(self, key: Numbers) -> torch.Tensor:
        """
        Get the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
        Returns:
            The value(s) associated with the given key(s).
        """
        return self.get(key)

    def __setitem__(self, key: Numbers, value: Numbers):
        """
        Set the value(s) associated with the given key(s).

        Args:
            key: A single key, or multiple keys (where the leftmost dimension
                of the given keys conform with the `batch_shape`).
            value: The new value(s).
        """
        self.set_(key, value)

    @property
    def data(self) -> torch.Tensor:
        """
        The entire value tensor
        """
        return self._data

    @property
    def key_shape(self) -> torch.Size:
        """
        Shape of a key
        """
        return torch.Size([self._internal_key_ndim]) if self._multi_key else torch.Size([])

    @property
    def key_ndim(self) -> int:
        """
        Number of dimensions of a key
        """
        return 1 if self._multi_key else 0

    @property
    def batch_shape(self) -> torch.Size:
        """
        Batch size of this memory object
        """
        return self._batch_shape

    @property
    def batch_ndim(self) -> int:
        """
        Number of dimensions expressed by `batch_shape`
        """
        return self._batch_ndim

    @property
    def is_batched(self) -> bool:
        """
        True if this CMemory object is batched; False otherwise.
        """
        return self._batch_ndim > 0

    @property
    def value_shape(self) -> torch.Size:
        """
        Tensor shape of a single value
        """
        return self._value_shape

    @property
    def value_ndim(self) -> int:
        """
        Number of dimensions expressed by `value_shape`
        """
        return self._value_ndim

    @property
    def dtype(self) -> torch.dtype:
        """
        `dtype` of the value tensor
        """
        return self._data.dtype

    @property
    def device(self) -> torch.device:
        """
        The device on which this memory object lives
        """
        return self._data.device

batch_ndim property

Number of dimensions expressed by batch_shape

batch_shape property

Batch size of this memory object

data property

The entire value tensor

device property

The device on which this memory object lives

dtype property

dtype of the value tensor

is_batched property

True if this CMemory object is batched; False otherwise.

key_ndim property

Number of dimensions of a key

key_shape property

Shape of a key

value_ndim property

Number of dimensions expressed by value_shape

value_shape property

Tensor shape of a single value

__getitem__(key)

Get the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
Source code in evotorch/tools/structures.py
def __getitem__(self, key: Numbers) -> torch.Tensor:
    """
    Get the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
    Returns:
        The value(s) associated with the given key(s).
    """
    return self.get(key)

__init__(*size, num_keys, key_offset=None, batch_size=None, batch_shape=None, fill_with=None, dtype=None, device=None, verify=True)

__init__(...): Initialize the CMemory.

Parameters:

Name Type Description Default
size Union[int, tuple, list]

Size of a tensor associated with a key, expected as an integer, or as multiple positional arguments (each positional argument being an integer), or as a tuple of integers.

()
num_keys Union[int, tuple, list]

How many keys (and therefore how many slots) will the memory have. If given as an integer n, then there will be n slots in the memory, and to access a slot one will need to use an integer key k (where, by default, the minimum acceptable k is 0 and the maximum acceptable k is n-1). If given as a tuple of integers, then the number of slots in the memory will be computed as the product of all the integers in the tuple, and a key will be expected as a tuple. For example, when num_keys is (3, 5), there will be 15 slots in the memory (where, by default, the minimum acceptable key will be (0, 0) and the maximum acceptable key will be (2, 4).

required
key_offset Optional[Union[int, tuple, list]]

Optionally can be used to shift the integer values of the keys. For example, if num_keys is 10, then, by default, the minimum key is 0 and the maximum key is 9. But together with num_keys=10, if key_offset is given as 1, then the minimum key will be 1 and the maximum key will be 10. This argument can also be used together with a tuple-valued num_keys. For example, with num_keys set as (3, 5), if key_offset is given as 1, then the minimum key value will be (1, 1) (instead of (0, 0)) and the maximum key value will be (3, 5) (instead of (2, 4)). Also, with a tuple-valued num_keys, key_offset can be given as a tuple, to shift the key values differently for each item in the tuple.

None
batch_size Optional[Union[int, tuple, list]]

If given as None, then this memory will not be batched. If given as an integer n, then this object will represent a contiguous batch containing n memory blocks. If given as a tuple (size0, size1, ...), then this object will represent a contiguous batch of memory, shape of this batch being determined by the given tuple.

None
batch_shape Optional[Union[int, tuple, list]]

Alias for the argument batch_size.

None
fill_with Optional[Numbers]

Optionally a numeric value using which the values will be initialized. If no initialization is needed, then this argument can be left as None.

None
dtype Optional[DType]

The dtype of the memory tensor.

None
device Optional[Device]

The device on which the memory will be allocated.

None
verify bool

If True, then explicit checks will be done to verify that there are no indexing errors. Can be set as False for performance.

True
Source code in evotorch/tools/structures.py
def __init__(
    self,
    *size: Union[int, tuple, list],
    num_keys: Union[int, tuple, list],
    key_offset: Optional[Union[int, tuple, list]] = None,
    batch_size: Optional[Union[int, tuple, list]] = None,
    batch_shape: Optional[Union[int, tuple, list]] = None,
    fill_with: Optional[Numbers] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    verify: bool = True,
):
    """
    `__init__(...)`: Initialize the CMemory.

    Args:
        size: Size of a tensor associated with a key, expected as an
            integer, or as multiple positional arguments (each positional
            argument being an integer), or as a tuple of integers.
        num_keys: How many keys (and therefore how many slots) will the
            memory have. If given as an integer `n`, then there will be `n`
            slots in the memory, and to access a slot one will need to use
            an integer key `k` (where, by default, the minimum acceptable
            `k` is 0 and the maximum acceptable `k` is `n-1`).
            If given as a tuple of integers, then the number of slots in
            the memory will be computed as the product of all the integers
            in the tuple, and a key will be expected as a tuple.
            For example, when `num_keys` is `(3, 5)`, there will be 15
            slots in the memory (where, by default, the minimum acceptable
            key will be `(0, 0)` and the maximum acceptable key will be
            `(2, 4)`.
        key_offset: Optionally can be used to shift the integer values of
            the keys. For example, if `num_keys` is 10, then, by default,
            the minimum key is 0 and the maximum key is 9. But together
            with `num_keys=10`, if `key_offset` is given as 1, then the
            minimum key will be 1 and the maximum key will be 10.
            This argument can also be used together with a tuple-valued
            `num_keys`. For example, with `num_keys` set as `(3, 5)`,
            if `key_offset` is given as 1, then the minimum key value
            will be `(1, 1)` (instead of `(0, 0)`) and the maximum key
            value will be `(3, 5)` (instead of `(2, 4)`).
            Also, with a tuple-valued `num_keys`, `key_offset` can be
            given as a tuple, to shift the key values differently for each
            item in the tuple.
        batch_size: If given as None, then this memory will not be batched.
            If given as an integer `n`, then this object will represent
            a contiguous batch containing `n` memory blocks.
            If given as a tuple `(size0, size1, ...)`, then this object
            will represent a contiguous batch of memory, shape of this
            batch being determined by the given tuple.
        batch_shape: Alias for the argument `batch_size`.
        fill_with: Optionally a numeric value using which the values will
            be initialized. If no initialization is needed, then this
            argument can be left as None.
        dtype: The `dtype` of the memory tensor.
        device: The device on which the memory will be allocated.
        verify: If True, then explicit checks will be done to verify
            that there are no indexing errors. Can be set as False for
            performance.
    """
    self._dtype = torch.float32 if dtype is None else to_torch_dtype(dtype)
    self._device = torch.device("cpu") if device is None else torch.device(device)
    self._verify = bool(verify)

    if isinstance(num_keys, (list, tuple)):
        if len(num_keys) < 2:
            raise RuntimeError(
                f"When expressed via a list or a tuple, the length of `num_keys` must be at least 2."
                f" However, the encountered `num_keys` is {repr(num_keys)}, whose length is {len(num_keys)}."
            )
        self._multi_key = True
        self._num_keys = tuple((int(n) for n in num_keys))
        self._internal_key_shape = torch.Size(self._num_keys)
    else:
        self._multi_key = False
        self._num_keys = int(num_keys)
        self._internal_key_shape = torch.Size([self._num_keys])
    self._internal_key_ndim = len(self._internal_key_shape)

    if key_offset is None:
        self._key_offset = None
    else:
        if self._multi_key:
            if isinstance(key_offset, (list, tuple)):
                key_offset = [int(n) for n in key_offset]
                if len(key_offset) != len(self._num_keys):
                    raise RuntimeError("The length of `key_offset` does not match the length of `num_keys`")
            else:
                key_offset = [int(key_offset) for _ in range(len(self._num_keys))]
            self._key_offset = torch.as_tensor(key_offset, dtype=torch.int64, device=self._device)
        else:
            if isinstance(key_offset, (list, tuple)):
                raise RuntimeError("`key_offset` cannot be a sequence of integers when `num_keys` is a scalar")
            else:
                self._key_offset = torch.as_tensor(int(key_offset), dtype=torch.int64, device=self._device)

    if self._verify:
        if self._multi_key:
            self._min_key = torch.zeros(len(self._num_keys), dtype=torch.int64, device=self._device)
            self._max_key = torch.tensor(list(self._num_keys), dtype=torch.int64, device=self._device) - 1
        else:
            self._min_key = torch.tensor(0, dtype=torch.int64, device=self._device)
            self._max_key = torch.tensor(self._num_keys - 1, dtype=torch.int64, device=self._device)
        if self._key_offset is not None:
            self._min_key += self._key_offset
            self._max_key += self._key_offset
    else:
        self._min_key = None
        self._max_key = None

    nsize = len(size)
    if nsize == 0:
        self._value_shape = torch.Size([])
    elif nsize == 1:
        if isinstance(size[0], (tuple, list)):
            self._value_shape = torch.Size((int(n) for n in size[0]))
        else:
            self._value_shape = torch.Size([int(size[0])])
    else:
        self._value_shape = torch.Size((int(n) for n in size))
    self._value_ndim = len(self._value_shape)

    if (batch_size is None) and (batch_shape is None):
        batch_size = None
    elif (batch_size is not None) and (batch_shape is None):
        pass
    elif (batch_size is None) and (batch_shape is not None):
        batch_size = batch_shape
    else:
        raise RuntimeError(
            "Encountered both `batch_shape` and `batch_size` at the same time."
            " None of them or one of them can be accepted, but not both of them at the same time."
        )

    if batch_size is None:
        self._batch_shape = torch.Size([])
    elif isinstance(batch_size, (tuple, list)):
        self._batch_shape = torch.Size((int(n) for n in batch_size))
    else:
        self._batch_shape = torch.Size([int(batch_size)])
    self._batch_ndim = len(self._batch_shape)

    self._for_all_batches = tuple(
        (
            torch.arange(self._batch_shape[i], dtype=torch.int64, device=self._device)
            for i in range(self._batch_ndim)
        )
    )

    self._data = torch.empty(
        self._batch_shape + self._internal_key_shape + self._value_shape,
        dtype=(self._dtype),
        device=(self._device),
    )

    if fill_with is not None:
        self._data[:] = fill_with

__setitem__(key, value)

Set the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The new value(s).

required
Source code in evotorch/tools/structures.py
def __setitem__(self, key: Numbers, value: Numbers):
    """
    Set the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The new value(s).
    """
    self.set_(key, value)

add_(key, value, where=None)

Add value(s) onto the existing values of slots with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be added onto the existing value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def add_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Add value(s) onto the existing values of slots with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be added onto the existing value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    address = self._get_address(key, where=where)
    value = self._get_value(value)

    if where is None:
        if self._is_dtype_bool:
            self._data[address] |= value
        else:
            self._data[address] += value
    else:
        if self._is_dtype_bool:
            mask_shape = self._batch_shape + tuple((1 for _ in range(self._value_ndim)))
            self._data[address] |= value & where.reshape(mask_shape)
        else:
            self._data[address] += do_where(where, value, torch.tensor(0, dtype=value.dtype, device=value.device))

add_circular_(key, value, mod, where=None)

Increase the values of the specified slots in a circular manner.

This operation combines the add and modulo operations. Circularly adding value onto x with a modulo mod means: x = (x + value) % mod.

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be added onto the existing value(s).

required
mod Numbers

After the raw adding operation, the modulos according to this mod argument will be computed and placed.

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def add_circular_(self, key: Numbers, value: Numbers, mod: Numbers, where: Optional[Numbers] = None):
    """
    Increase the values of the specified slots in a circular manner.

    This operation combines the add and modulo operations.
    Circularly adding `value` onto `x` with a modulo `mod` means:
    `x = (x + value) % mod`.

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be added onto the existing value(s).
        mod: After the raw adding operation, the modulos according to this
            `mod` argument will be computed and placed.
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    address = self._get_address(key, where=where)
    value = self._get_value(value)
    mod = self._get_value(mod)

    if self._is_dtype_bool:
        raise ValueError("Circular addition is not supported for dtype `torch.bool`")

    if where is None:
        self._data[address] = (self._data[address] + value) % mod
    else:
        old_value = self._data[address]
        new_value = (old_value + value) % mod
        self._data[address] = do_where(where, new_value, old_value)

divide_(key, value, where=None)

Divide the existing values of slots with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be used as divisor(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def divide_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Divide the existing values of slots with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be used as divisor(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self.multiply_(key, 1 / value, where)

get(key)

Get the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
Source code in evotorch/tools/structures.py
def get(self, key: Numbers) -> torch.Tensor:
    """
    Get the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
    Returns:
        The value(s) associated with the given key(s).
    """
    address = self._get_address(key)
    return self._data[address]

multiply_(key, value, where=None)

Multiply the existing values of slots with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be used as the multiplier(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def multiply_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Multiply the existing values of slots with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be used as the multiplier(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    address = self._get_address(key, where=where)
    value = self._get_value(value)

    if where is None:
        if self._is_dtype_bool:
            self._data[address] &= value
        else:
            self._data[address] += value
    else:
        if self._is_dtype_bool:
            self._data[address] &= do_where(
                where, value, torch.tensor(True, dtype=value.dtype, device=value.device)
            )
        else:
            self._data[address] *= do_where(where, value, torch.tensor(1, dtype=value.dtype, device=value.device))

prepare_key_tensor(key)

Return the tensor-counterpart of a key.

Parameters:

Name Type Description Default
key Numbers

A key which can be a sequence of integers or a PyTorch tensor with an integer dtype. The shape of the given key must conform with the key_shape of this memory object. To address to a different key in each batch item, the shape of the given key can also have extra leftmost dimensions expressed by batch_shape.

required
Source code in evotorch/tools/structures.py
def prepare_key_tensor(self, key: Numbers) -> torch.Tensor:
    """
    Return the tensor-counterpart of a key.

    Args:
        key: A key which can be a sequence of integers or a PyTorch tensor
            with an integer dtype.
            The shape of the given key must conform with the `key_shape`
            of this memory object.
            To address to a different key in each batch item, the shape of
            the given key can also have extra leftmost dimensions expressed
            by `batch_shape`.
    Returns:
        A copy of the key that is converted to PyTorch tensor.
    """
    return self._get_key(key)

prepare_value_tensor(value)

Return the tensor-counterpart of a value.

Parameters:

Name Type Description Default
value Numbers

A value that can be a numeric sequence or a PyTorch tensor. The shape of the given value must conform with the value_shape of this memory object. To express a different value for each batch item, the shape of the given value can also have extra leftmost dimensions expressed by value_shape.

required
Source code in evotorch/tools/structures.py
def prepare_value_tensor(self, value: Numbers) -> torch.Tensor:
    """
    Return the tensor-counterpart of a value.

    Args:
        value: A value that can be a numeric sequence or a PyTorch tensor.
            The shape of the given value must conform with the
            `value_shape` of this memory object.
            To express a different value for each batch item, the shape of
            the given value can also have extra leftmost dimensions
            expressed by `value_shape`.
    Returns:
        A copy of the given value(s), converted to PyTorch tensor.
    """
    return self._get_value(value)

prepare_where_tensor(where)

Return the tensor-counterpart of a boolean mask.

Parameters:

Name Type Description Default
where Numbers

A boolean mask expressed as a sequence of bools or as a boolean PyTorch tensor. The shape of the given mask must conform with the batch shape that is expressed by the property batch_shape.

required
Source code in evotorch/tools/structures.py
def prepare_where_tensor(self, where: Numbers) -> torch.Tensor:
    """
    Return the tensor-counterpart of a boolean mask.

    Args:
        where: A boolean mask expressed as a sequence of bools or as a
            boolean PyTorch tensor.
            The shape of the given mask must conform with the batch shape
            that is expressed by the property `batch_shape`.
    Returns:
        A copy of the boolean mask, converted to PyTorch tensor.
    """
    return self._get_where(where)

set_(key, value, where=None)

Set the value(s) associated with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The new value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def set_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Set the value(s) associated with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The new value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    where = None if where is None else self._get_where(where)
    address = self._get_address(key, where=where)
    value = self._get_value(value)

    if where is None:
        self._data[address] = value
    else:
        old_value = self._data[address]
        new_value = value
        self._data[address] = do_where(where, new_value, old_value)

subtract_(key, value, where=None)

Subtract value(s) from existing values of slots with the given key(s).

Parameters:

Name Type Description Default
key Numbers

A single key, or multiple keys (where the leftmost dimension of the given keys conform with the batch_shape).

required
value Numbers

The value(s) that will be subtracted from existing value(s).

required
where Optional[Numbers]

Optionally a boolean mask whose shape matches batch_shape. If a where mask is given, then modifications will happen only on the memory slots whose corresponding mask values are True.

None
Source code in evotorch/tools/structures.py
def subtract_(self, key: Numbers, value: Numbers, where: Optional[Numbers] = None):
    """
    Subtract value(s) from existing values of slots with the given key(s).

    Args:
        key: A single key, or multiple keys (where the leftmost dimension
            of the given keys conform with the `batch_shape`).
        value: The value(s) that will be subtracted from existing value(s).
        where: Optionally a boolean mask whose shape matches `batch_shape`.
            If a `where` mask is given, then modifications will happen only
            on the memory slots whose corresponding mask values are True.
    """
    self.add_(key, -value, where)

Structure

A mixin class for vectorized structures.

This mixin class assumes that the inheriting structure has a protected attribute _data which is either a CMemory object or another Structure. With this assumption, this mixin class provides certain methods and properties to bring a unified interface for all vectorized structures provided in this namespace.

Source code in evotorch/tools/structures.py
class Structure:
    """
    A mixin class for vectorized structures.

    This mixin class assumes that the inheriting structure has a protected
    attribute `_data` which is either a `CMemory` object or another
    `Structure`. With this assumption, this mixin class provides certain
    methods and properties to bring a unified interface for all vectorized
    structures provided in this namespace.
    """

    _data: Union[CMemory, "Structure"]

    @property
    def value_shape(self) -> torch.Size:
        """
        Shape of a single value
        """
        return self._data.value_shape

    @property
    def value_ndim(self) -> int:
        """
        Number of dimensions expressed by `value_shape`
        """
        return self._data.value_ndim

    @property
    def batch_shape(self) -> torch.Size:
        """
        Batch size of this structure
        """
        return self._data.batch_shape

    @property
    def batch_ndim(self) -> int:
        """
        Number of dimensions expressed by `batch_shape`
        """
        return self._data.batch_ndim

    @property
    def is_batched(self) -> bool:
        """
        True if this structure is batched; False otherwise.
        """
        return self._batch_ndim > 0

    @property
    def dtype(self) -> torch.dtype:
        """
        `dtype` of the values
        """
        return self._data.dtype

    @property
    def device(self) -> torch.device:
        """
        The device on which this structure lives
        """
        return self._data.device

    def prepare_value_tensor(self, value: Numbers) -> torch.Tensor:
        """
        Return the tensor-counterpart of a value.

        Args:
            value: A value that can be a numeric sequence or a PyTorch tensor.
                The shape of the given value must conform with the
                `value_shape` of this memory object.
                To express a different value for each batch item, the shape of
                the given value can also have extra leftmost dimensions
                expressed by `value_shape`.
        Returns:
            A copy of the given value(s), converted to PyTorch tensor.
        """
        return self._data.prepare_value_tensor(value)

    def prepare_where_tensor(self, where: Numbers) -> torch.Tensor:
        """
        Return the tensor-counterpart of a boolean mask.

        Args:
            where: A boolean mask expressed as a sequence of bools or as a
                boolean PyTorch tensor.
                The shape of the given mask must conform with the batch shape
                that is expressed by the property `batch_shape`.
        Returns:
            A copy of the boolean mask, converted to PyTorch tensor.
        """
        return self._data.prepare_where_tensor(where)

    def _get_value(self, value: Numbers) -> torch.Tensor:
        return self._data.prepare_value_tensor(value)

    def _get_where(self, where: Numbers) -> torch.Tensor:
        return self._data.prepare_where_tensor(where)

    def __contains__(self, x: Any) -> torch.Tensor:
        raise TypeError("This structure does not support the `in` operator")

batch_ndim property

Number of dimensions expressed by batch_shape

batch_shape property

Batch size of this structure

device property

The device on which this structure lives

dtype property

dtype of the values

is_batched property

True if this structure is batched; False otherwise.

value_ndim property

Number of dimensions expressed by value_shape

value_shape property

Shape of a single value

prepare_value_tensor(value)

Return the tensor-counterpart of a value.

Parameters:

Name Type Description Default
value Numbers

A value that can be a numeric sequence or a PyTorch tensor. The shape of the given value must conform with the value_shape of this memory object. To express a different value for each batch item, the shape of the given value can also have extra leftmost dimensions expressed by value_shape.

required
Source code in evotorch/tools/structures.py
def prepare_value_tensor(self, value: Numbers) -> torch.Tensor:
    """
    Return the tensor-counterpart of a value.

    Args:
        value: A value that can be a numeric sequence or a PyTorch tensor.
            The shape of the given value must conform with the
            `value_shape` of this memory object.
            To express a different value for each batch item, the shape of
            the given value can also have extra leftmost dimensions
            expressed by `value_shape`.
    Returns:
        A copy of the given value(s), converted to PyTorch tensor.
    """
    return self._data.prepare_value_tensor(value)

prepare_where_tensor(where)

Return the tensor-counterpart of a boolean mask.

Parameters:

Name Type Description Default
where Numbers

A boolean mask expressed as a sequence of bools or as a boolean PyTorch tensor. The shape of the given mask must conform with the batch shape that is expressed by the property batch_shape.

required
Source code in evotorch/tools/structures.py
def prepare_where_tensor(self, where: Numbers) -> torch.Tensor:
    """
    Return the tensor-counterpart of a boolean mask.

    Args:
        where: A boolean mask expressed as a sequence of bools or as a
            boolean PyTorch tensor.
            The shape of the given mask must conform with the batch shape
            that is expressed by the property `batch_shape`.
    Returns:
        A copy of the boolean mask, converted to PyTorch tensor.
    """
    return self._data.prepare_where_tensor(where)