フィルターのクリア

Convert python code to matlab code

3 ビュー (過去 30 日間)
Usman Taya
Usman Taya 2020 年 4 月 5 日
編集済み: Usman Taya 2020 年 4 月 6 日
I need to convert following python codes to Matlab. Can anyone help me to convert following codes.
for i in range(0,PopSize):
#pos[i,:]=checkBounds(pos[i,:],lb,ub)
for j in range(dim):
pos[i, j] = numpy.clip(pos[i,j], lb[j], ub[j])
#Calculate objective function for each particle
fitness=objf(pos[i,:])

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
Something like this
pos(pos < lb) = lb;
pos(pos > ub) = ub;
fitness = zeros(1,PopSize);
for i=1:PopSize
fitness(i)=objf(pos(i,:))
end
Also, see this basic course to get started in MATLAB: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 5 日
Can you share your objective function objf and the matrix pos?
Usman Taya
Usman Taya 2020 年 4 月 6 日
編集済み: Usman Taya 2020 年 4 月 6 日
It is full code.
def PSO(objf,lb,ub,dim,PopSize,iters):
# PSO parameters
dim=2
iters=200
Vmax=6
PopSize=50 #population size
wMax=0.9
wMin=0.2
c1=2
c2=2
lb=[-10 -10]
ub=[10 10]
s=solution()
vel=numpy.zeros((PopSize,dim))
pBestScore=numpy.zeros(PopSize)
pBestScore.fill(float("inf"))
pBest=numpy.zeros((PopSize,dim))
gBest=numpy.zeros(dim)
gBestScore=float("inf")
pos = numpy.zeros((PopSize, dim))
for i in range(dim):
pos[:, i] = numpy.random.uniform(0,1, PopSize) * (ub[i] - lb[i]) + lb[i]
convergence_curve=numpy.zeros(iters)
############################################
print("PSO is optimizing \""+objf.__name__+"\"")
timerStart=time.time()
s.startTime=time.strftime("%Y-%m-%d-%H-%M-%S")
for l in range(0,iters):
for i in range(0,PopSize):
for j in range(dim):
pos[i, j] = numpy.clip(pos[i,j], lb[j], ub[j])
#Calculate objective function for each particle
fitness=objf(pos[i,:])
if(pBestScore[i]>fitness):
pBestScore[i]=fitness
pBest[i,:]=pos[i,:].copy()
if(gBestScore>fitness):
gBestScore=fitness
gBest=pos[i,:].copy()
#Update the W of PSO
w=wMax-l*((wMax-wMin)/iters);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by