0

I am trying to make a q table. But as I am creating my q table I notice that all of my values are the same even as I changed the random.seed and used multiple variants of randint, such as np.random.uniform and random.uniform. My code is below

import os, time, random start = time.time() class space_info: SIZE = 10 class blob(): value = None q_table = {} def init(self): SIZE = space_info.SIZE for i in range(-SIZE+1, SIZE): for ii in range(-SIZE+1, SIZE): for iii in range(-SIZE+1, SIZE): for iiii in range(-SIZE+1, SIZE): random.seed(time.time()*1000000) self.q_table[((i, ii), (iii, iiii))] = [random.uniform(-5, 0) for i in range(4)] os.system('clear') x = blob() x.init() z = blob() z.init() if (z.q_table[((-9, -2), (3, 9))]) == (x.q_table[((-9, -2), (3, 9))]): print("WE FAILED ):") print(z.q_table[((-9, -2), (3, 9))]) print(x.q_table[((-9, -2), (3, 9))]) else: print("YES") 

How can I get new numbers for my q_table?

3
  • 1
    Do not reseed the generator for every call to the PRNG. Seed once and only once per program, unless you really really really understand how PRNGs work and are trying to implement so-called "variance reduction" through correlation induction strategies. Commented Jan 7, 2022 at 22:32
  • There is no reason for you to be calling random.seed() at all in this code. If you must use it, calling it once at the top would be sufficient. Commented Jan 7, 2022 at 22:35
  • Also note that random and np.random are completely separate implementations. Using the seed() function from one does nothing to the other. In other words, you must use the seed() function corresponding to the generator you're using. Commented Jan 7, 2022 at 22:35

1 Answer 1

1

The variable q_table belongs to class blob not instances of class blob. You can test this by id(x.q_table) == id(z.q_table). If this is true it means that both are in the same place in memory.

class blob(): def __init__(self): self.value = None self.q_table = {} # in case you don't want to call init explicitly # self.init() def init(self): ... 

I have left the init function as it was.

The __init__ function is what the class should now use to create a new instance of blob. Note that q_table is now self.q_table indicating it belongs to the object referred to as self, i.e., the instance of class blob.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.