Quick Start#
In this notebook, we will help you be familiar with EvoX through an simple example.
To start with, make sure you have already installed the EvoX.
# install EvoX, skip it if you have already installed EvoX
from importlib.util import find_spec
if find_spec("evox") is None:
%pip install evox
# the dependent packages or functions in this example
import time
import torch
from evox.algorithms import PSO
from evox.problems.numerical import Ackley
from evox.workflows import EvalMonitor, StdWorkflow
The basic running process in EvoX#
As a distributed GPU-accelerated framework for scalable evolutionary computation, EvoX can be used to do many kinds computations, so we say “EvoX is all you need”. Though many kinds of computations are different more or less, in EvoX we standardize the basic running process:
Initiate an algorithm and a problem#
EvoX offers a comprehensive suite of 50+ Evolutionary Algorithms (EAs) and a wide range of 100+ Benchmark Problems/Environments, all benefiting from distributed GPU-acceleration.
For more details, please refer to our API documentation: List of Algorithms and List of Problems.
Here we will create a PSO algorithm and an Ackley function problem.
# Initiate an algorithm
algorithm = PSO(
pop_size=1000,
lb=torch.tensor([-10.0]),
ub=torch.tensor([10.0]),
)
# Initiate a problem
problem = Ackley()
Notice the algorithm and problem themselves do not contain the monitoring of the steps, so we will not get any feed back only depending on them. It turns out that we need a monitor.
Set an monitor#
Monitor
is a standard class in EvoX to monitor the intermediate values inside a optimization process. Information like fitness or population can be easily obtained by the monitor.
Doing is better than saying, so let us create a “Evaluation monitor”:
# Set an monitor
monitor = EvalMonitor()
Initiate an workflow#
A workflow outlines the series of steps required to accomplish a task or project. In EvoX, a workflow represents the overall process of evolutionary computation, putting the algorithm, problem and monitor together.
If we want to run the algorithm to solve the problem with a monitor, we need to create a workflow of the Workflow
class.
# Initiate an workflow
workflow = StdWorkflow()
workflow.setup(
algorithm=algorithm,
problem=problem,
monitor=monitor,
)
Run the workflow#
Now, we can run the workflow:
start = time.time()
# Run the workflow
for i in range(100):
workflow.step()
if (i + 1) % 10 == 0:
run_time = time.time() - start
top_fitness = workflow.get_submodule("monitor").topk_fitness
print(f"The top fitness is {top_fitness} in {run_time:.4f} seconds at the {i+1}th generation.")
The top fitness is tensor([0.0010]) in 0.1065 seconds at the 10th generation.
The top fitness is tensor([4.7684e-06]) in 0.1087 seconds at the 20th generation.
The top fitness is tensor([9.5367e-07]) in 0.1102 seconds at the 30th generation.
The top fitness is tensor([9.5367e-07]) in 0.1115 seconds at the 40th generation.
The top fitness is tensor([9.5367e-07]) in 0.1128 seconds at the 50th generation.
The top fitness is tensor([9.5367e-07]) in 0.1140 seconds at the 60th generation.
The top fitness is tensor([9.5367e-07]) in 0.1152 seconds at the 70th generation.
The top fitness is tensor([9.5367e-07]) in 0.1166 seconds at the 80th generation.
The top fitness is tensor([9.5367e-07]) in 0.1179 seconds at the 90th generation.
The top fitness is tensor([9.5367e-07]) in 0.1191 seconds at the 100th generation.
workflow.get_submodule("monitor").plot()