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
>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 !