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

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.

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

Optimization with infinite constraints

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);

MATLAB genetic algorithm constraint (all variables can't be zero at the same time in a binary environment)

I'm using MATLAB ga function for my optimization problem. In my problem, I have some decision variables which are integer (0 and 1: I specified lower bound, upper bound, and IntCon for it) plus two continues varibales. Otherwise, all integer variables can't be zero at same time, so at least, we need a single "one" variable among integers. How can I implement mentioned constraint in MATLAB?
This is a Mixed-Integer optimization problem and it can be solved using ga in MATLAB. As mentioned in the documentations: ga can solve problems when certain variables are integer-valued. Not all the variables but certain variables. So you should have at least one real variable among the other integers.
Whit IntCon options, you can specify which variables are integer, for instance IntCon=[1 3] means that your first and third variables are integer. To avoid both integer variables to be 0 at the same time, I think you can add some inequality constraints.
For instance look at the following example:
Let's say we want to find the optimum value for the Ackley function with 5 variables (e.g. in 5 dimensions), [x(1)...x(5)] and let's assume that the first and third variables, x(1) and x(3), are integers. We can write the following script:
nVar = 5;
lb = -5*ones(1,nVar); % define the upper bound
ub = 5*ones(1,nVar); % define the lower bound
rng(1,'twister') % for reproducibility
opts = optimoptions('ga','MaxStallGenerations',50,'FunctionTolerance',1e-3,'MaxGenerations',300);
[x,~,~] = ga(#ackleyfcn,nVar,[],[],[],[],lb,ub,[],[1 3],opts);
disp('solution:');disp(x)
On my machine, I get this solution:
solution:
0 -0.000000278963321 0 0.979067345808285 -0.000000280775000
It can be seen that x(1) and x(3) are integers and both 0. Now let's say as you mentioned, they both cannot be 0 at the same time and if one is 0 the other should be 1. Here the boundaries of the Ackley's problem allows the variables to be in the range defined by lower and upper bounds. However, in your case the lower and upper bounds should be defined as [0] and [1] for both integer variables.
Now I want to avoid both variables to be 0, so I can write the following linear inequality constraint:
% x(1) + x(3) >= 1
% x(1) >= 0
% x(3) > 0
These inequalities should be written in form Ax <= b:
A = [-1 0 -1 0 0
-1 0 0 0 0
0 0 -1 0 0];
b = [-1
0
0];
Now if we run the optimization problem again we see the effect of the constraints on the output:
[x,~,~] = ga(#ackleyfcn,nVar,A,b,[],[],lb,ub,[],[1 3],opts);
disp('solution');disp(x)
solution
1.000000000000000 -0.000005031565831 0 -0.000011740569861 0.000008060759466

Matlab optimization - conditional sum of optimization variable in constraint

I'm trying to solve a ILP problem using binary variables in MATLAB (bintprog). I need to define a constraint similar as below :
I want to sum binary variables x<sub>i, j</sub> until one of them equals to zero and after that stop this summation. How can I define this constraint in ILP?
The way I read this (somewhat difficult as there is little explanation; I think the question could have been phrased more clearly), this is essentially counting the first x(k)'s that are 1. Here I assume x(i,j) is mapped to x(k) so we have a better defined ordering. The counting can be done using a new binary variable y(k):
y(k) = x(k)*y(k-1) if k>1
y(k) = x(k) if k=1
y(k) in {0,1}
For example:
x = [1 1 1 0 1 0]
y = [1 1 1 0 0 0]
The nonlinear expression x(k)*y(k-1) can easily be linearized (it is a multiplication of two binary variables; see here; note that with this formulation we can even relax y(k) to continuous between 0 and 1). An optimized version can look like:
y(k) >= x(k)+y(k-1)-1 if k>1
y(k) = x(k) if k=1
0 <= y(k) <= 1
Now you just can do
sum(k, y(k)) <= c
This formulation would also allow to say: number of leading 1s should be between c1 and c2.
We can also just write immediately:
x(1) + ... + x(c+1) <= c
which also forbids more than c leading 1s and is even simpler.
A different problem we see more often (e.g. in generator scheduling) is the condition that we cannot have more than c consecutive x(k)'s turned on (i.e. the generator can not be on for more than c consecutive periods: after c periods on, the generator must be turned off for at least 1 period). This means we don't allow sequences of more than c 1's.
This is quite a non-typical MIP-constraint in my opinion. Here is my attempt in formulating this:
You have to think about:
the necessity of handling Case B (dependent on your objective and other constraints) and the downfalls of the bigM-approach nice read
how to implement the product of two binary-variables (which is easy; see here)
EDIT
At the cost of the double amount of binary-auxiliary variables, it is also possible to formulate this without bigM-constants.
Sketch:
Instead of using a one-sided implication to set the indicators, use equality == both-implications -> no aux-var is free; all are 0 or 1 depending on x(z) only
Now build a second layer of binary-aux variables (b_i) with the following idea: b_i >= smallConst * b_i-1 + smallConst * a_i (smallConst in (0, 0.5]; should not be close 0 )
Use b-vector for your final sum-constraint (like describes above; but not using a-vector anymore!)

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.