Since fixing the issue with the finesse model, we have been struggling to train the agent to learn a stable policy. The main problem stemmed from requiring at least 50k agent interactions (a few days) with the environment to assess the behavior before changing the architecture/hyper-parameters, etc.
I modified the baseline RL algorithm to improve the sample efficiency, which has led to improved tracking and disturbance rejection for the RL-Agent. The trick involved multiple reuses of previously collected experience samples multiple times (M=10) to calculate the gradient and update the actor and critic networks. This led to better exploitation of the off-policy continuous control agent. This scheme has also reduced the sensitivity to the exact choice of hyper-parameters.
Now, the agent executes the alignment task for several iterations but then forgets what to do after a while and goes rogue. This issue usually goes away with longer training. The way I am dealing with this right now is via curriculum learning, where I progressinvely increase the task complexity during training and improve the agent's robustness to unexpected situations. But I will talk to a few RL experts in the coming days to see if there are smarter ways to deal with this catastrophic forgetting.
I will now test the behavior on the real system and see the actual performance.
-----------------------------
PLOTS:
-----------------------------
The first plot shows the recovered rewards during training. Unlike supervised learning, the non-monotonic behavior in learning is a characteristic of RL that makes it harder to train and decide when to stop training. Remedy is to log and look at the involved loss functions.
Second plot shows how the agent keep the two beams aligned and maximize the power at the CCD. You can see how the agent performs its task for a while and then goes unstable.
Third plot shows the actuator signals. We penalize large variations in actions, hence the don't act like a bang-bang controller.
Fourth plot shows the cumutive reward collected by the agent.
Last plot shows the individual components used to construct the reward metric (= linear-quadratic-regualtor term + penalty terms boundary constrain violations + discrete encouragements for close to optimal behavior)
-----------------------------
################################################
The new pseudo-code is given below.
##############-------Pseudo Code-------###############
Initialize Q-networks (Q1, Q2)
Intialize target Q-networks (Q1_target, Q2_target)
Initialize Policy network
Intialize temperature parameter (alpha)
Initialize Optimizer for Q-networks, policy, alpha
For each training episode:
Initialize state s
While not done:
Choose action a from policy net, a = policy(s)
Take action in the environment:
Observe: next_state s'
reward r
isDone flag
Store transition (s,a,r,s',isDone) in the experience replay buffer
Sample a batch of transitions from the replay buffer
(s_batch, a_batch, r_batch, s_next_batch, isDone_batch)
for i in range(sample_multiple_reuse_ratio):
# Update Q-networks
Q_target = r_batch + gamma * min [ Q1_target(s_next_batch, policy(s_next_batch), Q2_target(s_next_batch, policy(s_next_batch)]
- alpha*log_probability(a_batch|s_batch)
# calculate Q-network loss functions
Loss_Q = MSE(Q1(s_batch,a_batch), Q_target) + MSE(Q2(s_batch,a_batch), Q_target)
update Q1 & Q2 using Loss_Q
# update Policy network and Temperature
Q_current = min [ Q1(s_batch,policy(s_batch)) , Q2(s_batch,policy(s_batch))
# Loss-Policy
-[ alpha * log_probability(a_batch|s_batch) - Q_current]
# Loss_alpha
-alpha * [ log_prbability(a_batch|s_batch) + target_entropy ]
Update Policy and Alpha using Loss_Policy and Loss_Alpha
# Update target Q-network (Soft update) using Polyak Averaging
Q1_target = tau * Q1 + (1-tau) * Q1_target
Q2_target = tau * Q2 + (1-tau) * Q2_target
# Update States for the next iteration
s = s'
################################################