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
I am using GA to optimize the parameters of the membership functions in my fuzzy system.
I create a function for fitness:
function y = gafuzzy(x)
global FISsys
global allData
global realResult
FISsys = readfis('aCAess.fis');
allData = importdata('ab.mat');
realResult = importdata('ad.mat');
FISsys.input(1,1).mf(1,1).params = [x(1) x(2) x(3)];
FISsys.input(1,1).mf(1,2).params = [x(4) x(5) x(6)];
FISsys.input(1,2).mf(1,1).params = [x(7) x(8) x(9)];
FISsys.input(1,2).mf(1,2).params = [x(10) x(11) x(12)];
FISsys.output.mf(1,1).params = [x(13) x(14) x(15)];
FISsys.output.mf(1,2).params = [x(16) x(17) x(18)];
c = evalfis(allData,FISsys);
e=sum(abs(c-realResult));
y = e;
end
And A[15*18] matrix for linear inequalities is :
A = [1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1;
0,1,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,0,0,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,-1,0]
and b[15*1] vector is:
b = [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
but when I run GA, I get this error:
Illegal parameters in fisTriangleMf() --> a > b
why?
Generally, in the triangle MF the first number, here a (shows the left vertex) should be smaller than the second number, here b (the top vertex). So you can have a triangle MF like [-1 0 1] but it cannot be like [0 -1 1].
in your code, I assume sometimes you don't satisfy the inequality in one of those places:
[x(1) < x(2) < x(3)];
[x(4) < x(5) < x(6)];
[x(7) < x(8) < x(9)];
[x(10) < x(11) < x(12)];
....
if the program is randomizing these values, you can bound them in your code easily by checking and replacing, for instance:
if x(1) >= x(2)
tmp = x(1);
x(1) = x(2);
x(2) = tmp;
end
I'm doing a homework assignment for scientific computing, specifically the iterative methods Gauss-Seidel and SOR in matlab, the problem is that for a matrix gives me unexpected results (the solution does not converge) and for another matrix converges.
Heres the code of sor, where:
A: Matrix of the system A * x = b
Xini: array of initial iteration
b: array independent of the system A * x = b
maxiter: Maximum Iterations
tol: Tolerance;
In particular, the SOR method, will receive a sixth parameter called w which corresponds to the relaxation parameter.
Here´s the code for sor method:
function [x2,iter] = sor(A,xIni, b, maxIter, tol,w)
x1 = xIni;
x2 = x1;
iter = 0;
i = 0;
j = 0;
n = size(A, 1);
for iter = 1:maxIter,
for i = 1:n
a = w / A(i,i);
x = 0;
for j = 1:i-1
x = x + (A(i,j) * x2(j));
end
for j = i+1:n
x = x + (A(i,j) * x1(j));
end
x2(i) = (a * (b(i) - x)) + ((1 - w) * x1(i));
end
x1 = x2;
if (norm(b - A * x2) < tol);
break;
end
end
Here´s the code for Gauss-seidel method:
function [x, iter] = Gauss(A, xIni, b, maxIter, tol)
x = xIni;
xnew = x;
iter = 0;
i = 0;
j = 0;
n = size(A,1);
for iter = 1:maxIter,
for i = 1:n
a = 1 / A(i,i);
x1 = 0;
x2 = 0;
for j = 1:i-1
x1 = x1 + (A(i,j) * xnew(j));
end
for j = i+1:n
x2 = x2 + (A(i,j) * x(j));
end
xnew(i) = a * (b(i) - x1 - x2);
end
x= xnew;
if ((norm(A*xnew-b)) <= tol);
break;
end
end
For this input:
A = [1 2 -2; 1 1 1; 2 2 1];
b = [1; 2; 5];
when call the function Gauss-Seidel or sor :
[x, iter] = gauss(A, [0; 0; 0], b, 1000, eps)
[x, iter] = sor(A, [0; 0; 0], b, 1000, eps, 1.5)
the output for gauss is:
x =
1.0e+304 *
1.6024
-1.6030
0.0011
iter =
1000
and for sor is:
x =
NaN
NaN
NaN
iter =
1000
however for the following system is able to find the solution:
A = [ 4 -1 0 -1 0 0;
-1 4 -1 0 -1 0;
0 -1 4 0 0 -1;
-1 0 0 4 -1 0;
0 -1 0 -1 4 -1;
0 0 -1 0 -1 4 ]
b = [1 0 0 0 0 0]'
Solution:
[x, iter] = sor(A, [0; 0; 0], b, 1000, eps, 1.5)
x =
0.2948
0.0932
0.0282
0.0861
0.0497
0.0195
iter =
52
The behavior of the methods depends on the conditioning of both matrices? because I noticed that the second matrix is better conditioned than the first. Any suggestions?
From the wiki article on Gauss-Seidel:
convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite
Since SOR is similar to Gauss-Seidel, I expect the same conditions to hold for SOR, but you might want to look that one up.
Your first matrix is definitely not diagonally dominant or symmetric. Your second matrix however, is symmetric and positive definite (because all(A==A.') and all(eig(A)>0)).
If you use Matlab's default method (A\b) as the "real" solution, and you plot the norm of the difference between each iteration and the "real" solution, then you get the two graphs below. It is obvious the first matrix is not ever going to converge, while the second matrix already produces acceptable results after a few iterations.
Always get to know the limitations of your algorithms before applying them in the wild :)