how to solve memory

meInvent bbird jobmattcon at gmail.com
Fri Jun 10 11:00:44 EDT 2016


i put final part of code inside one more space
and add constraint deep > 0
but still memory error

and print the deep number to see, it do not run infinity again


V6 is just my superstitution, because V is disable 6 is separation

i run your version directly, it has key = '0' error

M1 = {}
M2 = {}
M3 = {}
M4 = {}
M5 = {}
V6 = {}
M1['00']=0
M1['01']=1
M1['02']=2
M1['10']=1
M1['11']=1
M1['12']=2
M1['20']=2
M1['21']=2
M1['22']=2
M2['00']=0
M2['01']=0
M2['02']=2
M2['10']=0
M2['11']=1
M2['12']=2
M2['20']=2
M2['21']=2
M2['22']=2
M3['00']=0
M3['01']=0
M3['02']=0
M3['10']=0
M3['11']=1
M3['12']=2
M3['20']=0
M3['21']=2
M3['22']=2
M4['00']=0
M4['01']=1
M4['02']=0
M4['10']=1
M4['11']=1
M4['12']=1
M4['20']=0
M4['21']=1
M4['22']=2
M5['00']=0
M5['01']=0
M5['02']=0
M5['10']=0
M5['11']=1
M5['12']=2
M5['20']=0
M5['21']=2
M5['22']=2
V6['00']=2
V6['01']=2
V6['02']=2
V6['10']=1
V6['11']=2
V6['12']=2
V6['20']=0
V6['21']=1
V6['22']=2
MM = {}
MM[0] = M1
MM[1] = M2
MM[2] = M3
MM[3] = M4
MM[4] = M5
MM[5] = V6
m = 3
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
import itertools
deep = 3
final = []
mylist = [MM[i] for i in range(0,7-1)]
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
def DFS(b, deep, maxx, sourceoperators, path):
 initlist = []
 if deep > 0:
  print("deep=", deep)
  for aa,bb in itertools.combinations(sourceoperators, 2):
   print(aa,bb)
   if deep == maxx:
    finalresult = []
    op1xy = [aa[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
    op1yz = [aa[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
    op1xz = [aa[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
    op2xy = [bb[b[i][0:1]+b[i][1:2]] for i in range(len(b))]
    op2yz = [bb[b[i][1:2]+b[i][2:3]] for i in range(len(b))]
    op2xz = [bb[b[i][0:1]+b[i][2:3]] for i in range(len(b))]
    if sum(op1xy) == 54:
      path.append([(deep, aa, "xy")])
    if sum(op1yz) == 54:
      path.append([(deep, aa, "yz")])
    if sum(op1xz) == 54:
      path.append([(deep, aa, "xz")])
    if sum(op2xy) == 54:
      path.append([(deep, bb, "xy")])
    if sum(op2yz) == 54:
      path.append([(deep, bb, "yz")])
    if sum(op2xz) == 54:      
      path.append([(deep, bb, "xz")])
    initlist.append(op1xy)
    initlist.append(op1yz)
    initlist.append(op1xz)
    initlist.append(op2xy)
    initlist.append(op2yz)
    initlist.append(op2xz)
   else:
    level = []
    for j in range(len(b)):
     op1xy = [aa[b[j][i]] for i in range(len(b[j]))]
     op2xy = [bb[b[j][i]] for i in range(len(b[j]))]
     if sum(op1xy) == 54:
      path.append([(deep, aa, "xy")])
     if sum(op2xy) == 54:
      path.append([(deep, bb, "xy")])
     level.append(op1xy)
     level.append(op2xy)
     initlist.append(op1xy)
     initlist.append(op2xy)
  if deep == maxx:
   if deep > 0:
    b = []
    for aaa,bbb in itertools.combinations(initlist, 2):     
     b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
    print("deep=")
    print(deep)
    path = DFS(b, deep-1, maxx, sourceoperators, path)
  else:
   if deep > 0:
    for aaa,bbb in itertools.combinations(initlist, 2):
     b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
    print("deep=")
    print(deep)
    path = DFS(b, deep-1, maxx, sourceoperators, path)
 return path

path = []
mresult = DFS(b, 2, 2, mylist, path)


On Friday, June 10, 2016 at 10:08:44 PM UTC+8, Steven D'Aprano wrote:
> On Fri, 10 Jun 2016 10:58 am, meInvent bbird wrote:
> 
> [snip unreadable code]
> 
> I just ran your code, and it almost crashed my computer. I think it is quite
> rude to post code without an explanation of what the problem is.
> 
> 
> I'm afraid that your code is virtually unreadable to me. It is too verbose,
> the indentation is awful (single space indents is impossible for me to
> track by eye) and it makes too much work out of simple operations. And none
> of the variable names mean anything.
> 
> I've started to simplify and clean the code, and got to the following, which
> I hope you will agree is easier to read and more compact:
> 
> 
> 
> import itertools
> M1 = {'00': 0, '01': 2, '02': 1, '10': 1, '11': 1, 
>       '12': 1, '20': 1, '21': 1, '22': 2}
> M2 = {'00': 0, '01': 1, '02': 1, '10': 1, '11': 1, 
>       '12': 1, '20': 1, '21': 1, '22': 1}
> M3 = {'00': 2, '01': 2, '02': 2, '10': 0, '11': 2, 
>       '12': 1, '20': 0, '21': 1, '22': 2}
> M4 = {'00': 1, '01': 2, '02': 1, '10': 2, '11': 2, 
>       '12': 2, '20': 0, '21': 1, '22': 2}
> M5 = {'00': 0, '01': 1, '02': 1, '10': 0, '11': 2, 
>       '12': 1, '20': 0, '21': 1, '22': 1}
> # Why V instead of M?
> V6 = {'00': 1, '01': 1, '02': 2, '10': 1, '11': 2, 
>       '12': 1, '20': 1, '21': 2, '22': 2}
> 
> MM = {0: M1, 1: M2, 2: M3, 3: M4, 4: M5, 5: V6}
> m = 3
> b = [str(i)+str(j)+str(k) for i in range(m) 
>      for j in range(m) for k in range(m)]
> mylist = [MM[i] for i in range(6)]
> 
> def DFS(b, deep, maxx, sourceoperators, path):
>     initlist = []
>     if deep > 0:
>         print("deep=", deep)
>         for aa,bb in itertools.combinations(sourceoperators, 2):
>             print(aa,bb)
>             if deep == maxx:
>                 finalresult = []
>                 op1xy = [aa[b[i][0:1]] for i in range(len(b))]
>                 op1yz = [aa[b[i][1:2]] for i in range(len(b))]
>                 op1xz = [aa[b[i][0]+b[i][2]] for i in range(len(b))]
>                 op2xy = [bb[b[i][0:1]] for i in range(len(b))]
>                 op2yz = [bb[b[i][1:2]] for i in range(len(b))]
>                 op2xz = [bb[b[i][0]+b[i][2]] for i in range(len(b))]
>                 if sum(op1xy) == 54:
>                     path.append([(deep, aa, "xy")])
>                 if sum(op1yz) == 54:
>                     path.append([(deep, aa, "yz")])
>                 if sum(op1xz) == 54:
>                     path.append([(deep, aa, "xz")])
>                 if sum(op2xy) == 54:
>                     path.append([(deep, bb, "xy")])
>                 if sum(op2yz) == 54:
>                     path.append([(deep, bb, "yz")])
>                 if sum(op2xz) == 54:
>                     path.append([(deep, bb, "xz")])
>                 initlist.extend([op1xy, op1yz, op1xz, op2xy, op2yz, op2xz])
>             else:
>                 level = []
>                 for j in range(len(b)):
>                     op1xy = [aa[b[j][i]] for i in range(len(b[j]))]
>                     op2xy = [bb[b[j][i]] for i in range(len(b[j]))]
>                     if sum(op1xy) == 54:
>                         path.append([(deep, aa, "xy")])
>                     if sum(op2xy) == 54:
>                         path.append([(deep, bb, "xy")])
>                     level.extend([op1xy, op2xy])
>                     initlist.extend([op1xy, op2xy])
>     if deep == maxx:
>         b = []
>     for aaa,bbb in itertools.combinations(initlist, 2):
>         b.append([str(i)+str(j) for i,j in zip(aaa, bbb)])
>     path = DFS(b, deep-1, maxx, sourceoperators, path)
>     return path
> 
> path = []
> mresult = DFS(b, 2, 2, mylist, path)
> 
> 
> 
> Unfortunate, in cleaning up your code, I have changed something, because my
> version and your version do not do the same thing.
> 
> After nearly crashing my computer running your version, I am not going to
> spend the time trying to debug this. If you want our help, I suggest that
> you clean up the code. Currently it is an incomprehensible mess to me. Even
> after the cleanup, I have no idea what this piece of code is supposed to do
> or what it is calculating.
> 
> Can you explain the purpose of the code? What is it calculating? Can you
> show the expected results? What does "DFS" mean? Why do you have M1 - M5
> but then a mysterious V6?
> 
> I can see you are calculating *something* to do with permutations of 0 1 2,
> but I don't understand what.
> 
> I think I can see one problem: DFS is a recursive function, but it never
> ends the recursion. It ALWAYS calls DFS(... deep-1, ...) even if deep is
> zero. So I expect that you will have an infinite loop that just recurses
> over and over, until you run out of memory or hit the recursion limit.
> 
> 
> 
> -- 
> Steven




More information about the Python-list mailing list