generate combinations of binary variables

Hello,
I have a list of 10 materials (eg. A, B, C, ..., J), and I have to choose 2 materials from the set to form a mixture.
Their presence in the mixture are represented by the binary variable (first material in the mixture) and (second material in the mixture).
For example, a mixture would consist of A as the first material, and C as the second material. The result I would like to generate would be in such form:
The binary variable for A in 'first material' will take the value of 1, while the remainder in 'first material' will be 0.
y(m1, A) = 1
y(m1, B) = 0
...
y(m1, J) = 0
The binary variable for C in 'second material' will take the value of 1, while the remainder in 'second material' will be 0.
y(m2, A) = 0
y(m2, B) = 0
y(m2, C) = 1
...
y(m2, J) = 0
There is only one condition (which sometimes I will have to impose on certain material(s) and sometimes I don't):
For example in this particular case study, binary variable for material E and F have to be zero in both first and second material, i.e. y(m1, E) = y(m1, F) = y(m2, E) = y(m2, F) = 0.
I have to generate all possible combinations - i.e. 10C2 = 45 combinations (when without restriction) or will become 8C2 =28 combinations (when WITH restrictions, using the example above).
How do I do this...?
Thank you!!

 Risposta accettata

bins = dec2bin(0:(2^10-1), 10) - '0';
bins(sum(bins,2) ~= 2, :) = []; %first constraint, that exactly 2 must be on.

7 Commenti

Hi Walter, thanks for your swift reply and it works well!
I have 2 questions:
  1. Is it possible to formulate this problem in a FOR loop?
  2. How do I transform the results generated from the code above into the form I wanted in my question, ie. y(m1, A) = 1, y(m1, B) = 0, ..., y(m1, J) = 0 and y(m2, A) = 0, y(m2, B) = 0, y(m2, C) = 1, ..., y(m2, J) = 0. Because the combinations in this form will be as input in a downstreamoptimisation algorithm...
Thank you!
m = 0;
for k = 0 : 2^10-1
bits = dec2bin(k, 10) - '0';
if sum(bits) ~= 2; continue; end %first constraint
m = m + 1;
y(m, :) = bits;
end
And without the loop,
y = dec2bin(0:(2^10-1), 10) - '0';
y(sum(bins,2) ~= 2, :) = []; %first constraint, that exactly 2 must be on.
Sorry if I haven't been clear in expressing the way the result/combinations should be presented/returned from the code.
I would like my code to return the results in such a way, for example, the first set of results:
when y(m1, A) = 1 (with the others in m1 equal to 0) and y(m2, C) = 1 (with the others in m2 equal to 0):
{ y(m1, A) = 1
y(m1, B) = 0
...
y(m1, J) = 0
y(m2, A) = 0
y(m2, B) = 0
y(m2, C) = 1
...
y(m2, J) = 0 }
The second set of results:
when y(m1, B) = 1 (with the others in m1 equal to 0) and y(m2, J) = 1 (with the others in m2 equal to 0):
{ y(m1, A) = 0
y(m1, B) = 1
...
y(m1, J) = 0
y(m2, A) = 0
y(m2, B) = 0
y(m2, C) = 0
...
y(m2, J) = 1 }
and similarly for all other sets of results.
Is this possible to achieve? Because each expression in each set of result is an initial value for the subsequent optimisation algorithm...
I do not know what m1 and m2 are in this context?
Are you looking to return a set of symbolic equations? Are you looking to execute those statements?
My bad! Let me try to explain again.
In my optimisation program, the variable y's general form is y(nomaterial, material). 'nomaterial' is an array of [m1, m2], i.e. nomaterial := [m1, m2]. 'material' is the set of materials given above, material := [A, B, C, ..., J].
If A is the material chosen to be the first material of the mixture, i.e. m1, then y(m1, A) = 1 and other y variables - y(m1, B), y(m1, C), ..., y(m1, J) - have to be zero.
Then, if B is chosen to be the second material for the mixture, i.e. m2, then y(m2, B) = 1 and other y variables - y(m2, A), y(m2, C), ..., y(m2, J) - have to be zero.
The constraint is that the 'material' array is fixed for all mixture design problem, however in some case studies, some materials cannot be used, e.g. D and E cannot take values of 1, must be assigned 0 in both m1 and m2.
So in order to initialise the optimisation algorithm, these binary variables will have to assigned (':=' instead of '=') with values 1 or 0. I am trying to generate all possible sets of combinations so that the optimisation algorithm could run simultaneously with all different sets of initial values.
Apologies to another mixtake above, it should be a permutation problem instead of combination. So, in the absence of constraints, the number of all possible sets should be .
I am wondering if each set can be represented in the form of matrix (either vertical or horizontal axis can be m1 or m2),
matrix.PNG
or if it could be in the form I wrote previously, i.e.
set 1:
{ y(m1, A) := 1
y(m1, B) := 0
...
y(m1, J) := 0
y(m2, A) := 0
y(m2, B) := 0
y(m2, C) := 1
...
y(m2, J) := 0 }
set 2:
{ y(m1, A) := 0
y(m1, B) := 1
...
y(m1, J) := 0
y(m2, A) := 0
y(m2, B) := 0
y(m2, C) := 0
...
y(m2, J) := 1 }
...
hope I have made my problem statement clear! :)
Sorry Walter, I just broke down how the optimisation algorithm works, and here's a correction for what I said earier...
Instead of generating and storing all 90 sets of data and have the optimiser runs all of them simultaneously, it should generate a set of data or one matrix from a loop and that will be inputted into the optimisation algorithm to run and produce results. After that the loop runs again and gives the second set of data/matrix as input to the optimisation algorithm to run and generate another set of result. And this goes on for all possible combinations.
Sorry for the mess...

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Quantum Chemistry in Centro assistenza e File Exchange

Prodotti

Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by