将以下代码转换为python:function newpop=zmutate(pop,popsize,pm1,pm2,fitness1,M,N,Tn0,Tn1,Q,ST0,maxT,t,maxgen,LCR,ECR,MCR,FC,ICR) %M为辅助坑道数量;N为单元数 x=pop(:,1:2*M+1);%分段点位置 y=pop(:,2*M+2:4*M+2);%是否选择该分段点 z=pop(:,4*M+3:6*M+4);%开挖方向 W=pop(:,6*M+5:8*M+6);%作业班次 lenx=length(x(1,:)); leny=length(y(1,:)); lenz=length(z(1,:)); lenW=length(W(1,:)); avefit=sum(fitness1)/popsize; worstfit=min(fitness1); % sumy=sum(y); % lenz=sumy+1; % lenW=sumy+1; for i=1:popsize %选择popsize次,每次选择一个,输出一个 %随机选择一个染色体 pick=rand; while pick==0 pick=rand; end index=ceil(pick*popsize); f1=fitness1(index); if f1<=avefit % pm=(exp(-t/maxgen))*(pm1-(pm1-pm2)*(f1-avefit)/max(fitness1)-avefit); pm=1/(1+exp(t/maxgen))*(pm1-(pm1-pm2)*(f1-avefit)/max(fitness1)-avefit); else % pm=(exp(-t/maxgen))*pm1; pm=1/(1+exp(t/maxgen))*pm1; end pick=rand; while pick==0 pick=rand; end if pick>pm continue; end % flag0=0; % while(flag0==0) %随机选择变异位置 pick1=rand; pick2=rand; pick3=rand; pick4=rand; while pick1*pick2*pick3*pick4==0 pick1=rand; pick2=rand; pick3=rand; pick4=rand; end posx=ceil(pick1*lenx); posy=ceil(pick2*leny); %x,y变异 randx=randi([1,N-1]); while ismember(randx,x(index,:)) randx=randi([1,N-1]); end b=x(index,posx); x(index,posx)=randx; a=[0 1]; c=y(index,posy); y(index,posy)=setxor(y(index,posy),a); %z,W变异 posz=ceil(pick3*lenz); posW=ceil(pick4*lenW); d=z(index,posz); z(index,posz)=setxor(z(index,posz),a); randW=randi([1,3]); while randW==W(index,posW) randW=randi([1,3]); end e=W(index,posW); W(index,posW)=randW; mpop=[x(index,:),y(index,:),z(index,:),W(index,:)]; mtime=ztime(mpop,M,N,Tn0,Tn1,Q,ST0); mutfit=zcost(mpop,M,N,mtime(:,1),mtime(:,2:2*M+3),mtime(:,2*M+4:2*M+2+N),LCR,ECR,MCR,FC,ICR,Q); if mtime(:,1)>maxT||mutfit<=worstfit x(index,posx)=b; y(index,posy)=c; z(index,posz)=d; W(index,posW)=e; end end newpop=[x,y,z,W]; end
时间: 2023-08-26 19:05:21 浏览: 81
以下是Python代码:
```python
import numpy as np
def zmutate(pop, popsize, pm1, pm2, fitness1, M, N, Tn0, Tn1, Q, ST0, maxT, t, maxgen, LCR, ECR, MCR, FC, ICR):
# M为辅助坑道数量;N为单元数
x = pop[:, :2*M+1] # 分段点位置
y = pop[:, 2*M+2:4*M+2] # 是否选择该分段点
z = pop[:, 4*M+3:6*M+4] # 开挖方向
W = pop[:, 6*M+5:8*M+6] # 作业班次
lenx = x.shape[1]
leny = y.shape[1]
lenz = z.shape[1]
lenW = W.shape[1]
avefit = np.sum(fitness1) / popsize
worstfit = np.min(fitness1)
newpop = np.zeros(pop.shape)
for i in range(popsize):
# 随机选择一个染色体
pick = np.random.rand()
while pick == 0:
pick = np.random.rand()
index = int(np.ceil(pick * popsize))
f1 = fitness1[index]
if f1 <= avefit:
pm = (np.exp(-t / maxgen)) * (pm1 - (pm1 - pm2) * (f1 - avefit) / np.max(fitness1) - avefit)
pm = 1 / (1 + np.exp(t / maxgen)) * (pm1 - (pm1 - pm2) * (f1 - avefit) / np.max(fitness1) - avefit)
else:
pm = (np.exp(-t / maxgen)) * pm1
pm = 1 / (1 + np.exp(t / maxgen)) * pm1
pick = np.random.rand()
while pick == 0:
pick = np.random.rand()
if pick > pm:
newpop[i, :] = pop[index, :]
continue
# 随机选择变异位置
pick1 = np.random.rand()
pick2 = np.random.rand()
pick3 = np.random.rand()
pick4 = np.random.rand()
while pick1 * pick2 * pick3 * pick4 == 0:
pick1 = np.random.rand()
pick2 = np.random.rand()
pick3 = np.random.rand()
pick4 = np.random.rand()
posx = int(np.ceil(pick1 * lenx))
posy = int(np.ceil(pick2 * leny))
# x,y变异
randx = np.random.randint(1, N)
while randx in x[index, :]:
randx = np.random.randint(1, N)
b = x[index, posx]
x[index, posx] = randx
c = y[index, posy]
y[index, posy] = np.setxor1d(y[index, posy], [0, 1])
# z,W变异
posz = int(np.ceil(pick3 * lenz))
posW = int(np.ceil(pick4 * lenW))
d = z[index, posz]
z[index, posz] = np.setxor1d(z[index, posz], [0, 1])
randW = np.random.randint(1, 4)
while randW == W[index, posW]:
randW = np.random.randint(1, 4)
e = W[index, posW]
W[index, posW] = randW
mpop = np.concatenate((x[index, :], y[index, :], z[index, :], W[index, :]))
mtime = ztime(mpop, M, N, Tn0, Tn1, Q, ST0)
mutfit = zcost(mpop, M, N, mtime[:, 0], mtime[:, 1:2*M+3], mtime[:, 2*M+4:2*M+2+N], LCR, ECR, MCR, FC, ICR, Q)
if mtime[:, 0] > maxT or mutfit <= worstfit:
x[index, posx] = b
y[index, posy] = c
z[index, posz] = d
W[index, posW] = e
newpop[i, :] = np.concatenate((x[index, :], y[index, :], z[index, :], W[index, :]))
return newpop
```
注意,Python的数组下标是从0开始的,而MATLAB是从1开始的。此外,Python中的setxor函数的用法与MATLAB有些不同,需要将第二个参数改为一个数组。
阅读全文