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
|
# fmt: off
import numpy as np
class Prior():
"""Base class for all priors for the bayesian optimizer.
The __init__ method and the prior method are implemented here.
Each child class should implement its own potential method, that will be
called by the prior method implemented here.
When used, the prior should be initialized outside the optimizer and the
Prior object should be passed as a function to the optimizer.
"""
def __init__(self):
"""Basic prior implementation."""
def prior(self, x):
"""Actual prior function, common to all Priors"""
if len(x.shape) > 1:
n = x.shape[0]
return np.hstack([self.potential(x[i, :]) for i in range(n)])
else:
return self.potential(x)
class ZeroPrior(Prior):
"""ZeroPrior object, consisting on a constant prior with 0eV energy."""
def __init__(self):
Prior.__init__(self)
def potential(self, x):
return np.zeros(x.shape[0] + 1)
class ConstantPrior(Prior):
"""Constant prior, with energy = constant and zero forces
Parameters:
constant: energy value for the constant.
Example:
>>> from ase.optimize import GPMin
>>> from ase.optimize.gpmin.prior import ConstantPrior
>>> op = GPMin(atoms, Prior = ConstantPrior(10)
"""
def __init__(self, constant):
self.constant = constant
Prior.__init__(self)
def potential(self, x):
d = x.shape[0]
output = np.zeros(d + 1)
output[0] = self.constant
return output
def set_constant(self, constant):
self.constant = constant
class CalculatorPrior(Prior):
"""CalculatorPrior object, allows the user to
use another calculator as prior function instead of the
default constant.
Parameters:
atoms: the Atoms object
calculator: one of ASE's calculators
"""
def __init__(self, atoms, calculator):
Prior.__init__(self)
self.atoms = atoms.copy()
self.atoms.calc = calculator
def potential(self, x):
self.atoms.set_positions(x.reshape(-1, 3))
V = self.atoms.get_potential_energy(force_consistent=True)
gradV = -self.atoms.get_forces().reshape(-1)
return np.append(np.array(V).reshape(-1), gradV)
|