Minimizing a function using fmincon with special constraints and intervals

I would like to minimize a function using fmincon as follows:
The function to be minimized is: fun = @(x)x(1)^2 + 3*x(2)/x(3) - x(4)^2 ;
The constraints are:
  1. 1/9 <=x(i)/x(j)<=9, for i,j=1,2,3,4 by considering i<j.
  2. x(1) + x(2) + x(3) + x(4) = 1;
  3. x(i) > 0 for i=1,2,3,4.
My challenge is how to apply the first constraint (1) in fmincon. Without the first constraint, I was able to solve it using only the second and the third constraints as follows:
clear; clc
% function to be minimized
fun = @(x)x(1)^2 + 3*x(2)/x(3) - x(4)^2 ;
%initial value
x0=ones(1,4);
%bound limits
lb=0.0001*ones(1,4); % lower bound as x(i)>0
ub=[]; %no upper bound
% normalize x inside fmincon as x(1)+...+x(4) = 1
Aeq = ones(1,4);
beq = 1;
% using Matlab's fmincon
x= fmincon(fun,x0,[],[],Aeq,beq,lb,ub)
Local 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.
x = 1×4
0.0001 0.0001 0.0123 0.9875
I was trying to convert the first constraint into a set of 12 inequalites below. But still I couldn't manage to implement it inside the fmincon. I wonder if you could provide me help. Thank you in advance!
% writing inequalities for: 1/9 <=x(i)/x(j)<=9, for i,j=1,2,3,4 by
% considering i<j.
% (1) for x(i)/x(j)<=9:
x(1)-9*x(2)<=0;
x(1)-9*x(3)<=0;
x(1)-9*x(4)<=0;
x(2)-9*x(3)<=0;
x(2)-9*x(4)<=0;
x(3)-9*x(4)<=0;
%(2) for 1/9<=x(i)/x(j), i,j=1,2,3,4:
1/9*x(2)-x(1)<=0;
1/9*x(3)-x(1)<=0;
1/9*x(4)-x(1)<=0;
1/9*x(3)-x(2)<=0;
1/9*x(4)-x(2)<=0;
1/9*x(4)-x(3)<=0;

 Accepted Answer

Here is one approach
% function to be minimized
fun = @(x)x(1)^2 + 3*x(2)/x(3) - x(4)^2 ;
%initial value
x0=ones(1,4);
%bound limits
lb=0.0001*ones(1,4); % lower bound as x(i)>0
ub=[]; %no upper bound
% normalize x inside fmincon as x(1)+...+x(4) = 1
Aeq = ones(1,4);
beq = 1;
% using Matlab's fmincon
x = fmincon(fun,x0,[],[],Aeq,beq,lb,ub,@mycon)
function [c,ceq] = mycon(x)
% compute nonlinear inequality constraints
c = zeros(12,1); % preallocate
k = 0;
for i = 1:3
for j = i+1:4
k = k+1;
c(k) = 1/9 - x(i)/x(j);
c(k+6) = x(i)/x(j) - 9;
end
end
% compute nonlinear equality constraints at x.
ceq = []; % no nonlinear equality constraints
end

6 Comments

But actually all of your constraints are linear, so it would be better to formulate your linear inequality constraints in matrix form and use these
So this would be better, as it is probably "easier" for the solver to use a linear constraint than a nonlinear one,
note that I made the initial guess [1/4,1/4,1/4,1/4] so that it satisfied the equality constraint.
% function to be minimized
fun = @(x)x(1)^2 + 3*x(2)/x(3) - x(4)^2 ;
%initial value
x0=ones(1,4)/4;
%bound limits
lb=0.0001*ones(1,4); % lower bound as x(i)>0
ub=[]; %no upper bound
% normalize x inside fmincon as x(1)+...+x(4) = 1
Aeq = ones(1,4);
beq = 1;
% define inequality constraints
A = zeros(12,4);
b = zeros(12,1);
k = 0;
for i = 1:3
for j = i+1:4
k = k + 1;
A(k,i) = 1;
A(k,j) = -9;
A(k+6,i) = -1;
A(k+6,j) = 1/9;
end
end
% using Matlab's fmincon
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local 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.
x = 1×4
0.0500 0.0500 0.4500 0.4500
fval = 0.1333
If you do try my original solution, using the non-linear constraints, I suggest modifying the initial guess so that it satisfies, the equality constraint, e.g. x = [1/4,1/4,1/4,1/4]. I noticed that using your initial guess, the solution seems to terminate prematurely, with a function value that is larger than actual optimum. Again, this reinforces that it is probably better to use the linear constraints, as then, even using the initial guess of [1 1 1 1] it reaches the same solution as using [1/4 1/4 1/4 1/4], so it is less sensitive to a correct initial guess.
Linear constraints are ALWAYS preferable to nonlinear constraints. ALWAYS.
Thanks @John D'Errico for reinforcing that point. This is an interesting case, because at first glance the constraints are non-linear, but with the additional constraint that the x's are all positive you can multiply through (without the possibility of a reversal in the direction of the inequality) to clear the denominator and obtain the linear constraints.
Thank you all very much. It helps a lot.

Sign in to comment.

More Answers (0)

Asked:

HAT
on 6 Jan 2023

Edited:

HAT
on 6 Jan 2023

Community Treasure Hunt

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

Start Hunting!