Doing genetic algorithm with heat transfer, need help sorting input based on how close output value is to the target

8 次查看(过去 30 天)
I attched two codes, one called genetic_alg2.m which is a sample of genetic algorithm code and gen_alg_ht.m is the one I'm working on
I'm trying to find a combination of thermal conductivity and endpoint flux value that will result in certain maximum temperature and endpoint temperature
On line 138-145 of gen_alg_ht.m, I'm trying to sort my input based on how close the output value is to the target temperature. I tried copying line 75-87 of genetic_alg2.m word for word but it didn't work. Please advise.

回答(1 个)

Umang Pandey
Umang Pandey 2024-7-18
Hi Shasha,
Ensure that the LAMDA matrix is sorted correctly based on the indices obtained from sorting the PI values. Here's the corrected and integrated version of your code:
clc;
clear all;
% Given function Pi(x)
syms x;
syms Pi_b(x);
Pi_b(x) = (x + (pi/2) * sin(x))^2; % INPUT FUNCTION HERE
% Genetic Algorithm parameters
S = 6; % Number of strings per generation
P = 3; % Number of design strings to be preserved
TOL = 1E-7; % Tolerance
G = 4; % Total generations
desvar = 2; % Number of design variables per generation
gencount = 1; % Generation counter
w1 = 1000; % Weight for peak temperature
w2 = 1000; % Weight for endpoint temperature
% Target parameters
Temp_max_target = 495;
Temp_end_target = 460;
% Heat transfer parameters
alpha = 0.9; % Joule heating efficiency
Capa = 1000; % Heat capacity
E = 100; % Magnitude of electric field Volt/m
L = 0.01; % Bar length in meter
M3 = 20; % Number of cells
dens = 6000; % Density
ECond = 5E5; % Isotropic electrical conductivity S/m
temp0 = 300; % Initial temperature Kelvin
T = 1; % Simulation time seconds
del_t = 1E-5; % Time step size
% Calculations
J = E * ECond; % Current density
N = T / del_t; % Number of time discretizations
del_x = L / M3;
M3 = floor(M3); % or ceil(M3) or round(M3)
N = floor(N); % or ceil(N) or round(N)
% Initial guesses
g_Tcond_max = 495; % Upper limit conductivity guess
g_Tcond_min = 450; % Lower limit conductivity guess
g_del_max = -1E+7; % Upper limit flux guess
g_del_min = -2E+7; % Lower limit flux guess
% Matrices
% Genetic Algorithm matrices
LAMDA = rand(S, desvar); % Input TCond and flux
LAMDA(:,1) = LAMDA(:,1) * (g_Tcond_max - g_Tcond_min) + g_Tcond_min;
LAMDA(:,2) = LAMDA(:,2) * (g_del_min - g_del_max) + g_del_min;
PI = zeros(G, S); % Cost
ORIG = zeros(G, S); % Original index
PI_min = zeros(1, G); % Minimum cost
PI_avg = zeros(1, G); % Average cost
% Heat transfer matrices
Temp = zeros(M3+1, N+1); % Temperature
Temp(:,1) = temp0; % Initial condition
Temp_max_rec = zeros(G, S); % Max temp recorder
Temp_end_rec = zeros(G, S); % End temp recorder
% Tridiagonal matrix
a = zeros(M3+1, 1);
b = zeros(M3+1, 1);
c = zeros(M3+1, 1);
d = zeros(M3+1, 1);
while gencount < G
for i = 1:S
TCond = LAMDA(i,1);
del = LAMDA(i,2);
for k = 2:N+1
for j = 2:M3
a(j) = -TCond / del_x^2;
b(j) = 1 / del_t + 2 * TCond / del_x^2;
c(j) = -TCond / del_x^2;
d(j) = Temp(j,k-1) / del_t + alpha * J^2 / (dens * Capa);
end
a(1) = 0; b(1) = 1; c(1) = 0; d(1) = temp0;
a(M3+1) = 0; b(M3+1) = 1; c(M3+1) = 0; d(M3+1) = Temp(M3+1,k-1) - del;
Temp(:,k) = TDMAsolver(a, b, c, d);
end
Temp_max_rec(gencount, i) = max(Temp(:,N+1));
Temp_end_rec(gencount, i) = Temp(M3+1, N+1);
end
% Cost calculation
for p = 1:S
PI(gencount, p) = (w1 * (abs(Temp_max_rec(gencount, p) - Temp_max_target) / Temp_max_target)^3) ...
+ (w2 * (abs(Temp_end_rec(gencount, p) - Temp_end_target) / Temp_end_target)^3);
end
% Sorting LAMDA based on PI values
[new_pi, ind] = sort(PI(gencount, :));
PI(gencount, :) = new_pi;
ORIG(gencount, :) = ind;
PI_min(1, gencount) = min(new_pi);
PI_avg(1, gencount) = mean(new_pi);
% Reorder LAMDA based on sorted indices
LAMDA = LAMDA(ind, :);
% Preserve the best P design strings
LAMDA_new = LAMDA(1:P, :);
% Generate new design strings for the next generation
for i = P+1:S
parent1 = LAMDA(randi([1, P]), :);
parent2 = LAMDA(randi([1, P]), :);
child = (parent1 + parent2) / 2; % Simple crossover
mutation_factor = 0.1; % Mutation factor
child = child + mutation_factor * (2 * rand(1, desvar) - 1); % Mutation
LAMDA_new = [LAMDA_new; child];
end
LAMDA = LAMDA_new;
gencount = gencount + 1;
end
% Output results
disp('Minimum cost per generation:');
disp(PI_min);
disp('Average cost per generation:');
disp(PI_avg);
function x = TDMAsolver(a, b, c, d)
% TDMAsolver: TriDiagonal Matrix Algorithm (Thomas Algorithm) solver
% a, b, c are the column vectors for the compressed tridiagonal matrix, d is the right-hand side column vector.
% x is the solution column vector.
n = length(b);
c(1) = c(1) / b(1);
d(1) = d(1) / b(1);
for i = 2:n
temp = b(i) - a(i) * c(i-1);
c(i) = c(i) / temp;
d(i) = (d(i) - a(i) * d(i-1)) / temp;
end
x(n) = d(n);
for i = n-1:-1:1
x(i) = d(i) - c(i) * x(i+1);
end
end
Best,
Umang

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by