Why does cplex not assign the value to the variables correctly? - variable-assignment

I have this constraint:
forall (j in J)
forall (i in I: macc [i][j] == 0)
{
V10b: sum(m in M) y[i][j][m] == 0;
}
where macc [i][j] is a matrix of values ranging from 0 to 20.
I have macc [7][2] == 0 so in output I should have y[7][2][m] == 0 forall m. But in output cplex assigns the value 1 to a random m. For example y[7][2][m==2] == 1. Assign well up to a certain point, then start making mistakes. What could be the mistake? Thanks.

By default OPL CPLEX will relax some constraints if the model is not feasible. If you do not want this to happen, in the settings editor turn those options off

Related

How to add a constraint to my optimization?

I am working on formulating an optimization problem where I have a 2-D matrix A.
A= [0 f1 0 f2]
[f3 f3 0 0]
.........
And I have another 2-D matrix B that I should fill. B has the same size of A. I need b_ij (element of B) to be zero if a_ij=0 (element of A) and I need b_ij to be greater than zero and less than or equal to a_ij if a_ij is not zero.
How can I represent this in my formulation? I have added this constraint/condition:
b_ij<=a_ij
But this does not satisfy the condition that states that b_ij is not equal zero when a_ij is not equal zero. Any help?
If all elements are positive, keep the smallest element of each matrix by doing an element by element comparison :
B2 = min(A,B)
Alternatively, create a logical matrix indicating if a condition is answered and multiply element by element with the matrix B , only elements who satisfy the condition remain, others are set to zero:
B = B.*(A~=0)
Then keep elements of B that are smaller or equal to elements of A, and replace them by the value of A otherwise.
B = B.*(B<=A) + A.*(B>A) )
This option lets you generalize your constraint.
You indicate needing elements of b_ij to be greater than zero if elements of a_ij are greater than zero. An option is to use the function max to ensure that all elements of B are positive.
B = max(1e-2,B); % exact value is yours to set.
This step is up to you and depend on your problem.
You want to implement the implication
a = 0 => b = 0
a <> 0 => 0 < b <= a
If a is (constant) data this is trivial. If a is a variable then things are not so easy.
You implemented part of the implications as
b <= a
This implies a is non-negative: a>=0. It also implies b is non-negative. The remaining implication a>0 => b>0 can now be implemented as
a <= δ * 1000
b >= δ / 1000
δ in {0,1}
Many MIP solvers support indicator constraints. That would allow you to say:
δ = 0 -> a = 0
δ = 1 -> b >= 0.001
δ in {0,1}

What does y==x mean in MATLAB?

I came across some MATLAB code online and it was running just fine, but I couldn't understand the meaning of (y == x) where y is a column matrix and x is an integer.
someFunction(y == x);
Is it some kind of comparing or setting some value of y?
The instruction
y == x
checks which values in the array y (if any) are equals to the scalar x and returns a logical array of the size of y in which 1 is set in the location where the value of the element of y is equal to the value of x and 0 in the other case.
It ha to be assumed tha also the array y is of integer type, otherwise the comparison does not have sense.
Therefore, the function someFunction seems accepting as input a logical array.
As an example, with
y = [10 2 10 7 1 3 6 10 10 2]
and
x=10
the code
(y == x)
returns the logical array:
1 0 1 0 0 0 0 1 1 0
This will be the input someFunction function.
Hope this helps,
QWapla'

Condition for columns based on same index as vector

I'm trying to get a logical matrix as a result of a condition that is specific for each column M(:,i) of the original matrix, based on the value of the same index i in vector N, that is, N(i).
I have looked this up online, but can't find anything quite like it. There must be a simple and clean way of doing this.
M =
3 -1 100 8
200 2 300 4
-10 0 0 400
N =
4 0 90 7
and my desired solution is, for each column of M(:,i), the values less than N(i):
1 1 0 0
0 0 0 1
1 0 1 0
It's a standard use-case for bsxfun:
O = bsxfun(#lt, M, N)
Here #lt is calling the "less than" function, i.e. it is the function handle to the < operator. bsxfun will then "expand" N along its singleton dimension by applying the function #lt to each row of M and the whole of N.
Note that you can easily achieve the same thing using a for-loop:
O = zeros(size(M));
for row = 1:size(M,1)
O(row,:) = M(row,:) < N;
end
Or by using repmat:
O = M < repmat(N, size(M,1), 1);
but in MATLAB the bsxfun is usually the most efficient.
Possible two-line solution using arrayfun to apply the comparison to each column and index pair:
T = arrayfun(#(jj)M(:,jj) < N(jj), 1:numel(N), 'UniformOutput', false);
result = cat(2,T{:});
Edit: Of course, the bsxfun solution is much more efficient.

Replace all values of matrix where another matrix is false (0)

I have matrix A and B. I want to set all values in A to 0, where B is false (has 0 as value). How can I do this the best way?
A and B have the same size.
Use Boolean operator, assuming A and B are of same sizes, but they don't have to be of same datatype
A(~B) = 0
More info here.
find all indices of B with 0 and set those indices in A as 0
>>A(B == 0) = 0
To see what is exactly happening you can use this as well
size=size(im);
for x=1:size(1)
for y=1:size(2)
if B(x,y)==0;
A(x,y)=0;
end
end
end

Solving for variables in an over-parameterised system

I am trying to write a Matlab program that accepts variables for a system from the user, but there are more variables than system parameters. To be specific, six variables in three equations:
w - d - M = 0
l - d - T = 0
N - T + M = 0
This could be represented in matrix form as A*x=0 where
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
x = [w l N d T M]';
I would like to be able to solve this system given a known subset of the variables. For example, if the user gives d, T, M, then the system is trivially solved for the other three variables. If the user supplies w, N, M, then it becomes a solvable 3-DOF system. And so on. (If the user over- or under-specifies the system then an error may of course result.)
Given any one of these combinations it's simple to (a priori) use matrix algebra to calculate the unknown quantities. But I don't know how to solve the general case, aside from using the symbolic toolbox (which I prefer not to do for compatibility reasons).
When I started with this approach I thought this step would be easy, but my linear algebra is rusty; am I missing something simple?
First, let x be a vector with NaN for the unknown values. This allows you to use ISNAN to find the indeces of the unknowns. If you calculate A*x for only the user-specified terms, that gives you a column of constants b. Take those constants to the right-hand side of the equation, and you have an equation of the form A*x = -b.
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
idx = ~isnan(x);
b = A(:,idx)*x(idx); % user provided constants
z = A(:,~idx)\(-b); % solution of Ax = -b
x(~idx) = z;
With input x = [NaN NaN NaN 1 1 1]', for instance, you get the result [2 2 0 1 1 1]'. This uses MLDIVIDE, I'm not well versed enough in linear algebra to know whether PINV or something else would be better.
Given the linear system
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
A*x = 0
Where the elements of x are identified as:
x = [w l N d T M]';
Now, suppose that {d,T,M} have known, fixed values. What we need are the indices of these elements in x. We've chosen the 4th, 5th and 6th elements of x to be knowns.
known_idx = [4 5 6];
unknown_idx = setdiff(1:6,known_idx);
Now, let me pick some arbitrary numbers for those known variables.
xknown = [1; -3; 7.5];
We will partition A into two submatrices, corresponding to the known and unknown variables.
Aknown = A(:,known_idx);
Aunknown = A(:,unknown_idx);
Now, move the known values to the right hand side of the equality, and solve. See that Aknown is a 3x3 matrix, so the problem is (hopefully) well posed.
xunknown = Aunknown\(-Aknown*xknown)
xunknown =
-8.5
2
10.5
Combine it all into the final solution.
x = zeros(6,1);
x(known_idx) = xknown;
x(unknown_idx) = xunknown;
x =
-8.5
2
10.5
1
-3
7.5
Note that I've expanded this all out into a few lines to show what is happening more clearly. But I could have done it all in just a line or two of code had I wanted to be parsimonious.
Finally, see that had I chosen some other sets of numbers to be the knowns, such as {l,d,T}, then the resulting system would be singular. So you must watch for that event. A test on the rank of Aunknown might be useful to weed out the problems. Or you might choose to employ pinv to build the solution.
The system of equations is fixed? What if you store the variables present in your three equations in a list per equation:
(w, d, M)
(l, d, T)
(N, T, M)
Then you get the user input and you can calculate the number of variables given in each equation:
User input: w, N, M
Given variables:
(w, d, M) -> 2
(l, d, T) -> 0
(N, T, M) -> 1
This would trivially give you d from the first equation. Therefore you end up with two equations containing two variables and you know you the equation system you have to solve.
It's basically your own simple symbolic solver for a single system of equations.