Я написал код Matlab для кода python, но он дает другое значение "best_fitness", как это возможно? Я хочу записать этот код Matlab в python, это неправильный метод? Стоит ли использовать другой метод? matlabs значение best_fitness = 340, значение pytons best_fitness = 17? как это возможно? Вот версия 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)
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