Mixed-Integer Nearest Optimal Solution in Matlab - matlab

Is it possible to find the nearest solution to optimal for a mixed-integer problem? For example I would want the simplified problem below:
f = [1;1;1];
intcon = 1:3;
Aeq = [0.99,0.97,0.15];
beq = 0.16;
lb = zeros(3,1);
ub = [1;1;1];
x = intlinprog(f,intcon,[],[],Aeq,beq,lb,ub)
to return x=[0;0;1] as this is the nearest integer solution to the objective value of 0.16. Instead currently it returns
Intlinprog stopped because no point satisfies the constraints.
Does not necessarily have to run intlinprog. Would ideally need to also work if beq is low, for example 0.14.

You can introduce some slack variables to allow some constraint violation when needed as follows:
largeValue = 100; % choose some large value to penalise the constraint violation
f_ = [f; largeValue; largeValue]; % penalise both slack variables
Aeq_ = [Aeq, 1, -1]; % add a positive and negative slack variable to the constraint
beq_ = beq;
lb_ = [lb; 0; 0]; % limit the constraint to a positive number
ub_ = [ub; inf; inf];
x_ = intlinprog(f_,intcon,[],[],Aeq_,beq_,lb_,ub_); % solve the adapted problem
x = x_(1:3) % extract the solution of the original problem
Remarks
I added two (positive) slack variables, one for a positive constraint violation and another one for a negative constraint violation.
You should penalise the slack variables with a large value, otherwise it is beneficial to violate your constraints more than strictly necessary. A more general approach would be to determine a good penalisation value based on the values in f and Aeq, for example
largeValue = 2*max(abs(f))/min(abs(Aeq(Aeq ~= 0)))

Related

Why does MATLAB fmincon ignore non-linear constraints?

I want to use MATLABs fmincon function to solve a non-linear problem of which I know that it can be solved in a different way very easily but I want to use fmincon (you might not need the following detailed information about the problem but I provided them in case you need):
Function f(x) is a quadratic function with its vertex at point (5|1).
f(x)=0.1(x-5)^2+1 for 0<=x<=5
Function g(x) is a polynom of order 4 with its vertex at Point (c|0).
g(x)=(x-c)^4 for 0<=x<=c
Function h is just a line on the x axis.
h=0 for c<=x<=5
I want to minimize the Area between the function f(x) and the two connected functions g(x) and h, in the interval [0,5]
minimize A=2*(int(f,[0,5])-int(g,[0,c]))=55/3 - (2*c^5)/5
Also I have the constraint that f(x) must always be 1 unit above the functions g(x) and h.
From the graph I know that the variable c must be between 0 and 2 (just a range for the fmincon function).
This is my .m file:
clc
clear
format long;
options = optimoptions(#fmincon, 'Display', 'iter', 'Algorithm', 'interior-point');
fun=#(x)55/3 - (2*(x(1))^5)/5;
lb = [0];
ub = [2];
[x,fval] = fmincon(fun,[0.1],[],[],[],[],lb,ub,#cons_Q6,options)
The constraints file looks like this (I inserted a lot of values for x with an increment of 0.1):
function [c,ceq]=cons_Q6(x)
c=[(0.0-x(1))^4-0.1*(0.0-5)^2
(0.1-x(1))^4-0.1*(0.1-5)^2
(0.2-x(1))^4-0.1*(0.2-5)^2
(0.3-x(1))^4-0.1*(0.3-5)^2
(0.4-x(1))^4-0.1*(0.4-5)^2
(0.5-x(1))^4-0.1*(0.5-5)^2
(0.6-x(1))^4-0.1*(0.6-5)^2
(0.7-x(1))^4-0.1*(0.7-5)^2
(0.8-x(1))^4-0.1*(0.8-5)^2
(0.9-x(1))^4-0.1*(0.9-5)^2
(1.0-x(1))^4-0.1*(1.0-5)^2
(1.1-x(1))^4-0.1*(1.1-5)^2
(1.2-x(1))^4-0.1*(1.2-5)^2
(1.3-x(1))^4-0.1*(1.3-5)^2
(1.4-x(1))^4-0.1*(1.4-5)^2
(1.5-x(1))^4-0.1*(1.5-5)^2
(1.6-x(1))^4-0.1*(1.6-5)^2
(1.7-x(1))^4-0.1*(1.7-5)^2
(1.8-x(1))^4-0.1*(1.8-5)^2
(1.9-x(1))^4-0.1*(1.9-5)^2
(2.0-x(1))^4-0.1*(2.0-5)^2
(2.1-x(1))^4-0.1*(2.1-5)^2
(2.2-x(1))^4-0.1*(2.2-5)^2
(2.3-x(1))^4-0.1*(2.3-5)^2
(2.4-x(1))^4-0.1*(2.4-5)^2
(2.5-x(1))^4-0.1*(2.5-5)^2
(2.6-x(1))^4-0.1*(2.6-5)^2
(2.7-x(1))^4-0.1*(2.7-5)^2
(2.8-x(1))^4-0.1*(2.8-5)^2
(2.9-x(1))^4-0.1*(2.9-5)^2
(3.0-x(1))^4-0.1*(3.0-5)^2
(3.1-x(1))^4-0.1*(3.1-5)^2
(3.2-x(1))^4-0.1*(3.2-5)^2
(3.3-x(1))^4-0.1*(3.3-5)^2
(3.4-x(1))^4-0.1*(3.4-5)^2
(3.5-x(1))^4-0.1*(3.5-5)^2
(3.6-x(1))^4-0.1*(3.6-5)^2
(3.7-x(1))^4-0.1*(3.7-5)^2
(3.8-x(1))^4-0.1*(3.8-5)^2
(3.9-x(1))^4-0.1*(3.9-5)^2
(4.0-x(1))^4-0.1*(4.0-5)^2
(4.1-x(1))^4-0.1*(4.1-5)^2
(4.2-x(1))^4-0.1*(4.2-5)^2
(4.3-x(1))^4-0.1*(4.3-5)^2
(4.4-x(1))^4-0.1*(4.4-5)^2
(4.5-x(1))^4-0.1*(4.5-5)^2
(4.6-x(1))^4-0.1*(4.6-5)^2
(4.7-x(1))^4-0.1*(4.7-5)^2
(4.8-x(1))^4-0.1*(4.8-5)^2
(4.9-x(1))^4-0.1*(4.9-5)^2
(5.0-x(1))^4-0.1*(5.0-5)^2
];
ceq=[];
As you can see, I've set the bounds for the unknown variable so that x(1)=[0,2] and I have set the constraints in the range [0,5] although I would only need them in the range of [0,2] because of the bounds for x(1).
Now, when I solve it like this I get a solution that doesn't fit all the constraints. But when I delete the unneccessary constraints in the range ]2;5]
function [c,ceq]=cons_Q6(x)
c=[(0.0-x(1))^4-0.1*(0.0-5)^2
(0.1-x(1))^4-0.1*(0.1-5)^2
(0.2-x(1))^4-0.1*(0.2-5)^2
(0.3-x(1))^4-0.1*(0.3-5)^2
(0.4-x(1))^4-0.1*(0.4-5)^2
(0.5-x(1))^4-0.1*(0.5-5)^2
(0.6-x(1))^4-0.1*(0.6-5)^2
(0.7-x(1))^4-0.1*(0.7-5)^2
(0.8-x(1))^4-0.1*(0.8-5)^2
(0.9-x(1))^4-0.1*(0.9-5)^2
(1.0-x(1))^4-0.1*(1.0-5)^2
(1.1-x(1))^4-0.1*(1.1-5)^2
(1.2-x(1))^4-0.1*(1.2-5)^2
(1.3-x(1))^4-0.1*(1.3-5)^2
(1.4-x(1))^4-0.1*(1.4-5)^2
(1.5-x(1))^4-0.1*(1.5-5)^2
(1.6-x(1))^4-0.1*(1.6-5)^2
(1.7-x(1))^4-0.1*(1.7-5)^2
(1.8-x(1))^4-0.1*(1.8-5)^2
(1.9-x(1))^4-0.1*(1.9-5)^2
(2.0-x(1))^4-0.1*(2.0-5)^2
];
ceq=[];
then I get the right result. Does anyone know why this happens and why MATLAB does not respect the constraints when I put them up for the whole range [0,5]?
-Your problem is more related to calculus than matlab tool
constraints like
function [c]=cons_Q6(x)
c=[x < 0; x > 0]; are just ignored by fmincon, because they are not logical
Technically you need to know the optimum c before solving
this optimization problem
- Another issue A = int(f,[0,5])-int(g,[0,c]) = 55/6 - c^5/5 instead of
A = 2*(int(f,[0,5])-int(g,[0,c])) = 55/3 - (2*c^5)/5
Factor 2 is used whether for even whether for odd function (like cosine or since).
Even for those kind of function the integration interval is reduced by half
I updated your optimization function and the solution c is as follow
x = [0, c], constraint is g(x)-f(x)-1<= 0--> (x-c)^4 -0.1(x-5)^2 <=0
x = [c, 5], constraint is h(x)-f(x)-1<= 0--> -0.1(x-5)^2 <=0
c must be predefined or guessed in advance, here I supposed c = 2
because your upper bound ub = 2
As a result
x = [0, 2], --> (x-c)^4 -0.1(x-5)^2 <=0
x = [2, 5], --> -0.1(x-5)^2 <=0
cons_Q6(x) is as follow
function [c,ceq]=cons_Q6(x)
c=[(0.0-x)^4-0.1*(0.0-5)^2;
(0.1-x)^4-0.1*(0.1-5)^2;
(0.2-x)^4-0.1*(0.2-5)^2;
(0.3-x)^4-0.1*(0.3-5)^2;
(0.4-x)^4-0.1*(0.4-5)^2;
(0.5-x)^4-0.1*(0.5-5)^2;
(0.6-x)^4-0.1*(0.6-5)^2;
(0.7-x)^4-0.1*(0.7-5)^2;
(0.8-x)^4-0.1*(0.8-5)^2;
(0.9-x)^4-0.1*(0.9-5)^2;
(1.0-x)^4-0.1*(1.0-5)^2;
(1.1-x)^4-0.1*(1.1-5)^2;
(1.2-x)^4-0.1*(1.2-5)^2;
(1.3-x)^4-0.1*(1.3-5)^2;
(1.4-x)^4-0.1*(1.4-5)^2;
(1.5-x)^4-0.1*(1.5-5)^2;
(1.6-x)^4-0.1*(1.6-5)^2;
(1.7-x)^4-0.1*(1.7-5)^2;
(1.8-x)^4-0.1*(1.8-5)^2;
(1.9-x)^4-0.1*(1.9-5)^2;
(2.0-x)^4-0.1*(2.0-5)^2;
-0.1*(2.1-5)^2;
-0.1*(2.2-5)^2;
-0.1*(2.3-5)^2;
-0.1*(2.4-5)^2;
-0.1*(2.5-5)^2;
-0.1*(2.6-5)^2;
-0.1*(2.7-5)^2;
-0.1*(2.8-5)^2;
-0.1*(2.9-5)^2;
-0.1*(3.0-5)^2;
-0.1*(3.1-5)^2;
-0.1*(3.2-5)^2;
-0.1*(3.3-5)^2;
-0.1*(3.4-5)^2;
-0.1*(3.5-5)^2;
-0.1*(3.6-5)^2;
-0.1*(3.7-5)^2;
-0.1*(3.8-5)^2;
-0.1*(3.9-5)^2;
-0.1*(4.0-5)^2;
-0.1*(4.1-5)^2;
-0.1*(4.2-5)^2;
-0.1*(4.3-5)^2;
-0.1*(4.4-5)^2;
-0.1*(4.5-5)^2;
-0.1*(4.6-5)^2;
-0.1*(4.7-5)^2;
-0.1*(4.8-5)^2;
-0.1*(4.9-5)^2;
-0.1*(5.0-5)^2;
];
ceq=[];
The constraints in the range ]2;5] are very necessary keep them
clc
clear
format long;
options = optimoptions(#fmincon, 'Display', 'iter', 'Algorithm',...
'interior-point');
fun=#(x)55/6 - (x^5)/5;
lb = [0];
ub = [2];
[c, A] = fmincon(fun,[0.1],[],[],[],[],lb,ub,#cons_Q6,options)
solution :
c = 1.257432726024430
A = 8.537951710969493

Normal data distribution as a constraint during data minimization using fmincon

I want to solve a minimization problem between two datasets A and B defined here:
% sample data
nvars = 1000;
A = rand(69,1);
B = rand(69,nvars);
% Objective function
fun_Obj = #(Alpha,A,B) norm(A- sum(Alpha.*B(:,:),2),2);
I would like to introduce two constraints (confuneq1 and confuneq2) during the data minimization of the objective function fun_Obj (between A and B), with Alpha as a variable parameter. Fmincon is defined as follow:
[~]= fmincon(#(Alpha)fun_Obj(Alpha,A,B),x0,[],[],[],[],lb,ub,{confuneq1;confuneq2},options);
with:
- confuneq1 = #(Alpha)deal(-1, sum(Alpha)-1); % 1st constraint
- x0 = ones(1,nvars)/nvars; % starting values
- lb = zeros(1,nvars); ub = ones(1,nvars); % lower and upper bounds
The first constraint is working well. Regarding the second one, I want to force the module of Alpha to decrease when its index is increasing following a defined distribution (linear, normal...).
I tried to define several expression such as :
- confuneq2 = #(Alpha)deal(-1,Alpha(i+1)<Alpha(i));
- confuneq2 = #(Alpha)deal(-1,normpdf(Alpha,lb,sigma)-1) % Normal distribution decrease
but I can't manage to implement it correctly for fmincon.
Thanks a lot for your precious help.

Constrained minimization using fmincon

I wanted to solve a constrained minimization problem using fmincon. But the constraints are defined in terms of a function like f(x_0)<a, where x_0 is a solution to the problem. Is it possible?
On the docs, the example only include this x_0<a form.
Code:
f_obj = #(x)var_zcors(x,t_cw);
opt_theta = fminbnd(f_obj,0,360);
Now, x should constrained such that f_constraint(x)< a.
Update(From answer by #Phil Goddard):
f_obj = #(x)var_zcors(x,t_cw);
f_nl = #(x)deal(f_constraint(x)-a,[]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);
Say in the above code f_constraint returns a vector [x_min y_max] instead of a scalar. And I want to specify the following constraints:
x_min>b
y_max<a
What is a possible way to achieve that?
You have a nonlinear constraint and hence need to use the nonlinear constraint input to fmincon. That is,
f_obj = #(x)var_zcors(x,t_cw);
f_nl = #(x)deal(f_constraint(x)-a,[]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);
If you have multiple (non-linear) constraints, then as per the examples in the doc, you write a function to return a vector of constraints. In your case you want to write a function in a separate file like the following:
function [c,ceq] = my_nonlinear_constraints(x,ab)
% define the non-linear inequality constraints
% (This assumes that ab is a 2 element vector containing your a and b
% variables.)
[x_min,y_max] = f_constraint(x);
c = nan(2,1);
c(1) = -x_min+ab(2); % this is x_min>b
c(2) = y_max-ab(1); % this is y_max<a
% There are no non-linear equality constraints, but this is required
ceq = [];
Then, to perform the optimization, you want
% Variables a and b must be defined prior to this.
f_obj = #(x)var_zcors(x,t_cw);
f_nl = #(x)my_nonlinear_constraints(x,[a b]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);

Matlab Optimisation - Minimise objective function using genetic algorithm

I want to set up the generic algorithm for a function that includes roughly 400 lines of script. The script itself is an optimisation process and I want to use the genetic algorithm to find the best input parameters into the optimisation process (M and OPratio). M lies between 0 and 10^7 and OPratio between 0 and 1.
The function of the script is:
NPVtotal = cut_off_optimisation(M,OPratio)
set up for the genetic algorithm:
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options.InitialPopulationMatrix = X0;
[M,OPratio,fval] = ga(cut_off_optimisation(M,OPratio),nvars,[],[],[],[],LB,UB)
I get following error:
Undefined function or variable 'M'.
I am new to optimisation and the genetic algorithm so would appreciate any help, please let me know if more information is necessary.
First of all I am assuming that the objective is to minimize the Objective function cut_off_optimisation.
Now first update your function to look like this
function y = cut_off_optimisation(x)
M=x(1);
OPratio=x(2);
%
% paste body of your currently used function here
%
y=NPVtotal ;
Now use this code to minimize your objective function.
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{#gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(#cut_off_optimisation,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
Update: If you don't want to update your function. Just run this main code. Keep the function NPVtotal = cut_off_optimisation(M,OPratio) in the same folder as that of the main code.
objectiveFunction=#(x)cut_off_optimisation(x(1),x(2));
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [10000000 1]; % Upper bound
X0 = [6670000 0.45]; % Start point
options = gaoptimset('PlotFcns',{#gaplotbestf},'Display','iter','InitialPopulation',X0);
[x,fval] = ga(objectiveFunction,nvars,[],[],[],[],...
LB,UB,[],options);
M=x(1);
OPratio=x(2);
fval
M
OPratio
Update: For getting final population members and fitness values. Replace the above ga function call statement to below statement.
[x,fval,exitflag,output,population,score] = ga(objectiveFunction,nvars,[],[],[],[],LB,UB,[],options);
M=x(1);
OPratio=x(2);
In here population will have the members of the final population and score will have fitness values for the final population. Default population size is 20. So you will have 20 rows in both the matrix. Number of columns in population will be equivalent to number of variables in the problem and score will be a column matrix. You can change the population size by adding option PopulationSize to gaoptimset.
options = gaoptimset('PlotFcns',{#gaplotbestf},'Display','iter','InitialPopulation',X0,'PopulationSize',30);
To know more about the options available for gaoptimset and their expected values and their {default values}. Go to matlab help and search for gaoptimset. There you will find a table with all these details. Here is the link from matlab website http://in.mathworks.com/help/gads/gaoptimset.html .There may be changes according to your matlab version. So its better to use help in matlab.

Minimizing a function in matlab

I would like to minimize w'Hw, with respect to w, where w is a vector, and H is matrix.
And with the following constraint, |w1|+|w2|+|w3| < 3, ie. the l1 norm of the weights vector is less that 3.
How can I do this in matlab?
Thanks
You're trying to solve a quadratic minimization problem with linear constraints (also known as quadratic programming).
Do you know anything about your matrix H -- in particular, is it positive semidefinite? I would really expect this to be the case, since this is usual for the problem domains in which quadratic programming problems usually crop up.
If H really is positive semidefinite, and your only constraint is |w1|+|w2|+|w3| < 3, then, as Richie Cotton has already pointed out, the minimum is trivially at w=0. Maybe you have some additional constraints?
If you do have additional constraints, but H is still positive semidefinite, there are existing efficient solvers for this class of problem. In MATLAB, take a look at quadprog.
You'll have to reformulate your single nonlinear constraint |w1|+|w2|+|w3| < 3 as a series of linear constraints.
In the one-dimensional case, the constraint |w1| < 1 turns into two linear constraints:
w1 < 1
-w1 < 1.
In the two-dimensional case, the constraint |w1| + |w2| < 1 turns into four linear constraints:
w1+w2 < 1
w1-w2 < 1
-w1+w2 < 1
-w1-w2 < 1
I'll leave the extension to three dimensions to you.
you need to use the optimization toolbox, specifically fmincon:
use fun to establish w'Hw, and you want c(eq) = (|w1|+|w2|+|w3| - 3) <0 which you set with nonlcon (in the documentation).
I'd suggest you look at the fminsearch function in the matlab documentation.
Rasman, below is the fmincon code I am using:
function PortfolioWeights = GMVPC1Type2(SCM)
w0 = [0.1 0.1 0.1 0.1 0.1];
n = length(w0);
options = optimset('Display','final-detailed');
PortfolioWeights = fmincon(#myobj2,w0,[],[],ones(1,n),1,[],[],#myconstraint1,options)
function f = myobj2(w)
w = [w(1);w(2);w(3);w(4);w(5)];
f = w'*SCM*w;
end
end
-----------------------------------------------------------------------------
function [c ceq] = myconstraint1(w)
c = abs(w(1))+abs(w(2))+abs(w(3))+abs(w(4))+abs(w(5))-1
ceq = [];
end
------------------------------------------------------------------------------
I added in options = optimset('Display','final-detailed'); as you suggested. I get the following message:
Optimization stopped because the predicted change in the objective function,
6.115031e-009, is less than options.TolFun = 1.000000e-006, and the maximum constraint
violation, 0.000000e+000, is less than options.TolCon = 1.000000e-006.
Optimization Metric Options
abs(steplength*directional derivative) = 6.12e-009 TolFun = 1e-006 (default)
max(constraint violation) = 0.00e+000 TolCon = 1e-006 (default)
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
1
PortfolioWeights =
0.2000 0.2000 0.2000 0.2000 0.2000
The matrix I am using is:
0.000257165759136336 8.48196702102889e-05 9.27141501220362e-05 0.000111360154790061 0.000155196440517440
8.48196702102889e-05 0.000277377166669392 0.000101880007672550 0.000107375764193076 0.000117042329431538
9.27141501220362e-05 0.000101880007672550 0.000300697293925817 0.000112004860252653 0.000134354417344316
0.000111360154790061 0.000107375764193076 0.000112004860252653 0.000311028738698100 0.000147296211557256
0.000155196440517440 0.000117042329431538 0.000134354417344316 0.000147296211557256 0.000376418027192374