I received the error message: Unable to perform assignment because the left and right sides have a different number of elements. Error in augLagFun>augLagScalarFun (line 175)
12 次查看(过去 30 天)
显示 更早的评论
I am trying to solve an optimization problem where the number of constraints change from one point to another during the optimization. The function which defines the constraints is as follows. y_hat1, mspe1, y_hat2, and mspe2 are computed in the code. I deleted that part to simplify the code.
function [c,ceq] = non_linear_constraints(x,model1,model2,alpha_A,rhs,q,alpha_infe)
binding_output = check_binding_output_constraints(y_hat1,mspe1,y_hat2,mspe2,alpha_A,rhs,q);
ceq=[];
z_A = norminv(1-(alpha_A/2*(q-1)));
z_infe = norminv(1-(alpha_infe/(q-1)));
i=1;
if (sum(binding_output) > 0)
c=zeros(q-1+2*sum(binding_output),1);
if (binding_output(1) == 1)
c(i) = y_hat1 - z_A * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = -y_hat1 - z_A * sqrt(mspe1) + rhs(1);
i=i+1;
end
if (binding_output(2) == 1)
c(i) = y_hat2 - z_A * sqrt(mspe2) - rhs(2);
i=i+1;
c(i) = -y_hat2 - z_A * sqrt(mspe2) + rhs(2);
i=i+1;
end
c(i) = y_hat1 + z_infe * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = y_hat2 + z_infe * sqrt(mspe2) - rhs(2);
else
c=zeros(q-1,1);
c(i) = y_hat1 + z_infe * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = y_hat2 + z_infe * sqrt(mspe2) - rhs(2);
end
In that code, the sum of the components of the function binding_output gives the number of extra constraints (if there are any). So, the size of c changes from one point x to another point x during the iterations. I am solving the problem using patternsearch (which is a function in Matlab's optimization toolbox). But a similar error message occurs if I use fmincon.
I will be very pleased if you can help me to solve this problem. Thanks in advance.
Ebru
0 个评论
采纳的回答
Manikanta Aditya
2024-2-29
Hello Ebru,
The error message you’re seeing is typically due to a mismatch in the number of elements between the left and right sides of an assignment operation. In your case, this could be happening because the size of 'c' changes during the iterations, as you mentioned.
One way to address this issue is to pre-allocate the maximum possible size for 'c' before the iterations begin, and then fill in the necessary values during each iteration. This way, the size of 'c' remains constant, and you avoid the error.
Here’s a modified version of your code that implements this idea:
function [c,ceq] = non_linear_constraints(x,model1,model2,alpha_A,rhs,q,alpha_infe)
binding_output = check_binding_output_constraints(y_hat1,mspe1,y_hat2,mspe2,alpha_A,rhs,q);
ceq=[];
z_A = norminv(1-(alpha_A/2*(q-1)));
z_infe = norminv(1-(alpha_infe/(q-1)));
% Pre-allocate c with maximum possible size
max_size = q-1 + 2*length(binding_output);
c = zeros(max_size,1);
i=1;
if (sum(binding_output) > 0)
if (binding_output(1) == 1)
c(i) = y_hat1 - z_A * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = -y_hat1 - z_A * sqrt(mspe1) + rhs(1);
i=i+1;
end
if (binding_output(2) == 1)
c(i) = y_hat2 - z_A * sqrt(mspe2) - rhs(2);
i=i+1;
c(i) = -y_hat2 - z_A * sqrt(mspe2) + rhs(2);
i=i+1;
end
c(i) = y_hat1 + z_infe * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = y_hat2 + z_infe * sqrt(mspe2) - rhs(2);
else
c(i) = y_hat1 + z_infe * sqrt(mspe1) - rhs(1);
i=i+1;
c(i) = y_hat2 + z_infe * sqrt(mspe2) - rhs(2);
end
% Remove unused elements of c
c(i+1:end) = [];
end
This code first calculates the maximum possible size of 'c' and pre-allocates a zero vector of that size. Then, it fills in the necessary values during each iteration. Finally, it removes any unused elements of 'c' after the iterations are complete.
Thanks!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!