Matlab algorithms for traffic light assignment using fuzzy graph, fuzzy chromatic number, and fuzzy inference system

We propose algorithms in Matlab that combine fuzzy graph, fuzzy chromatic number (FCN), and fuzzy inference system (FIS) to create traffic light assignment based on traffic flow, conflict, and queue length in an intersection. We evaluate the algorithms through two case studies each on a signalized intersection at Semarang City (Indonesia) and compare the result to the existing systems. The case studies show that the algorithm based on fuzzy graph-FCN-FIS could reduce traffic light cycle time on the intersections. We provide three results as follows:• A pseudocode to construct fuzzy graph of traffic data in an intersection.• Algorithm 1 is to Determine fuzzy graph model of a traffic light data and phase scheduling using FCN function which is presented using Matlab programming language.• Algorithm 2 is to Determine duration of green lights of each phase using Mamdani-FIS codes in Matlab.


Specifications table Subject Area
Computer Science, Mathematics More specific subject area Fuzzy systems, computational methods of a fuzzy graph and its chromatic number for traffic light assignment. Method name Fuzzy graph and FCN for traffic light assignment. Name and reference of original method I. Rosyida, Widodo, C. R. Indrati, and K. A. Sugeng, "A new approach for determining fuzzy chromatic number of fuzzy graph," J. Intell. Fuzzy Syst., 28 (5), 2331-2341, 2015. I. Rosyida, Widodo, C. R. Indrati, D. Indriati, and Nurhaida, "Fuzzy chromatic number of union of fuzzy graphs: An algorithm, properties and its application," Fuzzy Sets Syst., 384, 115-131, 2020. Resource availability -

Method details
Throughout this paper, we use some terms in traffic light problems. A phase on an intersection is a portion of a signal cycle when the green lights are assigned to a certain combination of traffic movements. Traffic flow is the study of the movement of individual drivers and conveyances among two locations and the interactions between them. Meanwhile, a volume is defined as a number of traffic elements flowing an area on a road per unit of time (vehicles/hour or passenger car unit/hour) [1] . One of the most important problems that is needed to be solved is traffic congestion. In Indonesia, generally, a fixed phase and fixed time period of green lights on each phase are common applied on an intersection. In this case, whether there are or none vehicles passing through the intersection, the green light is still on until the time elapses. This condition will lead to accumulating queue on a lane with a high traffic volume, particularly at peak hour. One of the alternative options is to create a traffic assignment where the phase scheduling applied considers traffic flow, conflict, and queue. We call this state as a fuzzy phase scheduling.
In general, we model traffic flow on an intersection by using classical graph G( V , E ) where V a vertex set and E an edge set, represent a set of traffic flows and a set of conflicting traffic flows, respectively. Two vertices that are connected by an edge mean that the flows are in conflict and should be assigned in different phases. In modeling traffic flow using graph vertex coloring, a color represents a phase on an intersection. A minimum number of colors used in the vertex coloring of G , called as chromatic number, represents a minimum number of phases required on the intersection. Given the load of traffic volumes on conflicting flows, it is interesting to figure out safety level on these flows. Moreover, a conflict between two traffic flows cannot be predicted exactly because it highly depends on their traffic volumes which vary and fluctuate in time. In other words, magnitudes of the conflict exhibit fuzzy phenomena. Hence, we need an alternative graph that accommodates indeterminate phenomena on conflicting traffic flows to model traffic light assignments. Therefore, we proceed to model traffic flows in an intersection in a fuzzy graph where, on each edge, we assign a degree of membership that represents a degree of conflict between two vertices. Next, by using fuzzy chromatic number (FCN) of the graph we produce the number of phases, their respective degree of safety and their phase scheduling. We choose, heuristically a set of phase schedulings to be implemented in an intersection. By setting queue length of flows in a phase scheduling as the inputs and duration of green light as the output of a Mamdami-FIS, we are able to get the desire traffic assignment which the green light setting considering the queue length of the respective flows. This assignment, as can be seen later, has shorter cycletime compared to fixed green duration setting. Further, we use term "traffic flows" in place of "traffic movements" and vice versa, both to describe vertices.
In one hand, several researchers studied traffic light problems using fuzzy graph in [2 -6] , However, these works studied only on fixed phases and none of these works gave fuzzy phase scheduling and computation of their proposed method. In other hand, many researchers applied only fuzzy logic in traffic light problems. The application of fuzzy logic i.e., Fuzzy Inference System (FIS), in traffic control was first initiated by Pappis and Mamdani [7] . Later, many researchers discussed this problem, such as in [8 -15] . In contrast to other research of traffic light based on fuzzy graph or FIS, this research focuses on constructing fuzzy phase scheduling that links fuzzy graph, FCN and FIS.
Different traffic flows on different conditions ideally require different phase scheduling. Hence, it can be said that setting an optimal phase is a fuzzy phenomenon. In this research, we propose a phase scheduling that considers traffic intensities using fuzzy graph and FCN. Firstly, we construct an algorithm in Matlab to model a traffic light system using fuzzy graph. Secondly, we determine the phase scheduling using FCN algorithm and calculate duration of green lights using Mamdani-FIS function in Matlab. The concept and algorithm of FCN were given in the main article [16 , 17] . Basic terminologies used in the algorithm are presented in the appendix.
The algorithm is divided into two parts as follows: 1. Determining fuzzy graph model of a traffic light system and phase scheduling using FCN function.
2. Determining duration of green lights of the phases in part (1) using Mamdani-FIS codes in Matlab.

Determining fuzzy graph and phase scheduling
We constrcut a pseudocode to represent traffic data in an intersection into a fuzzy graph as shown in Table 1 .

Table 1
Pseudocode to construct fuzzy graph of traffic data in an intersection.
Steps Commands Count FE = {fe 1 ,fe 2 ,…,fe ne |fe = max{fv i, fv j }} 16 For k = 1 to ne 17 If mins-2 ≤ fe k < L1 18 Data of traffic are first inputed into a mat file. The following scripts are used.
As shown on the above script, we use an intersection with 4 approaches, namely: W = west, N = north, E = east, and S = south. If users want to applied this program in an intersection with more than 4 approaches, than they can modify these commands. There are 4 inputs. Flows is a set of traffic flows which are represented as vertices in the fuzzy graph. Flow 'WN' states that there is a traffic movement from West to North. Users should identify which traffic flows represents the intersection and they can modify this step based on traffic assignments in the intersection. Volumes is a set of the number of vehicles for all traffic flows. The third input is Conflicts . It is a set of conflicting traffic flows. They are represented as edges in the fuzzy graph. For example, let traffic flow 'WN' and SN' are in conflict. Then, there is an edge 'WN SN' in the edge set. Lastly, we input Queue , a set of queue length of the flows, Flows. This input, will be used later in the next part. The users can modify this step according to conflicting traffic flows that they are observed in the intersection.
Step 1 in the script is the first 3 steps on the pseudocode. The following command on Matlab command window shows the load of mat.file 'case1.mat' that we input on the above algorithm.
Further, we create the following algorithm which are based on the above pseudocode and FCN in [17] . We call this algorithm as Algorithm 1 for ease of referencing in this article.
Explanation of Algorithm 1 is as follows: In step 1. We load traffic data called case1. In step 2, fuzzy graph construction is done. We calculate crisp volume of conflicting movements or crisp weight of edges in step 2.1. In step 2.2 and step 2.3, we determine intervals and fuzzy sets of traffic volumes, i.e., {( low , μ low ), ( medium , μ medium ), ( high , μ high )} where μ is the membership function and we display the membership functions of the fuzzy sets, respectively. We fuzzify the edge crisp weights in step 2.4 to get the fuzzy edge set, ˜ E = (E , μ E ) . Plot of the fuzzy graph is given in step 2.5. The script on this step describes step 4 up to the last step in the pseudocode.
Determining the phase scheduling is done in step 3. We use a call function namely FCNwithpartition which is on the appendix B. This function is a slighty modified form of its original flowchart in [17] . Input for this function is a fuzzy edge set, while outputs are k,Lk and P . The output k is chromatic number. In graph traffic modeling, it represents the number of phases. Lk is the degree of safety of its respective value k . Setting both side by side, we get ( k,Lk ) is the fuzzy chromatic number. The output P is the associated phase scheduling to the number of phases k . Also, known as k -phase scheduling. Saving the phase scheduling in a mat file to be processed further as input for the second part of the modeling is done in the last step, step 4.
The result of Algorithm 1 is a mat file called, in this example, 'case1_FuzzyPhases.mat'.
The following scripts on Matlab Command Window show the content of the file. There are three commands on the window. The first command is to load the mat file. The file contains one structure namely Patterns . The second command is to check the contents of Patterns. There are four substructures on Patterns . The last command, Patterns. FuzzyChromaticNumber displays FCN, i.e., the number of minimum phases, k and its respective degree of safety, Lk.
The next step is to determine phases which are eligible for fuzzy phase scheduling. Given that FCN shows safety level of the number of phases applied on the traffic network, to be eligible, we can determine the k -phase based on the degree. We omit the case of k = 1, i.e. 1-phase scheduling, as definitely not safe (see the above scripts). Meanwhile, arrangement with 3 to 8 phases have 1 safety level. However, as the number of phases applied on an intersection affects the length of cycle time, of all the number of phases which has 1 safety degree we choose the smallest number. In this example, we choose k = 3. By that setting we work only on k = 2 and k = 3 phases. The last 2 substructures on Patterns, i.e., for_3phase and for_2phase, show all possible patterns of their k-phase.
As mentioned previously, chromatic number (k) represents the number of phases in a traffic assignment. A term k-phase scheduling means we assign k phases for traffic assignment on an intersection. Fuzzy chromatic number (k,Lk) gives a degree of applying k-phase scheduling in a traffic assignment. As shown on FCN on the above scripts, 1-phase scheduling comes with the lowest degree (0.0041), as it is obviously not safe if traffic elements are allowed to flow (from all directions) all at once. Assignments within 2 phases comes with higher degree which is 0.5723. Whereas, assignments within 3 or more phases has 1 degree of safety. However, the more the number of phases the lengthier the traffic cycle time. There is inevitably a trade-off between safety level and cycle time length. Therefore, we are looking for a phase scheduling which is safe at a reasonable cycle time.

Determining duration of green light
Having constructing traffic phases, we would like to determine duration of the traffic lights which considers queue length of flows on the phases. This is done by Mamdami-FIS codes. The highligth of the system is as follows: 1. The input parameter is flowing queue length in each phase and is labelled as Short, Medium, Long.
The formula used to determine the queue length in each traffic flow is presented in Eqn 1 : where Ql is the queue length (meter), L i is the area of each vehicle-i (m 2 ), Q i is the number of vehicles per hour, c is the number of cycles of green light per hour, width is the wide of the approach (meter), and i is the type of vehicle (i = 1 for MC(Motor Cycle), i = 2 for LV(Light Vehicle), and i = 3 for HV(Heavy Vehicle)).
2. The output parameter is green light-duration in each phase (Short, Medium, Long). According to standard cycle time for the 3-phase or 4-phase scheduling [1] , we use the maximum length of interval for the output is 100 s. The fuzzy sets of input and output parameters have triangular membership functions. 3. The fuzzy rules are defined based on the number of phases used and maximum traffic flows in a phase. 4. The defuzzification method is centroid.
The following matlab algorithm which we call Algorithm 2 is mainly composed of Mamdani-FIS in codes. Algorithm 2 consists of three main steps. The first step is loading data. We need to input Queue which is in file case1.mat . The queue length of the movements is calculated using the formula in [1] . We load fuzzy phases obtained in the previous algorithm that is in variable Patterns.allphases in case2_FuzzyPhases mat file. As our Mamdami input is queue length of flows, we need to fuzzify the queue length into fuzzy sets, i.e., short, medium and long where each has triangular membership function. Hence, we need to load the range inRange and parameters of each membership function inMFparams.
Step 2 is the Mamdami-FIS system written in coding. The number of inputs of the FIS is set to be not more than 4, e.g., Queue-length1, Queue-length2, Queue-length3, and Queue-length4. The label Queue-length1 means the queue length in the first flow and so on.
Given any k-phase scheduling, Algorithm 2 will work only if maximum number of flows/movements on the phase scheduling is 2,3 or 4. We limit the algorithm due to the empirical fact that applying a phase with more than 4 flows/movements results in a lengthier cycletime.    Fig. 1 show available systems of the FIS stucture, i.e., the input, output and evaluation. As can be seen, we set 9, 27 and 15 rules for 2, 3 and 4 inputs, respectively. One can see that we use a call function getmyrule() as in command rule = getmyrule(numOfinLabels) in Step 2 Algorithm 2. We give this function in Appendix C .
In step 3, we save all the outputs. By inputting fuzzy phases obtained previously, 'fuzzy-traffic' system gives one phase scheduling that is a scheduling for 3 phases. The result is in a mat file namely 'case1_3-FuzzyPhaseScheduling.mat' (see the scripts below). There are 5 options of phase patterns to arrange the traffic using 3 phases. We can see the results interactively in Variable Window as well.

Location 1 (Kaligarang intersection, Semarang City, Central Java, Indonesia)
Let us consider a case study on an intersection in Semarang City, Central Java, Indonesia, particularly in "Kaligarang" intersection [18] . Fig. 2 shows the sketch and image of the intersection. Data of traffic volumes on all movements are gathered by using video camera during week days (Monday, Tuesday, and Wednesday) on August 6-8, 2018. The data was taken four hours each day e.g., during two peak hours in the morning (06.0 0-08.0 0 a.m.) and two peak hours at afternoon (16.0 0-18.0 0 p.m.). Data of this example have been shown to illustrate the building of our algorithms (1 and 2).
We call this example as case 1. There are four variables to model the intersection into a fuzzy graph. These are traffic flows, traffic volumes, flow queue length and conflicting flows. The first three variables are shown in Table 2 and the fourth variable is on the 2nd or 3rd column of Table 3 . Traffic flows and their volumes are used in Algorithm 1. Whereas, queue length is used in Algorithm 2. As said in introduction, in graph modeling, traffic flows and conflicting flows are modeled as vertex ( V )   Table 3 (left) and representation of traffic in Fig. 2 into a fuzzy graph (right).

Table 4
Number of phases ( k ) and the associated degree ( Lk ) in case 1.  Fig. 3 . Both plots are the outputs of step 2 in Algorithm 1.
As stated previously, output of the first algorithm is fuzzy phases of an intersection. The phases are defined as "fuzzy" because based on FCN definition, a level of safety is assigned to each number of phases. Table 6 shows FCN of traffic network of case 1. The table is interactively screenshootted and cropped from Matlab Variable Window. The number k in Table 4 represents the amount of phases to arrange traffic flows, while Lk represents a possibility to apply the k -phase. According to the table, traffic assignment with 3 number of phases on Location 1 has one degree of safety. Therefore, we can apply k = 3 phases during peak time, i.e., between 06.30-09.00 a.m. or between 16.0 0-18.0 0 p.m. All Table 5 All possible patterns of k-phase for k = 2 (top) and k = 3 (bottom) in case 1.  Table 6 The 1st option of scheduling with 2-phase in Table 5 .

Phase
Movements / Flows 1 WE, EW 2 WN, WS, EN, SN,SE, SW Table 7 The 1st option of scheduling with 3-phases in Table 5 . possible k-phase scheduling with k = 2 and 3 can be seen on Patterns structure (on the Variable Window) as on the screenshots in Table 5 . Table 5 is comprised of two sub tables. The top and bottom sub tables show options of phases of two and three-phase scheduling, respectively. There are 2 options for 2-phase scheduling whereas 5 alternatives are available for 3-phase scheduling. Table 6 and Table 7 show the first option in 2 and 3-phase scheduling, respectively. As can be seen on these two arrangements, maximum number of movements in 2 phases is 6, whereas maximum number of movements for 3 phases is 4. Later, in the second algorithm which constitutes Mamdami-FIS, we limit the algorithm to proceed only phase schedulings where maximum number of the flows are 2, 3, or 4. This is due to empirical facts that more number of movements/flows in a phase increases the cycle time of the assignment. The next step is to determine green light duration on each phase. The inputs are fuzzy phases shown in Table 5 . that is k = 3 and k = 2 fuzzy phases. The system "fuzzy-traffic" reads these inputs and automatically produces the k-phase scheduling according to the rules created. The name of the output mat file is dynamicaly coding, i.e., the algorithm sets the name based on the number of phases of the phase scheduling that is executed online in the system. Hence, the results may be more than one mat file.
It means that the available phase scheduling is only one for this case which is the fuzzy scheduling with 3 phases. There are 5 options of phase patterns suggested in the file. Subtables in Table 8 display duration of traffic lights (in seconds) of their respective scheduling with 3 phases shown on screenshots of Matlab Variable Window. One can see that a phase which has on average higher queue will be assigned longer green time.
The 5th pattern of phases in the phase scheduling (on the bottom on Table 8 ) has similar pattern to three phase scheduling in the existing system shown in Table 9 . We can see that the proposed fuzzy phase scheduling reduces cycle time of the traffic light system, i.e. cycle time of the proposed phase scheduling, has lower length. Therefore, the proposed method offers a valuable alternative traffic assignment with 3 phases on the intersection. Table 10 Traffic flows, volumes, and queue length at location 2 during peak time (afternoon).

No
Traffic flows Volumes (pcu) Queue length (meter)

Location 2 (Lamper Gadjah intersection, Semarang City, Central Java, Indonesia)
Let us consider the second case study at Lamper Gadjah intersection in Semarang City, Central Java, Indonesia. Fig. 4 . displays the schema and image of Lamper intersection. Data of traffic volumes acquired from video camera during week days (Monday-Friday) on August 2018 [25] . The data was taken on one hour each day e.g., during peak hour in the afternoon (16.0 0-17.0 0 p.m.). Data of this example is inputed and saved as 'case2.mat'. In Table 10 and Table 11 , we present traffic flows, volumes, queue length and conflicting flows at peak hour and it membership degrees. Further, Step 2 in Algorithm 1 displays plots of membership functions for the edge set and plot of the fuzzy graph ( Fig. 5 ).
Number of phases and its level of safety, the result of FCN function, for case 2 is shown in Table 12 . It is shown that assignments with at least 4 phases have degree of safety 1, whereas assignments with less number of phases, i.e., k = 3, 2 and k = 1 have very low degree of safety. The function also give all possible phase patterns for k = 2,3, and 4 as displayed in Table 13 .
All possible phases in Table 13 are then inputed into the second algorithm. However, we need to put in mind that only scheduling with 4 phases that has 1 degree of safety, the other two have very low degrees of safety (see Table 12 ). We input the range of input variable as   Fig. 4 into a fuzzy graph (right).

Table 12
Number of phases ( k ) and the associated degree ( Lk ) for case 2.

No
Number  Two mat files pop up as the result of algorithm 2 suggesting that there are 2 types of phase scheduling. There is only one option in arranging the traffic light in 3 phases ( Table 14 ), whereas, three options are for scheduling with 4 phases ( Table 15 ). In order to validate the result we collect duration of green lights in the existing system (Location 2) which is presented in Table 16 . The system is an intersection where 4-phase scheduling is implemented. The scheduling is equal to the second phase pattern in the fuzzy scheduling with 4 phases (shown on the 2nd subtable from bottom on Table 14 Duration of green, red, and yellow lights of 3-phase scheduling in Location 2 (afternoon).  Table 15 ). We may argue that the fuzzy phase scheduling proposed is superior in reducing the average time a driver spends his/her time on the intersection as the cycle time has shorter length than the cycle time applied in the existing system (in Table 16 ).

Conclusions
We proposed algorithms to determine the number of phases and the fuzzy phase scheduling for a traffic light system based on fuzzy graph and fuzzy chromatic number. Further, we determine duration of green lights in each phase by applying Mamdani-FIS. We evaluate the algorithms through two case studies. The results show that the combination of the algorithms and the Mamdani-FIS gives some options of phase scheduling with different cycle times. The phase scheduling proposed in this research increases performances of intersections under study in that the cycle times of the proposed scheduling are shorter than that of the existing systems. In our future research, we will improve the performance of the algorithms and implement it on various types of intersections.

Fuzzy graph and fuzzy chromatic number (FCN)
After a concept of fuzzy set was introduced by Zadeh in 1965 [22] , the concept of a fuzzy graph that consists of a crisp vertex set and a fuzzy edge set was initiated by Kaufmann in 1973 [23] . A fuzzy set ˜ A on X is a set { ( a, μ( a ) ) | a ∈ X} with a membership function μ : X → [ 0 , 1 ] . Further, the classical sets are named as crisp sets.
Given V which is non-empty set and E ⊆ V × V . A fuzzy graph ˜ G = ( V , ˜ E ) consists of a vertex set V and ˜ E which is a fuzzy edge set with μ : V × V → [ 0 , 1 ] as its membership function. Thereafter, the graph G = ( V, E ) is named as a crisp graph. In crisp graphs, a set A is called an independent vertex set if each pair of vertices in A is independent (not adjacent). The concept is generalized into fuzzy graph as follows. Given δ ∈ [ 0 , 1 ] , a fuzzy independent vertex set (FIVS) of ˜ G = ( V , ˜ E ) is a set I ⊆ V which satisfies the condition μ( xy ) ≤ δ or each pair x, y ∈ I . The FIVS I can be denoted as I δ . Moreover, a k -coloring of ˜ G could be defined through a partition of V into k -FIVS { I δ 1 , I δ 2 , . . . , I δ k } such that I δ i ∩ I δ j = ∅ for all i = j and I δ 1 ∪ I δ 2 ∪ . . . ∪ I δ k = V . The minimum k in the k -coloring of ˜ G is named δ-chromatic number of ˜ G , symbolized by χ δ ( ˜ G ) [24] .