Normal data distribution as a constraint during data minimization using fmincon - matlab

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.

Related

Manipulating matrices in Matlab using fmincon

I'm currently trying to use fmincon in Matlab.
I can get it to operate correctly, but this falls over when i include manipulation of matrix elements which don't meet a condition.
As below
ub = 1.1;
lb = -1.2;
aut = -0.25;
h = #(aut)eQ_Optim(aut);
u = fmincon(h,aut, [], [], [], [], lb, ub)
The function i'm using is as follows
function [Maxim] = eQ_Optim(aut);
Data = [-0.23183483,-0.003274012;
-0.289945477,0.000282334;
-0.483591973,0.006588649;
-0.257735378,0.000887691;
-0.286463622,-0.003235662;
-0.453939127,0.004358216;
-0.196363243,0.004186609;
-0.209783591,0.001715187];
Data(Data(:,1)<aut,2)=0
MDOnx=Data(:,2)+1;
MD_Cumx=cumprod(MDOnx,1);
Maxim = MD_Cumx(end)*-1
end
I'm trying to get fmincon to optimise the variable 'aut', such that it maximises the cumulative sum of the right hand column (Data(:,2)).
for reference, the output i'm receiving is
Initial point is a local minimum that satisfies the constraints.
Optimization completed because at the initial point, the objective function is non-decreasing
in feasible directions to within the default value of the optimality tolerance, and
constraints are satisfied to within the default value of the constraint tolerance.
u =
-0.250000000000000
Which as you can see, is just my original guess being fed back to me.
I hope everything is clear. I've simplified everything as much as possible. Is this possible?
The solution was to use fminbid with applicable intervals
% % % https://uk.mathworks.com/help/matlab/ref/fminbnd.html

fmincon doesn't find a global minimum for a convex function

In my opinion, fmincon is a built-in function for local minimum in matlab. If the objective function is a convex problem, there is only one basin and the local minimum is the global minimum. While starting from different initial points in my experiment, the algorithm got different minimums function. I wonder if fmincon guarantees to be converged to a global minimum for convex problem. If not, is there any other techiniques I can use for convex opimization as fast as possible? Thanks.
P.S. fmincon use interior-point-method for searching minimum in default. Is this a normal problem for interior-point method, that is ,starting from different intial point, the method can get different global minimum for convex problem?
EDIT:
The objective is to minimize the sum of energy consumption by a group of users in a communication process, while the allocation of bandwidth is search. The transmission rate is
$r_k = x_k * log_2(1+\frac{g_k*p_k}{x_k})$
The optimization problem is as follow
$min_{x} sum_k \frac{p_k*b_k}{r_k}$
s.t. $sum_k x_k \leq X_{max}$
The objective and constraints are all convex, thus this should be a convex optimization problem.
For programming code, it is just as follow,
options = optimoptions('fmincon');
problem.options = options;
problem.solver = 'fmincon';
problem.objective = #(x) langBW(x, in_s, in_e, C1, a, p_ul);
problem.Aineq = ones(1,user_num);
problem.bineq = BW2;
problem.nonlcon = #(x) nonlConstr_bw(x,a,p_ul,T1,in_s,in_e,BW2);
problem.x0 = ones(user_num,1)
[b_ul,fval] = fmincon(problem);
langBW is the objective function, which is a convex function of x, the code of langBW is as follow,
function fmin = langBW(x, in_s, in_e, C1, a, p_ul)
if size(x,1)<size(x,2)
x = x';
end
b_ul = x;
r_ul = b_ul .* log2(1 + a.*p_ul./b_ul);
fmin = sum((in_s+in_e).*p_ul./r_ul) + sum(C1);
end
The nonlConstr_bw is the function of nonlinear constraints. It is shown as follow,
function [c,ceq] = nonlConstr_bw(x,a,p_ul,T1,in_s,in_e)
user_num = size(p_ul,1);
if size(x,1)<size(x,2)
x = x';
end
b_ul = x;
r_ul = b_ul .* log2(1 + a.*p_ul./b_ul);
c1 = max(in_s./r_ul) + in_e./r_ul - T1;
c = c1;
ceq = zeros(user_num,1);
end
Except x, all other variables are supplied. The problem is that when I set different problem.x0, for example, when problem.x0=ones(user_num,1);, the solution of [b_ul,fval] = fmincon(problem); is different from that when problem.x0=2*ones(user_num,1);. That is what I am confused about.
fmincon uses the following algorithms:
'interior-point' (default)
'trust-region-reflective'
'sqp' (Sequential Quadratic Programming)
'sqp-legacy'
'active-set'
These methods will converge to a local minimia but not necessarily a global minimum. Further minima may not be unique. The only way to guarantee a global minima is to search the whole solution space.
From your comment, there appears to be only a signal minima? (For example, a shifted parabola?) Then it should converge.
edit--
Even if your function appears convex, the constraints can lead to multiple local minima. Sometimes this is called a "loosely" convex function

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.

How do I optimize constrained integral expressions in MATLAB using anonymous functions?

I have an integrated error expression E = int[ abs(x-p)^2 ]dx with limits x|0 to x|L. The variable p is a polynomial of the form 2*(a*sin(x)+b(a)*sin(2*x)+c(a)*sin(3*x)). In other words, both coefficients b and c are known expressions of a. An additional equation is given as dE/da = 0. If the upper limit L is defined, the system of equations is closed and I can solve for a, giving the three coefficients.
I managed to get an optimization routine to solve for a purely based on maximizing L. This is confirmed by setting optimize=0 in the code below. It gives the same solution as if I solved the problem analytically. Therefore, I know the equations to solve for the coefficent a are correct.
I know the example I presented can be solved with pencil and paper, but I'm trying to build an optimization function that is generalized for this type of problem (I have a lot to evaluate). Ideally, polynomial is given as an input argument to a function which then outputs xsol. Obviously, I need to get the optimization to work for the polynomial I presented here before I can worry about generalizations.
Anyway, I now need to further optimize the problem with some constraints. To start, L is chosen. This allows me to calculate a. Once a is know, the polynomial is a known function of x only i.e p(x). I need to then determine the largest INTERVAL from 0->x over which the following constraint is satisfied: |dp(x)/dx - 1| < tol. This gives me a measure of the performance of the polynomial with the coefficient a. The interval is what I call the "bandwidth". I would like to emphasis two things: 1) The "bandwidth" is NOT the same as L. 2) All values of x within the "bandwidth" must meet the constraint. The function dp(x)/dx does oscillate in and out of the tolerance criteria, so testing the criteria for a single value of x does not work. It must be tested over an interval. The first instance of violation defines the bandwidth. I need to maximize this "bandwidth"/interval. For output, I also need to know which L lead to such an optimization, hence I know the correct a to choose for the given constraints. That is the formal problem statement. (I hope I got it right this time)
Now my problem is setting this whole thing up with MATLAB's optimization tools. I tried to follow ideas from the following articles:
Tutorial for the Optimization Toolbox™
Setting optimize=1 for the if statement will work with the constrained optimization. I thought some how nested optimization is involved, but I couldn't get anything to work. I provided known solutions to the problem from the IMSL optimization library to compare/check with. They are written below the optimization routine. Anyway, here is the code I've put together so far:
function [history] = testing()
% History
history.fval = [];
history.x = [];
history.a = [];
%----------------
% Equations
polynomial = #(x,a) 2*sin(x)*a + 2*sin(2*x)*(9/20 -(4*a)/5) + 2*sin(3*x)*(a/5 - 2/15);
dpdx = #(x,a) 2*cos(x)*a + 4*cos(2*x)*(9/20 -(4*a)/5) + 6*cos(3*x)*(a/5 - 2/15);
% Upper limit of integration
IC = 0.8; % initial
LB = 0; % lower
UB = pi/2; % upper
% Optimization
tol = 0.003;
% Coefficient
% --------------------------------------------------------------------------------------------
dpda = #(x,a) 2*sin(x) + 2*sin(2*x)*(-4/5) + 2*sin(3*x)*1/5;
dEda = #(L,a) -2*integral(#(x) (x-polynomial(x,a)).*dpda(x,a),0,L);
a_of_L = #(L) fzero(#(a)dEda(L,a),0); % Calculate the value of "a" for a given "L"
EXITFLAG = #(L) get_outputs(#()a_of_L(L),3); % Be sure a zero is actually calculated
% NL Constraints
% --------------------------------------------------------------------------------------------
% Equality constraint (No inequality constraints for parent optimization)
ceq = #(L) EXITFLAG(L) - 1; % Just make sure fzero finds unique solution
confun = #(L) deal([],ceq(L));
% Objective function
% --------------------------------------------------------------------------------------------
% (Set optimize=0 to test coefficent equations and proper maximization of L )
optimize = 1;
if optimize
%%%% Plug in solution below
else
% Optimization options
options = optimset('Algorithm','interior-point','Display','iter','MaxIter',500,'OutputFcn',#outfun);
% Optimize objective
objective = #(L) -L;
xsol = fmincon(objective,IC,[],[],[],[],LB,UB,confun,options);
% Known optimized solution from IMSL library
% a = 0.799266;
% lim = pi/2;
disp(['IMSL coeff (a): 0.799266 Upper bound (L): ',num2str(pi/2)])
disp(['code coeff (a): ',num2str(history.a(end)),' Upper bound: ',num2str(xsol)])
end
% http://stackoverflow.com/questions/7921133/anonymous-functions-calling-functions-with-multiple-output-forms
function varargout = get_outputs(fn, ixsOutputs)
output_cell = cell(1,max(ixsOutputs));
[output_cell{:}] = (fn());
varargout = output_cell(ixsOutputs);
end
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x(1)];
history.a = [history.a; a_of_L(x(1))];
case 'done'
otherwise
end
end
end
I could really use some help setting up the constrained optimization. I'm not only new to optimizations, I've never used MATLAB to do so. I should also note that what I have above does not work and is incorrect for the constrained optimization.
UPDATE: I added a for loop in the section if optimizeto show what I'm trying to achieve with the optimization. Obviously, I could just use this, but it seems very inefficient, especially if I increase the resolution of range and have to run this optimization many times. If you uncomment the plots, it will show how the bandwidth behaves. By looping over the full range, I'm basically testing every L but surely there's got to be a more efficient way to do this??
UPDATE: Solved
So it seems fmincon is not the only tool for this job. In fact I couldn't even get it to work. Below, fmincon gets "stuck" on the IC and refuses to do anything...why...that's for a different post! Using the same layout and formulation, fminbnd finds the correct solution. The only difference, as far as I know, is that the former was using a conditional. But my conditional is nothing fancy, and really unneeded. So it's got to have something to do with the algorithm. I guess that's what you get when using a "black box". Anyway, after a long, drawn out, painful, learning experience, here is a solution:
options = optimset('Display','iter','MaxIter',500,'OutputFcn',#outfun);
% Conditional
index = #(L) min(find(abs([dpdx(range(range<=L),a_of_L(L)),inf] - 1) - tol > 0,1,'first'),length(range));
% Optimize
%xsol = fmincon(#(L) -range(index(L)),IC,[],[],[],[],LB,UB,confun,options);
xsol = fminbnd(#(L) -range(index(L)),LB,UB,options);
I would like to especially thank #AndrasDeak for all their support. I wouldn't have figured it out without the assistance!

Matlab: finding the maximum point in for loop

For two variable function, say f(x,y)=x^2+y+b, where b is:
b=raylrnd(1*sqrt(2/pi),10^6,1) %% b is 1x1000000 vector
and subject to the constraint that: 2*x+1<=b.
I want to find the maximum of the function for a interval say x is between [-10,10], and y is between [-10,10] (Off course, my actual function is more complete than this, I will need help to set up the framework so I can apply it to my actual function).
Is there a way to implement this?
Attempt:
Step 1: Write a file objfun.m.
function f = objfun(x,b)
f = x(1)^2+(2)+b;
Step 2: Write a file confuneq.m for the nonlinear constraints.
function [c, ceq] = confuneq(x)
% Nonlinear inequality constraints
c = 2*x(1)+1-b;
Step 3: Invoke constrained optimization routine.
for i=1:1:length(b)
bi=b(i);
x0 = [-1,1]; % Make a starting guess at the solution
options = optimoptions(#fmincon,'Algorithm','sqp');
[x,fval] = fmincon(#objfun,x0,[],[],[],[],[],[],...
#confuneq,options);