I would like to conduct an optimization with inequality and bound constraints:
To maximize f= SEA (a,b,c,d)
with constraints
Pmax (a,b,c,d)<=145
0 <= a <= 10
80 <= b <= 240
0.5<= c <= 1.5
0 <= d <=0.534
Based on literatures, fmincon is an appropriate option. However, in Minitab help, the examples are separated between inequality and bound constraints. How can i write this problem in matlab using both types of constraints?
Related
When I was trying to solve a very simple bin packing problem, the Gurobi solver just won't work. I do try some very simple optimization problem with 1 inequality constraint with Gurobi and it works. But it always return NA for little complicated ones. I am quite frustrated. Highly appreciate for help if anyone can help
%% By Linear programming
clear;clc;
weight = [4,4,5,7]';
cvx_begin
cvx_solver SDPT3
variables I(4,1) X(4,4)
minimize sum(I)
subject to
X * weight <= 10 * I;
sum(X) == [1,1,1,1];
X >= 0
X <= 1
I >= 0
I <= 1
cvx_end
X
I
%% By Integer programming
clear;clc;
weight = [4,4,5,7]';
cvx_begin
cvx_solver Gurobi
variables I(4,1) X(4,4)
minimize sum(I)
subject to
X * weight <= 10 * I
sum(X) == [1,1,1,1]
X >= 0
X <= 1
I >= 0
I <= 1
cvx_end
X
I
And this is the error message
Calling Gurobi 9.00: 44 variables, 28 equality constraints
------------------------------------------------------------
------------------------------------------------------------
Status: Error
Optimal value (cvx_optval): NaN
Error using cvx_end (line 267)
model.quadcon must be a struct array with fields q, and rhs
Try use these commands instead of Gurobi solver
cvx_solver Gurobi_3
or
cvx_solver Gurobi_2
Seems using Gurobi as external solver of cvx is not a wise choice. See: http://ask.cvxr.com/t/cvx-with-gurobi-error-warning/7072/3. They have reported the bug a few months ago.
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
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'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!)
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.