this is my question:
I have six parametres that I'll vary in fmincon toolbox of MATLAB, and these are the restrictions:
variables=[x1,x2,x3,x4,x5,x6]
x1<=x2
x3<=x4
x5<=x6
So, I defined the A matrix for fmincon like this:
A=[1 -1 0 0 0 0; 0 0 1 -1 0 0; 0 0 0 0 1 -1];
and vector b:
b=[0;0;0];
And the problem is that fmincon doesn't respect the conditions (if they are well defined), I mean por the first iteration, the parameters were:
ddx=[1 1 1 1 1 1];
And for the second iteration the variation was:
ddx=[2 1 1 1 1 1];
As you can see, there's a mistake with respect the definition in the boundaries, because x1=2 and x2=1; which is not right.
I hope you can give me some ideas.
This is an example:
function Example
clc
global xo
lbr=[2,2,300,300,3,3];
upr=[8,8,2000,2000,6,6];
xo =[3,4,400,500,4,5];
Aeq=[];
beq=[];
% MatrÃz de condiciones
Ain=[1 -1 0 0 0 0;0 0 1 -1 0 0;0 0 0 0 1 -1];
bin=[0;0;0];
lb=lbr./xo;
ub=upr./xo;
xo_n=ones(1,size(upr,2));
options1 = optimset('Display','iter',...
'Algorithm','sqp','PlotFcns',#optimplotfval,...
'MaxIter',400,'MaxFunEvals',1000,'DiffMinChange',1);
[ddx, FOval] = fmincon(#MyfunSSI,xo_n,Ain,bin,Aeq,beq,lb,ub,[],options1);
end
function [FO]=MyfunSSI(ddx)
global xo
ddx
x1=xo(1)*ddx(1);
x2=xo(2)*ddx(2);
x3=xo(3)*ddx(3);
x4=xo(4)*ddx(4);
x5=xo(5)*ddx(5);
x6=xo(6)*ddx(6);
% These are the conditions:
x1<=x2
x3<=x4
x5<=x6
FO=1;
end
As you can see, the restrictions are not respected.
After the function fmincon terminates, it should either satisfy the constraints, or indicated that it cannot find a feasible solution.
That during the optimization iterations constraints are violated, can be advantageous for certain problems, and hence this is supported by some of the algorithms (at least the Active Set algorithm) in fmincon. If you dont like this behavior, use Interior Point. For more details, see fmincon algorithms and violating constraints.
edit The thing that is going wrong is your objective function MyfunSSI. It just returns 1, and hence there is nothing to minimize. In addition, you are just putting the constraint statements, without processing them in any way. The constraints are dealt with by the A and b matrix, so forget about these. But the main thing is: change the function a proper objective function that uses the input ddx to come up with an output value FO.
And by the way, using sqp in this way means that the optimizer will use finite differences and hence evaluate the objective function near feasible and hence possibly infeasible points.
Related
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'm using MATLAB ga function for my optimization problem. In my problem, I have some decision variables which are integer (0 and 1: I specified lower bound, upper bound, and IntCon for it) plus two continues varibales. Otherwise, all integer variables can't be zero at same time, so at least, we need a single "one" variable among integers. How can I implement mentioned constraint in MATLAB?
This is a Mixed-Integer optimization problem and it can be solved using ga in MATLAB. As mentioned in the documentations: ga can solve problems when certain variables are integer-valued. Not all the variables but certain variables. So you should have at least one real variable among the other integers.
Whit IntCon options, you can specify which variables are integer, for instance IntCon=[1 3] means that your first and third variables are integer. To avoid both integer variables to be 0 at the same time, I think you can add some inequality constraints.
For instance look at the following example:
Let's say we want to find the optimum value for the Ackley function with 5 variables (e.g. in 5 dimensions), [x(1)...x(5)] and let's assume that the first and third variables, x(1) and x(3), are integers. We can write the following script:
nVar = 5;
lb = -5*ones(1,nVar); % define the upper bound
ub = 5*ones(1,nVar); % define the lower bound
rng(1,'twister') % for reproducibility
opts = optimoptions('ga','MaxStallGenerations',50,'FunctionTolerance',1e-3,'MaxGenerations',300);
[x,~,~] = ga(#ackleyfcn,nVar,[],[],[],[],lb,ub,[],[1 3],opts);
disp('solution:');disp(x)
On my machine, I get this solution:
solution:
0 -0.000000278963321 0 0.979067345808285 -0.000000280775000
It can be seen that x(1) and x(3) are integers and both 0. Now let's say as you mentioned, they both cannot be 0 at the same time and if one is 0 the other should be 1. Here the boundaries of the Ackley's problem allows the variables to be in the range defined by lower and upper bounds. However, in your case the lower and upper bounds should be defined as [0] and [1] for both integer variables.
Now I want to avoid both variables to be 0, so I can write the following linear inequality constraint:
% x(1) + x(3) >= 1
% x(1) >= 0
% x(3) > 0
These inequalities should be written in form Ax <= b:
A = [-1 0 -1 0 0
-1 0 0 0 0
0 0 -1 0 0];
b = [-1
0
0];
Now if we run the optimization problem again we see the effect of the constraints on the output:
[x,~,~] = ga(#ackleyfcn,nVar,A,b,[],[],lb,ub,[],[1 3],opts);
disp('solution');disp(x)
solution
1.000000000000000 -0.000005031565831 0 -0.000011740569861 0.000008060759466
So I have this piece of code (in MATLAB)
% Define vectorfield
g1=[5,0,0];
g2=[0,3,0];
g3=[0,0,4];
% Define on-off
u=[0;1];
% Define set to make field symmetric
symm=[1;-1];
k=1;
%% Generate possible combinations of vector fields
for a=1:length(u)
for b=1:length(symm)
for c=1:length(u)
for d=1:length(symm)
for e=1:length(u)
for f=1:length(symm)
allvecfields(k,:)=u(a).*symm(b).*g1+u(c).*symm(d).*g2+u(e).*symm(f).*g3;
k=k+1;
end
end
end
end
end
end
realfields=transpose(unique(allvecfields,'rows'));
Each column of realfields is a unique positive, negative or zero combination of the g's. I need some help generalizing this. That is the size of each g can be n, and the number of g's can be m. The code should still return all the unique possible combinations of the g's. I have a feeling that recursion will have to be used but all my attempts have failed so far.
Also allvecfields(k,:) just means kth row, all columns. Even if your answer contains C/C++ or Java code (without any special functions from them) that is fine for me. I will translate it into MATLAB.
I have looked at combvec and the allcomb file but they don't do what I need. For example transpose(unique(combvec(g1,g2,g3,-g1,-g2,-g3)','rows')) returns a 6x63 matrix not the 3x27 that I want. Doing
vals=transpose(unique(combvec(g1,g2,g3)','rows'));
vals=[vals transpose(unique(combvec(-g1,g2,g3)','rows'))];
vals=[vals transpose(unique(combvec(g1,-g2,g3)','rows'))];
vals=[vals transpose(unique(combvec(g1,g2,-g3)','rows'))];
vals=[vals transpose(unique(combvec(g1,-g2,-g3)','rows'))];
vals=[vals transpose(unique(combvec(-g1,g2,-g3)','rows'))];
vals=[vals transpose(unique(combvec(-g1,-g2,g3)','rows'))];
vals=[vals transpose(unique(combvec(-g1,-g2,-g3)','rows'))];
vals=unique(vals','rows');
Does give what I want, but that does not help in generalizing.
EDIT: Fixed error in last line of first code block. The required output for this case is quite large (27 columns) but if we just had g1 and g2 then the output would be:
realfields =
-5 -5 -5 0 0 0 5 5 5
-3 0 3 -3 0 3 -3 0 3
0 0 0 0 0 0 0 0 0
EDIT: Based on a suggestion in the comments, I have been able to rewrite the above code as,
u=[-1,0,1];
k=1;
for a=1:length(u)
for b=1:length(u)
for c=1:length(u)
uMat(k,:)=[u(a) u(b) u(c)];
k=k+1;
end
end
end
g1=[5,0,0];
g2=[0,3,0];
g3=[0,0,4];
gMat=[g1' g2' g3'];
for a=1:size(uMat,1)
allvecfields(k,:)=sum(bsxfun(#times,gMat,uMat(a,:)),2);
end
realfields=transpose(unique(allvecfields,'rows'))
I think this is slightly more elegant but I am still stuck at how to dynamically generate uMat given the number of columns in gMat. I can't believe a function doesn't exist for this. Any help would be appreciated.
Here is the solution
g1=[5,0,0];
g2=[0,3,0];
g3=[0,0,4];
gMat=[g1' g2' g3'];
m=size(gMat',1);
allvecfields = zeros(3^m,m);
for k = 1:3^m
allvecfields(k,:) = double(dec2base(k-1,3,m)-'1');
end
realfields = (allvecfields*gMat')'
Credits to: Roger Stafford for this ingenious solution.
I have a point set P and I construct it's adjacent matrix A by k-nearest neighbor. Each row of A is [...+1...-1...], indicates a pair of neighbor points. The size of A is 48348 x 8058, sprank(A) is 8058. But when I do the following, it gives me a warning: "Warning: Rank deficient, rank = 8055, tol = 8.307912e-10."
a=A*b;
c=A\a;
and norm(c-b) is quite large. It seems something is wrong with the adjacent matrix A, but I can't figure it out. Thanks in advance!
sprank only tells you how many rows/columns of your matrix have non-zero elements, while A\b is reporting the actual rank of the matrix which indicates how many rows of your matrix are linearly independent. For example, for following matrix:
A = [-1 1 0 0;
0 1 -1 0;
1 0 -1 0;
0 0 1 -1]
sprank(A) is 4 but rank(A) is only 3 because you can write the third row as a linear combination of the other rows, specifically A(2,:) - A(1,:).
The issue that you need to address is either in how you're computing A (if you expect that to generate a system of linearly independent equations) or you need to find a way to use A that doesn't require factorizing a rank deficient matrix.
I have two vectors o and c of equal length:
o = [-1 -1 -1 0 0 0 1 1 0 0];
c = [-1 -1 -1 -1 0 1 1 1 0 -1];
o represents opening signals (neg or pos) and c represents closing signals, assuming an opening signal has preceeded it with opposite sign. Only one signal can be active at a time so consecuitive signals must be ignored. In the two vectors above, my first signal would be in o(1) and its corresponding closing signal would be found in c(6). This also means that the opening signals in o(2) and o(3) should be ignored and my next opening signal is found at o(7) with its corresponding close at c(10), consequently leading to a void signal at o(8)
I am trying to find a vectorized solution to identifying a correct sequence or indices of opened/closed signals to produce something along the lines of the following solution example:
o = [-1 0 0 0 0 0 1 0 0 0];
c = [ 0 0 0 0 0 1 0 0 0 -1];
I can obviously solve this by looping through each element in a for loop but since my dataset can be up to millions of elements and I find looping in Matlab can be rather 'expensive', I would greatly appreciate if someone has a solution to my problem that is more matrix-oriented, or through arrayfun or something equivalent that may make the code more efficient?
You can use diff, along with some logical operations to get your answer.
o=[-1,-1,-1,0,0,0,1,1,0,0];
oFinal=abs(diff([0,o])).*o;
oFinal=
-1 0 0 0 0 0 1 0 0 0
The trick is that the output of diff and your original vector o both have a non-zero value at the same index only for the first occurrence of the value in o (i.e., first occurrence in a chain). So, by multiplying it element-wise with o, you get your answer. The abs is to ensure that a sign change doesn't occur due to the output from diff.
The approach is similar for c, and I'll leave that for you to try :)
Usually looping is not more expensive as performing some other operation which just hiddes the loop behind another function (e.g. arrayfun). From your text it just sounds that you just chose the wrong algorithm. Your problem sounds very linear, that is O(n), but you write about loop in loop which means O(n^2). With millions of elements quadratic runtime is not so nice.
The algorithm you want is something like this:
open = 0;
for i=1:length(o)
if (open == 0)
open=o(i)
else
o(i) = 0;
end
if (c(i) ~= -open)
c(i) = 0;
else
open = 0;
end
end
It maybe needs some finetunig, as you didnt describe in detail e.g. what the order of the c and o signals are (e.g. if the same index opens and closes is first the open processed or the closed, my example code assumes open), or whether the order of the signals is always ok, or if there must be some error treatment - but I guess you get the idea of the single loop.