In [1]:
%matplotlib notebook
import numpy as np
import os, string
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy as sp
import tensorflow as tf

xs = np.linspace(0,20,100)
ys = np.linspace(0,20,100)

In [2]:
X, Y = np.meshgrid(xs,ys)
A1 = 3.
mu1 = 7.
sigma1 = 13.
A2 = 6.
mu2 = 13.
sigma2 = 9.
x = A1 * np.exp(-(xs-mu1)**2/ sigma1) + A2 * np.exp(-(xs-mu2)**2/ sigma2)
y = A2 * np.exp(-(ys-mu1)**2/ sigma1) + A1 * np.exp(-(ys-mu2)**2/ sigma2)
#X, Y = np.meshgrid(x,y)
mesh = np.dot(np.mat(x).T , np.mat(y))

In [110]:
pnum = 5
pPos = np.mat(np.random.random((pnum,2)) * 20.)
#print(pPos)
pV = np.mat(-.5 + np.random.random((pnum,2)))#speed
#print(pV)
pBestPos = np.mat(pPos.copy())
pBestFit = np.mat(np.zeros((100,1)))
pFit = np.mat(pBestFit.copy())
gBestPos = np.mat(pPos[0,:].copy())
gFit = 0

In [122]:
for n in range(1):
    #if i % 100 == 0:
    #    print(i)
        
    #update_par
    pPos += pV
    pPos[:,0] = np.minimum(pPos[:,0],20.)
    pPos[:,0] = np.maximum(pPos[:,0],0.)
    pPos[:,1] = np.minimum(pPos[:,1],20.)
    pPos[:,1] = np.maximum(pPos[:,1],0.)
    tmp1 = A1 * np.exp(-np.square(pPos[:,0]-mu1)/ sigma1) + A2 * np.exp(-np.square(pPos[:,0]-mu2)/ sigma2)
    tmp2 = A2 * np.exp(-np.square(pPos[:,1]-mu1)/ sigma1) + A1 * np.exp(-np.square(pPos[:,1]-mu2)/ sigma2)
    pFit = np.multiply(tmp1,tmp2 )
    #print(pFit)
    #print(pFit.shape)
    #par.vx=par.vx+c1*rand()*(par_best.x-par.x)+c2*rand()*(par.bestx-par.x)
    #print((np.dot(np.random.random((100)), (pBestPos[:,0]-pPos[:,0]))).shape)
    #print(np.subtract(gBestPos[0,0],pPos[:,0]).shape)
    pV[:,0] += np.multiply(np.random.random((pnum,1)), (pBestPos[:,0]-pPos[:,0]))
    pV[:,0] += 2.*np.multiply(np.random.random((pnum,1)), (gBestPos[0,0]-pPos[:,0]))
    pV[:,1] += np.multiply(np.random.random((pnum,1)), (pBestPos[:,1]-pPos[:,1]))
    pV[:,1] += 2.*np.multiply(np.random.random((pnum,1)), (gBestPos[0,1]-pPos[:,1]))
    #print(pFit.shape[0])
    for i in range(pFit.shape[0]):
        if pFit[i] > pBestFit[i]:
            print('fit:',pBestFit[i],pFit[i])
            print('pos:',pBestPos[i,:],pPos[i,:])
            print('pv:',pV[i,:])
            pBestFit[i] = pFit[i]
            pBestPos[i,:]=pPos[i,:]
            
#     logic = np.array(pFit) > np.array(pBestFit)
#     ind = np.array(np.where(logic!=False)[0])
#     pBestFit[ind] = pFit[ind,0]
#     pBestPos[ind,:] = pPos[ind,:]

    if gFit < np.max(pBestFit):
        ind = np.argmax(pBestFit)
        print('g',n,ind)
        gFit = pBestFit[ind]
        gBestPos = pBestPos[ind]
        print('gpos,gfit:',gBestPos,gFit)


fit: [[ 13.7942513]] [[ 31.66744151]]
pos: [[ 15.84385025   6.07213975]] [[ 14.07075435   6.52051252]]
pv: [[ 3.42022734  4.52851008]]
fit: [[ 18.73799377]] [[ 20.27522782]]
pos: [[ 9.35613645  8.16615573]] [[ 13.90182828  11.14149369]]
pv: [[-7.2455922   2.53447591]]

In [97]:
print(pPos[:3,:],pV[:3,:])


[[  9.19070513  13.93757546]
 [ 11.48013128   0.84850827]
 [ 20.           2.48188436]] [[ 1.88741941  0.61998382]
 [-0.19457221 -4.88406169]
 [-7.55413935 -3.67563624]]

In [123]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.plot_surface(X,Y,mesh)
ax.scatter(np.array(pPos[:,1]),np.array(pPos[:,0]),np.array(pFit+2.),s=20, c='r')
ax.scatter(np.array(gBestPos[:,1]),np.array(gBestPos[:,0]),np.array(gFit+2.),s=40, c='b')


Out[123]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1201bfef0>

In [3]:
with tf.device('/cpu:0'):
    inputX = tf.placeholder('float',shape=[100,])
    inputY = tf.placeholder('float',shape=[100,])
    inputpVX = tf.placeholder('float',shape=[100,])
    inputpVY = tf.placeholder('float',shape=[100,])
    inputpFit = tf.placeholder('float',shape=[100,])
    inputpBestX = tf.placeholder('float',shape=[100,])
    inputpBestY = tf.placeholder('float',shape=[100,])
    inputpBestFit = tf.placeholder('float',shape=[100,])
    inputgBestX = tf.placeholder('float',shape=None)
    inputgBestY = tf.placeholder('float',shape=None)
    inputgFit = tf.placeholder('float',shape=None)
    
    pX = tf.Variable(tf.random_uniform([100,]) * 20.)#tf.random_uniform([100,]) * 20.
    pY = tf.Variable(tf.random_uniform([100,]) * 20.)
    pVX = tf.Variable(tf.random_uniform([100,]) * 20.)
    pVY = tf.Variable(tf.random_uniform([100,]) * 20.)
    pBestX = tf.Variable(tf.random_uniform([100,]) * 20.)#pX
    pBestY = tf.Variable(tf.random_uniform([100,]) * 20.)
    pBestFit = tf.Variable(tf.random_uniform([100,]) * 20.)
    pFit = tf.Variable(tf.random_uniform([100,]) * 20.)
    gBestX = tf.Variable(0.)
    gBestY = tf.Variable(0.)
    gFit = tf.Variable(0.)
    #test = tf.Variable(0.)
    
    pX = inputX
    pY = inputY
    pVX = inputpVX
    pVY = inputpVY
    pFit = inputpFit
    pBestX = inputpBestX
    pBestY = inputpBestY
    pBestFit = inputpBestFit
    gBestX = inputgBestX
    gBestY = inputgBestY
    gFit = inputgFit

    pX += pVX
    pY += pVY
    pX = tf.minimum(pX,20.)
    pY = tf.minimum(pY,20.)
    pX = tf.maximum(pX,0.)
    pY = tf.maximum(pY,0.)
    #print('pX:',pX.get_shape())
    tmp1 = A1 * tf.exp(-tf.square(pX - mu1) / sigma1) + A2 * tf.exp(-tf.square(pX-mu2)/ sigma2)
    tmp2 = A2 * tf.exp(-tf.square(pY - mu1) / sigma1) + A1 * tf.exp(-tf.square(pY-mu2)/ sigma2)
    #print(tmp1.get_shape(),tmp2.get_shape())
    pFit = tf.multiply(tmp1,tmp2)
    #print(pFit.get_shape())
    pVX += tf.multiply(tf.random_uniform((100,)), (pBestX-pX))
    pVX += tf.multiply(tf.random_uniform((100,)), (gBestX-pX))
    pVY += tf.multiply(tf.random_uniform((100,)), (pBestY-pY))
    pVY += tf.multiply(tf.random_uniform((100,)), (gBestY-pY))
    #print('pVX:',pVX.get_shape())
    logic = pFit > pBestFit
    #print(pFit.get_shape(),pBestFit.get_shape())
    #print(logic.get_shape())
    pBestFit  = tf.where(logic,pFit, pBestFit)
    pBestX  = tf.where(logic,pX, pBestX)
    pBestY  = tf.where(logic,pY, pBestY)
    pmaxBestFit = tf.reduce_max(pBestFit)
    #print(pmaxBestFit.get_shape(),gFit.get_shape())
    boolCond = tf.greater(pmaxBestFit,gFit)
    #print(boolCond.eval(),boolCond.get_shape())
    ind = tf.cast(tf.arg_max(pBestFit,dimension=0),tf.int32)
    #test = ind
    #print('test:',test.eval())
    gBestX = tf.cond(boolCond, lambda: pBestX[ind], lambda: tf.abs(gBestX))
    gBestY = tf.cond(boolCond, lambda: pBestY[ind], lambda: tf.abs(gBestY))
    gFit = tf.cond(boolCond, lambda: tf.abs(pmaxBestFit), lambda : tf.abs(gFit))

In [34]:
config = tf.ConfigProto(allow_soft_placement = True)
sess = tf.Session(config = config)
sess.run(tf.global_variables_initializer())
resX = np.random.random((100,))*20.
resY = np.random.random((100,))*20.
resZ = np.zeros_like(resX)
resBestZ = np.zeros_like(resX)
resBestX = np.zeros_like(resX)
resBestY = np.zeros_like(resX)
resVX = np.random.random((100,))
resVY = np.random.random((100,))
gresX = resX[0]
gresY = resY[0]
gresZ = 0.

for i in range(200):
    resX,resY,resVX,resVY,resZ,resBestZ,resBestX,resBestY,gresX,gresY,gresZ\
    = sess.run([pX,pY,pVX, pVY,pFit,pBestFit,\
                pBestX, pBestY,gBestX,gBestY,gFit], \
               feed_dict={inputX:resX, inputY:resY,\
                          inputpVX:resVX, inputpVY:resVY,inputpFit:resZ,inputpBestX:resBestX,inputpBestY:resBestY,\
                          inputpBestFit:resBestZ,inputgBestX:gresX,inputgBestY:gresY, inputgFit:gresZ})
print(gresX,gresY,gresZ)


12.8564 7.09114 37.565

In [35]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.plot_surface(X,Y,mesh)
ax.scatter(np.array(resY),np.array(resX),np.array(resZ+2.),s=20, c='r')
ax.scatter(np.array(gresY),np.array(gresX),np.array(gresZ+2.),s=40, c='b')


Out[35]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x11d2a28d0>

In [6]:
sess.close()

In [21]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#ax.plot_surface(X,Y,mesh)
ax.scatter(np.array(resY),np.array(resX),np.array(resZ+2.),s=20, c='r')
ax.scatter(np.array(gresY),np.array(gresX),np.array(gresZ+2.),s=40, c='b')


Out[21]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x11a011a20>

In [96]:
print(np.random.random((5,1)))


[[ 0.66375287]
 [ 0.26774003]
 [ 0.80165196]
 [ 0.65931166]
 [ 0.32263928]]

In [ ]: