Function optimization meeting conditions

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 Comments

Ins is a matrix of size (length(lt), length(lc)). If I assume that with "minimize I" you mean "minimize Ins", what do you mean with "minimizing a matrix" ?
Tha right with minimizing I i mean Ins, Ins(i,j) is a matrix and its formula is Ins(i,j)=ht(i,j)-h(j), a need the values of h(j) that make the solutions of the sustraction the minimun posible
Torsten
Torsten on 7 Jun 2023
Edited: Torsten on 7 Jun 2023
"Ins" must be a single number, not a vector or matrix of values because you cannot minimize vector-valued functions.
But your problem formulation seems to be bad. Note that the matrix elements of "Ins" can become arbitrarily small:
If M > 0 is a very big number, use
h(1) = M
h(2) = M+1
...
h(length(lt)) = M + length(lt)
I'm going to try to explain myself better. In the problem I want to solve, I have a matrix ht(i,j), which means there is a value ht for each i,j. What I need is the value h(j), which does not depend on i. This value should minimize the differences with all the ht values in "i", and I need to repeat this for each j. Therefore, the difference Ins is not a single value; there will be as many differences as ht values. My objective is not to make the differences between ht and h equal to 0; rather, I want to find the h that minimizes the maximum value of Ins across all "i". For example, if I have a matrix ht with the following rows (7,8) and (5,9), I need to find h(1) such that the maximum value between 7-h(1) and 8-h(1) is minimized, and h(2) such that the maximum value between 5-h(2) and 9-h(2) is minimized, with the additional condition that h(2) > h(1). Currently, the function I have is as follows:
unction [h] = hp3(ht, Lc)
lc = 0:0.5:Lc;
Ins = @(h) ht - h;
h = zeros(size(lc));
h0 = zeros(size(lc));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = inf(size(h));
ub(end) = h(end);
h = fmincon(Ins, h0, A, b, Aeq, beq, lb, ub, @constraints);
end
function [c, ceq] = constraints(h)
c = h(2:end) - h(1:end-1);
ceq = [];
end
Don't you have to add the additional constraints ht(i,j) - h(i) >= 0 for all i and j ? Think about it.
I gave you a solution that can make all matrix elements of ht(i,j) - h(i) arbitrarily small (if these expressions are allowed to become negative).
Maybe your problem can be written as
min: max_i,j (ht(i,j)-h(i)))
s.c.
ht(i,j) - h(i) >= 0 for all i,j
h(1) <= h(2) <= ... <= h(end)
?
Sorry I misunderstood a part of the problem. You were correct, Ins is not a matrix but a scalar obtained by summing all the h(i,j) - h(j) values. You need to find the values of h(j) that minimize the scalar Ins.
If you mean that your problem turns into
min sum_i,j (ht(i,j)-h(i))
s.c.
h(1) <= h(2) <= ... <= h(end)
then - as shown above - you can construct solutions for h with arbitrary small value for the objective function. Thus the problem is unbounded.
Try to state your problem properly in its mathematical form.
There some more conditions,
We know ht(i,j) as a entrance data and i wanto to calculate the h(j) That make de sumatory of all the Ins (Ins=sum((ht(i,j)-h(j))^2)) minimum meeting the next conditions:
  1. h(j)<h(j+1)
  2. h(end)<=120
  3. (h(j+1)-h(j))< 1.25
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
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

Sign in to comment.

 Accepted Answer

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

More Answers (1)

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 Comments

Its returns me this error i dont know why:
Error using fmincon
Supplied objective function must return a scalar value.
Error in hp3 (line 22)
h = fmincon(I, h0, A, b, Aeq, beq, lb, ub, @constraints);
function [h] = hp3(ht, Lc)
lc = 0:0.5:Lc;
Ins = @(h) ht - h;
h = zeros(size(lc));
h0 = zeros(size(lc));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = inf(size(h));
ub(end) = h(end);
h = fmincon(Ins, h0, A, b, Aeq, beq, lb, ub, @constraints);
end
function [c, ceq] = constraints(h)
c = h(2:end) - h(1:end-1);
ceq = [];
end
The error indicates the objective function 'Ins' should output a scalar value that can be used with 'fmincon'.
You can modify the objective function Ins to resolve this issue by
Ins = @(h) sum((ht - h).^2);
Hope this helps!!
I keep having the same problem, I don't know why. With the latest change, the Ins should be scalar
Maybe is because h is not scalar
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.
But h its not of the same size as ht

Sign in to comment.

Asked:

on 7 Jun 2023

Commented:

on 8 Jun 2023

Community Treasure Hunt

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

Start Hunting!