How to write Max(L)<1 without using MAX func in MATLAB - matlab

I have an optimization problem which is solved with fmincon. In this problem I have a constraint that says every element of the vector L must be less than or equal to 1. So basicaly I can add this constraint like this
C=max(L)-1 ; % C(X)<0
But now I want to write the above constraint with out using Max function.
Any idea?

every element of the vector L must be less than or equal to 1.
This should be written as a set of constraints, not a single constraint. Artificially bundling the constraints L(1)<=1, L(2)<=1, ... into one constraint is just going to cause more pain to the solver.
Example with linear constraints: minimizing -x(1)*x(2) subject to x(1)<=1 and x(2)<=1
fmincon(#(x) -x(1)*x(2), [0.5; 0.5], [1 0; 0 1], [1; 1])
(Here the simple form fmincon(fun,x0,A,b) is used.)
Example with nonlinear constraints: minimizing -x(1)*x(2) subject to x(1)^2+x(2)^2<=1 and x(1)+x(2)^2<=1
fmincon(#(x) -x(1)*x(2), [0.1; 0.1], [],[],[],[],[],[], #(x) deal([x(1)^2+x(2)^2-1; x(1)+x(2)^2-1],[]))
Here the form fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) is used, where nonlcon returns multiple inequality constraints and equality constraints. Specifically, the first output of the nonlinear constraint function is [x(1)^2+x(2)^2-1; x(1)+x(2)^2-1]; both of these are required to be <=0. The second output, nonlinear equality constraints, is an empty array.

Related

GA with integer and linear constraints in MATLAB global optimization toolbox

According to the documentation of global optimization toolbox "ga does not enforce linear constraints when there are integer constraints. Instead, ga incorporates linear constraint violations into the penalty function".
In an optimization problem, I am trying to solve if linear constraints are violated, other constraints will return non-real values. How can I tackle this issue?
Simplified version of problem
Objective Function : min a + b
0 <= a <= 10
0 <= b <= 10
Subject to;
a*b - 25*a < 0
theta < 0
Where theta is function of a and b, and returns non-real value if first constraint is not satisfied

How to minimize a function with the constraint that its derivative should be always greater than 0

I am trying to optimize a nonlinear function with 1500 variables(instantaneous phase), with the help of fmincon in Matlab. The constraint to the optimum variable is that the difference between consecutive elements in the optimal variable obtained should be greater than 0. How can I implement this in the cost function? I have used a nonlinear constraint:
function [c,ceq] = insta_freq(phase)
f=diff(phase);
c=-1*double(min(f));
ceq = [];
The optimization is performed by:
nonlcon=#insta_freq;
[variable_opt,fval,exitflag,output] = fmincon(fun,ph0,[],[],[],[],[],[],nonlcon,options);
The optimization should be such that the constraint nonlcon<=0 but while optimizing with fmincon, these constraints are not satisfied. Thus, is there any other way to make sure that difference of the optimal variable vector is always greater than 0?
You could try and reduce the constraint tolerance. Also in the question it seems you are referring to the derivatives of the objective function, whereas in the question itself it seems you want every single term to be greater than the preceding one as in x1 <= x2 <= x3 <= ... <= xn. I am suggesting a possible solution for the latter problem (the first one would not even define a local optimum, so I am assuming the reported condition is what you want).
You could rewrite the condition in a matrix for as in
A = [ 1 -1 0 ... 0 ; 0 1 -1 0 ... 0 ; .... 1 -1 ] so your constraints are inequality linear constraints, simply written Aineq x <= b where b = [0;...; 0];
you just then call
[variable_opt,fval,exitflag,output] = fmincon(fun,ph0,A,b,[],[],[],[],nonlcon,options);
where A and b are the ones defined above.

Non-linear constraint in matlab

I am using the solver fmincon in matlab and I would like to add a non-linear constraint such that the variable to be optimized has a fixed number of non-zero elements. This number is equal to 25 and fixed as seen in the function below.
I have therefore set my non-linear constraint as follow:
nonlcon = #limitSizeBasket;
function [c,ceq] = limitSizeBasket(x,maskTop,maskBottom)
%This function limit the size of the basket
c = sum(any(x(1,:),1)) - 25;
ceq = [];
end
x = fmincon(#(x)fun(x,scoreTop,scoreBottom),x0,A,b,Aeq,beq,lb,ub,#(x)nonlcon(x,maskTop,maskBottom),options)';
Unfortunately, the resulting values of x seems not to take this constraint into account at all as all x are non-zero. Here I have x is a vector of 70 elements and I would restrict the numbers of non-zero elements to 25 at most. Is there a problem with the way I defined my constraints or is the problem coming from something else?

using fmincon in matlab

Say i have a function f(X) which i want to minimize with constraints such that some other functions- A(X) = 0 and B(X) = 0 and 0 < C(X) < pi. There are many algorithms to do it, but to make my life easier, i want to use built in function fmincon() in matlab. So i read this documentation:
http://www.mathworks.com/help/optim/ug/fmincon.html
But I don't understand how I should I pass the parameters to solve my problem in specific. How do I do it? Can I do it at all?
Use the nonlcon parameter of fmincon (I'm assuming here your constraints are nonlinear?). Then A(X) and B(X) are fine but for C(X) it must be in the form c(X) < 0 so you'll need to break it into two constraints of that form.
I pulled this example of how to specify a function for nonlcon from elsewhere in the documentation:
function [c,ceq]=myNonlinearContraints(x)
%First deal with your nonlinear equalities
c(1) = A(X);
c(2) = B(X);
%Then your inequalities transformed to be in the form ceq < 0
ceq(1) = -C(X);
ceq(2) = C(X) - pi;
See if the functions A,B are linear or nonlinear. That is maybe A(X) is simply a integral, then the interpretation should be that it is linear. It does actually make a difference if you supply a linear constraint as nonlinear.
If they are nonlinear, then create a
function [c,ceq] = nonlcon(X)
which gives out the equality constraint value (ceq) and inequality constraint value (c). Remember that inequality constraints are interpreted as
ineq(X) < 0
So you need to compute it that way.
C(X) seems to be a trigonometric function, so it will also be part of nonlcon function. This nonlcon, you will pass to fmincon as an argument. Nonlcon is called for a specific value of X and it returns the constraint value. Pass your lower and upper bounds if any and try the optimization for different initial points x0. For some problems, more than one solution can be found.

doing optimizations in matlab: figuring out constraint equation

I have N lines that are defined by a y-intercept and an angle, q. The constraint is that all N lines must intersect at one point. The equations I can come up with to eventually get the constraint are these:
Y = tan(q(1))X + y(1)
Y = tan(q(2))X + y(2)
...
I can, by hand, get the constraint if N = 3 or 4 but I am having trouble just getting one constraint if N is greater than 4. If N = 3 or 4, then when I solve the equations above for X, I get 2 equations and then can just set them equal to each other. If N > 4, I get more than 2 equations that equal X and I dont know how to condense them down into one constraint. If I cannot condense them down into one constraint and am able to solve the optimization problem with multiple constraints that are created dynamically (depending on the N that is passed in) that would be fine also.
To better understand what I am doing I will show how I get the constraints for N = 3. I start off with these three equations:
Y = tan(q(1))X + y(1)
Y = tan(q(2))X + y(2)
Y = tan(q(3))X + y(3)
I then set them equal to each other and get these equations:
tan(q(1))X + y(1) = tan(q(2))X + y(2)
tan(q(2))X + y(2) = tan(q(3))X + y(3)
I then solve for X and get this constraint:
(y(2) - y(1)) / (tan(q(1)) - tan(q(2))) = (y(3) - y(2)) / (tan(q(2)) - tan(q(3)))
Notice how I have 2 equations to solve for X. When N > 4 I end up with more than 2. This is OK if I am able to dynamically create the constraints and then call an optimization function in MATLAB that will handle multiple constraints but so far have not found one.
You say the optimization algorithm needs to adjust q such that the "real" problem is minimized while the above equations also hold.
Note that the fifth Euclid axoim ensures that all lines will always intersect with all other lines, unless two qs are equal but the corresponding y0s are not. This last case is so rare (in a floating point context) that I'm going to skip it here, but for added robustness, you should eventually include it.
Now, first, think in terms of matrices. Your constraints can be formulated by the matrix equation:
y = tan(q)*x + y0
where q, y and y0 are [Nx1] matrices, x an unknown scalar. Note that y = c*ones(N,1), e.g., a matrix containing only the same constant. This is actually a non-linear constraint -- that is, it cannot be expressed as
A*q <= b or A*q == b
with A some design matrix and b some solution vector. So, you'll have to write a function defining this non-linear constraint, which you can pass on to an optimizer like fmincon. From the documentation:
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) subjects the
minimization to the nonlinear inequalities c(x) or equalities ceq(x)
defined in nonlcon. fmincon optimizes such that c(x) ≤ 0 and ceq(x) =
0. If no bounds exist, set lb = [] and/or ub = [].
Note that you were actually going in the right direction. You can solve for the x-location of the intersection for any pair of lines q(n),y0(n) and q(m),y0(m) with the equation:
x(n,m) = (y0(n)-y0(m)) / (q(m)-q(n))
Your nonlcon function should find x for all possible pairs n,m, and check if they are all equal. You can do this conveniently something like so:
function [c, ceq] = nonlcon(q, y0)
% not using inequalities
c = -1; % NOTE: setting it like this will always satisfy this constraint
% compute tangents
tanq = tan(q);
% compute solutions to x for all pairs
x = bsxfun(#minus, y0, y0.') ./ -bsxfun(#minus, tanq, tanq.');
% equality contraints: they all need to be equal
ceq = diff(x(~isnan(x))); % NOTE: if all(ceq==0), converged.
end
Note that you're not actually solving for q explicitly (or need the y-coordinate of the intersection at all) -- that is all fmincon's job.
You will need to do some experimenting, because sometimes it is sufficient to define
x = x(~isnan(x));
ceq = norm(x-x(1)); % e.g., only 1 equality constraint
which will be faster (less derivatives to compute), but other problems really need
x = x(~isnan(x));
ceq = x-x(1); % e.g., N constraints
or similar tricks. It really depends on the rest of the problem how difficult the optimizer will find each case.