Optimization in a for loop
Show older comments
Hi,
I have the following code which is a simiplification of a more complex one. The code contains an optimization problem (prob) that finds the optimum values of U1 and U2 to minimize the Root-Mean-Square-Error (prob.Objective) between the simulated (Heat_flow) and measured (M) data. As you can see, the code works and actually finds the optimum solutions. However, those solutions vary for each iteration of the loop.
What I would like to do, is to have only two optimum solutions (for U1 and U2, respectively) that do not vary at each iteration. I tried to introduce a constraint (prob.Constraints.U1=eq(U1,U1(1))), but the solver finds a solution for the first iteration and then applies it to the following ones.
How can I do it?
Thank you!
%Input data (outdoor temperature and areas)
T_out=[18 16 14 15 19 14 17];
Area_1=16;
Area_2=19;
%Measured data
M=[79 84 64 76 86 56 76]';
%Definition of the optimization problem
prob=optimproblem("Description","Esempio");
%Definition of the optimization variables (U-values)
U1 = optimvar('U1',7,1,"LowerBound",0);
U2 = optimvar('U2',7,1,"LowerBound",0);
%Calculation of the heat transfer coefficient
Heat_transfer=U1*Area_1+U2*Area_2;
%Loop for perfoming the calculation for each measurement
Heat_flow=[];
for i=1:max(size(T_out))
Heat_flow=Heat_transfer*(20-T_out(i));
end
%Definition of the objective function (minimization of RMSE)
prob.Objective=((sum((M-Heat_flow).^2))/7)^(1/2);
%Definition of the initial guess (not a linear problem)
initialGuess.U1 = [1 1 1 1 1 1 1];
initialGuess.U2 = [1 1 1 1 1 1 1];
%Solution of the problem
[sol,opt]=solve(prob,initialGuess)
4 Comments
The optimization problem is not formulated correctly, specifically the objective function. Can you describe the objective function, both looped and without loop?
% Input data (outdoor temperature and areas)
T_out = [18 16 14 15 19 14 17];
Area_1 = 16;
Area_2 = 19;
% Measured data
M = [79 84 64 76 86 56 76]';
% Definition of the optimization problem
prob = optimproblem("Description", "Esempio");
% Definition of the optimization variables (U-values)
U1 = optimvar('U1', 7, 1, "LowerBound", 0);
U2 = optimvar('U2', 7, 1, "LowerBound", 0);
% Calculation of the heat transfer coefficient
Heat_transfer = U1*Area_1 + U2*Area_2;
% Loop for perfoming the calculation for each measurement
Heat_flow = [];
% Disable this the loop
% for i = 1:max(size(T_out)) % loop till i = 7
% Heat_flow = Heat_transfer*(20 - T_out(i));
% end
% to test this Heat flow:
Heat_flow = Heat_transfer*(20 - T_out(7));
% Definition of the objective function (minimization of RMSE)
prob.Objective = (sum((M - Heat_flow).^2)/7)^(1/2);
show(prob)
%Definition of the initial guess (not a linear problem)
initialGuess.U1 = [1 1 1 1 1 1 1];
initialGuess.U2 = [1 1 1 1 1 1 1];
%Solution of the problem
[sol, opt] = solve(prob, initialGuess)
Matt J
on 4 Oct 2023
However, those solutions vary for each iteration of the loop.
That's not apparent to me. The code you have presented does not show multiple optimization problems in a loop. In your code, solve() is called only once.
Matt J
on 4 Oct 2023
It would be advisable to reformulate your objective in purely quadratic form. Then, more specialized solvers like lsqlin can be used.
prob.Objective= sum( (M-Heat_flow).^2 ); %equivalent to what you had before.
Andrea Costantino
on 4 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!