Binary constraint in non linear optimization in R (NLOPTR - COBYLA) - nonlinear-optimization

I am trying to solve a non linear optimization problem using NLOPTR library in R. Within NLOPTR, I am using COBYLA algorithm. Some of my constraints are binary and I am not able to specify it in NLOPTR.
Consider two values x, y. My requirement is as follows,
x can be 0 or 1.
y can be 0 or 1
x + y = 1 #so that one of x/y is 0 and the other one is 1.
If any of you has specified similar problem in Non-Linear Optimization, please help. If there is a better library to use, kindly suggest it.
Thanks / Sandeep

Related

Matlab: Binary Linear Programming

I am trying to solve some equations on Matlab using Binary Integer Programming.
I have 3 sets of equations:
Ma.X=1
Mp.X<=1
Mr.X<=m*
Where, Ma is a known matrix with size 5*12
X is unknown set with size 12*1
Also Mp is known matrix with size 5*12
and Mr is a known matrix with size 4*12.
1 in the equations is an unity matrix with size 5*1 in both sets(1&2)
m* is a given known matrix with size 4*1
I'm trying to use command bintprog but how to put 1 equality and 2 inequalities
to get values of X. Also I don't have Function f to insert, I just have set of equations. Given that X unknown values with values 1 or 0.
I tried this command bintprog([],Ma,One51,Mp,One51)
but it gives me The problem is infeasible. with zeros answer matrix.
Please help me to solve this on Matlab
The correct syntax for bintprog is X = bintprog(f,A,b,Aeq,beq).
If you don't have f (which means you just want any feasible point), you can set it to []. However, for the others, your syntax is slightly wrong.
I am assuming that the + in your constraints is actually a * since otherwise, the matrix algebra doesn't quite make sense.
Try this:
X = bintprog([],[Mp;Mr],[ones(5,1);mstar],Ma,ones(5,1))
If even then it tells you that the problem is infeasible; it might as well be true that there are no Xs that can satisfy all your constraints.

How can I create a vector from data?

I used matlab to solve linear programming with the common [x, fval] = linprog(f,a,b) and I got the solution. My problem is I want to find the binary vector for the variables(x), for example, the values of (x) after I solve the linear problem were 13,0, 8,0,5,8,0,4,0,0 and I would like to obtain the vector(h) 1,0,1,0,1,1,0,1,0,0 which is represents the binary vector for x. I mean when the value of x greater than 0 we put in h 1 and when the value of x less than or equal 0 put 0 in the vector h?
Thanks.
What about
binvect=x>0;
In Matlab it is as easy as doing that, he will do give you a vector of all the x that fill the condition (>0)

Finding "all solutions" of x in A.x = b in the finite field domain

Matlab provides a way to find a particular solution to Ax=b in GF(2^m). Here is the link http://www.mathworks.in/help/comm/ref/gflineq.html But it gives only one solution. How can I find the rest of the solutions?
Eg: A=[1 0 2 0 0 1] in GF(4), b=[0] in GF(4). x=A\b gives [0 0 0 0 0 0]' as the solution. I also know [0 1 2 3 2 3]' to be one other solution. But I cannot get any other solution except all zero.How to find all solutions in Matlab?
In General,
The solutioin to the matrix equation Ax=b need not exist and even if it does, need not be unique. In your case, you know that multiple solutions exist. In such a situation, you have a "particular solution" which is basically, any x that solves Ax=b and now, to get multiple solutions, you can keep adding vectors from the Nullspace of A.
Proof:
Let x be a particular solution and y be in the Nullspace of A (any vector in Nullspace is OK). We know Ax=b. We also know Ay=0. Add them up, A(x+y)=b.
TL;DR Solution:
Find the Nullspace of the Matrix in GF and add any vector of the Nullspace to the particular solution to generate more solutions.
I have not used it but there seems to be a code on MATLAB Central which finds the Nullspace of a Matrix in GF.

Matlab left - division in vectors?

x=[1;2;3]
x =
1
2
3
y=[4;5;6]
y =
4
5
6
x\y
ans =
2.2857
How did Matlab find that result ? (I searched many forums but I did not understand what they told.I would like to know the algorithm which gave this result.)
From MATLAB documentation of \:
If A is an M-by-N matrix with M < or > N and B is a column vector with M components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations A*X = B.
Here your system is not under/over-determined. Since both have 3 rows. So you can visualize your equation as:
xM=y
M=inv(x)*y
Now, since your matrix is not square, it will calculate the pseudo-inverse using SVD. Therefore,
M=pinv(x)*y;
You will get value of M as 2.2857.
Another explanation can be: It will give you the solution of xM=y in the sense of least squares. You can verify this as follows:
M=lsqr(x,y)
This will give you the value of M = 2.2857.
You can always do help \ in MATLAB command window to get more information.
You are encouraged to check more details about the least squares and pseudo-inverse.
This documentation should explain it
http://www.mathworks.com/help/matlab/ref/mrdivide.html
Here is a link to the algorithm
http://www.maths.lth.se/na/courses/NUM115/NUM115-11/backslash.html
You can see the source inside matlab much more easily though. (I don't have it locally so I can't check but the source of a lot of matlab functions is available inside matlab)

RBF and pseudoinverse XOR

The problem i am trying to understand is easy but i cant seem to get the correct result in matlab. The actual problem is that i want to get the weight vectors of a 2 hidden layer input RBF using just the plain distance as a function, i.e. no Baysian or Gaussian function as my φ. I will use the function with 2 centres let's say 0,0 and 1,1. So this will give me a Matrix φ of:
[0 sqrt(2) ; 1 1; 1 1; sqrt(2) 0] *[w1; w2] = [0;1;1;0] As defined my the XOR function.
When i apply the pseudoinverse of the Φ in matlab * [0;1;1;0] though i get [0.33 ; 0.33] which is not the correct value which would allow me to get the correct output values [0;1;1;0].
i.e. .33 * sqrt(2) != 0 .
Can someone explain to me why this is the case?
I'll take a swag at this. The matrix, I'll call A, A = [0 sqrt(2) ; 1 1; 1 1; sqrt(2) 0] has full column rank, but not full row rank, i.e. rank(A) = 2. Then you essentially solve the system Ax = b, where x is your weighting vector. You could also just do x = A\b in Matlab, which is supposedly a much more accurate answer. I get the same answer as you. This is a very rough explanation, when your system can not be solved for a certain solution vector, it means that there exists no such vector x that can be solved for Ax = b. What Matlab does is try to estimate the answer as close as possible. I'm guessing you used pinv, if you look at the Matlab help it says:
If A has more rows than columns and is not of full rank, then the overdetermined least squares problem
minimize norm(A*x-b)
does not have a unique solution. Two of the infinitely many solutions are
x = pinv(A)*b
and
y = A\b
So, this appears to be your problem. I would recommend looking at your φ matrix if possible to come up with a more robust system. Hope this is useful.