Finding the right solver - matlab

I need to solve the following non-linear problem with constraints but I am not sure to use the appropriate function and solver as changing from the algorithm: 'interior-point' to 'sqp' give different answers. Could you help first to make sure fmincon is the most appropriate function here and also if the options should be set up in a different way to ensure that the optimization does not stop too early. I have tried to set up: 'MaxFunctionEvaluations' to 1000000 and 'MaxIterations'to 10000 and this helps but the time required increases as well dramatically.
w is a vector whose values have to be optimized.
scores is a vector of the same size as w.
the size if w and scores is usually between 10 and 40.
Here is the optimization setup:
objective function: sum(log(abs(w)));
linear inequalities1: w_i > 0 if scores_i > 0
linear inequalities2: w_i < 0 if scores_i < 0
non-linear equalities1: sum(abs(w)) = 1
non-linear inequalities1: sqrt(w * sigma * w') <= 0.1
Here is my code:
N = numel(scores);
%%%LINEAR EQUALITIES CONSTRAINTS
Aeq = [];
beq = [];
%%%LINEAR INEQUALITIES CONSTRAINTS
temp = ones(0,N);
temp(scores >= 0) = -1;
temp(scores < 0) = 1;
A = diag(temp);
b = zeros(N,1);
%%%BOUNDs CONSTRAINTS
lb = [];
ub = [];
w0 = ones(1,N)/N;
nonlcon = #(w)nonlconstr(w,sigma,tgtVol);
options = optimoptions('fmincon','Algorithm','interior-point','MaxFunctionEvaluations',1000000,'MaxIterations',10000);
%options = optimoptions('fmincon','Algorithm','interior-point','OptimalityTolerance',1e-7);
[w,fval,exitflag,output] = fmincon(#(w)objectfun(w),w0,A,b,Aeq,beq,lb,ub,nonlcon,options);
function [c,ceq] = nonlconstr(w,sigma,tgtVol)
c = sqrt(w*sigma*w') - tgtVol;
ceq = sum(abs(w)) - 1;
end
function f = objectfun(w)
f = sum(-log(abs(w)));
end

Related

Solving Coupled partial differential equations of stiff nature using MATLAB

I want to solve coupled partial differential equations of first order, which are of stiff nature. I have coded in MATLAB to solve this pde's, I have used Method of line to convert PDE into ODE, and i have used beam and warmings(second order upwind) method to discritize the spatial derivative. The discretization method is total variation diminishing(TVD) to eliminate the oscillation. But rather using TVD and ode15s solver to integrate resultant stiff ode's the resultant plot is oscillatory(not smooth). What should i do to eliminate this oscillation and get correct results.
I have attached my MATLAB code.. please see it and suggest some improvement.
∂y(1)/∂t=-0.1 ∂y(1)/∂x + (0.5*e^(15*(y(2)⁄(1+y(2))))*(1- y(1))
∂y(2)/∂t=-0.1 ∂y(2)/∂x - (0.4*e^(15*(y(2)⁄(1+y(2))))*(1- y(1))-0.4
Initial condition: at t = 0 y(1)= y(2)=0
Boundary condition: y(1)= y(2) = 0 at x=0
I have attached my MATLAB code.. please see it and suggest some improvement.
function brussode(N)
if nargin<1
N = 149;
end
tspan = [0 10];
m = 0.00035
t = (1:N)/(N+1)*m;
y0 = [repmat(0,1,N); repmat(0,1,N)];
p = 0.5
q = 0.4
options = odeset('Vectorized','on','JPattern',jpattern(N));
[t,y] = ode15s(#f,tspan,y0,options);
a = size(y,2)
u = y(:,1:2:end);
x = (1:N)/(N+1);
figure;
%surf(x,t(end,:),u);
plot(x,u(end,:))
xlabel('space');
ylabel('solution');
zlabel('solution u');
%--------------------------------------------------------------
%Nested function -- N is provided by the outer function.
%
function dydt = f(t,y)
%Derivative function
dydt = zeros(2*N,size(y,2)); %preallocate dy/dt
x = (1:N)/(N+1);
% Evaluate the 2 components of the function at one edge of the grid
% (with edge conditions).
i = 1;
%y(1,:) = 0;
%y(2,:) = 0;
dydt(i,:) = -0.1*(N+1)*(y(i+2,:)-0)+ (0.01/2)*m*((N+1).^3)*(y(i+2,:)-0) + p*exp(15*(0/(1+0)))*(1-0);
dydt(i+1,:) = -0.1*(N+1)*(y(i+3,:)-0)+ (0.01/2)*m*((N+1).^3)*(y(i+3,:)-0) - q*exp(15*(0/(1+0)))*(1-0)+0.25;
i = 3;
%y(1,:) = 0;
%y(2,:) = 0;
dydt(i,:) = -0.1*(N+1)*(y(i+2,:)-y(i,:)) + (0.01/2)*m*((N+1).^3)*(y(i+3,:)-y(i,:)) + p*exp(15*(y(i+1,:)/(1+y(i+1,:))))*(1-y(i,:));
dydt(i+1,:) = -0.1*(N+1)*(y(i+3,:)-y(i+1,:)) + (0.01/2)*m*((N+1).^3)*(y(i+3,:)-y(i,:)) - q*exp(15*(y(i+1,:)/(1+y(i+1,:))))*(1-y(i,:))+0.25;
%Evaluate the 2 components of the function at all interior grid
%points.
i = 5:2:2*N;
%y(1,:) = 0;
% y(2,:) = 0;
dydt(i,:) = (-0.1/2)*(N+1)*(3*y(i,:)-4*y(i-2,:)+y(i-4,:)) +(0.01/2)*m*((N+1).^3)*(y(i,:)-2*y(i-2,:)+y(i-4,:))+ p*exp(15*(y(i+1,:)/(1+y(i+1,:))))*(1-y(i,:));
dydt(i+1,:) = (-0.1/2)*(N+1)*(3*y(i+1,:)-4*y(i-1,:)+y(i-3,:))+(0.01/2)*m*((N+1).^3)*(y(i+1,:)-2*y(i-1,:)+y(i-3,:)) - q*exp(15*(y(i+1,:)/(1+y(i+1,:))))*(1-y(i,:))+0.25;
end
%-------------------------------------------------------------
end %brussode
%-------------------------------------------------------------
% Subfunction -- the sparsity pattern
%
function S = jpattern(N)
% Jacobian sparsity patter
B = ones(2*N,5);
B(2:2:2*N,2) = zeros(N,1);
B(1:2:2*N-1,4) = zeros(N,1);
S = spdiags(B,-2:2,2*N,2*N);
end
%-------------------------------------------------------------

Numerically solving a pde with changing domain in Matlab

I'm trying to implement a changing domain into my Matlab code which solves a pde numerically. In the given problem, I am modelling the baking of bread. It is a 1D problem where there is a single heat source from above. I am using the heat diffusion equation and for a fixed domain, I have provided my code below which solves it. However, I need to also model the rising of the bread, so over time my domain, 'L' in my code, changes. I am told that I can assume it changes linearly over an hour and that the bread initially fills 80% of the tin. What suggestions do you guys have for how I can account for the rising?
close all
clear all
clc
Tend = 3600; %Time to end after
dt = 1; %Time step size
Nstep = (Tend/dt)+1; %Number of time points
M = 1000; %Number of x points
dx = 1/M; %Size of the x steps
L = 1; %Length of domain
x = [0:dx:L]; %Creating the x points
kdiff = 2.7e-4; %Diffusion constant value
R = kdiff*dt/(dx*dx);
for i = 1:M+1
u(i,1) = 20;
end
for n = 1:Nstep
DIG(1) = 1;
b(1) = 200; %Boundry condition
DIG(M+1) = 1;
LF(1) = 0;
RT(1) = 0;
LF(M+1) = -1; %Insulated boundry
RT(M+1) = 0;
b(M+1) = 0; %Boundry condition
for m = 2:M
DIG(m) = 2+(2/R);
LF(m) = -1;
RT(m) = -1;
b(m) = u(m+1,n) + u(m-1,n) + (-2 + (2/R))*u(m,n);
end
u(:,n+1) = tdma(LF,DIG,RT,b,M+1);
end
plot(x,u(:,1:600:end)

How to define a negative constraint only in Aeq * X <= Beq

I am using quadprog link to find a portfolio of optimal weights.
So far, I have managed to implement a long only constraint (i.e. weights cannot be smaller than zero w >= 0 and w1 + w2 + ... wN = 1) as follows:
FirstDegree = zeros(NumAssets,1);
SecondDegree = Covariance;
Aeq = ones(1,NumAssets);
beq = 1;
A = -eye(NumAssets);
b = zeros(NumAssets,1);
x0 = 1/NumAssets*ones(NumAssets,1);
MinVol_Weights = quadprog(SecondDegree,FirstDegree,A,b,Aeq,beq,[],[],x0, options);
I am now tring to setup a short-only constraints, i.e. all the weights need to add up to -1 and they should be all strict smaller or equal to zero. How can this be rewritten?
Note that you can rewrite any “greater than” inequality a ≥ b into a “less than” inequality -a ≤ -b by flipping signs. In your example, choose
Aeq = ones(1,NumAssets);
beq = -1;
A = eye(NumAssets);
b = zeros(NumAssets,1);
That means Aeq*w == w(1) + w(2) + … + w(NumAssets) == -1, and A*w <= 0 which is the same as saying w(i) <= 0 for all i.

Non-linear constrained optimization in Matlab

Given a function f(k',w') = ((k'-k)^2+(w'-w)^2)^(1/2), where k and w are known real parameters. The objective is to find a couple (k',w') such that f(k',w') is minimal under the following constraints
b(v,s,w') < 10s <=> w'< 10s
b(v,s,w')< a(v,s,k')^2 <=> (w'-10s)-(k'-3)^2/s < 0
q( a(v,s,k'),b(v,s,w')) < s[v^(1/2)]
where b(v,s,w') = (v/s)(w'-10s) and a(v,s,k') = (1/s)(k'-3)v^(1/2). Above that, v (=vari) > 0 and s (=skew) < 0 are known parameters. Furthermore q(a,b) is a root of the quartic polynomial:
(48a^2 + 16b)x^4 + (-40a^3 - 168ab)x^3 + (-45a^4 + 225a^2b + 72b^2)x^2 + (27a^3b - 162ab^2)x + 27b^3.
To be more precise, whenever the quartic has four real roots then q is the second greatest root. If the quartic has two real and two complex roots then q is the greatest real root. The problem is that the algebraic expressions for q are quite monstrous. Ideally, I would like an analytical solution for the above non-linear constrained optimization problem. However, I think that might turn out quite ugly. Therefore, I thought it was better to do it numerically with Matlab by using constrained optimizers such as fmincon.
f = #(x) sqrt((x(1)-hyp_skew)^2+(x(2)-kurt)^2);
A = [1,0];
d = 10*skew;
Aeq = [];
beq = [];
ub = [];
lb = [];
[x,fval,exitflag] = fmincon(f,[w,k],A,d,Aeq,beq,lb,ub,#(x)quarticcondition2(x,skew,vari),options);
where the (non-linear) optimization function is given by
function [c, ceq] = quarticcondition2(x,skew,vari)
av = ((x(2)-3)*sqrt(vari))/skew;
bv = (vari/skew)*(x(1)-10*skew);
A = (-40*av^3-168*av*bv)/(48*av^2+16*bv);
B = (-45*av^4+225*av^2*bv+72*bv^2)/(48*av^2+16*bv);
C = (27*av^3*bv-162*av*bv^2)/(48*av^2+16*bv);
D = (27*bv^3)/(48*av^2+16*bv);
roots_quartic = roots([1,A,B,C,D]);
z = imag(roots_quartic);
in = find(z ~= 0);
if isempty(in)
r = sort(roots_quartic);
c2 = r(2)-skew*sqrt(vari);
else
index = find(z == 0);
c2 = max(roots_quartic(index))-skew*sqrt(vari);
end
c1 = ((x(2)-3)^2/skew)-(x(1)-10*skew);
c = [c1 c2];
ceq = [];
end
My code works for some initial parameter sets [w,k]. However, finding this initial parameter set turns out to be quite difficult (since the constraints are hard to handle). I need to run the program for quite some possible scenarios, hence it would be nice to have some logic in choosing my starting values. I know this is a well known issue when using optimization solvers. However, is there a good/proper way to find start values?
Thanks!
Cheers

Giving priority to some variables in linear programming

I have to optimize an objective using binary integer linear programming, my objective function is:
Maximize f= (c1 * x1) + (c2 * x2) +(c3 * x3) + ... + (c10000 * x10000)
Subject to some constraints
For solving the problem efficiently I want to use some heuristics, according to one of the heuristics, some variables(xi) have more chance to be part of the answer (Xi=1), so my goal is to give priority (preference) to such variables to solve the problem faster than usual way, I know the solution may be sub-optimal but our main concern is time.
So my question are:
How to prioritize this variables in the LP model?
Can we multiply coefficients of this variables by constant (C>1) in order to increase their priority? or decrease priority of other variables by multiply their coefficients by another constant (D<1)?
If we use the approach of question #2, do we have to do that just with objective function coefficients or both of objective function coefficients and constraints coefficients should be altered related to those variables?
It should be noted that in the approach of question #2, after solving the LP model, we rollback any of changes in the coefficients according to the solution (Which variables are in the solution).
Thanks in advance
If you know that xi will be part of solution, you should include it as 1 into initial point x0 you pass to bintprog. The same for xj known to be likely not part of solution should be included as 0. If initial point is very close to the solution, this will reduce time to find it.
x = bintprog(f,A,b,Aeq,beq,x0);
Another option is to relax BILP problem to LP problem with adding two extra conditions
x <= 1
-x <= 0
and then using rounded solution for this problem as initial point for BILP problem.
Here authors state that bintprog performs well only on small problems. As I use Octave instead of Matlab, I tried GNU Linear Programming Kit (glpk). I tried to solve BILP problem from Matlab documentation and here is a script
close all; clear all;
f = [25,35,28,20,40,-10,-20,-40,-18,-36,-72,-11,-22,-44,-9,-18,-36,-10,-20]';
A = zeros(14,19);
A(1,1:19) = [25 35 28 20 40 5 10 20 7 14 28 6 12 24 4 8 16 8 16];
A(2,1) = 1; A(2,6) = -1; A(2,7) = -1; A(2,8) = -1;
A(3,2) = 1; A(3,9) = -1; A(3,10) = -1; A(3,11) = -1;
A(4,3) = 1; A(4,12) = -1; A(4,13) = -1; A(4,14) = -1;
A(5,4) = 1; A(5,15) = -1; A(5,16) = -1; A(5,17) = -1;
A(6,5) = 1; A(6,18) = -1; A(6,19) = -1;
A(7,1) = -5; A(7,6) = 1; A(7,7) = 2; A(7,8) = 4;
A(8,2) = -4; A(8,9) = 1; A(8,10) = 2; A(8,11) = 4;
A(9,3) = -5; A(9,12) = 1; A(9,13) = 2; A(9,14) = 4;
A(10,4) = -7; A(10,15) = 1; A(10,16) = 2; A(10,17) = 4;
A(11,5) = -3; A(11,18) = 1; A(11,19) = 2;
A(12,2) = 1; A(12,5) = 1;
A(13,1) = 1; A(13,2) = -1; A(13,3) = -1;
A(14,3) = -1; A(14,4) = -1; A(14,5) = -1;
b = [125 0 0 0 0 0 0 0 0 0 0 1 0 -2]';
lb = zeros(size(f));
ub = ones(size(f));
ctype = repmat("U" , size(b))'; # inequality constraint
sense = 1; # minimization
param.msglev = 0;
vartype = repmat("C" , size(f)); # continuous variables
tic
for i = 1:10000
[xopt, fmin, errnum, extra] = glpk (f, A, b, lb, ub, ctype, vartype, sense, param);
end
toc
fprintf('Solution %s with value %f\n', mat2str(xopt), fmin)
vartype = repmat("I" , size(f)); # integer variables
tic
for i = 1:10000
[xopt, fmin, errnum, extra] = glpk (f, A, b, lb, ub, ctype, vartype, sense, param);
end
toc
fprintf('Solution %s with value %f\n', mat2str(xopt), fmin)
These are found solutions:
Elapsed time is 7.9 seconds.
Solution [0;0.301587301587301;1;1;0;0;0;0;0;0.603174603174603;0;1;1;0.5;1;1;1;0;0] with value -81.158730
Elapsed time is 11.5 seconds.
Solution [0;0;1;1;0;0;0;0;0;0;0;1;0;1;1;1;1;0;0] with value -70.000000
I had to perform 10000 iterations to make performance difference visible as the problem is still quite small. LP solution is faster comparing to BILP solution, and they are quite close.
According to CPLEX Performance Tuning for Mixed Integer Programs and Issuing priority orders we can set priority orders to increase or decrease priority of some variables in CPLEX, this approach is as follows:
options = cplexoptimset('cplex');
options.mip.ordertype=fsl;
[x,fval,exitflag,output] = cplexbilp(f, Aineq, bineq, Aeq, beq,[],options);
fsl is priority array for problem variables.
Because CPLEX can generate a priority order automatically, based on problem-data characteristics, we can leave the prioritization decision to CPLEX as follows:
value branching priority order
===== ========================
0 no automatic priority order will be generated (default)
1 decreasing cost coefficients among the variables
2 increasing bound range among the variables
3 increasing cost per matrix coefficient count among the variables
After using priorities, my problem is solved, the solution is valid and converging to solution is faster than before!