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
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.
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 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?
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 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?