class Rosenbrock(BaseFunction): target_E = 0. target_coords = np.array([1., 1.]) xmin = np.array([-10.,-10.]) xmax = np.array([10.,10.]) def getEnergy(self, coords): x, y = coords return 100. * (y - x**2)**2 + (x-1)**2 def getEnergyGradient(self, coords): E = self.getEnergy(coords) x, y = coords dx = -400. * (y - x**2) * x + 2. * (x-1) dy = 200. * (y - x**2) return E, np.array([dx, dy]) if __name__ == "__main__": f = Rosenbrock() f.test_potential(f.target_coords) print "" f.test_potential(f.get_random_configuration()) from base_function import makeplot2d v = 3. xmin = np.array([0.,0.]) xmax = np.array([1.2,1.5]) makeplot2d(f, xmin=-xmax, xmax=xmax, nx=30)
if x > 0: return 1. elif x < 0: return -1. else: return 0. def getEnergyGradient_2(self, coords): """there is a bug here. it doesn't pass the test""" E = self.getEnergy(coords) x, y = coords dEdy = (-sin(sqrt(abs(47. + 0.5 * x + y))) + (x * cos(sqrt(abs(-47. + x - y))) * -1. * self.dabs(-47. + x - y)) / (2. * sqrt(abs(-47. + x - y))) + (0.5 * (-47. - y) * cos(sqrt(abs(47. + 0.5 * x + y))) * self.dabs(47. + 0.5 * x + y)) / sqrt(abs(47. + 0.5 * x + y))) dEdx = (-sin(sqrt(abs(-47. + x - y))) - (x * cos(sqrt(abs(-47. + x - y))) * self.dabs(-47. + x - y)) / (2. * sqrt(abs(-47. + x - y))) + (0.25 * (-47. - y) * cos(sqrt(abs(47. + 0.5 * x + y))) * 0.5 * self.dabs(47. + 0.5 * x + y)) / sqrt(abs(47. + 0.5 * x + y))) grad = np.array([dEdx, dEdy]) print "grad", grad return E, grad if __name__ == "__main__": f = EggHolder() # f.test_potential(f.get_random_configuration()) from base_function import makeplot2d makeplot2d(EggHolder())
class Levi(BaseFunction): target_E = 0. xmin = np.array([-10.,-10.]) xmax = np.array([10.,10.]) def getEnergy(self, coords): x, y = coords E = sin(3.*pi*x)**2 + (x-1.)**2 * (1. + sin(3*pi*y)**2) \ + (y-1.)**2 * (1. + sin(2*pi*y)**2) return E def getEnergyGradient(self, coords): x, y = coords E = self.getEnergy(coords) dEdx = 2.*3.*pi* cos(3.*pi*x) * sin(3.*pi*x) + 2.*(x-1.) * (1. + sin(3*pi*y)**2) dEdy = (x-1.)**2 * 2.*3.*pi* cos(3.*pi*y) * sin(3.*pi*y) + 2. * (y-1.) * (1. + sin(2*pi*y)**2) \ + (y-1.)**2 * 2.*2.*pi * cos(2.*pi*y) * sin(2.*pi*y) return E, np.array([dEdx, dEdy]) if __name__ == "__main__": f = Levi() f.test_potential(f.get_random_configuration()) from base_function import makeplot2d makeplot2d(f)
x, y = coords dEdy = (-sin(sqrt(abs(47. + 0.5*x + y))) + (x*cos(sqrt(abs(-47. + x - y))) * -1.*self.dabs(-47. + x - y))/ (2.*sqrt(abs(-47. + x - y))) + (0.5*(-47. - y)*cos(sqrt(abs(47. + 0.5*x + y))) * self.dabs(47. + 0.5*x + y) )/sqrt(abs(47. + 0.5*x + y))) dEdx = ( -sin(sqrt(abs(-47. + x - y))) - (x*cos(sqrt(abs(-47. + x - y))) *self.dabs(-47. + x - y))/ (2.*sqrt(abs(-47. + x - y))) + (0.25*(-47. - y)*cos(sqrt(abs(47. + 0.5*x + y))) *0.5*self.dabs(47. + 0.5*x + y) )/sqrt(abs(47. + 0.5*x + y))) grad = np.array([dEdx, dEdy]) print "grad", grad return E, grad if __name__ == "__main__": f = EggHolder() # f.test_potential(f.get_random_configuration()) from base_function import makeplot2d makeplot2d(EggHolder())
"""derivative of absolute value""" if x < 0: return -1. elif x > 0: return 1. else: return 0. def getEnergyGradient(self, coords): x, y = coords R = sqrt(x**2 + y**2) g = 1. - R / pi f = sin(x)* cos(y) * exp(abs(g)) E = -abs(f) dRdx = x / R dgdx = - dRdx / pi dfdx = cos(x) * cos(y) * exp(abs(g)) + f * self.dabs(g) * dgdx dEdx = - self.dabs(f) * dfdx dRdy = y / R dgdy = - dRdy / pi dfdy = -sin(x) * sin(y) * exp(abs(g)) + f * self.dabs(g) * dgdy dEdy = - self.dabs(f) * dfdy return E, np.array([dEdx, dEdy]) if __name__ == "__main__": f = HolderTable() f.test_potential(f.get_random_configuration()) from base_function import makeplot2d makeplot2d(f)
class Rosenbrock(BaseFunction): target_E = 0. target_coords = np.array([1., 1.]) xmin = np.array([-10., -10.]) xmax = np.array([10., 10.]) def getEnergy(self, coords): x, y = coords return 100. * (y - x**2)**2 + (x - 1)**2 def getEnergyGradient(self, coords): E = self.getEnergy(coords) x, y = coords dx = -400. * (y - x**2) * x + 2. * (x - 1) dy = 200. * (y - x**2) return E, np.array([dx, dy]) if __name__ == "__main__": f = Rosenbrock() f.test_potential(f.target_coords) print "" f.test_potential(f.get_random_configuration()) from base_function import makeplot2d v = 3. xmin = np.array([0., 0.]) xmax = np.array([1.2, 1.5]) makeplot2d(f, xmin=-xmax, xmax=xmax, nx=30)
class Beale(BaseFunction): target_E = 0. target_coords = np.array([3., 0.5]) xmin = np.array([-4.5,-4.5]) xmax = np.array([4.5,4.5]) def getEnergy(self, coords): x, y = coords return (1.5 - x + x*y)**2 + (2.25 - x + x * y**2)**2 + (2.625 - x + x * y**3)**2 def getEnergyGradient(self, coords): E = self.getEnergy(coords) x, y = coords dx = 2. * (1.5 - x + x*y) * (-1. + y) + 2. * (2.25 - x + x * y**2) * (-1. + y**2) + 2. * (2.625 - x + x * y**3) * (-1. + y**3) dy = 2. * (1.5 - x + x*y) * (x) + 2. * (2.25 - x + x * y**2) * (2. * y * x) + 2. * (2.625 - x + x * y**3) * (3. * x * y**2) return E, np.array([dx, dy]) if __name__ == "__main__": f = Beale() f.test_potential(f.target_coords) print "" f.test_potential(f.get_random_configuration()) f.test_potential(np.array([1.,1.]), print_grads=True) from base_function import makeplot2d v = 3. makeplot2d(f, nx=30)
from numpy import exp, cos, sqrt, pi import numpy as np from base_function import BaseFunction from pygmin.systems import BaseSystem class Bulkin(BaseFunction): xmin = np.array([-15.,-3.]) xmax = np.array([-5.,3.]) target_E = 0. def getEnergy(self, coords): x, y = coords E = 100. * sqrt(abs(y - 0.01 * x**2)) + 0.01 * abs(x + 10.) return E def get_random_configuration(self): x = np.random.uniform(-13,-8, 1) y = np.random.uniform(-4,-4, 1) return np.array([x,y]).flatten() class BukinSystem(BaseSystem): def get_potential(self): return Bulkin() if __name__ == "__main__": from base_function import makeplot2d makeplot2d(Bulkin())
target_coords = np.array([3., 0.5]) xmin = np.array([-4.5, -4.5]) xmax = np.array([4.5, 4.5]) def getEnergy(self, coords): x, y = coords return (1.5 - x + x * y)**2 + (2.25 - x + x * y**2)**2 + (2.625 - x + x * y**3)**2 def getEnergyGradient(self, coords): E = self.getEnergy(coords) x, y = coords dx = 2. * (1.5 - x + x * y) * (-1. + y) + 2. * (2.25 - x + x * y**2) * ( -1. + y**2) + 2. * (2.625 - x + x * y**3) * (-1. + y**3) dy = 2. * (1.5 - x + x * y) * (x) + 2. * (2.25 - x + x * y**2) * ( 2. * y * x) + 2. * (2.625 - x + x * y**3) * (3. * x * y**2) return E, np.array([dx, dy]) if __name__ == "__main__": f = Beale() f.test_potential(f.target_coords) print "" f.test_potential(f.get_random_configuration()) f.test_potential(np.array([1., 1.]), print_grads=True) from base_function import makeplot2d v = 3. makeplot2d(f, nx=30)