generate combinations of binary variables

12 Ansichten (letzte 30 Tage)
JYC214
JYC214 am 24 Jul. 2019
Kommentiert: JYC214 am 25 Jul. 2019
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!!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Jul. 2019
bins = dec2bin(0:(2^10-1), 10) - '0';
bins(sum(bins,2) ~= 2, :) = []; %first constraint, that exactly 2 must be on.
  7 Kommentare
JYC214
JYC214 am 25 Jul. 2019
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! :)
JYC214
JYC214 am 25 Jul. 2019
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...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Quantum Chemistry finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by