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

The University of Melbourne

School of Computing and Information Systems
COMP20005 Engineering Computation
Semester 2, 2023
Assignment 1
Due: 4:00 pm Monday 2nd October 2023
Version 1.0

1 Learning Outcomes

In this assignment you will demonstrate your understanding of loops and if statements by writing a program that sequentially processes a file of input data. You are also expected to make use of functions and arrays.

2 The Story...

Traffic congestion detection and analysis aims to detect regions of a road network where traffic speed is slow and to analyse the congestion patterns, such as the source of a congestion. It plays an important role in traffic optimisation. For example, real-time traffic congestion detection can help navigation apps such as Google Maps to recommend faster routes, hence improving fuel efficiency and travel experience. Meanwhile, traffic congestion pattern analysis can help authorities such as VicRoads make more effective traffic planning for major events such as AFL Grand Finals to reduce traffic congestions.

Figure 1 shows an example, where each coloured dot represents a traffic speed sensor in the freeway system of the metropolitan areas of California, USA1 . The different colours of the dots represent different average traffic speed values recorded by the sensors for the last five minutes. A red colour suggests a low traffic speed, while a green colour suggests a high traffic speed. If a navigation app can detect the low traffic speed at the red dots, it can suggest alternative routes to help users avoid these congested locations.

Figure 1: Traffic speed sensing (9:00 pm, 28 January 2021, California, USA)

In this assignment, we are given a set of traffic speed sensor readings (in km/hour) that represent the average traffic speed at 5-minute intervals at different (sensor) locations. The aim is to detect whether there are congestions at each location for the last 5 minutes, and if so, to identify the source of each congestion. You  do not need to be an expert in traffic management or sensor technologies to complete this assignment.

Below is a sample set of input data. The first line contains a positive integer num_locations representing the number of sensor locations in the input data. In the example below, num_locations = 10, meaning that there are 10 sensor locations.

Starting from the second line, each line contains data of a sensor locations. Each line has up to 16 columns separated by spaces (‘ ’):
• The first column contains a unique 3-digit positive integer representing the ID of the sensor location.
• The second column contains a positive integer representing the speed limit at the sensor location.
• The third column contains a real number representing the latest average traffic speed readings recorded by the sensor at the location at the last 5-minute interval.
• The next 10 columns each contains a real number representing the average traffic speed readings recorded by the sensor at 5-minute intervals for the past 10 weeks on the current day of week and time of day. Suppose the current time and day is 9:00 pm Wednesday. Then, Column 4 is the average traffic speed reading between 8:55 pm and 9:00 pm on Wednesday last week, Column 5 is the average traffic speed reading between 8:55 pm and 9:00 pm on Wednesday two weeks ago; . . .; and Column 13 is the average traffic speed reading between 8:55 pm and 9:00 pm on Wednesday 10 weeks ago. Each average traffic speed value is a positive real number with 2 digits after the decimal point. The average traffic speed values do not exceed the speed limit.

• The last three columns contains IDs of the sensor locations at the “downstream” of the current sensor location, that is, the traffic flows from the current location to these locations. If a column contains a value of -1, it means there are no more downstream sensor locations.

The last line contains two real numbers representing traffic speed thresholds to indicate a congestion, which will be described and used in Stage 3.

10
104 80 34.31 73.29 77.35 66.66 44.12 49.65 60.72 78.76 29.45 15.16 35.70 106 121 114
106 90 49.36 49.33 47.02 40.86 46.57 49.04 49.77 40.75 49.54 42.33 43.42 124 -1
114 90 80.85 81.50 66.26 84.71 50.63 17.01 70.02 54.95 82.38 28.54 69.71 132 -1
121 60 33.65 10.47 51.25 57.63 13.60 44.24 17.20 33.32 24.15 29.60 24.25 201 -1
124 40 31.13 36.52 24.10 29.71 28.05 16.02 32.55 15.17 36.31 30.95 20.39 221 351 -1
132 60 29.42 15.55 30.03 29.92 22.67 21.25 11.80 23.48 25.30 23.52 22.90 121 797 -1
201 80 43.27 06.62 10.43 12.28 21.59 32.42 22.72 22.11 27.61 21.36 27.77 -1
221 50 24.16 48.13 44.73 43.19 49.57 47.68 39.90 49.93 45.13 45.58 41.50 -1
351 90 77.83 86.72 82.94 85.29 85.66 85.58 81.95 84.71 88.00 89.49 84.69 104 797 -1
797 40 22.11 20.26 26.67 24.44 27.17 27.29 22.07 24.14 20.30 10.67 24.16 -1
0.6 0.8

The second line of the sample above represents sensor location #104. Its speed limit and latest average traffic speed reading are 80 and 34.31, respectively. Its past average traffic speed readings are: 73.29, 77.35, 66.66, 44.12, 49.65, 60.72, 78.76, 29.45, 15.16, and 35.70.

You may assume that the input is always correctly formatted, and 0 < num_locations < 101. The input is sorted by the sensor location ID in ascending order.

3 Your Task

Your task is to write a program that reads the input data and detects if there are congestions at any of the sensor locations, and if so, outputs the source of the congestions. The assignment consists of four stages.
3.1 Stage 1 - Process the First Sensor Location (6 Marks)

Write a program that reads the first two lines of the input data and prints out: num_locations, the ID of the first sensor location, the speed limit, the latest average traffic speed, the historical average (HA) traffic speed for the past 10 weeks of the first sensor location, and the IDs of any downstream sensor locations (-1 should not be outputted).

Given a sensor location L with m (m = 10 in our case) past average traffic speed readings s0, s1, . . . , sm−1 (excluding the latest average traffic speed), its historical average traffic speed is calculated as:

(1)
The output of this stage given the above sample input should be (where “mac:” is the command prompt):
mac: ./program < test0.txt
Stage 1
==========
num_locations: 10
ID of the first sensor location: #104
Speed limit: 80
Latest average traffic speed: 34.31
Historical average traffic speed: 53.09
Downstream sensor locations: #106 #121 #114

Here, 53.09 is the historical average traffic speed of the first sensor location as calculated by Equation 1.

You should write functions to process this and the following stages where appropriate.

Hint: To ensure the result accuracy, use double’s for storing the traffic speed readings and calculating the historical average. Use %05.2f for the average speed output formatting. You may also read all input data before printing out for Stage 1.

As this example illustrates, the best way to get data into your program is to save it in a text file (with a “.txt” extension, any text editor can do this), and then execute your program from the command line, feeding the data in via input redirection (using <).

In your program, you will still use the standard input functions such as scanf() to read the data. Inputredirection will direct these functions to read the data from the file, instead of asking you to type it in by hand. This will be more convenient than typing out the test cases manually every time you test your program. Our auto-testing system will feed input data into your submissions in this way as well. To simplify the marking, your program should not print anything except for the data requested to be output (as shown in the output example).

We will provide a sample test file “test0.txt” on LMS. You may download this file directly and use it to test your code before submission. This file is created under Mac OS. Under Windows systems, the entire file may be displayed in one line if opened in the Notepad app. You do not need to edit this file to add line breaks. The ‘\n’ characters are already contained in the file. They are just not displayed properly but the scanf() function in C can still recognise them correctly.
3.2 Stage 2 - Calculate the Historical Average (5 Marks)
Now modify your program so that the historical average traffic speed of each sensor location is computed and visualised. You may assume that the historical average traffic speed values are within the range of (0, 100) (use %05.2f for the historial average output formatting). On the same sample input data, the additional output for this stage should be (you need to work out how the visualisation works based on this example):
Stage 2
==========
#104, HA: 53.09 |--+--+
#106, HA: 45.86 |--+--
#114, HA: 60.57 |--+--+-
#121, HA: 30.57 |--+-
#124, HA: 26.98 |--+
#132, HA: 22.64 |--+
#201, HA: 20.49 |--+
#221, HA: 45.53 |--+--
#351, HA: 85.50 |--+--+--+
#797, HA: 22.72 |--+

3.3 Stage 3 - Detect Congestions (5 Marks)

Now read the last line of the input, which contains two threshold parameter values α and β (0 < α, β < 1), to help detect two typical types of congestions. Consider a sensor location L with speed limit SL, latest average traffic speed CL, and historical average traffic speed HL.
1. Recurring congestions (REC_CONGESTION): We say that a recurring congestion has occurred at a sensor
location L, if its latest average traffic speed CL is substantially lower than its speed limit SL but not
substantially lower than its historical average, that is:
CL < α · SL and CL ≥ β · HL (2)
2. Non-recurring congestions (NON_REC_CONGESTION): We say that a non-recurring congestion has oc
curred at a sensor location L, if its latest average traffic speed CL is substantially lower than both its
speed limit SL and its historical average, that is:
CL < α · SL and CL < β · HL (3)
In this stage, your task is to detect and output if either types of congestions have occurred at each of the sensor locations. On the same sample input data, the additional output for this stage should be:
Stage 3
==========
#104: NON_REC_CONGESTION
#106: REC_CONGESTION
#114: NO_CONGESTION
#121: REC_CONGESTION
#124: NO_CONGESTION
#132: REC_CONGESTION
#201: REC_CONGESTION
#221: NON_REC_CONGESTION
#351: NO_CONGESTION
#797: REC_CONGESTION

Here, location #104 has a speed limit SL = 80. Its latest average traffic speed CL = 34.31 is smaller than α·SL = 0.6×80 = 48.0, while CL is greater than β ·HL = 0.8×53.09 = 42.47. Thus, there is a non-recurring congestion at location #104.

Hint: You should store the HA values calculated in Stage 2 and reuse them for this stage.

3.4 Stage 4 - Detect Sources of the Congestions (4 Marks)

In this stage, we trace back the sources (that is, source locations) of the congestions. When a congestion has been detected at a location L, we examine if any of its downstream locations also has a congestion. If there is a congestion at some downstream location Ld, we further examine the downstream locations of Ld.

The process is performed recursively, until there are no more downstream sensor locations with congestions. The sensor locations that have congestions while their downstream sensor locations do not have congestions are the sources of the congestions, and your program should output these sensor locations. On the same sample input data, the additional output for this stage should be:

Stage 4
==========
#104: source(s) - #106 #201
#106: source(s) - #106
#114: NO_CONGESTION
#121: source(s) - #201
#124: NO_CONGESTION
#132: source(s) - #797 #201
#201: source(s) - #201
#221: source(s) - #221
#351: NO_CONGESTION
#797: source(s) - #797
In the example above, for sensor location #104, there are three downstream locations #106, #121, and $114.
Both #106 and #121 are congested (see output from Stage 3).
• Location #106 has only one downstream location, which is #124 and is not congested. Thus, #106 is one of the sources of the congestion at #104.

• Location #121 also has only one downstream location, #201, which is congested and does not have any further downstream locations. Thus, #201 is another source of the congestion at #104.

Hint: You can use loops instead of recursive functions for this stage, with the help of an array Q to store the sensor locations to be considered. The algorithm to trace the source of a congestion at location L works

as follows:
1. At start, Q is an empty array without any elements.
2. Sensor location L (that is, its ID) is added to Q as its first element.
3. Now we loop through locations in Q starting from the first location.
4. When the loop reaches a location L 0 in Q, we examine if any of its downstream locations are also congested. If so, we add those congested downstream locations to be after the last existing location in Q (if those congested downstream locations are not in Q already). If not, we have found a congestion source location, which is outputted.

5. The loop ends when we reach the last location in Q and there are no more congested locations to beadded to Q.

You may assume that there are no “congestion cycles”, that is, there will not be sensor locations such as

L1 → L2 → L3 → . . . → L1 (“Li → Lj” denotes that Lj is a downstream location of Li) where all locations
are congested.

4 Submission and Assessment

This assignment is worth 20% of the final mark. A detailed marking scheme will be provided on LMS. Submitting your code. To submit your code, you will need to: (1) Log in to LMS subject site, (2) Navigate to “Assignment 1” in the “Assignments” page, (3) Click on “Load Assignment 1 in a new window”, and (4) follow the instructions on the Gradescope “Assignment 1” page and click on the “Submit” link to make a submission. You can submit as many times as you want to. Only the last submission made before the deadline will be marked. Submissions made after the deadline will be marked with late penalties as detailed at the end of this document. Do not submit after the deadline unless a late submission is intended. Two hidden tests will be run for marking purposes. Results of these tests will be released after the marking is done.

You can (and should) submit both early and often – to check that your program compiles correctly on our test system, which may have some different characteristics to your own machines.

Testing on your own computer. You will be given a sample test file test0.txt and the sample output test0-output.txt. You can test your code on your own machine with the following command and compare the output with test0-output.txt:
mac: ./program < test0.txt /* Here ‘<’ feeds the data from test0.txt into program */
Note that we are using the following command to compile your code on the submission testing system (we name the source code file program.c).
gcc -Wall -std=c17 -o program program.c -lm
The flag “-std=c17” enables the compiler to use a modern standard of the C language – C17. To ensure that your submission works properly on the submission system, you should use this command to compile your code on your local machine as well. If you are using Grok to prepare your assignment, you may use the following command to compile it in Grok Terminal, which only supports C11 but should be close enough.
gcc -Wall -std=c11 -o program program.c -lm
You may discuss your work with others, but what gets typed into your program must be individual work, not from anyone else.
• Do not give (hard or soft) copies of your work to anyone else.
• Do not “lend” your memory stick to others.
• Do not leave your laptop unlocked in the lab or library.
• Do not upload your assignment solutions onto Github or any other public code repositories.
• Do not ask others to give you their programs “just so that I can take a look and get some ideas, I won’t copy, honest”.
The best way to help your friends in this regard is to say a very firm “no” when they ask for a copy of, or to see, your program, pointing out that your “no”, and their acceptance of that decision, is the only thing that will preserve your friendship. A sophisticated program that undertakes deep structural analysis of C code identifying regions of similarity will be run over all submissions in “compare every pair” mode. See

https://academichonesty.unimelb.edu.au for more information.

Deadline: Programs not submitted by 4:00 pm Monday 2nd October 2023 will lose penalty marks at the rate of 3 marks per day or part day late. Late submissions after 4:00 pm Friday 6th October 2023

will not be accepted. Students seeking extensions for medical or other “outside my control” reasons should email the lecturer at [email protected]. If you attend a GP or other health care professional as a result of illness, be sure to take a Health Professional Report (HRP) form with you (get it from the Special Consideration section of the Student Portal), you will need this form to be filled out if your illness develops into something that later requires a Special Consideration application to be lodged. You should scan the HPR form and send it in connection with any non-Special Consideration assignment extension requests. And remember, C programming is fun!