keep solution from function into an array

3 次查看(过去 30 天)
i want to save solution from a function into an array, there will be x & fval loop input, and i want to save all the incumbent from the previous input to find the maximums ones, can someone help me? sorry for my english
function [x,fval] = BnB(f, A, b, Aeq, beq, lb, ub)
[x,fval] = linprog(f,A,b,Aeq,beq,lb,ub);
[incumbent] = updateincumbent(x,fval)
for i=1:length(x)
if --- && --- && fval>=incumbent
K = ;
K(i)=;
Aeq1 = [--];
beq1 = [--];
Aeq2 = [--];
beq2 = [--];
[x1,fval1] = BnB(f, A, b, Aeq1, beq1, lb, ub);
[x2,fval2] = BnB(f, A, b, Aeq2, beq2, lb, ub);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%incumbent function file
function [incumbent] = updateincumbent(x, fval)
if mod(x,1)==0 & isempty(fval)==0
incumbent=[fval];
else
incumbent=[0];
%here i want to save the incumbent into an array with the previous
%incumbent then saerch the maximum ones
incumbent=max(incumbent)

回答(1 个)

LeoAiE
LeoAiE 2023-5-9
function [x, fval, incumbent] = BnB(f, A, b, Aeq, beq, lb, ub, incumbent)
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub);
[incumbent] = updateincumbent(x, fval, incumbent);
for i = 1:length(x)
if --- && --- && fval >= incumbent
K = ;
K(i) = ;
Aeq1 = [--];
beq1 = [--];
Aeq2 = [--];
beq2 = [--];
[x1, fval1, incumbent] = BnB(f, A, b, Aeq1, beq1, lb, ub, incumbent);
[x2, fval2, incumbent] = BnB(f, A, b, Aeq2, beq2, lb, ub, incumbent);
end
end
function [incumbent] = updateincumbent(x, fval, incumbent)
if mod(x, 1) == 0 && isempty(fval) == 0
incumbent = [incumbent, fval];
else
incumbent = [incumbent, 0];
end
incumbent = max(incumbent);
[x, fval, incumbent] = BnB(f, A, b, Aeq, beq, lb, ub, []);

类别

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