can do coding using DE algorithim
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I want to find the optimum solution using DE algorithm i tryied diffenet time but i coud not reach the optimum i now that optimum solution is $1772.77/day but I get very very lagre number . also the optimum X are not in the range. what i can change ?function alkylation_DE
% Problem settings
D = 7; % number of decision variables
NP = 70; % population size (10*D)
F = 0.5;
CR = 0.5;
maxGen = 1000;
penalty_coeff = 1e7;
% Bounds
lb = [1500, 1, 3000, 85, 90, 3, 145];
ub = [2000, 120, 3500, 93, 95, 12, 162];
% Initialize population
pop = rand(NP, D) .* (ub - lb) + lb;
fitness = arrayfun(@(i) penalized_fitness(pop(i,:), penalty_coeff), 1:NP)';
% Best solution tracking
[best_fit, best_idx] = min(fitness);
best_sol = pop(best_idx, :);
for gen = 1:maxGen
for i = 1:NP
idxs = randperm(NP, 3);
while any(idxs == i)
idxs = randperm(NP, 3);
end
a = pop(idxs(1), :);
b = pop(idxs(2), :);
c = pop(idxs(3), :);
mutant = a + F * (b - c);
mutant = max(min(mutant, ub), lb); % keep within bounds
% Crossover
trial = pop(i,:);
jrand = randi(D);
for j = 1:D
if rand < CR || j == jrand
trial(j) = mutant(j);
end
end
% Evaluate trial
trial_fit = penalized_fitness(trial, penalty_coeff);
if trial_fit < fitness(i)
pop(i,:) = trial;
fitness(i) = trial_fit;
if trial_fit < best_fit
best_fit = trial_fit;
best_sol = trial;
end
end
end
% Progress display
if mod(gen, 50) == 0
fprintf('Generation %d: Best Profit = %.4f\n', gen, -best_fit);
end
end
% Results
fprintf('\nOptimal Decision Variables:\n');
disp(best_sol);
fprintf('Maximum Profit = %.4f\n', -best_fit);
end
% Objective function with penalty for constraint violations
function f = penalized_fitness(x, penalty_coeff)
% Original objective (maximize profit => minimize -profit)
profit = 1.715*x(1)+ 0.035*x(1)*x(6) + 4.0565*x(3)+10*x(2) - 0.063*x(3)*x(5);
f = -profit;
% Constraints (14 inequalities: c(x) <= 0)
c = zeros(14,1);
% Constraint 1
c(1) = 0.0059553571 * x(6)^2 * x(1) + 0.88392857 * x(3) - 0.1175625 * x(6) * x(1) - x(1);
% Constraint 2
c(2) = 1.1088 * x(1) + 0.1303533 * x(1) * x(6) - 0.0066033 * x(1) * x(6)^2 - x(3);
% Constraint 3
c(3) = 6.66173269 * x(6)^2 + 172.39878 * x(5) - 56.596669 * x(4) - 191.20592 * x(6) - 10000;
% Constraint 4
c(4) = 1.08702 * x(6) + 0.32175 * x(4) - 0.03762 * x(6)^2 - x(5) + 56.85075;
% Constraint 5
c(5) = 0.006198 * x(7) * x(4) * x(3) + 2462.3121 * x(2) - 25.125634 * x(2) * x(4) - x(3) * x(4);
% Constraint 6
c(6) = 161.18996 * x(3) * x(4) + 5000.0 * x(2) * x(4) - 489510.0 * x(2) - x(3) * x(4) * x(7);
% Constraint 7
c(7) = 0.33 * x(7) - x(5) + 44.333333;
% Constraint 8
c(8) = 0.022556 * x(5) - 0.007595 * x(7) - 1.0;
% Constraint 9
c(9) = 0.00061 * x(3) - 0.0005 * x(1) - 1.0;
% Constraint 10
c(10) = 0.819672 * x(1) - x(3) + 0.819672;
% Constraint 11
c(11) = 24500.0 * x(2) - 250.0 * x(2) * x(4) - x(3) * x(4);
% Constraint 12
c(12) = 1020.4082 * x(4) * x(2) + 1.2244898 * x(3) * x(4) - 100000.0 * x(2);
% Constraint 13
c(13) = 6.25 * x(1) * x(6) + 6.25 * x(1) - 7.625 * x(3) - 100000.0;
% Constraint 14
c(14) = 1.22 * x(3) - x(6) * x(1) - x(1) + 1.0;
% Apply penalty if any constraint is violated
penalty = sum(max(0, c).^2);
f = f + penalty_coeff * penalty;
end
1 comentario
Torsten
el 10 de Mayo de 2025
For comparison:
fun = @(x)-(1.715*x(1)+ 0.035*x(1)*x(6) + 4.0565*x(3)+10*x(2) - 0.063*x(3)*x(5));
% Bounds
lb = [1500, 1, 3000, 85, 90, 3, 145];
ub = [2000, 120, 3500, 93, 95, 12, 162];
x0 = rand(1,7) .* (ub - lb) + lb;
x = fmincon(fun,x0,[],[],[],[],lb,ub,@nonlcon)
fun(x)
function [c,ceq] = nonlcon(x)
ceq = [];
% Constraints (14 inequalities: c(x) <= 0)
c = zeros(14,1);
% Constraint 1
c(1) = 0.0059553571 * x(6)^2 * x(1) + 0.88392857 * x(3) - 0.1175625 * x(6) * x(1) - x(1);
% Constraint 2
c(2) = 1.1088 * x(1) + 0.1303533 * x(1) * x(6) - 0.0066033 * x(1) * x(6)^2 - x(3);
% Constraint 3
c(3) = 6.66173269 * x(6)^2 + 172.39878 * x(5) - 56.596669 * x(4) - 191.20592 * x(6) - 10000;
% Constraint 4
c(4) = 1.08702 * x(6) + 0.32175 * x(4) - 0.03762 * x(6)^2 - x(5) + 56.85075;
% Constraint 5
c(5) = 0.006198 * x(7) * x(4) * x(3) + 2462.3121 * x(2) - 25.125634 * x(2) * x(4) - x(3) * x(4);
% Constraint 6
c(6) = 161.18996 * x(3) * x(4) + 5000.0 * x(2) * x(4) - 489510.0 * x(2) - x(3) * x(4) * x(7);
% Constraint 7
c(7) = 0.33 * x(7) - x(5) + 44.333333;
% Constraint 8
c(8) = 0.022556 * x(5) - 0.007595 * x(7) - 1.0;
% Constraint 9
c(9) = 0.00061 * x(3) - 0.0005 * x(1) - 1.0;
% Constraint 10
c(10) = 0.819672 * x(1) - x(3) + 0.819672;
% Constraint 11
c(11) = 24500.0 * x(2) - 250.0 * x(2) * x(4) - x(3) * x(4);
% Constraint 12
c(12) = 1020.4082 * x(4) * x(2) + 1.2244898 * x(3) * x(4) - 100000.0 * x(2);
% Constraint 13
c(13) = 6.25 * x(1) * x(6) + 6.25 * x(1) - 7.625 * x(3) - 100000.0;
% Constraint 14
c(14) = 1.22 * x(3) - x(6) * x(1) - x(1) + 1.0;
end
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!