Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

process value of an attribute of an object without changing it in python

Hello I am struggling with a Problem in my Python code.

My problem is the following: I want to copy the value of an attribute of an object.
I create the object agent, then in the function train_agent() I copy the value of the attribute state_vec to the variable last_state_vec. The Problem now is, when I change last_state_vec I automatically also change the attribute state_vec of agent state_vec = np.array(0,0,10,0,0). How can I only copy the value of state_vec, so that it doesn’t get changed in this case. I want state_vec to stay the zero vector.
Here’s some of my code:

class DQN():
  def __init__(self)
  self.state_vec = np.zeros(5)
 
 
agent = DQN()

def train_agent(agent): 
  last_state_vec = agent.state_vec
  last_state_vec[2] = 10
  
  return 0

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Inside train_agent() function you can set last_state_vec to agent.state_vec.copy().

Currently you are initializing the variable last_state_vec with the reference of agent.state_vec.

So By replacing it with agent.state_vec.copy() should do the trick !

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading