File size: 9,950 Bytes
483026f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 | # Manipulating Solutions
For many advanced use-cases in EvoTorch, it's essential to directly work with populations of solutions. In EvoTorch, solutions are represented through the [Solution][evotorch.core.Solution] class, and groups (populations) of solutions are represented through the [SolutionBatch][evotorch.core.SolutionBatch] class. This tutorial will demonstrate a variety of features of these classes. To begin, consider the simple sphere problem:
```python
from evotorch import Problem, Solution, SolutionBatch
import torch
def sphere(x: torch.Tensor) -> torch.Tensor:
return torch.sum(x.pow(2.0))
problem = Problem("min", sphere, solution_length=2, initial_bounds=(-1, 1))
```
## Creating and using SolutionBatch instances
A [SolutionBatch][evotorch.core.SolutionBatch] instance can be generated directly from the problem itself, with decision values sampled uniformly according to the `initial_bounds` parameter. Calling
```python
batch = problem.generate_batch(5)
```
will create `batch`, an instance of [SolutionBatch][evotorch.core.SolutionBatch] consisting of 5 [Solution][evotorch.core.Solution] instances. These solutions can be viewed in terms of their $5 \times 2$ tensor representation using the `values` attribute:
```python
print(batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[-0.3779, -0.0910],
[-0.7356, 0.8931],
[ 0.0012, -0.1703],
[ 0.4683, 0.8081],
[-0.1409, -0.6965]])
)
```
Note that the decision values are in the initial range `(-1, 1)` that was specified when the problem was created. The fitness values of the batch can be accessed with the `evals` attribute, and will initially be filled with `nan` values as the population has not yet been evaluated
```python
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[nan],
[nan],
[nan],
[nan],
[nan]])
)
```
Calling `problem.evaluate(batch)` will fill the fitness values with solutions' computed fitnesses:
```python
problem.evaluate(batch)
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[0.1511],
[1.3387],
[0.0290],
[0.8723],
[0.5049]])
)
```
The decision values can be overidden using the `set_values` function. Setting them with random gaussian values,
```python
batch.set_values(problem.make_gaussian((5, 2)))
```
will update the values
```python
print(batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.1206, -2.0179],
[-0.0508, -0.5009],
[-1.3668, -0.4877],
[ 1.0296, -1.1230],
[-0.0574, -0.4883]])
)
```
and clear the fitness values
```python
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[nan],
[nan],
[nan],
[nan],
[nan]])
)
```
The fitness values can also be set with the `set_evals` method to give updated fitnesses,
```python
batch.set_evals(problem.make_uniform(5))
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[0.5828],
[0.5208],
[0.7184],
[0.5949],
[0.7678]])
)
```
The `values` and `evals` properties yield read-only tensors that cannot be modified. In some advanced use-cases, you can directly access the underlying tensors using the `access_values` and `access_evals` methods. However, care should be taken when using these methods. By default, [SolutionBatch][evotorch.core.SolutionBatch] assumes that a call to `access_values` means that the underlying tensor will be modified, and the fitnesses are therefore cleared and reset to `nan`,
```python
values = batch.access_values()
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[nan],
[nan],
[nan],
[nan],
[nan]])
)
```
However, this behaviour can be disabled with the `keep_evals` flag, e.g.
```python
batch.set_evals(problem.make_uniform(5))
values = batch.access_values(keep_evals=True)
print(batch.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[0.3775],
[0.2806],
[0.1781],
[0.3854],
[0.4929]])
)
```
If you wish to make an exact copy of a [SolutionBatch][evotorch.core.SolutionBatch] instance, you can do so with
```python
cloned_batch = batch.clone()
```
The new instance `cloned_batch` will have identical `values` and `evals` to `batch`, but will share no memory locations, and can be freely modified without modifying its parent `batch`.
## Slicing and Indexing
[SolutionBatch][evotorch.core.SolutionBatch] instances can be arbitrarily sliced and indexed. For example,
```python
last_3 = batch[2:5]
```
will return a [SolutionBatch][evotorch.core.SolutionBatch] instance `last_3` which is a *view* of `batch` e.g. memory data locations with it. Thus we can see that the decision values are exactly the decision values of the last 3 solutions of `batch`
```python
print(last_3.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[-1.3668, -0.4877],
[ 1.0296, -1.1230],
[-0.0574, -0.4883]])
)
```
and the evaluations are exactly the evaluations of the last 3 solutions of `batch`
```python
print(last_3.evals)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[0.1781],
[0.3854],
[0.4929]])
)
```
A similar behaviour can be achieved with a list of indexes e.g. `subbatch = batch[[0, 1, 3]]` will give a solution batch that is a view of the solutions at indices 0, 1 and 3. When a sub-batch is updated, as can be achieved using `set_values` and `set_evals` or `access_values` and `access_evals` followed by modification,
```python
last_3.set_values(problem.make_gaussian(3, 2))
print(last_3.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.2202, 1.5362],
[ 0.5641, -0.0477],
[ 0.8012, -1.3022]])
)
```
the parent solution batch `batch` is also modified.
```python
print(batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.1206, -2.0179],
[-0.0508, -0.5009],
[ 0.2202, 1.5362],
[ 0.5641, -0.0477],
[ 0.8012, -1.3022]])
)
```
[SolutionBatch][evotorch.core.SolutionBatch] instances of the same [Problem][evotorch.core.Problem] class can also be arbitrarily concatenated. For example, if we now create a second [SolutionBatch][evotorch.core.SolutionBatch] with 3 instances,
```python
second_batch = problem.generate_batch(3)
print(second_batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.0433, -0.6526],
[ 0.9731, 0.2553],
[ 0.7482, -0.6220]])
)
```
and then concatenate `last_3` with `second_batch`, using either the `SolutionBatch.cat` or `batch.concat` methods,
```python
new_batch = SolutionBatch.cat([last_3, second_batch])
print(new_batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.2202, 1.5362],
[ 0.5641, -0.0477],
[ 0.8012, -1.3022],
[ 0.0433, -0.6526],
[ 0.9731, 0.2553],
[ 0.7482, -0.6220]])
)
```
Creating a new instance of [SolutionBatch][evotorch.core.SolutionBatch], `new_batch`, in this way is copying, so further modification of `new_batch` will not modify the parent batches `batch` and `second_batch`.
## Accessing and Manipulating individual Solutions
An individual [Solution][evotorch.core.Solution] instance can be accessed by indexing into the batch. For example, indexing the first [Solution][evotorch.core.Solution] will yield the solution at index 1 of `batch`
```python
solution1 = batch[1]
print(solution1)
```
???+ abstract "Output"
```
<Solution values=tensor([-0.0508, -0.5009]), evals=tensor([0.2806])>
```
A [Solution][evotorch.core.Solution] instance has the same properties `values` and `evals` and methods `set_values`, `set_evals`, `access_values` and `access_evals` as [SolutionBatch][evotorch.core.SolutionBatch], except that they now work in one dimension, rather than two. For example, accessing the values yields a 1-dimensional read-only tensor of decision values
```python
print(solution1.values)
```
???+ abstract "Output"
```python
ReadOnlyTensor(tensor([-0.0508, -0.5009]))
```
A [Solution][evotorch.core.Solution] instance is a view of its parent [SolutionBatch][evotorch.core.SolutionBatch], so modifying it will not only modify the solution,
```python
solution1.set_values(torch.tensor([1, 2]))
solution1.set_evals(0.5)
print(solution1)
```
???+ abstract "Output"
```
<Solution values=tensor([1., 2.]), evals=tensor([0.5000])>
```
but will also modify `batch`,
```python
print(batch.values)
```
???+ abstract "Output"
```
ReadOnlyTensor(
tensor([[ 0.1206, -2.0179],
[ 1.0000, 2.0000],
[ 0.2202, 1.5362],
[ 0.5641, -0.0477],
[ 0.8012, -1.3022]])
)
```
A [Solution][evotorch.core.Solution] instance can also be cloned with the `clone()` method e.g.
```python
cloned_sol = solution1.clone()
```
The cloned solution is no longer a view of `batch` and can be freely modified. To obtain the new, single-solution, [SolutionBatch][evotorch.core.SolutionBatch] instance of the cloned solution, you can use `cloned_sol.to_batch()`.
|