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