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?
Related
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.
I would need to express this condition:
a - b(a,x) < 0, for any a such that 0 < a < Inf
% b(a,x) is a function depending on a and x, the vector to be optimized
% a is an additional variable, of length 1
I have come across fseminf function, which seems to be exactly what I was looking to. However, it only takes into account close intervals for 'a'. I would need this constraint to be met for any positive real value of a with a precision of thousandths.
In another post in Stackoverflow and in Matlab documentation it is mentioned that it is impossible to cover the whole range of positive real numbers, and a guess about which values of a are expected must be done. I wonder if there is another way of doing this, because in my case the possible values of a do actually depend of another variable s, and depending on the value of s these values of a could vary up to infinity.
Many thanks in advance, and best regards.
Actually the substraction I need to implement is the one above in this image:
That is, the term of the left in that equality minus the one on the right must be lower than 0 for any value of m_u greater than 0. When the number of iterations (l) tends to infinity, so must do the value of m_u --which starts being 0 when (l)=0. That is equivalent to say, for any value of m_u, the one provided in next iteration should be greater than it.
The parameters to be optimized in this expresion are lambda and rho, not m_u.
What could I do?
Many thanks in advance and BR.
First, define a function for nonlinear condition:
function [c,ceq] = nonLinear(x)
c = x - b(x);
ceq = [];
Then, apply fmincon likes the following on target function f(x):
fun = #(x)f(x);
nonlcon = #nonLinear;
A = [-1]; % x > 0
b = [0];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0]; %initial point
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
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.
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.
I have a binary matrix of size m-by-n. Given below is a sample binary matrix (the real matrix is much larger):
1010001
1011011
1111000
0100100
Given p = m*n, I have 2^p possible matrix configurations. I would like to get some patterns which satisfy certain rules. For example:
I want not less than k cells in the jth column as zero
I want the sum of cell values of the ith row greater than a given number Ai
I want at least g cells in a column continuously as one
etc....
How can I get such patterns satisfying these constraints strictly without sequentially checking all the 2^p combinations?
In my case, p can be a number like 2400, giving approximately 2.96476e+722 possible combinations.
Instead of iterating over all 2^p combinations, one way you could generate such binary matrices is by performing repeated row- and column-wise operations based on the given constraints you have. As an example, I'll post some code that will generate a matrix based on the three constraints you have listed above:
A minimum number of zeroes per column
A minimum sum for each row
A minimum sequential length of ones per column
Initializations:
First start by initializing a few parameters:
nRows = 10; % Row size of matrix
nColumns = 10; % Column size of matrix
minZeroes = 5; % Constraint 1 (for columns)
minRowSum = 5; % Constraint 2 (for rows)
minLengthOnes = 3; % Constraint 3 (for columns)
Helper functions:
Next, create a couple of functions for generating column vectors that match constraints 1 and 3 from above:
function vector = make_column
vector = [false(minZeroes,1); true(nRows-minZeroes,1)]; % Create vector
[vector,maxLength] = randomize_column(vector); % Randomize order
while maxLength < minLengthOnes, % Loop while constraint 3 is not met
[vector,maxLength] = randomize_column(vector); % Randomize order
end
end
function [vector,maxLength] = randomize_column(vector)
vector = vector(randperm(nRows)); % Randomize order
edges = diff([false; vector; false]); % Find rising and falling edges
maxLength = max(find(edges == -1)-find(edges == 1)); % Find longest
% sequence of ones
end
The function make_column will first create a logical column vector with the minimum number of 0 elements and the remaining elements set to 1 (using the functions TRUE and FALSE). This vector will undergo random reordering of its elements until it contains a sequence of ones greater than or equal to the desired minimum length of ones. This is done using the randomize_column function. The vector is randomly reordered using the RANDPERM function to generate a random index order. The edges where the sequence switches between 0 and 1 are detected using the DIFF function. The indices of the edges are then used to find the length of the longest sequence of ones (using FIND and MAX).
Generate matrix columns:
With the above two functions we can now generate an initial binary matrix that will at least satisfy constraints 1 and 3:
binMat = false(nRows,nColumns); % Initialize matrix
for iColumn = 1:nColumns,
binMat(:,iColumn) = make_column; % Create each column
end
Satisfy the row sum constraint:
Of course, now we have to ensure that constraint 2 is satisfied. We can sum across each row using the SUM function:
rowSum = sum(binMat,2);
If any elements of rowSum are less than the minimum row sum we want, we will have to adjust some column values to compensate. There are a number of different ways you could go about modifying column values. I'll give one example here:
while any(rowSum < minRowSum), % Loop while constraint 2 is not met
[minValue,rowIndex] = min(rowSum); % Find row with lowest sum
zeroIndex = find(~binMat(rowIndex,:)); % Find zeroes in that row
randIndex = round(1+rand.*(numel(zeroIndex)-1));
columnIndex = zeroIndex(randIndex); % Choose a zero at random
column = binMat(:,columnIndex);
while ~column(rowIndex), % Loop until zero changes to one
column = make_column; % Make new column vector
end
binMat(:,columnIndex) = column; % Update binary matrix
rowSum = sum(binMat,2); % Update row sum vector
end
This code will loop until all the row sums are greater than or equal to the minimum sum we want. First, the index of the row with the smallest sum (rowIndex) is found using MIN. Next, the indices of the zeroes in that row are found and one of them is randomly chosen as the index of a column to modify (columnIndex). Using make_column, a new column vector is continuously generated until the 0 in the given row becomes a 1. That column in the binary matrix is then updated and the new row sum is computed.
Summary:
For a relatively small 10-by-10 binary matrix, and the given constraints, the above code usually completes in no more than a few seconds. With more constraints, things will of course get more complicated. Depending on how you choose your constraints, there may be no possible solution (for example, setting minRowSum to 6 will cause the above code to never converge to a solution).
Hopefully this will give you a starting point to begin generating the sorts of matrices you want using vectorized operations.
If you have enough constraints, exploring all possible matrices could be attempted:
// Explore all possibilities starting at POSITION (0..P-1)
explore(int position)
{
// Check if one or more constraints can't be verified anymore with
// all values currently set.
invalid = ...;
if (invalid) return;
// Do we have a solution?
if (position >= p)
{
// print the matrix
return;
}
// Set one more value and continue exploring
for (int value=0;value<2;value++)
{ matrix[position] = value; explore(position+1); }
}
If the number of constraints is low, this approach will take too much time.
In this case, for the kind of constraints you gave as examples, simulated annealing may be a good solution.
You must design an energy function, high when all constraints are met. That would be something like that:
Generate a random matrix
Compute energy E0
Change one cell
Compute energy E1
If E1>E0, or E0-E1 is smaller than f(temperature), keep it, otherwise reverse the move
Update temperature, and goto 2 unless stop criterion is reached
If all the contraints relate to columns (as is the case in the question), then you can find all possible valid columns and check that each column in the matrix is in this set. (i.e. when you consider each column independently, you reduce the number of possibilities a lot.)
I might be way off here, but I remember doing something similar once with some genetic algorithm.
Check out pseudo boolean constraints (also called 0-1 integer programming).
This is virtually impossible if your constraint set is complex enough. You might try to use a stochastic optimizer, like simulated annealing, particle swarm optimization, or a genetic algorithm to find a feasible solution.
However, if you can generate one (non-random) solution to such a problem, then often you can generate others by random permutations made to the existing solution.