A simple example showcasing CBX#
[ ]:
%load_ext autoreload
%autoreload 2
import cbx as cbx
from cbx.dynamics import CBO
from cbx.objectives import Rastrigin
import cbx.utils.termination as term
from cbx.scheduler import effective_sample_size
Define the parameters#
[ ]:
conf = {'alpha': 40.0,
'dt': 0.1,
'sigma': 1.,
'lamda': 1.0,
'batch_args':{
'batch_size':200,
'batch_partial': False},
'd': 2,
'term_criteria':[term.max_it_term(50)],
'N': 50,
'M': 3,
'track_args': {'names':
['update_norm',
'energy','x',
'consensus',
'drift']},}
Define the initial positions of the particles and objective function#
[ ]:
x = cbx.utils.init_particles(shape=(conf['M'], conf['N'], conf['d']), x_min=-2., x_max = 1.)
f = Rastrigin()
Define the dynamic and scheduler#
[ ]:
dyn = CBO(f, x=x, noise='isotropic', f_dim='3D',
**conf)
sched = effective_sample_size(name='alpha', eta = 0.1)
Run the CBO algorithm#
[ ]:
dyn.optimize(sched=sched)