0

So here is my next problem. I am trying to to loop through and find out how many of the entries in State_Space have a 1 as their 25th entry yet it keeps telling me that the answer is 0. Here is the code.

import random
import numpy

M=numpy.zeros((52),dtype=int)


z=0
State_Space=[]

for i in range(1,100500):
    x=random.randint(1,50)


    T=M.copy()
    if T[x]==1:
        T[x]=0
    if T[x]==0:
        T[x]=1


    if not any(numpy.array_equal(T, X) for X in State_Space):
        if T[x+1]==0 and T[x-1]==0:
            State_Space.append(T)
            if T[25]==1:
                z=z+1
            M=T


    else:
         if T[x+1]==0 and T[x-1]==0:
            M=T


print z
print len(State_Space)

Output:

0, 22

EhBabay
  • 149
  • 8
  • I reread your first question and you said `State_Space` is supposed to be uniformly distributed among all NxN (or in this case 1xN) matrices. Why then do you need `M=T` at the end of the loop. – kalhartt Nov 18 '14 at 02:21
  • This code prints non-zero z for me, but pretty rarely. I suspect that at some point `T[24]` or `T[26]` is set, which means `T[25]` will not be set since it would have a non-zero neighbor. It has to wait for both neighbors to first get unset before `T[25]` can be set, which can be rather unlikely. – kalhartt Nov 18 '14 at 02:24
  • @kalhartt ok sorry I am supposed to be able to code this with a 52X52 matrix and 1X52 row vector. So lets just just talk about the row vector case. We need M=T because we don't want to remember the Markov Chain movement if it isn't in the State Space of vector with no neighboring 1's. – EhBabay Nov 18 '14 at 02:25
  • I see, in any case I believe your code is working as expected. Sometimes I obtain a very high count (>80% of the State_Space), the rest I have a very low or zero count. I think this is just a property of your system. – kalhartt Nov 18 '14 at 02:36
  • you can look at the count for each index by adding this to the end of the script: `for i in range(52): print i, sum(X[i] for X in State_Space)` For me, maybe 1/2~2/3 of the indicies are 0 and the rest are fairly unevenly distributed – kalhartt Nov 18 '14 at 02:38
  • Also, are you allowed to unset a location if it has neighbors set? Should the if statement checks be `T[x]==0 or (T[x+1]==0 and T[x-1]==0)` – kalhartt Nov 18 '14 at 02:49
  • I think you have answered all of our questions. – EhBabay Nov 18 '14 at 02:54

0 Answers0