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")

36 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
  5. 46D0032ADC
    İnternet üzerinde gezinirken ilginizi çeken içerikleri daha sonra tekrar göz atmak için kaydetmek isteyebilirsiniz. Bu noktada, "instagram kaydetme" işlemi oldukça kullanışlıdır ve pratik bir çözüm sunar. Eğer bu konuda daha fazla bilgi almak veya hizmet satın almak isterseniz, [instagram kaydetme](https://begenisatinal.com.tr/221/instagram-kaydetme-satin-al) sayfasını ziyaret edebilirsiniz. Böylece, sevdiğiniz paylaşımlara kolayca ulaşmayı sürdürebilirsiniz.
    Instagram'da reels videolarınızın daha fazla kişiye ulaşmasını istiyorsanız, "reels izlenme satın al" seçeneğini değerlendirebilirsiniz. Bu sayede içeriklerinizin görünürlüğü artarken, takipçi sayınız da hızla büyüyebilir. Daha fazla etkileşim ve dikkat çekmek için bu yöntemi kullanmak faydalı olabilir. Detaylar ve güvenilir hizmetler için https://begenisatinal.com.tr/196/reels-izlenme-satin-al adresini ziyaret edebilirsiniz.
    İnternet üzerindeki sosyal medya varlığınızı güçlendirmek ister misiniz? Bu konuda size yardımcı olabilecek çeşitli yöntemler bulunmaktadır. Örneğin, kwai takipçi satın al seçeneği, hesabınızın görünürlüğünü artırmak ve daha fazla etkileşim almak için tercih edilebilir. Bu sayede, içerikleriniz daha geniş kitlelere ulaşabilir ve profiliniz daha popüler hale gelebilir. Unutmayın, doğru stratejilerle sosyal medya başarınızı katlayabilirsiniz.
    İşte sosyal medya etkileşimini artırmak isteyenler için çeşitli yöntemler mevcut. Örneğin, daha fazla görünürlük sağlamak adına reels beğeni satın almak tercih edilen bir seçenek olabilir. Bu sayede içerikleriniz daha geniş kitlelere ulaşabilir ve takipçi sayınız hızla artabilir. Ancak, organik büyüme ve etkili stratejilerle desteklenmediğinde, uzun vadeli başarı zorlaşabilir.

    ReplyDelete