1

I am trying to create a script that can allows me to output all the different combinations possible for different project optimization choices. In short, there are 6 projets (A B C D E F) that each have 2, 3 or 6 possible choices of optimization, that are mutually exclusive (you can't choose F4 and F5 at the same time for example).

    import numpy as np

A = range(1, 3)
B = range(1, 3) 
C = range(1, 7) 
D= range(1,3) 
E=range(1,3) 
F=range(1,4) 

length = len(A) + len(B) + len(C) + len(D) + len(E) + len(F) 
nb_projet = len(A) * len(B) * len(C) * len(D) * len(E) * len(F)

result = np.zeros((length, nb_projet))


for k in range(len(A)):
    for i in range(len(A)):
        for j in range(nb_projet):
            result[i, j] = (i+j % len(A)) == k

for k in range(len(B)):
    for i in range(len(B)):
        for j in range(nb_projet):
            result[i + len(A), j] = (i+j % len(B)) == k

for k in range(len(C)):
    for i in range(len(C)):
        for j in range(nb_projet):
            result[i + len(A)+len(B), j] = (i+j % len(C)) == k

for k in range(len(D)):
    for i in range(len(D)):
        for j in range(nb_projet):
            result[i + len(A)+len(B)+len (C), j] = (i+j % len(D)) == k


for k in range(len(E)):
    for i in range(len(E)):
        for j in range(nb_projet):
            result[i + len(A)+len(B)+len (C)+len(D), j] = (i+j % len(E)) == k

for k in range(len(F)):
    for i in range(len(F)):
        for j in range(nb_projet):
            result[i + len(A)+len(B)+len (C)+len(D)+len(E), j] = (i+j % len(F)) == 0


print (result.T)

np.savetxt("ResultsS2.txt", result, delimiter=" ")

Basically the code is supposed to add a 1 if the optimization is chosen. At the moment it only generates 6 differents scenarios and not the 250+ that are possible.

Does anyone have an idea on how to fix this ?

Thank yu a lot !

Doule
  • 57
  • 1
  • 8

2 Answers2

2

This is just a bunch of concatenated one-hot arrays, so utilizing the second answer here and meshgrid to create the full factorial, you can just do something like this:

projects = [2,2,6,2,2,3] #[A.size, B.size, C.size . . .]
m = np.meshgrid(*[np.arange(i) for i in projects])
oneHots = [np.eye(projects[i])[m[i].flat] for i in range(len(projects))]
out = np.hstack(oneHots).T

out.shape
>(17, 288)
Daniel F
  • 13,620
  • 2
  • 29
  • 55
1

You can use something like this. Not the smartest, but if the arrays are not too long, it would work perfectly.

import numpy as np

A = range(0, 2)
B = range(0, 2) 
C = range(0, 6)

length = len(A) + len(B) + len(C)
nb_projet = len(A) * len(B) * len(C)
result = np.zeros((length, nb_projet))

selectedList = []
count = 0
for i in A:
    for j in B:
        for k in C:
            result[(i,count)]= 1
            result[(len(A)+j,count)]=1
            result[(len(A)+len(B)+k, count)] = 1
            count+=1

Note, that I had changed the ranges to fit better.

Manish Goel
  • 843
  • 1
  • 8
  • 21
  • I think this doesn't create a matrix with 0 and 1 or does it ? The output of my script is a matrix with 0 and 1 in it, where 0 marks all the optimizations not chosen. – Doule Jul 13 '17 at 09:19
  • Have added the matrix based solution. – Manish Goel Jul 13 '17 at 09:48