这个MATLAB代码来自于来自Main_MOHHO.m的https://www.mathworks.com/matlabcentral/fileexchange/80776-multi-objective-harris-hawks-optimization-mohho。我想使用python编写相同的代码,但我不能使用Rabbits变量。
clc;
clear;
close all;
%% Problem Definition
nVar=3; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=0; % Lower Bound of Variables
VarMax=1; % Upper Bound of Variables
nPop=5; % Population Size
%% Initialization
empty_Rabbit.Location=[];
empty_Rabbit.Cost=[];
empty_Rabbit.Sol=[];
empty_Rabbit.IsDominated=[];
empty_Rabbit.GridIndex=[];
empty_Rabbit.GridSubIndex=[];
Rabbits=repmat(empty_Rabbit,nPop,1);
for i=1:nPop
Rabbits(i).Location = rand(VarSize).*(VarMax-VarMin)+VarMin;
X(i,:) = rand(VarSize).*(VarMax-VarMin)+VarMin;
end我试着在google上这样做。
import numpy as np
nVar = 3 # Number of Decision Variables
VarSize = np.array((1, nVar)) # Size of Decision Variables Matrix
VarMin = 0 # Lower Bound of Variables
VarMax = 1 # Upper Bound of Variables
nPop = 5 # Population Size
class empty_Rabbit:
Location = []
Cost = []
IsDominated = []
GridIndex = []
GridSubIndex = []
Sol = []
Rabbits = np.tile(empty_Rabbit, (nPop, 1))
X = np.zeros((nPop, nVar))
Rabbit_Location = np.zeros((VarSize))
Rabbit_Energy = math.inf
for i in range(nPop):
Rabbits[i, 0].Location = np.multiply(np.random.rand(VarSize[0], VarSize[1]),
(VarMax-VarMin) + VarMin)
print(Rabbits[i,0].Location)但是,对于每一行,Rabbits_Location都是相同的。
在python中创建Rabbits变量的正确方法是什么,以便输出类似于图中数字1的输出?谢谢。
发布于 2022-06-02 13:15:02
代码中存在两个问题。首先,np.tile重复相同的对象 (nPop, 1)时间。因此,当您更改其中一个对象时,您实际上更改了相同的内存位置。其次,您希望每次初始化不同的对象,而不是引用同一个对象,因此您希望编写empty_Rabbit()来创建该对象的新实例。这两种建议都可以通过像[empty_Rabbit() for i in range(nPop)]这样的理解来实现,并在需要的情况下重塑到任何新的维度。
import numpy as np
nVar = 3 # Number of Decision Variables
VarSize = np.array((1, nVar)) # Size of Decision Variables Matrix
VarMin = 0 # Lower Bound of Variables
VarMax = 1 # Upper Bound of Variables
nPop = 5 # Population Size
class empty_Rabbit:
Location = []
Cost = []
IsDominated = []
GridIndex = []
GridSubIndex = []
Sol = []
Rabbits = np.array([empty_Rabbit() for i in range(nPop)]).reshape(nPop,1)
X = np.zeros((nPop, nVar))
Rabbit_Location = np.zeros((VarSize))
Rabbit_Energy = np.inf
for i in range(nPop):
Rabbits[i, 0].Location = np.multiply(np.random.rand(VarSize[0], VarSize[1]),
(VarMax-VarMin) + VarMin)
print(Rabbits[i,0].Location)
for i in range(nPop):
print(Rabbits[i,0].Location)现在,两个print语句的输出将与不同的行相同:
[[0.5392264 0.39375339 0.59483626]]
[[0.53959355 0.91049574 0.58115175]]
[[0.46152304 0.43111977 0.06882631]]
[[0.13693784 0.82075653 0.49488394]]
[[0.06901317 0.34133836 0.91453956]]
[[0.5392264 0.39375339 0.59483626]]
[[0.53959355 0.91049574 0.58115175]]
[[0.46152304 0.43111977 0.06882631]]
[[0.13693784 0.82075653 0.49488394]]
[[0.06901317 0.34133836 0.91453956]]发布于 2022-06-02 15:16:28
scipy.io.loadmat在从MATLAB .mat文件加载struct时使用结构化数组。但我觉得这对你来说太高级了。
我认为您需要创建一组numpy数组,而不是尝试某种类或更复杂的结构。
empty_Rabbit.Location=[];
empty_Rabbit.Cost=[];
empty_Rabbit.Sol=[];
empty_Rabbit.IsDominated=[];
empty_Rabbit.GridIndex=[];
empty_Rabbit.GridSubIndex=[];反而变成
location = np.zeros(nPop)
cost = np.zeros(nPop)
sol = np.zeros(nPop)
isDominated = np.zeros(nPop) # or bool dtype?
gridIndex = np.zeros(nPop)
gridSubIndex = np.zeros(nPop)np.zeros生成一个浮点数组;对于一些您可能想要的np.zeros(nPop, dtype=int) (如果用作索引)。
rabbit= np.zeros(nPop, dtype=[('location',float), ('cost',float),('sol',float), ....])可以用来制作结构化数组,但您需要更多地了解这些内容。
MATLAB允许您自由地使用迭代,如
for i=1:nPop
Rabbits(i).Location = rand(VarSize).*(VarMax-VarMin)+VarMin;
X(i,:) = rand(VarSize).*(VarMax-VarMin)+VarMin;
end但是这很慢(因为在jit编译之前,它曾经是MATLAB )。最好是使用整个数组计算
location = np.random.rand(nPop,VarSize) * (VarMax-VarMin)+VarMin将生成一个(nPop,VarSize) 2d数组,而不是np.zeros(nPop)创建的1d数组。
看起来,X可以以同样的方式创建(不需要迭代)。
https://stackoverflow.com/questions/72472362
复制相似问题