Function optimization meeting conditions

조회 수: 1 (최근 30일)
Jon Bilbao
Jon Bilbao 2023년 6월 7일
댓글: Jon Bilbao 2023년 6월 8일
How can i optimize the I function, i want to find the values of h(j) that minimize I, meetentig the conditions h(j+1)>h(j), h(end)<120 and h(j+1)-h(j)<1.25 ?
ht is a array beiing its size ht(lt,lc) or the same ht(i,j) and it is calculated in another function. The formula of Ins is Ins=ht(i,j)-h(j).
Thanks for the help
function [h] = hp(ht, Lc, Lt)
lt = 0:0.5:Lt;
lc = 0:0.5:Lc;
Ins = cell(length(lt), length(lc));
h= cell(length(Lc));
for i = 1:length(lt)
for j = 1:length(lc)
Ins{i,j} = @(h) (ht(i,j) - h);
end
end
h0 = zeros(size(lc));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = @constraints;
h = fmincon(@(h) Ins, h0, A, b, Aeq, beq, lb, ub, nonlcon);
end
function [c] = constraints(h)
c>0;
c = h(2:end) - h(1:end-1);
end
  댓글 수: 10
Torsten
Torsten 2023년 6월 8일
편집: Torsten 2023년 6월 8일
If ht is nxm, the linear constraints can be defined by A and b as in the code below.
A and b are then used in the call to the optimizer, e.g.
Now it's your turn to define the objective function and the call to "fmincon" (or some similar optimizer).
(And incidentally the .^2 appears for the summands in the objective :-) )
m = 4;
v1 = ones(m,1);
w1 = -ones(m-1,1);
A1 = diag(v1) + diag(w1,1)
A1 = 4×4
1 -1 0 0 0 1 -1 0 0 0 1 -1 0 0 0 1
b1 = [zeros(m-1,1);120];
v2 = -ones(m,1);
w2 = ones(m-1,1);
A2 = diag(v2) + diag(w2,1);
A2(end,:) = []
A2 = 3×4
-1 1 0 0 0 -1 1 0 0 0 -1 1
b2 = 1.25*ones(m-1,1);
A = [A1;A2]
A = 7×4
1 -1 0 0 0 1 -1 0 0 0 1 -1 0 0 0 1 -1 1 0 0 0 -1 1 0 0 0 -1 1
b = [b1;b2]
b = 7×1
0 0 0 120.0000 1.2500 1.2500 1.2500
Jon Bilbao
Jon Bilbao 2023년 6월 8일
it cant be solved like thah because ht is too big, ht is a 401x51 matrix
Error using fmincon
A must have 2601 column(s).
Error in hpp (line 22)
h = fmincon(I,h0,A,b);
function [h] = hpp(ht)
[n,m]=size(ht);
I=@(h) (ht-h);
h0=zeros(m);
v1 = ones(m,1);
w1 = -ones(m-1,1);
A1 = diag(v1) + diag(w1,1);
b1 = [zeros(m-1,1);120];
v2 = -ones(m,1);
w2 = ones(m-1,1);
A2 = diag(v2) + diag(w2,1);
A2(end,:) = [];
b2 = 1.25*ones(m-1,1);
A = [A1;A2];
b = [b1;b2];
h = fmincon(I,h0,A,b);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2023년 6월 8일
이동: Torsten 2023년 6월 8일
ht = rand(401,51);
[n,m]=size(ht);
I=@(h) sum(sum((ht-h).^2));
h0 = zeros(1,m);
v1 = ones(m,1);
w1 = -ones(m-1,1);
A1 = diag(v1) + diag(w1,1);
b1 = [zeros(m-1,1);120];
v2 = -ones(m,1);
w2 = ones(m-1,1);
A2 = diag(v2) + diag(w2,1);
A2(end,:) = [];
b2 = 1.25*ones(m-1,1);
A = [A1;A2];
b = [b1;b2];
[h,fval,exitflag] = fmincon(I,h0,A,b)
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
h = 1×51
0.4889 0.5003 0.5003 0.5003 0.5003 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042 0.5042
fval = 1.7157e+03
exitflag = 2

추가 답변 (1개)

rakshit gupta
rakshit gupta 2023년 6월 7일
You can consider following changes to the code to optimize the function while meeting the condition h(j+1)>h(j).
  1. Modify the Ins cell array to a function handle that takes in the h array.
Ins = @(h) ht - h;
2. Modify the h array to a vector instead of a cell array.
h = zeros(size(lc));
3. Add the upper bound constraint to ensure h(j+1) > h(j).
ub = inf(size(h));
ub(end) = h(end);
4. Modify the constraints function to return the inequality constraint.
function [c, ceq] = constraints(h)
c = h(2:end) - h(1:end-1);
ceq = [];
end
5. Call the fmincon function with the changes made above.
h = fmincon(Ins, h, A, b, Aeq, beq, lb, ub, @constraints);
These changes could help in optimizing the function.
  댓글 수: 6
rakshit gupta
rakshit gupta 2023년 6월 8일
Yes, you can try modifying 'h' vector by changing the creation of the 'h' vector to use the same size and data type as 'ht',
h = zeros(size(ht), 'like', ht);
This may help in making Ins scalar.
Jon Bilbao
Jon Bilbao 2023년 6월 8일
But h its not of the same size as ht

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by