gpt4 book ai didi

python - 如何正确地为这个优化模型制作动画

转载 作者:行者123 更新时间:2023-12-05 03:34:26 25 4
gpt4 key购买 nike

我实现了一种简单的随机化、基于群体的优化方法——灰狼优化器。我在使用 camera 包在每次迭代中正确捕获 Matplotlib 图时遇到了一些问题。

我正在为目标函数 f(x,y) = x^2 + y^2 运行 GWO。我只能看到候选解收敛到最小值,但没有显示等值线图。

你有什么建议吗,我怎样才能在后台显示等高线图?

enter image description here

GWO 算法实现

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera
import ffmpeg
import pillow

# X : Position vector of the initial population
# n : Initial population size

def gwo(f,max_iterations,LB,UB):

fig = plt.figure()
camera = Camera(fig)

def random_population_uniform(m,a,b):
dims = len(a)
x = [list(a + np.multiply(np.random.rand(dims),b - a)) for i in range(m)]
return np.array(x)

def search_agent_fitness(fitness):
alpha = 0
if fitness[1] < fitness[alpha]:
alpha, beta = 1, alpha
else:
beta = 1

if fitness[2] > fitness[alpha] and fitness[2] < fitness[beta]:
beta, delta = 2, beta
elif fitness[2] < fitness[alpha]:
alpha,beta,delta = 2,alpha,beta
else:
delta = 2

for i in range(3,len(fitness)):
if fitness[i] <= fitness[alpha]:
alpha, beta,delta = i, alpha, beta
elif fitness[i] > fitness[alpha] and fitness[i]<= fitness[beta]:
beta,delta = i,beta
elif fitness[i] > fitness[beta] and fitness[i]<= fitness[delta]:
delta = i

return alpha, beta, delta


def plot_search_agent_positions(f,X,alpha,beta,delta,a,b):


# Plot the positions of search agents

x = X[:,0]
y = X[:,1]


s = plt.scatter(x,y,c='gray',zorder=1)
s = plt.scatter(x[alpha],y[alpha],c='red',zorder=1)
s = plt.scatter(x[beta],y[beta],c='blue',zorder=1)
s = plt.scatter(x[delta],y[delta],c='green',zorder=1)

camera.snap()



# Initialize the position of the search agents
X = random_population_uniform(50,np.array(LB),np.array(UB))

n = len(X)

l = 1

# Plot the first image on screen
x = np.linspace(LB[0],LB[1],1000)
y = np.linspace(LB[0],UB[1],1000)

X1,X2 = np.meshgrid(x,y)


Z = f(X1,X2)
cont = plt.contour(X1,X2,Z,20,linewidths=0.75)

while (l < max_iterations):
# Take the x,y coordinates of the initial population
x = X[:,0]
y = X[:,1]

# Calculate the objective function for each search agent
fitness = list(map(f,x,y))

# Update alpha, beta and delta
alpha,beta,delta = search_agent_fitness(fitness)

# Plot search agent positions
plot_search_agent_positions(f,X,alpha,beta,delta,LB,UB)

# a decreases linearly from 2 to 0
a = 2 - l *(2 / max_iterations)

# Update the position of search agents including the Omegas
for i in range(n):
x_prey = X[alpha]

r1 = np.random.rand(2) #r1 is a random vector in [0,1] x [0,1]
r2 = np.random.rand(2) #r2 is a random vector in [0,1] x [0,1]
A1 = 2*a*r1 - a
C1 = 2*r2

D_alpha = np.abs(C1 * x_prey - X[i])
X_1 = x_prey - A1*D_alpha

x_prey = X[beta]
r1 = np.random.rand(2)
r2 = np.random.rand(2)
A2 = 2*a*r1 - a
C2 = 2*r2

D_beta = np.abs(C2 * x_prey - X[i])
X_2 = x_prey - A2*D_beta

x_prey = X[delta]
r1 = np.random.rand(2)
r2 = np.random.rand(2)
A3 = 2*a*r1 - a
C3 = 2*r2

D_delta = np.abs(C3 * x_prey - X[i])
X_3 = x_prey - A3*D_delta

X[i] = (X_1 + X_2 + X_3)/3

l = l + 1


return X[alpha],camera

函数调用

# define the objective function
def f(x,y):
return x**2 + y**2

minimizer,camera = gwo(f,7,[-10,-10],[10,10])

animation = camera.animate(interval = 1000, repeat = True,
repeat_delay = 500)

最佳答案

x = np.linspace(LB[0],LB[1],1000) 行是否可能是 x = np.linspace(LB[0], UB[1],1000) 代替?根据您当前对 x 的定义,x 是一个仅填充值 -10 的数组,这意味着您不太可能找到轮廓。您可能想要做的另一件事是将 cont = plt.contour(X1,X2,Z,20,linewidths=0.75) 行移动到您的 plot_search_agent_positions 函数中以确保在动画的每次迭代中绘制轮廓。进行这些更改后,代码如下所示:

import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera
import ffmpeg
import PIL
from matplotlib import animation, rc
from IPython.display import HTML, Image # For GIF
from scipy.interpolate import griddata

rc('animation', html='html5')

# X : Position vector of the initial population
# n : Initial population size

def gwo(f,max_iterations,LB,UB):

fig = plt.figure()
fig.gca(aspect='equal')
camera = Camera(fig)

def random_population_uniform(m,a,b):
dims = len(a)
x = [list(a + np.multiply(np.random.rand(dims),b - a)) for i in range(m)]
return np.array(x)

def search_agent_fitness(fitness):
alpha = 0
if fitness[1] < fitness[alpha]:
alpha, beta = 1, alpha
else:
beta = 1

if fitness[2] > fitness[alpha] and fitness[2] < fitness[beta]:
beta, delta = 2, beta
elif fitness[2] < fitness[alpha]:
alpha,beta,delta = 2,alpha,beta
else:
delta = 2

for i in range(3,len(fitness)):
if fitness[i] <= fitness[alpha]:
alpha, beta,delta = i, alpha, beta
elif fitness[i] > fitness[alpha] and fitness[i]<= fitness[beta]:
beta,delta = i,beta
elif fitness[i] > fitness[beta] and fitness[i]<= fitness[delta]:
delta = i

return alpha, beta, delta


def plot_search_agent_positions(f,X,alpha,beta,delta,a,b,X1,X2,Z):


# Plot the positions of search agents

x = X[:,0]
y = X[:,1]


s = plt.scatter(x,y,c='gray',zorder=1)
s = plt.scatter(x[alpha],y[alpha],c='red',zorder=1)
s = plt.scatter(x[beta],y[beta],c='blue',zorder=1)
s = plt.scatter(x[delta],y[delta],c='green',zorder=1)
Z=f(X1,X2)
cont=plt.contour(X1,X2,Z,levels=20,colors='k',norm=True)
plt.clabel(cont, cont.levels, inline=True, fontsize=10)
camera.snap()



# Initialize the position of the search agents
X = random_population_uniform(50,np.array(LB),np.array(UB))

n = len(X)

l = 1

# Plot the first image on screen
x = np.linspace(LB[0],UB[1],1000)
y = np.linspace(LB[0],UB[1],1000)

X1,X2 = np.meshgrid(x,y)
Z=f(X1,X2)

while (l < max_iterations):
# Take the x,y coordinates of the initial population
x = X[:,0]
y = X[:,1]

# Calculate the objective function for each search agent
fitness = list(map(f,x,y))

# Update alpha, beta and delta
alpha,beta,delta = search_agent_fitness(fitness)

# Plot search agent positions
plot_search_agent_positions(f,X,alpha,beta,delta,LB,UB,X1,X2,Z)

# a decreases linearly from 2 to 0
a = 2 - l *(2 / max_iterations)

# Update the position of search agents including the Omegas
for i in range(n):
x_prey = X[alpha]

r1 = np.random.rand(2) #r1 is a random vector in [0,1] x [0,1]
r2 = np.random.rand(2) #r2 is a random vector in [0,1] x [0,1]
A1 = 2*a*r1 - a
C1 = 2*r2

D_alpha = np.abs(C1 * x_prey - X[i])
X_1 = x_prey - A1*D_alpha

x_prey = X[beta]
r1 = np.random.rand(2)
r2 = np.random.rand(2)
A2 = 2*a*r1 - a
C2 = 2*r2

D_beta = np.abs(C2 * x_prey - X[i])
X_2 = x_prey - A2*D_beta

x_prey = X[delta]
r1 = np.random.rand(2)
r2 = np.random.rand(2)
A3 = 2*a*r1 - a
C3 = 2*r2

D_delta = np.abs(C3 * x_prey - X[i])
X_3 = x_prey - A3*D_delta

X[i] = (X_1 + X_2 + X_3)/3

l = l + 1


return X[alpha],camera
# define the objective function
def f(x,y):
return x**2 + y**2

minimizer,camera = gwo(f,7,[-10,-10],[10,10])

animation = camera.animate(interval = 1000, repeat = True,repeat_delay = 500)

输出给出:

enter image description here

关于python - 如何正确地为这个优化模型制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70145946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com