Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP3702 Artiicial Intelligence (Semester 2, 2023)

Assignment 3:  Reinforcement Learning

Key information:

. Due:  3pm,  Friday 27 October

.  This assignment assesses your skills in developing and understanding algorithms for solving Reinforce- ment Learning Problems.

.  Assignment 3 contributes 20% to your inal grade.

.  This assignment consists of two parts:  (1) programming and (2) a report.

.  This is an individual assignment.

. Your program (code) must be zipped and submitted to Blackboard.  It will not be explicitly graded, and instead will form the basis for analysis in the report.

. Your report should be in .pdf format and named according to the format a3-COMP3702-[SID].pdf, where SID is your student ID. Each question should start on a separate page and be clearly indicated. The report is to be submitted via Gradescope.  Reports will be graded by the teaching team.

Reinforcement Learning

In this assignment, you will implement Deep Reinforcement Learning algorithms and analyse their parameters and performance. This assignment will test your skills in training reinforcement learning algorithms for practical problems and understanding of key algorithm features and parameters.

Gymnasium API

This assignment will make use of the OpenAI Gym/Gymnasium1 , which is a standard API for reinforcement learning with a diverse collection of reference environments.

This assignment will  investigate the  Cart  Pole  and  Lunar  Lander  environments  shown  in  Figure 1,  which are within the classic-control and box2d set of environments respectively.  If using pip, you can install Gymnasium and the dependencies for these set of environments via the following:

pip  install  gymnasium

pip  install  gymnasium[classic-control]

pip  install  gymnasium[box2d]

Figure 1:  Cart Pole and Lunar Lander environments

To get an understanding of these environments, you can visualise them using the human render mode using code as below:

import  gymnasium  as  gym

env  =  gym .make("CartPole-v1",  render_mode='human')

observation,  info  =  env .reset(seed=42)

for  _  in  range(500):

action  =  env .action_space .sample()

observation,  reward,  terminated,  truncated,  info  =  env .step(action)

if  terminated  or  truncated:

observation,  info  =  env .reset()

env .close()

PyTorch

To train neural networks, we will be using the PyTorch machine learning framework.  To download PyTorch, follow the instructions here: https://pytorch.org/get-started/locally/.

If you have a GPU, select PyTorch with GPU support as it will be much faster, and download the appropriate drivers (ROCm for AMD GPUs and CUDA for Nvidia GPUs).  Make sure you only install one - either the GPU or the CPU version!

Task

Your task is to run the Deep Q-Network (DQN) algorithm and variants including Duelling DQN and Double DQN, and to write a report investigating the algorithms’ performance and hyper-parameters as detailed in the report section.

For background, we recommend that you read through the following tutorials. You can make use of the code in your solutions with attribution:

.  Oicial DQN PyTorch Tutorial: https://pytorch.org/tutorials/intermediate/reinforcement_ q_learning.html

.  Tutorial sample code: https://github.com/comp3702/tutorial11

When training reinforcement learning algorithms, typically we assess the solution quality using the 100-step moving average episode reward (i.e., R100)  received by your learning agent.  At time step t, the 100-step moving reward is the average episode reward earned by your learning agent in the episodes [t - 100; t].  If the Q-values imply a poor quality policy, this value will be low.  If the Q-values correspond to a high-value policy, the 100-step moving average reward will be higher.  We use a moving average because rewards may only be received occasionally and the episode reward is afected by sources of randomness including the exploration strategy. You should write a function that plots the R100 vs episodes for analysis in the report.

The report

The report tests your understanding of Reinforcement Learning algorithms and the methods used in the code. The code at https://github.com/comp3702/tutorial11 provides a good foundation for the assignment.

Question 1. Q-learning vs Value Iteration

Q-learning is closely related to the Value Iteration algorithm for Markov decision processes.

a)  (5 marks)  Describe two key similarities between Q-learning and Value Iteration.

b)  (5 marks) Give one key diference between Q-learning and Value  Iteration.

Question 2. Loss function

When training neural networks, a loss function is used to compare the current neural network’s predictions to ground truth values,  and then gradient descent with  backpropagation  is  applied to adjust the weights of the neural network so the predictions move closer to the ground truth values.  In supervised learning we make use of paired input and output labels fx; yg for training.  In  reinforcement learning, the agent receives experiences of states, actions and rewards and through exploration it must estimate the state-action values (in Q-learning).

a)  (5  marks) With reference to TD-learning, describe the loss function used to train the neural network in DQN. Use equations and highlight the components corresponding to the target value and the neural network’s current prediction.

b)  (5  marks)  Describe  two  key  diferences  between  TD  learning  used  in  reinforcement  learning  versus supervised learning.

Question 3. Target network

An important implementation detail in DQNs is having a separate target network.

a)  (5  marks) Describe why a separate target network is needed.

b)  (10 marks) Compare the performance of synchronising the target network periodically (e.g.  target net sync = 1000), versus using soft updates (e.g.  alpha sync = true, tau = 0.005) .  You  may select which

environment to apply this to (e.g. CartPole-v1).

Question 4.   Comparing CartPole-v0 and CartPole-v1

In  this  question,  you  will  train  DQNs  for  two  versions  of CartPole –  v0  and  v1 –  till  the  R100  value reaches the reward_threshold  (or  “stopping reward”)  and  compare the two  learnt  policies.   According to the source code, the diference between CartPole-v0 and CartPole-v1 are the max_episode_steps and reward_threshold as speciied in Table 1.

Table 1:  Diferences between CartPole-v0 and -v1

Recall from tutorials that an episode ends if any one of the following occurs:

. Termination:  Pole Angle is greater than 干12

. Termination:  Cart Position is greater than 2:4 (centre of the cart reaches the edge of the display)

. Truncation:  Episode length is greater than 500 (for CartPole-v1) or 200 (for CartPole-v0)

a)  (5 marks) Plot the episode reward and R100 value vs Episode number for CartPole-v0 and CartPole-v1 DQN models.

b)  (5  marks)  Describe and compare the  learnt  policies for CartPole-v0 and CartPole-v1.  Based on your observation of the learnt policies, explain why you think the values were increased from v0 to v1.

You  may  need  to  train  the  model  several  times  to  observe  the  desired  behaviour  diferences.    You can use the human render mode to visualise the policies extracted from the trained neural networks as in the Tutorial solutions.

Question 5. Learning-rate

For this question, consider the CartPole-v1 environment and use the R100 value (100-step moving average episode reward) as a measure of the quality of the learnt policy.

a)  (7.5  marks) Plot the quality of the policy learned by DQN, as given by R100, against episode number for three diferent ixed values of the learning_rate (which is called     in the  lecture  notes  and in many texts and online tutorials).  For this question, do not adjust     over time, rather keep it the same value throughout the learning process.  Your  plot should display the solution quality up to an episode count where the performance stabilises (typically > 1000 episodes).  Note the policy quality may still

be noisy, but the algorithm’s performance will stop increasing and its average quality will level out.

b)  (7.5 marks) With reference to your plot(s), comment on the efect of varying the learning_rate.

Question 6. Epsilon

a)  (5 marks)  Describe the purpose of the epsilon hyperparameter.

b)  (5 marks) How do the values of epsilon decay , epsilon final and epsilon start afect training performance?

Question 7.   DQN vs  Double  DQN and Duelling DQN

Two variants of DQN are Double DQN and Duelling DQN. Details of the methods can be read in the papers: Double DQN: https://arxiv.org/abs/1509.06461.

Duelling DQN: https://arxiv.org/abs/1511.06581.

a)  (7.5 marks) Choose one of these algorithms and discuss its improvements relative to vanilla  DQN.

b)  (7.5 marks)  Implement or use the supplied code to demonstrate the results numerically.

Question 8. Applying DQN

a)  (15 marks) Based on your study of hyperparameters and understanding of DQN, train a DQN agent for an additional environment from Gymnasium and report on any implementation/hyperparameter changes you had to make, and your agent’s performance.  For a simple environment consider LunarLander-v2 or MountainCar-v0.  For environments that are image-based like Pong, you will need to use convolutional neural networks, and training takes signiicantly longer so will require a GPU. See sample code here: https://github.com/comp3702/dqn-pong.

Academic Misconduct

The University deines Academic Misconduct as involving  “a range of unethical behaviours that are designed to give a student an unfair and unearned advantage over their peers.”  UQ takes Academic  Misconduct very seriously and any suspected cases will be investigated through the  University’s standard  policy (https:// ppl.app.uq.edu.au/content/3.60.04-student-integrity-and-misconduct).  If you are found guilty, you may be expelled from the University with no award.

It is the responsibility of the student to ensure that you understand what constitutes Academic Misconduct and to ensure that you do not break the rules.  If you are  unclear about what is required, please ask.

It is also the responsibility of the student to take reasonable precautions to guard against unauthorised access by others to his/her work, however stored in whatever format, both before and after assessment.

In the coding part of COMP3702 assignments, you are allowed to draw on publicly-accessible resources and provided tutorial solutions, but you must make reference or attribution to its source, by doing the following:

.  All blocks of code that you take from public sources must be referenced in adjacent comments in your code or at the top of your code.

.  Please also include a list of references indicating code you have drawn on in your solution.py docstring.

If you have utilised Generative AI tools such as ChatGPT, you must cite the tool and version in your code as well as describe in the report. A failure to reference generative AI use may constitute student misconduct under the Student Code of Conduct.

However, you must not show your code to, or share your code with, any other student under any circumstances.  You must not post your code to public discussion forums (including Ed Discussion) or save your code in publicly accessible repositories  (check your security settings). You  must not look at or copy code from any other student.

All submitted iles (code and report) will be subject to electronic plagiarism detection and misconduct proceed- ings will be instituted against students where plagiarism or collusion is suspected.  The electronic plagiarism detection can detect similarities  in code structure even  if comments, variable  names,  formatting etc.   are modiied.  If you collude to develop your code or answer your report questions, you will be caught.

For more information, please consult the following University web pages:

.  Information regarding Academic Integrity and Misconduct:

https://my.uq.edu.au/information-and-services/manage-my-program/student-integrity-and-

conduct/academic-integrity-and-student-conduct

http://ppl.app.uq.edu.au/content/3.60.04-student-integrity-and-misconduct

.  Information on Student Services:

https://www.uq.edu.au/student-services/

Late submission

Students should not leave assignment preparation until the last minute and must plan their workloads to meet advertised or notiied deadlines.  It is your responsibility to manage your time efectively.

It may take the autograder up to an hour to grade your submission.  It is your responsibility to ensure you are uploading your code early enough and often enough that you are able to resolve any issues that may be revealed by the autograder before the deadline. Submitting non-functional code just before the deadline, and not allowing enough time to update your code in response to autograder feedback is not considered a valid reason to submit late without penalty.

Assessment submissions received after the due time (or any approved extended deadline) will be subject to a 100% late penalty.  A one-hour grace period will be applied to the due time after which time the 100% late penalty will be imposed. This grace period is designed to deal with issues that might arise during submission (e.g.  delays with Blackboard or Turnitin) and should not be considered a shift of the due time.  Please  keep a record of your submission time.

In the event of exceptional circumstances, you may submit a request for an extension.  You can ind guide- lines on acceptable reasons for an extension here https://my.uq.edu.au/information-and-services/manage-my- program/exams-and-assessment/applying-extension. All requests for extension must be submitted on the UQ Application for Extension of Assessment form at least 48 hours prior to the submission deadline.