In [1]:
import scipy.optimize as csyopt
import math
import tensorflow as tf
import numpy as np
import datetime

In [2]:
#http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.641.3566&rep=rep1&type=pdf
#rosenbrock f=0 at (1,1) start = (-1.2, 1) 
def rosenbrock(x):
    return (10 * (x[0] - (x[1] ** 2))) ** 2 + ((1 -x[0]) ** 2)

#freudenstein and Roth function f= 0 at (5,4) start = (0.5 ,-2)
def freudenstein(x):
    return (-13 + x[0] + ((5 - x[1])* x[1] - 2) * x[1]) ** 2 + (-29 +x[0]+((x[1]+1)*x[1]- 14)* x[1]) ** 2

#Powell badly scaled function f=0 at (1.098 * (10 ** -5),9.106)  start = (0, 1)
def powell(x):
    return (((10 ** 4)* x[0] * x[1] - 1) ** 2) + ((math.exp(-x[0]) + math.exp(-x[1]) - 1.0001) ** 2)


#brown badly scaled function [unpublished] start = (1, 1) f=0 at (10*6, 2*10 ** -6)
def brown(x):
    f1 = x[0] - (10 ** 6)
    f2 = x[1] - (2*10 ** -6)
    f3 =  x[0]*x[1] - 2
    return (f1 ** 2) + (f2 ** 2) + (f3 ** 2)

#beale function start = (1, 1) f=0 at (3, 0.5)
def beale(x):
    y= (1.5,2.25,2.625)
    result = 0
    for i in range(3):
        result += (y[i] - x[0]*(1 - x[1]** (i+1))) ** 2
    return result

#Jennrich and Sampson function fmin = 22.86657580612254 start = (0.3, 0.4)
def jennrich(x):
    result = 0
    for i in range(1,11):
        result += 2 + 2* i - (math.exp(i*x[0]) + math.exp(i*x[1]))
    return result


#Helical valley function start x0=(-1,0,0) f0 = (1,0,0)
def helical_valley(x):
    def theta():
        value = (1/(2 * math.pi)) * math.atan2(x[1],x[0])
        if(x[0] < 0):
            value += 0.5
        return value
    f1 = 10 * (x[2] - 10 * theta())
    f2 = 10* ((((x[0]**2 + x[1]**2)) ** (1/2)) -1)
    f3 = x[2]
    return (f1 ** 2) + (f2 ** 2) + (f3 ** 2)

#bard function start in (1,1,1) min (0.8406,-inf,-inf)
def bard(x):
    y = (0.14,0.18,0.33,0.25, 0.29, 0.32,0.35,0.39,0.37,0.58,0.73,0.96,1.34,2.10,4.39)
    result = 0
    for i in range(1,16):
        v =(16- i)
        u = i
        w = min(u,v)
        result += (y[i-1] - (x[0] +  u/(v*x[1] + w*x[2] ) ) ) ** 2
    return result
        
    
#Gaussian function Xo = (0.4, 1, 0) f= 1.12793 10-8
def gaussian(x):
    y= (0.0009,.0044, 0.0175, 0.0540, 0.1295, 0.2420, 0.3521, 0.3989, 0.3521, 0.2420,0.1295,0.0540, 0.0175, 0.0044, 0.0009)
    result = 0
    for i in range(1,16):
        t = (8 - i) /2
        result += (x[0]* math.exp((-x[1] * (t - x[2]) ** 2 ) / 2 ) - y[i-1]) ** 2
    return result

#meyer function f = 87.9458 start = (.02, 4000, 250)
def meyer(x):
    y = (34780, 28610, 23650, 19630,16370, 13720,11540, 9744, 8261, 7030, 6005, 5147,4427, 3820,3307,2872)
    result = 0
    for i in range(1,17):
        t = 45 + (5 * i)
        result += (x[0] * math.exp(x[1]/(t+ x[2])) - y[i-1]) ** 2
    return result

#gulf research and development function start (5, 2.5, 0.15) f= 0 at (50,25,1.5) revisar no da 0
def gulf(x):
    iterations = 3
    result = 0
    for i in range(1,iterations + 1):
        t= i/100
        y= 25 + (-50* math.log(t)) ** (2/3)
        result += (math.exp(-(abs(y* iterations * i * x[1]) ** x[2]  )/ x[0]  ) -t) ** 2
    return result


#Box three-dimensional function  start = (0, 10, 20) f=0 at (1,10,1),(10,1,-1) wherever (xl = x2 and x3 = 0)
def box3dimecional(x):
    iterations = 4
    result = 0
    for i in range(1,iterations + 1):
        t = .1* i
        result += (math.exp(-t * x[0]) - math.exp(-t * x[1]) - x[2] * (math.exp(-t) - math.exp(-10 * t))) ** 2
    return result

#powell singular function f = 0 at the origin start = (3, -1, 0, 1)
def powell_singular(x):
    f1 = x[0] + 10 * x[1]
    f2 = (5 ** (1/2)) * (x[2] - x[3])
    f3 = (x[1] -2*x[2]) ** 2
    f4 = (10 ** (1/2)) * ((x[0] - x[3]) ** 2)
    return (f1 ** 2) + (f2 ** 2) + (f3 ** 2) + (f4 ** 2)

#Wood function Xo= (-3,-1,-3,-1) f = 0 at (1,1,1,1)
def wood(x):
    f1 = 10 * (x[0] - (x[1]** 2))
    f2 = 1 - x[0]
    f3= (x[3] - (x[2]** 2)) * (90 ** (1/2))
    f4 = 1- x[2]
    return (f1 ** 2) + (f2 ** 2) + (f3 ** 2) + (f4 ** 2)

#kowalik and osborne funcion no funciona 1.02734 x 10 ** -3 at (10 ** 99, -14.07, -10** 99, -10** 99)
def kowalik(x):
    y = (0.1957, 0.1947, 0.1735, 0.1600, 0.0844, 0.0627, 0.0456, 0.0342, 0.0323, 0.0235, 0.0246)
    u = (4.0000, 2.0000, 1.0000, 0.5000, 0.2500, 0.1670, 0.1250, 0.1000, 0.0833, 0.0714, 0.0625)
    result = 0
    for i in range(11):
        result +=  (y[i] - ((x[0] * ((u[i] ** 2) + (u[i] * x[1]))) / ((u[i] ** 2) + (u[i] * x[2]) +  x[3] ))) ** 2
    return result

In [3]:
init = datetime.datetime.now()
print(csyopt.minimize(rosenbrock , (-1.2, 1) , method = 'Nelder-Mead'   ))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[0.99995555, 0.99997821],
       [1.00001647, 1.00000589],
       [1.00005122, 1.00002754]]), array([2.05196900e-09, 2.47801465e-09, 4.11920381e-09]))
           fun: 2.0519690043222226e-09
       message: 'Optimization terminated successfully.'
          nfev: 163
           nit: 86
        status: 0
       success: True
             x: array([0.99995555, 0.99997821])
9.932

In [4]:
init = datetime.datetime.now()
print(csyopt.minimize(freudenstein , (0.5 ,-2),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[11.41278135, -0.89680399],
       [11.41283714, -0.8968021 ],
       [11.41271323, -0.89680896]]), array([48.98425368, 48.98425368, 48.98425368]))
           fun: 48.98425367981376
       message: 'Optimization terminated successfully.'
          nfev: 120
           nit: 63
        status: 0
       success: True
             x: array([11.41278135, -0.89680399])
6.278

In [5]:
init = datetime.datetime.now()
print(csyopt.minimize(powell , (0, 1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[1.17714307e-05, 8.49512936e+00],
       [1.17991888e-05, 8.47504727e+00],
       [1.18285673e-05, 8.45422234e+00]]), array([8.59453336e-09, 9.54568776e-09, 1.04145728e-08]))
           fun: 8.594533356831049e-09
       message: 'Maximum number of function evaluations has been exceeded.'
          nfev: 401
           nit: 222
        status: 1
       success: False
             x: array([1.17714307e-05, 8.49512936e+00])
14.084

In [6]:
init = datetime.datetime.now()
print(csyopt.minimize(brown , (1, 1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[1.00000000e+06, 1.99996767e-06],
       [1.00000000e+06, 2.00003010e-06],
       [1.00000000e+06, 2.00015572e-06]]), array([2.00355540e-09, 1.20596332e-08, 2.55108727e-08]))
           fun: 2.0035553999661334e-09
       message: 'Optimization terminated successfully.'
          nfev: 275
           nit: 146
        status: 0
       success: True
             x: array([1.00000000e+06, 1.99996767e-06])
11.502

In [7]:
init = datetime.datetime.now()
print(csyopt.minimize(beale , (1, 1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[3.00002489, 0.50000749],
       [2.99993609, 0.49998427],
       [3.00005157, 0.50000776]]), array([1.39263183e-10, 6.54120047e-10, 1.00978644e-09]))
           fun: 1.3926318302583701e-10
       message: 'Optimization terminated successfully.'
          nfev: 107
           nit: 56
        status: 0
       success: True
             x: array([3.00002489, 0.50000749])
5.904999999999999

In [8]:
init = datetime.datetime.now()
#print(csyopt.minimize(jennrich , (0.3, 0.4),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


0.034999999999999996

In [9]:
init = datetime.datetime.now()
print(csyopt.minimize(helical_valley , (-1,0,0) ,  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[0.99992607, 0.0094207 , 0.01502868],
       [0.99994899, 0.00944558, 0.01503589],
       [1.00000846, 0.00942039, 0.01502881],
       [1.00002386, 0.00945604, 0.01504055]]), array([0.00022607, 0.00022608, 0.00022628, 0.0002267 ]))
           fun: 0.0002260676253182685
       message: 'Optimization terminated successfully.'
          nfev: 175
           nit: 93
        status: 0
       success: True
             x: array([0.99992607, 0.0094207 , 0.01502868])
11.545

In [10]:
init = datetime.datetime.now()
print(csyopt.minimize(bard ,(1,1,1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[0.07678587, 0.7027171 , 2.76692131],
       [0.07679516, 0.70281664, 2.76683057],
       [0.07678598, 0.70265979, 2.76697948],
       [0.07678682, 0.70272319, 2.76690274]]), array([0.01711162, 0.01711162, 0.01711162, 0.01711162]))
           fun: 0.017111616738674788
       message: 'Optimization terminated successfully.'
          nfev: 197
           nit: 111
        status: 0
       success: True
             x: array([0.07678587, 0.7027171 , 2.76692131])
16.067999999999998

In [11]:
init = datetime.datetime.now()
print(csyopt.minimize(gaussian ,(0.4, 1, 0),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[3.98960167e-01, 1.00000475e+00, 4.16647356e-05],
       [3.98949373e-01, 9.99958348e-01, 3.94257138e-05],
       [3.98954786e-01, 1.00006846e+00, 4.21779485e-05],
       [3.98956660e-01, 9.99962581e-01, 4.24159444e-05]]), array([1.18891931e-08, 1.19798181e-08, 1.20928316e-08, 1.21464971e-08]))
           fun: 1.1889193134595934e-08
       message: 'Optimization terminated successfully.'
          nfev: 62
           nit: 31
        status: 0
       success: True
             x: array([3.98960167e-01, 1.00000475e+00, 4.16647356e-05])
9.27

In [12]:
init = datetime.datetime.now()
print(csyopt.minimize(meyer , (.02, 4000, 250),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[1.25761983e-02, 5.52693658e+03, 3.22593783e+02],
       [1.26028282e-02, 5.52517302e+03, 3.22526461e+02],
       [1.26309314e-02, 5.52374756e+03, 3.22484071e+02],
       [1.26820886e-02, 5.52056133e+03, 3.22374428e+02]]), array([6409.11926448, 6424.91696529, 6482.24753313, 6544.75273709]))
           fun: 6409.119264481873
       message: 'Maximum number of function evaluations has been exceeded.'
          nfev: 600
           nit: 343
        status: 1
       success: False
             x: array([1.25761983e-02, 5.52693658e+03, 3.22593783e+02])
31.940999999999995

In [13]:
init = datetime.datetime.now()
print(csyopt.minimize(gulf ,(5, 2.5, 0.15),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[ 0.0249534 , 12.22167143, -0.27875974],
       [ 0.024953  , 12.22170072, -0.27876157],
       [ 0.02495284, 12.22172558, -0.27876203],
       [ 0.0249526 , 12.22176388, -0.27876328]]), array([3.44571004e-07, 3.44571010e-07, 3.44571013e-07, 3.44571015e-07]))
           fun: 3.445710037605985e-07
       message: 'Optimization terminated successfully.'
          nfev: 344
           nit: 196
        status: 0
       success: True
             x: array([ 0.0249534 , 12.22167143, -0.27875974])
15.273

In [14]:
init = datetime.datetime.now()
print(csyopt.minimize(box3dimecional , (0, 10, 20) ,  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[1.23555847e-03, 1.61057591e+01, 1.46049540e+00],
       [1.23554995e-03, 1.61058115e+01, 1.46049679e+00],
       [1.23556773e-03, 1.61057002e+01, 1.46049532e+00],
       [1.23554902e-03, 1.61058177e+01, 1.46049641e+00]]), array([0.00411, 0.00411, 0.00411, 0.00411]))
           fun: 0.004109997815407901
       message: 'Optimization terminated successfully.'
          nfev: 246
           nit: 131
        status: 0
       success: True
             x: array([1.23555847e-03, 1.61057591e+01, 1.46049540e+00])
13.667

In [15]:
init = datetime.datetime.now()
print(csyopt.minimize(powell_singular ,(3, -1, 0, 1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[ 0.00942089, -0.00094117,  0.01661581,  0.0166153 ],
       [ 0.00943187, -0.00094258,  0.01661579,  0.01661425],
       [ 0.00943412, -0.00094274,  0.01661578,  0.01661505],
       [ 0.0094289 , -0.00094201,  0.01661581,  0.01661357],
       [ 0.00932399, -0.00093171,  0.01661609,  0.01661445]]), array([1.39058605e-06, 1.39058761e-06, 1.39058936e-06, 1.39059280e-06,
       1.39059903e-06]))
           fun: 1.3905860499424258e-06
       message: 'Optimization terminated successfully.'
          nfev: 305
           nit: 185
        status: 0
       success: True
             x: array([ 0.00942089, -0.00094117,  0.01661581,  0.0166153 ])
11.079

In [16]:
init = datetime.datetime.now()
print(csyopt.minimize(wood , (-3,-1,-3,-1),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[ 0.99998467, -0.99999193,  0.99998385,  0.99996799],
       [ 0.99999557, -0.99999835,  1.0000222 ,  1.00004426],
       [ 1.0000293 , -1.00001559,  0.99999405,  0.99998759],
       [ 1.00000345, -1.00000233,  1.00002238,  1.00004768],
       [ 1.00000907, -1.0000026 ,  1.00000354,  1.00000839]]), array([5.70347377e-10, 6.43601243e-10, 1.27313714e-09, 1.42394320e-09,
       1.74956645e-09]))
           fun: 5.703473773569645e-10
       message: 'Optimization terminated successfully.'
          nfev: 438
           nit: 255
        status: 0
       success: True
             x: array([ 0.99998467, -0.99999193,  0.99998385,  0.99996799])
14.706999999999999

In [17]:
init = datetime.datetime.now()
print(csyopt.minimize(kowalik , (0.25, 0.39, 0.415, 0.39),  method = 'Nelder-Mead'))
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


 final_simplex: (array([[0.19280828, 0.19129484, 0.1230799 , 0.13606312],
       [0.19280425, 0.19128406, 0.12302842, 0.13606177],
       [0.19280981, 0.19120381, 0.12302495, 0.13603415],
       [0.19280384, 0.19124946, 0.1230306 , 0.13604671],
       [0.19280023, 0.1913863 , 0.12304816, 0.13611169]]), array([0.00030751, 0.00030751, 0.00030751, 0.00030751, 0.00030751]))
           fun: 0.00030750561104950664
       message: 'Optimization terminated successfully.'
          nfev: 260
           nit: 154
        status: 0
       success: True
             x: array([0.19280828, 0.19129484, 0.1230799 , 0.13606312])
15.461

In [18]:
@tf.function
def tf_rosenbrock(x):
    return tf.convert_to_tensor(rosenbrock(x))

@tf.function
def tf_freudenstein(x):
    return freudenstein(x)

@tf.function
def tf_powell(x):
    return powell(x)

@tf.function
def tf_brown(x):
    return brown(x)

@tf.function
def tf_beale(x):
    return beale(x)

@tf.function
def tf_jennrich(x):
    return jennrich(x)

@tf.function
def tf_helical_valley(x):
    return helical_valley(x)

@tf.function
def tf_bard(x):
    return bard(x)

@tf.function
def tf_gaussian(x):
    return gaussian(x)

@tf.function
def tf_meyer(x):
    return meyer(x)

@tf.function
def tf_gulf(x):
    return gulf(x)

@tf.function
def tf_box3dimecional(x):
    return box3dimecional(x)

@tf.function
def tf_powell_singular(x):
    return powell_singular(x)

@tf.function
def tf_wood(x):
    return wood(x)

@tf.function
def tf_kowalik(x):
    return kowalik(x)

In [19]:
#modo sin tensor:
class basic_nelder():
    
    p = 1
    d = 2
    r = 0.5
    g = 0.5
    
    def __init__(self,x,f):
        self.f = f
        self.x = x
        self.points = [self.evaluation(x)]
        self.generate()
        
    def evaluation(self,x):
        return np.array([x,self.f(x)])
    
    def generate(self):
        x2 = self.evaluation(self.x + np.random.rand(self.x.shape[0]))
        x3 = self.evaluation(self.x + np.random.rand(self.x.shape[0]))
        self.points.append(x2)
        self.points.append(x3)
    
    def sorting(self):
        self.points = sorted(self.points, key=lambda x: x[-1])
    
    def create_centroide(self):
        size = len(self.points) - 1
        self.centroide =  np.sum(self.points[:-1], axis = 0) / size 
        
    def create_xr(self):
        self.xr = self.centroide + self.p * ( self.centroide - self.points[-1])
        self.xr = self.evaluation(self.xr[0])
        
    def create_xe(self):
        self.xe = (1 + self.p * self.d ) * self.centroide - (self.p* self.d*self.points[-1])
        self.xe = self.evaluation(self.xe[0])
        
    def create_xce(self):
        self.xce = (1 + self.p * self.r ) * self.centroide - (self.p* self.r *self.points[-1])
        self.xce = self.evaluation(self.xce[0])
        
    def create_xci(self):
        self.xci = (1 -  self.r ) * self.centroide + ( self.r *self.points[-1])
        self.xci = self.evaluation(self.xci[0])
        
    def iteration(self):
        self.sorting()
        self.create_centroide()
        self.create_xr()
        if self.points[0][-1] <= self.xr[-1] and self.xr[-1] <  self.points[-2][-1]:
            self.points.append(self.xr)
            self.points = self.points[1:]
            return
        if  self.xr[-1] <= self.points[0][-1]:
            self.create_xe()
            if  self.xr[-1] <= self.xe[-1]:
                self.points = self.points[:-1]
                self.points.append(self.xr)
            else:
                self.points = self.points[:-1]
                self.points.append(self.xe)
            return
        if   self.points[-2][-1] <= self.xr[-1] and  self.xr[-1] <= self.points[-1][-1]:
            self.create_xce()
            if  self.xce[-1] <= self.xr[-1]:
                self.points = self.points[:-1] 
                self.points.append(self.xce)
            else:
                self.contract()
            return
        if   self.points[-1][-1] <= self.xr[-1] :
            self.create_xci()
            if  self.xci[-1] <= self.points[-1][-1]:
                self.points = self.points[:-1]
                self.points.append(self.xci)
            else:
                self.contract()
    def contract(self):
        newpoints = [self.points[0]]
        for xi in self.points[1:]:
            point = self.points[0][0] +self.d* (xi[0] - self.points[0][0])
            newpoints.append( self.evaluation (point))
        self.points = newpoints
        
    def solve(self, iterations):
        for i in range(iterations):
            self.iteration()
        print(self.points)

In [20]:
init = datetime.datetime.now()
method = basic_nelder(np.array([-1.2, 1]) ,rosenbrock)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([1., 1.]), 4.930380657631324e-32], dtype=object), array([array([1., 1.]), 4.930380657631324e-32], dtype=object), array([array([1., 1.]), 0.0], dtype=object)]
45.14

In [21]:
init = datetime.datetime.now() #rectificar 
method = basic_nelder(np.array([0.5 ,-2]) ,freudenstein)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([11.4127789 , -0.89680526]), 48.98425367924003], dtype=object), array([array([11.41277886, -0.89680526]), 48.98425367924003], dtype=object), array([array([11.41277894, -0.89680526]), 48.98425367924003], dtype=object)]
63.98800000000001

In [22]:
init = datetime.datetime.now()
method = basic_nelder(np.array([0, 1]) ,powell)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([-0.00994809, -0.00994809]), 1.0402940039616395],
      dtype=object), array([array([-0.00994809, -0.00994809]), 1.0402940039616395],
      dtype=object), array([array([-0.00994809, -0.00994809]), 1.0402940039616395],
      dtype=object)]
55.025999999999996

In [23]:
init = datetime.datetime.now()
method = basic_nelder(np.array([1, 1]) ,brown)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([1.e+06, 2.e-06]), 4.930380657635808e-30], dtype=object), array([array([1.e+06, 2.e-06]), 7.099748146995563e-30], dtype=object), array([array([1.e+06, 2.e-06]), 3.155443620886917e-30], dtype=object)]
49.429

In [24]:
init = datetime.datetime.now()
method = basic_nelder(np.array( [1, 1]) ,beale )
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([3. , 0.5]), 3.944304526105059e-31], dtype=object), array([array([3. , 0.5]), 3.944304526105059e-31], dtype=object), array([array([3. , 0.5]), 9.860761315262648e-31], dtype=object)]
59.507

In [25]:
init = datetime.datetime.now()
method = basic_nelder(np.array([0.3, 0.4]) ,jennrich )
#method.solve(100)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


0.159

In [26]:
init = datetime.datetime.now()
method = basic_nelder(np.array([-1,0,0]) ,helical_valley  )
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([0.67446505, 0.73212104, 1.31141239]), 1.7233156694583576],
      dtype=object), array([array([0.67446505, 0.73212104, 1.3114124 ]), 1.7233156694584386],
      dtype=object), array([array([0.67446507, 0.73212105, 1.31141241]), 1.723315669458356],
      dtype=object)]
59.391

In [27]:
init = datetime.datetime.now()
method = basic_nelder(np.array([1,1,1]) ,bard )
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([0.09378985, 1.17041122, 2.32181206]), 0.019428158277877756],
      dtype=object), array([array([0.09378985, 1.17041122, 2.32181206]), 0.01942815827787778],
      dtype=object), array([array([0.09378985, 1.17041122, 2.32181206]), 0.019428158277877753],
      dtype=object)]
105.765

In [28]:
init = datetime.datetime.now()
method = basic_nelder(np.array([0.4, 1, 0]) ,gaussian)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([ 3.98884337e-01,  9.99158550e-01, -2.29485940e-05]),
       6.440827029494373e-08], dtype=object), array([array([ 3.98884341e-01,  9.99158552e-01, -2.29429622e-05]),
       6.440827035790909e-08], dtype=object), array([array([ 3.98884332e-01,  9.99158545e-01, -2.29443323e-05]),
       6.440827024755655e-08], dtype=object)]
90.23

In [29]:
init = datetime.datetime.now()
method = basic_nelder(np.array([.02, 4000, 250]) ,meyer )
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([1.02878736e-01, 3.99406333e+03, 2.63624609e+02]),
       112894.79374261426], dtype=object), array([array([1.02878735e-01, 3.99406333e+03, 2.63624608e+02]),
       112894.79374262638], dtype=object), array([array([1.02878736e-01, 3.99406333e+03, 2.63624609e+02]),
       112894.79374262437], dtype=object)]
101.804

In [30]:
init = datetime.datetime.now()
method = basic_nelder(np.array([5, 2.5, 0.15]) ,gulf)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([ 0.02460239, 12.85804622, -0.27876164]),
       3.44570989296555e-07], dtype=object), array([array([ 0.02460239, 12.85804622, -0.27876164]),
       3.445709892965578e-07], dtype=object), array([array([ 0.02460239, 12.85804622, -0.27876164]),
       3.445709892965581e-07], dtype=object)]
59.497

In [31]:
init = datetime.datetime.now()
method = basic_nelder(np.array([0, 10, 20]) ,box3dimecional)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([ 0.03139738, 18.12480369,  1.46786423]),
       0.005036406339060495], dtype=object), array([array([ 0.03139741, 18.12480373,  1.46786421]),
       0.005036406339060504], dtype=object), array([array([ 0.0313974 , 18.12480372,  1.46786422]),
       0.005036406339060503], dtype=object)]
63.694

In [32]:
init = datetime.datetime.now()
method = basic_nelder(np.array([3, -1, 0, 1]) ,powell_singular)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([ 0.93555299, -0.74334737, -1.24293857, -0.84794906]),
       153.40279005957387], dtype=object), array([array([ 0.93555302, -0.74334736, -1.24293854, -0.84794903]),
       153.40279005957387], dtype=object), array([array([ 0.93555301, -0.74334736, -1.24293855, -0.84794904]),
       153.40279005957387], dtype=object)]
57.847

In [33]:
init = datetime.datetime.now()
method = basic_nelder(np.array([-3,-1,-3,-1]) ,wood)
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([-2.35213996,  0.16026227, -1.05489043,  1.23886326]),
       582.2945073901153], dtype=object), array([array([-2.35213996,  0.16026226, -1.05489044,  1.23886326]),
       582.2945073901154], dtype=object), array([array([-2.35213996,  0.16026226, -1.05489043,  1.23886327]),
       582.2945073901154], dtype=object)]
50.642

In [34]:
init = datetime.datetime.now()
method = basic_nelder(np.array([0.25, 0.39, 0.415, 0.39]) ,kowalik )
method.solve(1000)
end = datetime.datetime.now()
print((end - init).total_seconds() * 1000)


[array([array([0.2112353 , 0.63342594, 0.69039832, 0.29727663]),
       0.0009030557880939211], dtype=object), array([array([0.2112353 , 0.63342595, 0.69039833, 0.29727663]),
       0.0009030557880939213], dtype=object), array([array([0.2112353 , 0.63342595, 0.69039833, 0.29727663]),
       0.0009030557880939212], dtype=object)]
85.169

In [58]:
x = tf.Variable([-1.2, 1])
f = tf_rosenbrock(x)


with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op);
    #la funcion solo returna los nodos del grafo que deben ser ejecutados, a la final yo los encadeno en evaluar el assign del 
    #valor en el que deben terminar las variables 
    x0 = tf.convert_to_tensor([1,1])
    x1 = tf.convert_to_tensor([[2,1],[1,1]])
    x2 = tf.convert_to_tensor([[3,1],[1,1]])
    evaluatex0 = tf_rosenbrock(x0)
    evaluatex1 = tf_rosenbrock(x1)
    evaluatex2 = tf_rosenbrock(x2)
    #compare = tf.reduce_min([evaluatex0,evaluatex1])
    getmin = tf.equal(x1,x2)
    print(tf.math.reduce_any(getmin).eval())
    c = tf.constant([[8,2,9,4], [4, 3, 2, 1], [5,6,7,2]])
    print(tf.segment_max(c, tf.constant([0,0,1])).eval())
    print(compare.eval())
    print(sess.run(evaluatex1))
    print(sess.run(compare))


True
[[8 3 9 4]
 [5 6 7 2]]
0
[101   0]
0

In [36]:
# nelder-mead con tensores

class Nelder_Mead(list):

    p = 1
    d = 2
    r = 0.5
    g = 0.5
    
    def __init__(self,x,f):
        self.f = f
        self.x = x
        evalx = self.evaluation(x)
        self.points = [[x, evalx]]
        self.evaluations = [evalx]
        self.generate()
        
    def evaluation(self,x):
        return self.f(x)
    
    def generate(self):
        x2 = self.x + tf.random.uniform()
        x2eval = self.evaluation(x2)
        x3 = self.x + tf.random.uniform()
        x3eval = self.evaluation(x3)
        self.points.append([x2, x2eval])
        self.points.append([x3, x3eval])
    
    def sorting(self):
        
        self.points = sorted(self.points, key=lambda x: x[-1])
    
    def create_centroide(self):
        size = len(self.points) - 1
        self.centroide =  np.sum(self.points[:-1], axis = 0) / size 
        
    def create_xr(self):
        self.xr = self.centroide + self.p * ( self.centroide - self.points[-1])
        self.xr = self.evaluation(self.xr[0])
        
    def create_xe(self):
        self.xe = (1 + self.p * self.d ) * self.centroide - (self.p* self.d*self.points[-1])
        self.xe = self.evaluation(self.xe[0])
        
    def create_xce(self):
        self.xce = (1 + self.p * self.r ) * self.centroide - (self.p* self.r *self.points[-1])
        self.xce = self.evaluation(self.xce[0])
        
    def create_xci(self):
        self.xci = (1 -  self.r ) * self.centroide + ( self.r *self.points[-1])
        self.xci = self.evaluation(self.xci[0])
        
    def iteration(self):
        self.sorting()
        self.create_centroide()
        self.create_xr()
        if self.points[0][-1] <= self.xr[-1] and self.xr[-1] <  self.points[-2][-1]:
            self.points.append(self.xr)
            self.points = self.points[1:]
            return
        if  self.xr[-1] <= self.points[0][-1]:
            self.create_xe()
            if  self.xr[-1] <= self.xe[-1]:
                self.points = self.points[:-1]
                self.points.append(self.xr)
            else:
                self.points = self.points[:-1]
                self.points.append(self.xe)
            return
        if   self.points[-2][-1] <= self.xr[-1] and  self.xr[-1] <= self.points[-1][-1]:
            self.create_xce()
            if  self.xce[-1] <= self.xr[-1]:
                self.points = self.points[:-1] 
                self.points.append(self.xce)
            else:
                self.contract()
            return
        if   self.points[-1][-1] <= self.xr[-1] :
            self.create_xci()
            if  self.xci[-1] <= self.points[-1][-1]:
                self.points = self.points[:-1]
                self.points.append(self.xci)
            else:
                self.contract()
    def contract(self):
        newpoints = [self.points[0]]
        for xi in self.points[1:]:
            point = self.points[0][0] +self.d* (xi[0] - self.points[0][0])
            newpoints.append( self.evaluation (point))
        self.points = newpoints
        
    def solve(self, iterations):
        for i in range(iterations):
            self.iteration()
        print(self.points)

In [76]:
centroide([[1,2],[3,4]])


Out[76]:
array([3., 7.])

In [97]:
q = [tf.convert_to_tensor([13,1,20]), tf.convert_to_tensor([2,10,10]),tf.convert_to_tensor([3,2,30])]
t =tf.argsort(q)
with tf.Session() as sess:
    print(sess.run(t))
    print(sess.run(b))
print(d)


[[1 0 2]
 [0 1 2]
 [1 0 2]]
[[1 0 2]
 [0 1 2]
 [1 0 2]]
[<tf.Tensor 'Const_117:0' shape=(3,) dtype=int32>, <tf.Tensor 'Const_118:0' shape=(3,) dtype=int32>, <tf.Tensor 'Const_119:0' shape=(3,) dtype=int32>]

In [72]:
sorted(array, key=lambda x: x[-1])


Out[72]:
[array([ 2, 10, 10], dtype=int32),
 array([ 3,  1, 20], dtype=int32),
 array([ 3,  2, 30], dtype=int32)]

In [115]:
for i in rep(1,1000):
    i


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-115-50c1233b2328> in <module>
----> 1 for i in rep(1,1000):
      2     i

NameError: name 'rep' is not defined

In [ ]: