Thursday, October 18, 2018

A simple example of 3D-FOA by Python

##A simple example of 3D-FOA by Python
# Find the min value of QF by FOA

"""
author: Wei-Yuan Lin
Soochow University, Taipei
Taiwan
Ver.2
"""

import numpy as np
import matplotlib.pyplot as plt

Population = 100
Gen = 100
def initParams(bestX,bestY,bestZ,Population):
    location = []
    fitness = []
    for num in range(Population):
        x,y,z= bestX+2*np.random.rand()-1,bestY+2*np.random.rand()-1,bestZ+2*np.random.rand()-1
        S = 1/np.sqrt(x**2+y**2+z**2)
        smell = -7+S**2
        location.append((x,y,z))
        fitness.append(smell)
   
    bestIndex = np.argmin(fitness)
    bestX,bestY,bestZ = location[bestIndex]
    bestSmell = fitness[bestIndex]
    return bestX,bestY,bestZ,bestSmell
bestX,bestY,bestZ,bestSmell = initParams(10*np.random.rand(),10*np.random.rand(),10*np.random.rand(),Population)
xlist,ylist,zlist,value = [],[],[],[]
for time in range(Gen):
    tmpX,tmpY,tmpZ,tmpSmell = initParams(bestX,bestY,bestZ,Population)
    if tmpSmell < bestSmell:
        bestX = tmpX
        bestY = tmpY
        bestZ = tmpZ
        bestSmell = tmpSmell
        xlist.append(bestX)
        ylist.append(bestY)
        zlist.append(bestZ)
        value.append(bestSmell)
# fig,ax
fig,ax = plt.subplots(1,2,figsize=(12,4))
       
plt.subplot(1, 3, 1)
plt.plot(range(1,Gen+1),value,'*',color='blue')
plt.xlabel('Gen')
plt.title('Min f(S)=-7+S**2',fontsize=16)
plt.ylabel('Best Value',fontsize=10)
plt.grid(linestyle='--')

plt.subplot(1, 3, 2)
plt.plot(xlist,ylist,'*',color='black')
plt.title('2D-Route of Best Fly',fontsize=16)
plt.xlabel('X Axis',fontsize=12)
plt.ylabel('Y Axis',fontsize=12)
plt.grid(linestyle='--')

# plt.subplot(1, 3, 3)
# add 3-D Graph
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(xlist,ylist,zlist,'*')
ax.set_title("3D-Route of Best Fly",fontsize=16)
# plt.title('3D-Route of Best Fly',fontsize=16)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
# ax.xlabel('X',fontsize=12)
# plt.ylabel('Y',fontsize=12)
# plt.zlabel('Z',fontsize=12)
plt.show()
fig.savefig("3dFOA.png")

19 comments:

  1. There is lots of Post about Python But your way of Writing is so Good & Knowledgeable. Thanks for Sharing a Nice & Informative Information
    Python Training in Delhi
    Python Training in Noida
    Python Training in Gurgaon

    ReplyDelete
  2. Live instructional courses from bunches of universities propose this as significant theme because of its dynamic upmachine learning certificationdates in the web and industry.

    ReplyDelete
  3. # Previous example
    """
    Typo corrected

    @author: Wei-Yuan Lin
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits import mplot3d

    Population = 10
    Gen = 100
    def initParams(bestX,bestY,bestZ,Population):
    location = []
    fitness = []
    for num in range(Population):
    x,y,z= bestX+2*np.random.rand()-1,bestY+2*np.random.rand()-1,bestZ+2*np.random.rand()-1
    S = 1/np.sqrt(x**2+y**2+z**2)
    smell = -7+S**2
    location.append((x,y,z))
    fitness.append(smell)

    bestIndex = np.argmin(fitness)
    bestX,bestY,bestZ = location[bestIndex]
    bestSmell = fitness[bestIndex]
    return bestX,bestY,bestZ,bestSmell
    bestX,bestY,bestZ,bestSmell = initParams(10*np.random.rand(),10*np.random.rand(),10*np.random.rand(),Population)
    xlist,ylist,zlist,value = [],[],[],[]
    for time in range(Gen):
    tmpX,tmpY,tmpZ,tmpSmell = initParams(bestX,bestY,bestZ,Population)
    if tmpSmell < bestSmell:
    bestX = tmpX
    bestY = tmpY
    bestZ = tmpZ
    bestSmell = tmpSmell
    xlist.append(bestX)
    ylist.append(bestY)
    zlist.append(bestZ)
    value.append(bestSmell)
    # fig,ax
    fig,ax = plt.subplots(1,2,figsize=(12,4))

    plt.subplot(1, 3, 1)
    plt.plot(range(1,Gen+1),value,'*',color='green')
    plt.title('FOA_Min f(S)=-7+S**2 ',fontsize=14)
    plt.xlabel('Gen',fontsize=12)
    plt.ylabel('Optimal Value',fontsize=12)
    plt.grid(linestyle='--')

    plt.subplot(1, 3, 2)
    plt.plot(xlist,ylist,'*',color='blue')
    plt.title('2D-Route of Best FOA Fly',fontsize=14)
    plt.xlabel('X ',fontsize=12)
    plt.ylabel('Y ',fontsize=12)
    plt.grid(linestyle='--')

    #plt.subplot(1, 3, 3)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot(xlist,ylist,zlist,'*')
    ax.set_title("3D-Route of Best FOA Fly",fontsize=14)

    # plt.title('3D-Route of Best Fly',fontsize=16)
    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_zlabel("Z")
    plt.show()
    fig.savefig("3dFOA.png")

    ReplyDelete
  4. t mpY -> tmpY
    it need to be done with another import
    "from mpl_toolkits.mplot3d import Axes3D;"

    ReplyDelete