Problem in solving an optimization problem

1 次查看(过去 30 天)
Hi, guys. This is my code for my optimization problem:
Objective=@Case;
p0 =[1,1,1,1,1];
A = [];
b = [];
Aeq = [];
beq = [];
lb=[1;1;1;1;1];
ub=[7;7;7;7;7];
nonlc=[];
%options = optimoptions('fmincon','Algorithm','interior-point','OptimalityTolerance',1e-20,'FunctionTolerance',1e-20,'StepTolerance',1e-20);
C = fmincon(Objective, p0, A, b, Aeq, beq, lb, ub,nonlc);
disp(C)
function E=Case(p)
moleWt=[28;56;84;112;140;168;156]; % mole weight C2,C4,...,C12,C11 [g/mol]
Woo_Datas_g = {[0.377984 0.696286 2.9244 4.43634 4.63528 4.83422 4.47613],[0.656499 1.69098 5.5504 11.1605 10.882 9.33024 10.6034],[0.69628 1.53183 4.43634 5.74934 6.78382 4.66446 6.98276],[0.497347 1.17374 2.68568 3.6008 2.9244 3.95889 4.07825],[0.497347 1.01459 1.57162 2.2878 2.88462 2.96419 2.48674]}; %Woo's data set for C2,C4,..,C12
F_G_out_mmol = [0.354 0.207 0.169 0.0724 0.0179 0.278 0.291]; %Gas outlet for each case[mmol/s]
t_max = 18000; %Reaction duration[s]
F_G_out_mol = zeros(1,7); %Gas outlet for each case at the end of the reaction[mol]
for j=1:7
F_G_out_mol(j) = F_G_out_mmol(j)*t_max*1e-3;
end
C4_mol = zeros(1,7); %C4H8 product(mol)
C6_mol = zeros(1,7); %C6H12 product(mol)
C8_mol = zeros(1,7); %C8H16 product(mol)
C10_mol = zeros(1,7); %C10H24 product(mol)
C12_mol = zeros(1,7); %C12H224 product(mol)
C4_g = cell2mat(Woo_Datas_g(1)); %C4H8 product(mol)
C6_g = cell2mat(Woo_Datas_g(2)); %C6H12 product(mol)
C8_g = cell2mat(Woo_Datas_g(3)); %C8H16 product(mol)
C10_g = cell2mat(Woo_Datas_g(4)); %C10H20 product(mol)
C12_g = cell2mat(Woo_Datas_g(5)); %C12H24 product(mol)
for j=1:7
C4_mol(j) = C4_g(j)./moleWt(2);
C6_mol(j) = C6_g(j)./moleWt(3);
C8_mol(j) = C8_g(j)./moleWt(4);
C10_mol(j) = C10_g(j)./moleWt(5);
C12_mol(j) = C12_g(j)./moleWt(6);
end
a = p(1);
b = p(2);
c = p(3);
d = p(4);
e = p(5);
E = (F_G_out_mol(1)-(C4_mol(round(a))+C6_mol(round(b))+C8_mol(round(c))+C10_mol(round(d))+C12_mol(round(e))))^2;
end
As you can see I want E as my objective function. The problem is my parameters for this problem. a,b,c,d,e should be positive integers and they can't be something like 3.5, 0.5 etc. They can only be 1,2,3,..,7. The problem is I can't figure out how define this in fmincon(). Any suggestions for solving this issue?? I have tried to use floor() and run before the parameters but fmincon() gave me a weird answer (1.99 for all the parameters). I also want E to be as minimum as possible. How can I adjust the error in fmincon()??
  2 个评论
naiva saeedia
naiva saeedia 2024-4-30
I completely understand your reason. Sum of the max(Ci_moles) is so small and there is no way it's going to be equal to F_G_out_mol so what's I'm doing is just useless since F_G_out_mol is just greater but again running this code helped me to understand this.

请先登录,再进行评论。

采纳的回答

Manikanta Aditya
Manikanta Aditya 2024-4-30
The fmincon function in MATLAB is designed for continuous optimization problems, and it doesn’t directly support integer constraints. However, you can use the ga function from the Global Optimization Toolbox, which supports integer constraints.
Here’s how you can modify your code:
Objective=@Case;
p0 =[1,1,1,1,1,1,1];
A = [];
b = [];
Aeq = [];
beq = [];
lb=[1;1;1;1;1;1;1];
ub=[7;7;7;7;7;7;7];
IntCon = 1:5; % Define the indices of the variables to be integer
options = optimoptions('ga','Display','iter'); % Display iteration information
C = ga(Objective,7,A,b,Aeq,beq,lb,ub,[],IntCon,options);
disp(C)
function E=Case(p)
% ... rest of your code ...
end
In this code, IntCon is a vector that specifies which variables are integer variables, and options is used to set the options for the ga function. The ga function then solves the problem with the integer constraints.
Hope this helps.
  16 个评论
Torsten
Torsten 2024-5-1
编辑:Torsten 2024-5-1
p(1) can have values 1,2,3,4,5,6,7
p(2) can have values 1,2,3,4,5,6,7
...
p(5) can have values 1,2,3,4,5,6,7
->
[p(1),p(2),..,p(5)] can be 7*7*7*7*7 = 7^5 different vectors.
At least this is what your code suggests.
naiva saeedia
naiva saeedia 2024-5-2
Sorry for late reply. Yes you are correct. Thanks for explanation about 7^5 vectors.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by