가장 쉬운 REMC [python 3] by 바죠

가장 쉬운 REMC [python 3]


import random
import numpy as np
def functuser(x):
    case=3

    if case == 1:
       total=0.
       for j in range(len(x)):
           total+=(x[j])**2
    if case == 2:
#    Rastrigin
       total=10.*len(x)
       for j in range(len(x)):
           total+=x[j]**2-10.*np.cos(2.*np.pi*x[j])
    if case == 3:
#   Rosenbrock
       xarray0=np.zeros(len(x))
       for j in range(len(x)):
          xarray0[j]=x[j]
       total=sum(100.0*(xarray0[1:]-xarray0[:-1]**2.0)**2.0 + (1-xarray0[:-1])**2.0)
    if case == 4:
#   Styblinski-Tang
       total=0.
       for j in range(len(x)):
           total+=(x[j]**4-16.*x[j]**2+5.*x[j])/2.

    return total
class PARTICLE:
    def __init__(self,startx0,tmprt,xbounds,lverbo):
        self.position_i=[]
        self.qosition_i=[]
        self.position_best_i=[]
        self.obj_best_i=1e18
        self.obj_i=1e18
        self.dimensions=len(startx0)
        self.tmprt=tmprt
        if lverbo:
           print(self.tmprt)
        for j in range(self.dimensions):
            self.position_i.append(startx0[j]+(random.random()-0.5)*2.*np.sqrt(self.tmprt)*0.101)
        if random.random() < 0.8:
           for j in range(self.dimensions):
               self.position_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
        for j in range(self.dimensions):
            if self.position_i[j] > xbounds[j][1]:
               self.position_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
            if self.position_i[j] < xbounds[j][0]:
               self.position_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
        self.position_best_i=self.position_i.copy()
        self.qosition_i=self.position_i.copy()
    def evaluate(self,objfunct,xbounds):
        before=objfunct(self.position_i)
        for _ in range(20):
           for j in range(self.dimensions):
               self.qosition_i[j]=self.position_i[j]+(random.random()-0.5)*2.*np.sqrt(self.tmprt)*0.101
               if self.qosition_i[j] > xbounds[j][1]:
                  self.qosition_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
               if self.qosition_i[j] < xbounds[j][0]:
                  self.qosition_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
           after=objfunct(self.qosition_i)
           tmp=-(after-before)/self.tmprt
           if tmp > 300. :
              tmp=300.
           if tmp < -300. :
              tmp=-300.
           if min(1.,np.exp(tmp)) > random.random():
              before=after
              self.obj_i=after
              self.position_i=self.qosition_i.copy()
           if self.obj_i < self.obj_best_i :
              self.position_best_i=self.position_i.copy()
              self.obj_best_i=self.obj_i
        for _ in range(20):
           for j in range(self.dimensions):
               self.qosition_i[j]=self.position_i[j]+(random.random()-0.5)*2.*np.sqrt(self.tmprt)*0.101
               if self.qosition_i[j] > xbounds[j][1]:
                  self.qosition_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
               if self.qosition_i[j] < xbounds[j][0]:
                  self.qosition_i[j]=xbounds[j][0]+(xbounds[j][1]-xbounds[j][0])*random.random()
           after=objfunct(self.qosition_i)
           tmp=-(after-before)/self.tmprt
           if tmp > 300. :
              tmp=300.
           if tmp < -300. :
              tmp=-300.
           if min(1.,np.exp(tmp)) > random.random():
              before=after
              self.obj_i=after
              self.position_i=self.qosition_i.copy()
           if self.obj_i < self.obj_best_i :
              self.position_best_i=self.position_i.copy()
              self.obj_best_i=self.obj_i
class REMC():
    def __init__(self, objfunct, startx0, xbounds, nparticles, maxiter, verbose=False):
        obj_best_g=1e18
        position_best_g=[]
        swarm=[]
        tpset=[]
        x1vec=[]
        x2vec=[]
        for i in range(nparticles):
            tmprt=0.01+1.0*float(i)/float(nparticles-1)
            tpset.append(tmprt)
            swarm.append(PARTICLE(startx0,tmprt,xbounds,verbose))
        it=0
        while it < maxiter:
            if verbose:
               print(f'iter: {it:>6d} best solution: {obj_best_g:16.8e}')
            for i in range(nparticles):
                swarm[i].evaluate(objfunct,xbounds)
                if swarm[i].obj_i < obj_best_g :
                   position_best_g=list(swarm[i].position_best_i)
                   obj_best_g=float(swarm[i].obj_best_i)
            lxcd=False
            for i in range(nparticles-1,0,-1):
                if lxcd == True:
                   lxcd=False
                   continue
                if lxcd == False:
                   x1vec=list(swarm[i].position_i)
                   x2vec=list(swarm[i-1].position_i)
                   tmp=(1./tpset[i]-1./tpset[i-1])*(swarm[i].obj_i-swarm[i-1].obj_i)
                   if tmp > 300. :
                      tmp=300.
                   if tmp < -300. :
                      tmp=-300.
                   if min(1.,np.exp(tmp)) > random.random():
                      lxcd=True
                      swarm[i].position_i=x2vec.copy()
                      swarm[i-1].position_i=x1vec.copy()
                      print('exchanged',i,i-1)
            it+=1
        print('\nfinal solution:')
        print(f'   > {position_best_g}')
        print(f'   > {obj_best_g}\n')
        if True:
           abc=np.zeros(nparticles)
           abcvec=np.zeros((nparticles,len(startx0)))
           for i in range(nparticles):
               abc[i]=swarm[i].obj_best_i
               abcvec[i]=swarm[i].position_best_i
           idx=abc.argsort()
           abc=abc[idx]
           abcvec=abcvec[idx,:]
           for i in range(nparticles):
               print(abc[i])
               print(abcvec[i,:])

startx0=[]
xbounds=[]
for j in range(10):
    startx0.append(0.)
for j in range(len(startx0)):
    xbounds.append((-20., 20.))
REMC(functuser, startx0, xbounds, nparticles=50, maxiter=2000, verbose=True)





--------------------------------------------------------------------------------------------------------------

mpi4py를 활용한 함수 최소화
단, 함수는 모든 cpu들이 동원되어 병렬적으로 계산함. 
최종적으로 변수들의 최적값들은 1개의 cpu에서만 이루어짐. 1개의 cpu가 scipy의  minimization을 수행한다. 

scipy.optimize


#from scipy.optimize import minimize
from scipy.optimize import fmin
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

N = 100            # for testing
step = N//size   # say that N is divisible by size

def parallel_function_caller(x,stopp):
    stopp[0]=comm.bcast(stopp[0], root=0)
    summ=0
    if stopp[0]==0:
        #   your function here in parallel
        x=comm.bcast(x, root=0)
        array= np.arange(x[0]-N/2.+rank*step-42,x[0]-N/2.+(rank+1)*step-42,1.)
        summl=np.sum(np.square(array))
        summ=comm.reduce(summl,op=MPI.SUM, root=0)
        if rank==0:
            print ("value is "+str(summ))
    return summ

if rank == 0 :
   stop=[0]
   x = np.zeros(1)
   x[0]=20
   #xs = minimize(parallel_function_caller, x, args=(stop))
   xs = fmin(parallel_function_caller,x0= x, args=(stop,))
   print( "the argmin is "+str(xs))
   stop=[1]
   parallel_function_caller(x,stop)

else :
   stop=[0]
   x=np.zeros(1)
   while stop[0]==0:
      parallel_function_caller(x,stop)

--------------------------------------------------------------------------------------------------------------

import os
import sys
import numpy as np
from scipy.optimize import minimize
import scipy.optimize as optimize
from scipy.optimize import dual_annealing
from scipy.optimize import differential_evolution

def append_multiple_lines(file_name, lines_to_append):
    with open(file_name, "a+") as file_object:
        appendEOL = False
        file_object.seek(0)
        data = file_object.read(100)
        if len(data) > 0:
            appendEOL = True
        for line in lines_to_append:
            if appendEOL == True:
                file_object.write("\n")
            else:
                appendEOL = True
            file_object.write(line)
def eggholder(x):
    return (-(x[1] + 47) * np.sin(np.sqrt(abs(x[0]/2 + (x[1]  + 47))))
             -x[0] * np.sin(np.sqrt(abs(x[0] - (x[1]  + 47)))))
def rosen(x):
    """The Rosenbrock function"""
    return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

if False:
   optimize.show_options(solver='minimize',method='nelder-mead')
if False:
    x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
    ndim=len(x0)
    bnds=[]
    for _ in range(ndim):
       bnds.append((-512., 512.))
fname='input.txt'
if not os.path.isfile(fname) :
    print('input.txt is not present')
    sys.exit()
afile=open(fname,'r')
jline=0
for line in afile:
     if jline == 0:
          ndim=int(line.split()[0])
          x0=np.zeros(ndim)
     if jline > 0:
        if jline-1 < ndim:
            x0[jline-1]=float(line.split()[0])
            print(x0[jline-1])
     if jline == 1+ndim :
          ncal=int(line.split()[0])
     jline=jline+1
afile.close()
fname='bnds.txt'
if not os.path.isfile(fname) :
    print('bnds.txt is not present')
    sys.exit()
afile=open(fname,'r')
jline=0
for line in afile:
     if jline == 0:
          bnds=[]
     if jline > 0:
        if jline-1 < ndim:
            print( (float(line.split()[0]),float(line.split()[1]))   )
            bnds.append( (float(line.split()[0]),float(line.split()[1]))   )
     jline=jline+1
afile.close()
bnds=np.array(bnds)
if True:
    res = minimize(rosen, x0, method='nelder-mead', bounds=bnds, options={'xatol': 1e-8, 'disp': True})
if False:
    res = dual_annealing(rosen, x0=x0, bounds=bnds)
if False:
   res=differential_evolution(rosen, bounds=bnds, maxiter=10)
print(res.x)
print(res.fun)

lines_to_append=[]
lines_to_append.append(str(ndim))
for i in range(ndim):
    lines_to_append.append(str(res.x[i]))
lines_to_append.append(str(res.fun))
lines_to_append.append(str(ncal))
fname='output.txt'
if os.path.isfile(fname) :
    os.remove(fname)
append_multiple_lines(fname, lines_to_append)

--------------------------------------------------------------------------------------------------------------

from scipy.optimize import minimize
from scipy.optimize import fmin_slsqp
import numpy as np

def obj(x):
    return (x-1)*(x-1)
x0 = np.array([10])
res = minimize(obj,x0,method="SLSQP")
print(res.x)


def f1array(x):
    return x[0] ** 2 + x[1] ** 2
def eq_constraint(x):
    return x[0] + x[1] - 1
fmin_slsqp(f1array, np.array([1, 1]), eqcons=[eq_constraint])



def f2(x):
    return np.sqrt((x[0] - 4) ** 2 + (x[1] - 2) ** 2)
k = 1
def ieq_constraint(x):
    return np.atleast_1d(k - np.sum(np.abs(x)))
fmin_slsqp(f2, np.array([0, 0]), ieqcons=[ieq_constraint])


fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 2 * x[1] + 2},
        {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
        {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})
bnds=((0, None),(0, None))
res=minimize(fun, (2,0), method='SLSQP', bounds=bnds, constraints=cons)
print(res.fun)
print(res.x)


def obj_fun(x):
    return x[0] ** 2 + x[1] ** 2
def eq_const1(x):
    return x[0] + x[1] - 1
def ieq_const1(x): # returned value should be positive  constraint >=0
    return np.atleast_1d(1 - np.sum(np.abs(x)))

x0=np.zeros(2)
x0[0]=0.
x0[1]=0.
res = fmin_slsqp(obj_fun, x0, eqcons=[eq_const1], ieqcons=[ieq_constraint])
print(res)


def obj_fun(x):
    return x[0] ** 2 + x[1] ** 2
def eq_const1(x):
    return x[0] + x[1] - 1
def eq_const2(x):
    return x[0] + x[1] - 2
def ieq_const1(x): # returned value should be positive  constraint >=0
    return x[0]

res = fmin_slsqp(obj_fun, np.array([0, 0]), eqcons=[eq_const1, eq_const2], ieqcons=[ieq_const1])
print(res)

def obj_fun(x):
    return x[0] ** 2 + x[1] ** 2
def eq_const1(x):
    return x[0] + x[1] - 1
def eq_const2(x):
    return x[0] + x[1] - 2
def ieq_const1(x): # returned value should be positive  constraint >=0
    return x[0]

res = fmin_slsqp(obj_fun, np.array([0, 0]), eqcons=[eq_const1, eq_const2], ieqcons=[ieq_const1])
print(res)


def objective(x):
    return x[0]*x[3]*(x[0]+x[1]+x[2])+x[2]

def constraint1(x):
    return x[0]*x[1]*x[2]*x[3]-25.0

def constraint2(x):
    sum_eq = 40.0
    for i in range(4):
        sum_eq = sum_eq - x[i]**2
    return sum_eq

# initial guesses
n = 4
x0 = np.zeros(n)
x0[0] = 1.0
x0[1] = 5.0
x0[2] = 5.0
x0[3] = 1.0

# show initial objective
print('Initial SSE Objective: ' + str(objective(x0)))

# optimize
b = (1.0,5.0)
bnds = (b, b, b, b)
con1 = {'type': 'ineq', 'fun': constraint1}


from scipy.optimize import (BFGS, SR1, Bounds, NonlinearConstraint, minimize)

class problem:
  arg1 = 1
  arg2 = 2
  arg3 = 3

  def f(self,x):
    return -x[0]*self.arg1-x[1]*self.arg2

  def g(self,x):
    return x[0]-self.arg3*x[1]

p = problem()


bounds = Bounds([0,0], [2,3])
nonlinear_constraint = NonlinearConstraint(p.g, 0.0, 0.0, jac='2-point', hess=BFGS())
res = minimize(p.f,
                x0=[0,0],
                method='trust-constr',
                jac="2-point",
                hess=SR1(),
                constraints=[nonlinear_constraint],
                options={'verbose': 1},
                bounds=bounds)
print(res )

from math import cos, atan
def f(x):
    return 0.1 * x[0] * x[1]

def ineq_constraint(x):
    return x[0]**2 + x[1]**2 - (5. + 2.2 * cos(10 * atan(x[0] / x[1])))**2


con = {'type': 'ineq', 'fun': ineq_constraint}
x0 = [1, 1]
res = minimize(f, x0, method='SLSQP', constraints=con)
print(res)



--------------------------------------------------------------------------------------------------------------

Nonlinear constraints :






--------------------------------------------------------------------------------------------------------------

덧글

댓글 입력 영역

최근 포토로그