Optimization in a for loop

10 views (last 30 days)
Andrea Costantino
Andrea Costantino on 4 Oct 2023
Edited: Matt J on 4 Oct 2023
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
Matt J
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
Andrea Costantino on 4 Oct 2023
Yes, solve() is called once, but the optimization variables are 7x1. I had to define them as 7x1 because 7 iterations are present. To create Heat_flow in the for loop, a new optimization expression has to be created in each iteration since it is written as a symbolic expression.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Oct 2023
Edited: Matt J on 4 Oct 2023
Perhaps this is what you wanted. If so, it is a rather ill-posed problem, because your C matrix is only rank 1.
%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)
C=(20-T_out(:))*[Area_1,Area_2];
U = optimvar('U',2,1,"LowerBound",0);
%Definition of the objective function (minimization of RMSE)
prob.Objective=sum(C*U-M).^2;
%Solution of the problem
[sol,opt]=solve(prob)
Solving problem using lsqlin. Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol = struct with fields:
U: [2×1 double]
opt = 1.1953e-22
  2 Comments
Andrea Costantino
Andrea Costantino on 4 Oct 2023
Thank you Matt J!
I understand the process. Basically, you skip the loop by using vectors operation. Unfortunately, in my real code I cannot do it because each iteration represents a step and the elements of the vector are computated from the previous iteration.
Matt J
Matt J on 4 Oct 2023
Edited: Matt J on 4 Oct 2023
That's irrelevant. Each row C(i,:) represents the coefficients for a particular step. I could have used a loop to create C in a way that depends in some way on previous steps, e.g.,
C=nan( numel(T_out), 2);
for i=1:height(C)
C(i,:)=(20-T_out(i))([Area1,Area2]);
if i>1
C(i,:)=C(i,:) + C(i-1,:);
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!