optimization of nested loop with two variables

2 Ansichten (letzte 30 Tage)
Nilton Vicente
Nilton Vicente am 13 Jan. 2021
Beantwortet: Amal Raj am 13 Mai 2024
I have an optimization problem of minimizing a two-stage sampling variance subject to the expected variable cost as constraint, and here are my functions:
the objective function is:
function dblSum=dblSumFun(n_h,m_hi)
h=length(N_h);
dblSum=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(1/m_hi(i)-1/M_hi(i))*M_hi(i).^2*swh2(h);
iSum=iSum+iTerm;
end
hTerm = (1/n_h(h)-1/N_h(h))*N_h(h)^2*sbh2(h)+N_h(h)/n_h(h)*iSum;
dblSum=dblSum+hterm;
end
end
and the constraint is:
function [c,ceq]=nonlcon(n_h,m_hi)
h=length(N_h)
function dblSumc=dblSumFun(n_h,m_hi)
dblSumc=0;
for h=1:length(N_h)
iSum=0;
for i=1:N_h(h)
iTerm=(0.5*m_hi(i))
iSum=iSum+iTerm;
end
hTerm = 3.5-n_h(h)*0.7-n_h(h)/N_h(h)*iSum;
dblSumc=dblSumc+hterm;
end
end
ceq=dblSumc;% this will be satisfied if it eval to zero
c=[];
and the main coding for optimization is as follows
n_h0=zeros(length(N_h),1);%initial guess
m_hi0=zeros(26*n_h(h),1);
X0=[n_h0;m_hi0];
A=[];
B=[];
Aeq=[];
Beq=[];
options=optimoptions('fmincon','Algorithm','sqp','Display','iter-detailed',...
'MaxFunctionEvaluations',100000,'MaxIterations',2000,...
'FunctionTolerance',1e-10);
[n_h,m_hi,const]=fmincon(@(n_h,m_hi) obj_function(n_h,m_hi),X0,A,B,Aeq,Beq,LB,UB,@(n_h,m_hi) nonlcon(n_h,m_hi),options)
I wanted to obtain a vector of n_h for h=1,...,26; and a vector of m_hi for h=1,...,26 and i=1,...,n_h(h). Any help?

Antworten (1)

Amal Raj
Amal Raj am 13 Mai 2024
Hi Nilton, To obtain the vectors n_h and m_hi for your optimization problem, you can modify your code as follows:
% Define the objective function
objective = @(x) obj_function(x(1:length(N_h)), x(length(N_h)+1:end));
% Define the constraint function
constraint = @(x) nonlcon(x(1:length(N_h)), x(length(N_h)+1:end));
% Set initial guess
x0 = [n_h0; m_hi0];
% Set lower and upper bounds for variables
LB = [zeros(length(N_h), 1); zeros(26*sum(N_h), 1)];
UB = [inf(length(N_h), 1); inf(26*sum(N_h), 1)];
% Solve the optimization problem
x = fmincon(objective, x0, [], [], [], [], LB, UB, constraint, options);
% Extract the optimized values
n_h = x(1:length(N_h));
m_hi = x(length(N_h)+1:end);
In this code, the fmincon function is used to minimize the objective function obj_function subject to the constraint nonlcon. The initial guess x0, lower bounds LB, upper bounds UB, and optimization options options are also provided. The optimized values of n_h and m_hi are extracted from the solution vector x.

Kategorien

Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by