Python 相同的代码但不同的结果,怎么可能呢?
我已经把matlab的代码写成了python代码,但是它给出了不同的最佳适配值,这怎么可能呢?我想把这个matlab代码写成python,是不是方法不对?我应该实现不同的方法吗?matlabs最佳适配值为340,pytons最佳适配值为17?这怎么可能? 这是python版本Python 相同的代码但不同的结果,怎么可能呢?,python,matlab,numpy,Python,Matlab,Numpy,我已经把matlab的代码写成了python代码,但是它给出了不同的最佳适配值,这怎么可能呢?我想把这个matlab代码写成python,是不是方法不对?我应该实现不同的方法吗?matlabs最佳适配值为340,pytons最佳适配值为17?这怎么可能? 这是python版本 import numpy as np coord=[] coord = np.array([[11, 47, 62], [31, 37, 69], [32, 38, 46],
import numpy as np
coord=[]
coord = np.array([[11, 47, 62],
[31, 37, 69],
[32, 38, 46],
[33, 46, 10],
[34, 61, 33],
[35, 62, 63],
[36, 63, 69],
[37, 32, 22],
[38, 45, 35],
[39, 59, 15],
[40, 5, 6],
[41, 10, 17],
[42, 21, 10],
[43, 5, 64],
[44, 30, 15],
[45, 39, 10],
[46, 32, 39],
[47, 25, 32],
[48, 25, 55],
[49, 48, 28],
[50, 56, 37],
[61, 40, 50]],dtype=np.int)
city=len(coord)
best_solution=[]
best_fitness=100000000
pop_size=1000
CR=0.8 # %YÜZDE 90 OLASILIKLA CAPRAZLANIYOR.
MR=0.5
MaxIter=5000
# distance = np.zeros((coord.shape[0], coord.shape[0]))
distance = np.zeros([city,city])
for i in range(city):
for j in range(city):
distance[i][j] = np.sqrt((coord[i][1] - coord[j][1]) ** 2 + (coord[i][2] - coord[j][2]) ** 2)
population=np.zeros([pop_size,city],dtype=np.int)
for i in range(pop_size):
population[i][:]=np.random.permutation(city)
fitness=np.zeros([1,pop_size])
for i in range(pop_size):
fitness[0][i]=0
for j in range(city-1):
# fitness[0][i]=fitness[0][i]+distance[[population[i][j]][population[i][j+1]]]
fitness[0][i]=fitness[0][i]+ distance[population[i][j]][population[i][j+1]]
fitness[0][i]=fitness[0][i]+ distance[population[i][city-1]][population[i][1]]
if best_fitness > fitness[0][i]:
best_solution=population[i][:]
best_fitness=fitness[0][i]
b=np.min(fitness) # there is no lower than this value but it gives 13 or 20.
这是matlab版本
clc
clear
coord=[11 47 62;
31 37 69;
32 38 46;
33 46 10;
34 61 33;
35 62 63;
36 63 69;
37 32 22;
38 45 35;
39 59 15;
40 5 6;
41 10 17;
42 21 10;
43 5 64;
44 30 15;
45 39 10;
46 32 39;
47 25 32;
48 25 55;
49 48 28;
50 56 37;
61 40 50];
[city,~]=size(coord);
best_solution=[];
best_fitness=100000000;
pop_size=1000;
CR=0.8; %YÜZDE 90 OLASILIKLA CAPRAZLANIYOR.
MR=0.5;
MaxIter=5000;
%51x51 matriste her şehirden şehire uzaklıgı bul
distance=[];
for i=1:city
for j=1:city
distance(i,j)=sqrt((coord(i,2)-coord(j,2))^2+(coord(i,3)-coord(j,3))^2);
end
end
%rastgele 100 tane cozum olusturdu.
population=[];
for i=1:pop_size
population(i,:)=randperm(city);
end
%population
for i=1:pop_size
fitness(i)=0; %başta bos olusturuyor dikkat.
for j=1:city-1
%dk 32:48
%mesela 14 ile 22. sutunun arasındaki mesafe
%j+1 var diye city -1 yazdık.
%sırayla 2 sehir arasındaki mesafe hesaplanıyor.
fitness(i)=fitness(i)+distance(population(i,j),population(i,j+1));
end
%son sehirden bastaki sehire donme icin.
fitness(i)=fitness(i)+distance(population(i,city),population(i,1));
if best_fitness>fitness(i) %fitness(i)<best_fitness
best_solution=population(i,:);
best_fitness=fitness(i);
end
end
由于随机生成涉及randperm和np.random.permutation,因此最佳适应度在每次迭代中都会发生变化。但是你是对的,Python脚本生成的数字大约是10-20,而Matlab则是数百 在python脚本中,您正在调用: 适合度[0][i]=适合度[0][i]+距离[population[i][j]][population[i][j+1]] 适合度[0][i]=适合度[0][i]+距离[population[i][city-1]][population[i][1]] 在相同的for循环中,Matlab脚本中不会出现这种情况,请删除python脚本中的缩进,如下所示:
import numpy as np
coord=[]
coord = np.array([[11, 47, 62],
[31, 37, 69],
[32, 38, 46],
[33, 46, 10],
[34, 61, 33],
[35, 62, 63],
[36, 63, 69],
[37, 32, 22],
[38, 45, 35],
[39, 59, 15],
[40, 5, 6],
[41, 10, 17],
[42, 21, 10],
[43, 5, 64],
[44, 30, 15],
[45, 39, 10],
[46, 32, 39],
[47, 25, 32],
[48, 25, 55],
[49, 48, 28],
[50, 56, 37],
[61, 40, 50]],dtype=np.int)
city=len(coord)
best_solution=[]
best_fitness=100000000
pop_size=1000
CR=0.8 # %YÜZDE 90 OLASILIKLA CAPRAZLANIYOR.
MR=0.5
MaxIter=5000
# distance = np.zeros((coord.shape[0], coord.shape[0]))
distance = np.zeros([city,city])
for i in range(city):
for j in range(city):
distance[i][j] = np.sqrt((coord[i][1] - coord[j][1]) ** 2 + (coord[i][2] - coord[j][2]) ** 2)
#print(distance)
population=np.zeros([pop_size,city],dtype=np.int)
for i in range(pop_size):
population[i][:]=np.random.permutation(city)
fitness=np.zeros([1,pop_size])
for i in range(pop_size):
fitness[0][i]=0
for j in range(city-1):
# fitness[0][i]=fitness[0][i]+distance[[population[i][j]][population[i][j+1]]]
fitness[0][i]=fitness[0][i]+ distance[population[i][j]][population[i][j+1]]
fitness[0][i]=fitness[0][i]+ distance[population[i][city-1]][population[i][1]]
if best_fitness > fitness[0][i]:
best_solution=population[i][:]
best_fitness=fitness[0][i]
b=np.min(fitness) # there is no lower than this value but it gives 13 or 20.
但这条线的适合度[0][i]=适合度[0][i]+距离[population[i][city-1]][population[i][1]]应该在i循环中。它在循环中吗?在spider程序中,它看起来像是脱离了i循环。我通过命令提示符在python解释器中运行脚本,在上面的示例中:fitness[0][i]=fitness[0][i]+distance[population[i][city-1]][population[i][1]]在i for循环中,fitness[0][i]=fitness[0][i]+distance[population[i][i][j]]我的第二个问题是:我知道matlabs和pythons,0-1索引。但我在python中没有使用-1,除了最后一个适应度方程。为什么我必须使用city-1???为什么在其他代码中没有任何-1。还有第一个方程,没有city-1,在i和j循环中。第二健身功能。在matlab中只有i循环。对于j for循环,在matlab和python脚本中都有city-1,这是因为您正在调用人口[j+1],它比city的长度限制高一个。记住,如果有任何有用的评论或答案,请向上投票: