// 01
EvoTorch provides SOTA algorithms and utilities.
1import torch
2from evotorch import Problem
3from evotorch.algorithms import SNES
4from evotorch.logging import StdOutLogger
// 02
Use PyTorch to define a function to optimize
1def norm(x: torch.Tensor) -> torch.Tensor:
2 return torch.linalg.norm(x, dim=-1)
// 03
Instantiate the problem and select device(s) to use
1problem = Problem(
2 "min",
3 norm,
4 initial_bounds=(-10.0, 10.0),
5 solution_length=100,
6 vectorized=True,
7 # device="cuda", # Enable for GPU support
8)
// 04
1searcher = SNES(problem, popsize=1000, stdev_init=10.0)
2_ = StdOutLogger(searcher, interval=50)
3
4searcher.run(num_generations=1000)
// Examples
1import torch
2from evotorch.algorithms import SNES
3from evotorch.logging import StdOutLogger
4from evotorch import Problem
5
6# Minimize the Lennard-Jones atom cluster potential
7def pairwise_distances(positions: torch.Tensor) -> torch.Tensor:
8 positions = positions.view(positions.shape[0], -1, 3)
9 deltas = positions.unsqueeze(2) - positions.unsqueeze(1)
10 distances = torch.norm(deltas, dim=-1)
11 return distances
12
13
14def cluster_potential(positions: torch.Tensor) -> torch.Tensor:
15 distances = pairwise_distances(positions)
16 pairwise_cost = (1 / distances).pow(12) - (1 / distances).pow(6.0)
17 ut_pairwise_cost = torch.triu(pairwise_cost, diagonal=1)
18 potential = 4 * ut_pairwise_cost.sum(dim=(1, 2))
19 return potential
20
21
22problem = Problem(
23 "min",
24 cluster_potential,
25 initial_bounds=(-1e-12, 1e-12),
26 device="cuda:0" if torch.cuda.is_available() else "cpu",
27 solution_length=150,
28 # Evaluation is vectorized
29 vectorized=True,
30 # Higher-than-default precision
31 dtype=torch.float64,
32)
33
34searcher = SNES(problem, popsize=1000, stdev_init=0.01)
35logger = StdOutLogger(searcher, interval=100)
36
37searcher.run(5000)
// Features
Evolutionary Algorithms and Neural Networks both thrive at scale. That's why EvoTorch lets you easily unleash experiments on multiple CPU nodes, GPU nodes, or both, building on top of Ray and PyTorch.
Explore the space of images, programs, designs, or anything else
Solve large-scale single/multiobjective engineering optimization problems
Implement fast Model Predictive Control with learned dynamics models
Evolve high-performance neural controllers for complex reinforcement learning tasks (neuroevolution)
NNAISENSE offers paid support including customization for your preferred domain-specific problems, compute environments, production deployments and maintenance of trained solutions.
// Evolved atNNAISENSEin Switzerland