# CHAPTER 1 INTRODUCTION

1.1. DEFINITION OF THE PROBLEM:
Now-a-days, we are living in the world of automation. Everything is controlled with the help of microcontrollers. Traffic Light Controller is also an area which incorporates such type of control, i.e., using microcontrollers. But, this control is not flexible. Transportation Research has the main aim to optimize the transportation flow of people and goods. As the number of road users increases, and resources provided by the current infrastructure are limited, intelligent control of traffic will become an important issue in future. Avoiding traffic jams is thought to be beneficial to both environment and economy [1]. To optimize the traffic control, one way would be to control the traffic using the traffic density at that moment of time. Fuzzy control has been successfully applied to many tasks of automatic control over the last decades and can very well used to facilitate this approach [5]. This technique uses the real-time parameters like traffic density behind the Red and Green lights which thereby controls the on and off timings of the red and green lights. This technique provides the flexibility to the present inflexible approach.

1.2. MOTIVATION:
1.2.1. JUSTIFICATION:
Current traffic light controllers used to change the cycle time at a constant rate. This approach increases the average waiting time and, thus, is not an optimized way causing this method to be fruitless. Fuzzy logic approach is based upon traffic density in a real-time environment. It provides mathematical tools for solving and working out approximate reasoning 1

processes when having to deal with real-time data. This logic is composed of fuzzy sets, which are provided by a mathematical definition rising from the concept of degrees of membership. Thus, fuzzy logic may well hold the key to usefully capturing the real-time traffic data and making the approach flexible. It works in the same manner as human controls traffic.

1.2.2. OBJECTIVES:  The objective of this project is centered on development of a method that is
capable of controlling the traffic using the real-time parameters which plague the traffic light controller systems.

 Other objectives include creating a robust traffic light controller system which
reduces the average waiting time.

1.2.3. FOCUS:
The focus of this project is centered upon determining the on and off timings of the green and red lights of the traffic light. A working model is being prepared in order to implement the idea using any hypothetical situation. The same idea may work for the real-time situations as well.

1.2.4 ORGANISATION OF THE THESIS:
The remaining of the document is organized as follows: Section 2 contains the theoretical description of the fuzzy logic as well as the hardware components. Section 3 contains the feasibility study and the requirement analysis of the project. Section 4 contains the Present day technique. Section 5 contains the proposed technique with all the assumptions and the methodology. Section 6 contains the System analysis and the design of the project. 2

Section 7 contains the experiments with results and the graphs. Section 8 contains the conclusion.

3

CHAPTER 2 THEORETICAL BACKGROUND
2.1. FUZZY LOGIC:
2.1.1. BASIC CONCEPTS:
In classical or crisp sets, an element in the universe has a well defined membership or non membership to a given set. Membership to a crisp set F can be defined through a membership function defined for every element x of the universe as 1 xεF µ= 0 xF An example of a graphic for the membership function of a crisp set is illustrated in Figure 1. Here, scanning the universe, there is an abrupt and well-defined transition from membership to non membership and vice versa. It is said to be “crisp” [2].

Figure 2.1. Classic Sets [2]

For an element in a universe with fuzzy sets, the membership transition can be gradual. So the membership function can take any value between 0 and 1. This transition among various degrees of membership can be thought of as conforming to the fact that the 4

boundaries of the fuzzy sets are vague and ambiguous. Fuzzy membership counterpart for Figure 1 would be that of Figure 2. Hence, membership of an element from the universe in this set is measured by a function that attempts to describe vagueness and ambiguity. In fuzzy logic, linguistic variables take on linguistic values which are words (linguistic terms) with associated degrees of membership in the set.

Figure 2.2. Fuzzy sets Formally, a fuzzy set is defined as a set of pairs where each element in the universe U F = {(x, µF(x)) | x ∈ U, µF(x) ∈ [0, 1]} has a degree of membership associated with it:

µF(x) is known as the membership function of the set F. Most often, one refers to the fuzzy set just by mentioning the membership function, the universe being implicit.

2.1.2. FUZZY LOGIC SYSTEMS:
A FLS receives a crisp input and may deliver either a fuzzy set or a crisp value. The basic FLS contains four components: a rule set, a fuzzifier, an inference engine, and a defuzzifier. Rules may be provided by experts or can be extracted from numerical data. In either case, the engineering rules are expressed as a collection of IF-THEN statements. These statements are related to fuzzy sets associated with linguistic variables. The fuzzifier maps the input crisp numbers into the fuzzy sets to obtain degrees of membership. It is needed in order to activate rules, which are in terms of the linguistic variables. The inference engine of the FLS maps the antecedent fuzzy (IF part) sets into 5

2. This engine handles the way in which the rules are combined. Once membership functions are defined.2.3). and compares it with the stored membership function information to produce fuzzy input values.consequent fuzzy sets (THEN part). only a very small number of rules are actually used in engineering applications of Fuzzy Logic (FL).1. membership functions must first be defined for each input. fuzzification takes a real time input value.3 Fuzzification [4] 6 . Figure 2. In practice.1. To transform crisp input into fuzzy input. FUZZIFICATION: The first step in fuzzy logic processing involves a domain transformation called fuzzification (Figure 2. such as temperature. Crisp inputs are transformed into fuzzy inputs.

In PRODUCT inferencing. Fuzzy rule inference consists of two steps: • Inferencing. which combines the fuzzy subsets for each output variable into a single fuzzy subset. the combined output fuzzy subset is constructed by taking the pointwise sum over all of the fuzzy subsets assigned to the output variable by the inference rule. The computation of fuzzy rules is called Fuzzy Inference. the output membership function is scaled by the rule premises' computed degree of truth.2. Usually MAX or SUM are used. the combined output fuzzy subset is constructed by taking the point wise maximum over all of the fuzzy subsets assigned to variable by the inference rule (fuzzy logic OR). • Composition. In MIN inferencing.2. The precondition can consist of multiple conditions linked together with AND or OR conjunctions.2. Conditions may be negated with a NOT. the output membership function is clipped off at a height corresponding to the rule premise’s computed degree of truth (fuzzy logic AND). Anyone who has written a program or machine code knows how complicated (and difficult to debug. Fuzzy rules offer a way of getting around that by trading the precise representation of the values that variables must assume with much more intuitive fuzzy representations. read. which determines the fuzzy subset of each output variable for each rule. FUZZY INFERENCE: Fuzzy logic based systems use RULES to represent the relationship between observations and actions. and maintain) the if-then lines can get. IF-THEN rules are a common way of representing and communicating knowledge in everyday conversation. In SUM composition. In MAX composition. Usually only MIN or PRODUCT are used as inference rules.1. 7 . These rules consist of a precondition (IF-part) and a consequence (THEN-part).

2. DEFUZZIFICATION: This stage is used to convert the fuzzy output set to a crisp number.4 Fuzzy Inference Process [4] 2. In general a rule by itself does not do much.3. Figure 2. In the Centroid method. the crisp value of the output variable is computed 8 . What is needed are a set of rules that can play off one another. In fuzzy logic partial truths are allowed so the consequent is as partially true as the antecedent allows it to be. Two of the more common techniques are the Centroid and Maximum methods.1. The fuzzy inference methodology allows “fair” competition between these rules to produce sophisticated answers using seemingly simple premises.In binary logic the consequent is either true or false.

In the Maximum method.by finding the value of the center of gravity of the membership function. Figure 2.2. Figure 2.1 TRANSMITTER CIRCUIT: Figure 2. Transmitter Circuit [8] 9 . ABOUT MICROCONTROLLERS AND OTHER HARDWARES: 2.2. the crisp value of the output variable is the maximum truth-value (membership weight) of the fuzzy subset.3.5 Defuzzification [4] 2.5 illustrates the complete process with an example.

Features [9]:         Extra high radiant power and radiant intensity High reliability Low forward voltage Suitable for high pulse current operation Standard T–1(ø 5 mm) package Angle of half intensity  = ± 17 Peak wavelength p = 940 nm Good spectral matching to Si photodetectors Applications [9]: Infrared remote control units with high power requirements Free air transmission systems Infrared source for optical counters and card readers IR source for smoke detectors 10 . molded in clear. Therefore these emitters are ideally suitable as high performance replacements of standard emitters.Description [9]: TSAL6200 is a high efficiency infrared emitting diode in GaAlAs on GaAs technology. blue-grey tinted plastic packages. The forward voltages at low current and at high pulse current roughly correspond to the low values of the standard technology. In comparison with the standard GaAs on GaAs technology these emitters achieve more than 100 % radiant power improvement at a similar wavelength.

Absolute Maximum Ratings: Table 2.1 Absolute maximum Ratings of TSAL6200 [9] Basic Characteristics: Table 2.2 Basic Characteristics of TSAL6200 [9] 11 .

TSOP17. – series are miniaturized receivers for infrared remote control systems. RECEIVER: Figure 2. The demodulated output signal can directly be decoded by a microprocessor. is the standard IR remote control receiver series.5 Basic Internal Block Diagram [7] 12 . PIN diode and preamplifier are assembled on lead frame. the epoxy package is designed as IR filter.2.2. It consumes less power. TSOP series is available in frequency range 30-56 KHz.. The output of the receiver can be decoded by a microcontroller. PIN Diode and preamplifier are on the Lead frame and IR filter is in Epoxy package. In the TSOP package. supporting all major transmission codes..Pin Diagram of Receiver [7] Description: The TSOP17.2.4. Figure 2. It is compatible with TTL and CMOS [7].

2.6 Receiver PIN-OUT [7] 2. MICROCONTROLLER PIN OUT: ATMEGA8 MICROCONTROLLER: Figure 2.3. Microcontroller Pin Diagram [6] 13 .7.Figure 2.

3.7-5.5 volts [6]. 512K EEPROM.Description: low-power Atmel 8-bit AVR RISC-based microcontroller combines 8KB of programmable flash memory. and a 6 or 8 channel 10-bit A/D converter. The device supports throughput of 16 MIPS at 16 MHz and operates between 2. 1KB of SRAM. Key Parameters of Atmega8: [6] Table 2.sensor LCD Max I/O Pins On-chip RC Oscillator Debug VALUE 8 bit 8 Included Yes 512 1K 2 1 0 No No 23 1 MHz+cal 1/2/4/8 MHz No 14 . Key parameters PARAMETERS AVR Core Flash (Kbytes) Boot Code Self Program Memory EEPROM(bytes) SRAM(bytes) 8-bit timers 16-bit timer USB Temp.

8.2. COMPLETE HARDWARE CIRCUIT: Figure 2.2. Hardware circuit [8] 15 .4.

1. the question of operational 3.1 Technical Feasibility: The project requires the knowledge of various hardware components as well microcontroller programming and fuzzy logic as well.2 Economical Feasibility: The project cost includes only the hardware component’s cost which is quite nominal. The SRS is a document that completely describes what the proposed software should do without describing how the software will do it. And. THREE PHASES OF FEASIBILITY STUDY 3. So.1. REQUIREMENT ANALYSIS A requirement is a condition or capability that must be met or possessed by a system to satisfy a contract.CHAPTER 3 FEASIBILITY STUDY AND REQUIREMENT ANALYSIS 3. 3. This phase ends with the Software Requirements Specifications (SRS).1. specification or other formally imposed specification of the client.1. standard. 3. the software like AVR Studio for microcontroller programming can be downloaded from the Internet free of cost.3 Operational Feasibility: feasibility does not arise. 16 . The project is fully automated as it is controlled with the help of microcontrollers. These can be easily learnt from any tutorials.2.

600 MHz.63 GHz. Transmitters Receivers ATMEGA 8 AVR Microcontrollers. 3.  Microsoft Word for preparation of Report.2 HARDWARE REQUIREMENTS:         Processor: PC with a Pentium IV-class processor. Recommended: Pentium IV-class.2. rule viewer and Output in Fuzzy logic. RAM: Windows XP Home -512 MB RAM. 1.2.3.  WIN AVR as compiler for microcontroller programming.  MATLAB for making graphs for membership values. Dummy Vehicles Burner LEDs 17 .1 SOFTWARE REQUIREMENTS:  AVR Studio as editor for Microcontroller programming.

If there are number of alternatives. DATA FLOW DIAGRAMS (DFD): The Traffic light controller system calculates the current cycle time. The program burnt in microcontroller should be in . 2. Firstly. It is the most important part in the development part in the development of the system. the sensors senses for the vehicle and pass the IR rays to the receiver which in turn acts as an input 18 . hardware specifications. and data management. Any design has to be constantly evaluated to ensure that it meet its requirements. database design.C format. 3. 4. AVR Studio should be able to save the file in . as in the design phase the developer brings into existence the proposed system the analyst through of in the analysis phase.1.2. design tells ‘HOW’ to do it.CHAPTER 4 SYSTEM ANALYSIS & SYSTEM DESIGN Requirement analysis defines “WHAT” the system should do. is practical and workable in the given environment. then all alternatives must be evaluated and the best possible solution must be implemented. 4. SPECIFICATION OF PROJECT: The proposed system should have following features: 1.1.2 SYSTEM DESIGN: System Design is the technique of creating a system that takes into notice such factors such as needs. performance levels. The fuzzy rule base should be made in general. This is the simplest way to defines system design. 4.HEX format.

2.3.1.2 1st Level DFD 4. the fuzzy logic approach is burnt which helps us to calculate the current cycle time.1. In the microcontroller.3 2nd Level DFD 19 . TWO LEVEL DFD:  senses Input to the transmitter: INCRE MENTS COUNT FOR CARS INPUT TRANS MITTER RECEIV ER MICRO CONTR OLLER Figure 4.1.1 Zero Level DFD 4. 4. ONE LEVEL DFD: INPUT SENSORS MICROCO NTROLLER OUTPUT Figure 4.1.2. ZERO LEVEL DFD: INPUT TRAFFIC LIGHT CONTROLLER SYSTEM OUTPUT Figure 4.2.2.to the microcontroller.

2. Calculation of current cycle time using fuzzy logic: No. FLOWCHART: For burning a program to microcontroller: Figure 4.2.5 Flowchart depicting burning of a program to microcontroller 20 . Fuzzifica tion Rule base evaluation Defuzzific ation Current cycle time Figure 4.4 2nd Level DFD 4. of cars behind red and green light.

CHAPTER 5 PRESENT DAY TECHNIQUE The most commonly used technique for traffic light controller is based on microcontrollers. which controls four sets of traffic lights at the traffic crossing. Another commonly used technique is to prefix the cycle time to a constant value and the same value is being rotated to all the four sided of traffic for the on and off timings of green and red lights [3]. The timings for the red and green lights are prefixed according to the normal level of traffic at that crossing as per the earlier experience or past records. 21 . They use previous years data for the place and at particular time to set the on and off timing of the red and green lights.

[3].2. In the same way.   The first sensor behind each traffic light counts the number of cars coming to the intersection. 3. 6.2. ALGORITHM: 1. a mathematical model for this is possible.1. The human operator observes the traffic density in different directions and then changes the timing keeping in mind the fair rotation of the turns. The second sensor counts the cars crossing the traffic light.1. Take input from sensors. 2. However. For the output produced in step 4. 5. it controls the traffic according to the real-time situations of the traffic at that moment of time. Calculate density. 6. ASSUMPTIONS: 6. METHODOLOGY: This is explained with reference to the Fig. The technique controls the traffic in the same way as humans do. a mathematical model for this is enormously difficult to find. 6. find its crisp value. 4.1. Obviously. Use fuzzy rule to infer the fuzzy output. 22 . Calculate membership values for different values. The optimal way would be to pass more cars at the green interval if there are a few cars behind the red light.CHAPTER 6 PROPOSED TECHNIQUE The technique described above is a typical non-flexible technique.

FUZZIFICATION:  For input One (Cars behind the red light) Levels Zero Low Medium High Ranges (0-4) (0-7) (4-10) (7-10) 23 . 6.2. Figure 6. cars behind the green light and the cars behind the red light. Illustration of control of traffic light using the proposed technique  The fuzzy decision process uses the two inputs.2. This is done by calculating the number of cars between the path and dividing it by the total distance D. The probability of change of the current cycle time is dependent on the above two inputs [3]. The distance D is used to determine the maximum density of cars allowed to wait in a crowded situation.1.   The distance D is kept constant.

2.3 FUZZY RULE BASE: If Cars behind red light ZERO ZERO ZERO ZERO LOW LOW LOW LOW MEDIUM MEDIUM MEDIUM MEDIUM HIGH AND AND AND AND AND AND AND AND AND AND AND AND AND Cars behind Then ZERO LOW MEDIUM HIGH ZERO LOW MEDIUM HIGH ZERO LOW MEDIUM HIGH ZERO Current green light cycle time SHORT MEDIUM LONG VERY LONG SHORT SHORT MEDIUM LONG SHORT SHORT MEDIUM MEDIUM SHORT 24 . For input Two (Cars behind the green light) Levels Zero Low Medium High Ranges (0-4) (0-7) (4-10) (7-10)  For output (Current cycle time) Levels Short Medium Long Very Long Ranges (5-15) (5-25) (15-35) (25-35) 6.

Membership graph for cars behind red light 25 .2.1 Fuzzy rule base 6.HIGH HIGH HIGH AND AND AND LOW MEDIUM HIGH SHORT MEDIUM LONG Table 6.2.4. CALCULATING MEMBERSHIP VALUES FOR INPUT AND OUTPUT:  Membership graph for cars behind red lights: Figure 6.

3. If no. of car=(7-10) Then membership value of x in medium= (10-x)/3 And membership of x in high=(x-7)/3  Membership graph for cars behind green lights: Figure 6. If no. Membership graph for cars behind green light 26 . If no. Formula for fuzzification[4]: 1. of car=(4-7) Then membership value of x in low=(7-x)/3 And membership of x in medium=(x-4)/3 3. of car=(0-4) Then membership value of x in zero=(4-x)/4 And membership of x in low=(x-1)/4 2.

Membership graph for cycle time 27 . If no. of car=(7-10) Then membership value of x in medium=(10-x)/3 And membership of x in high=(x-7)/3  Membership graph for cycle time: Figure 6. of car=(4-7) Then membership value of x in low=(7-x)/3 And membership of x in medium=(x-4)/3 3.4. If no. Formula for fuzzification[4]: 1. of car=(0-4) Then membership value of x in zero=(4-x)/4 And membership of x in low=(x-1)/4 2. If no.

If cycle time=(5-15) Then membership value of t in low=(15-x)/10 And membership of x in low=(x-5)/10 2.1.2.3.2. Formula for fuzzification[4]: 1.5 DEFUZZIFICATION: The defuzzification Technique we have used in our project is Center of Gravity method which is explained in Section 2. If cycle time=(25-35) Then membership value of t in low=(35-x)/10 And membership of x in low=(x-25)/10 6. 28 . If cycle time=(15-25) Then membership value of t in low=(25-x)/10 And membership of x in low=(x-15)/10 3.

1. INPUTS: 1.1 Inputs And Output 29 . Cars behind Green Light: 3 Figure 7.CHAPTER 7 EXPERIMENT AND RESULTS FOR EXAMPLE: 7. Cars behind Red Light: 6 2.

0)=0 Rule 4: min(0. membership in medium=0.33. membership in low=0.0)=0 Rule 12: min(0.33 And.25)=0 Rule 14: min(0. they lie in the range (4-7).0.5 Rule 11: min(0.0.0)=0 Rule 9: min(0.25 Rule 6: min(0.66.0)=0 Rule 13: min(0.33 Rule 7: min(0.0)=0 30 .0.0)=0 Rule 5: min(0.7.0)=0 Rule 8: min(0. FUZZIFICATION:  For cars behind red light. RULE BASE EVALUATION: Rule 1: min(0.33.2.5)=0.0.66  For cars behind green light.66.25)=0 Rule 2: min(0. membership in zero=0.5)=0 Rule 15: min(0.25 And.25)=0.0.5 7.33.33.66. So.5)=0 Rule 3: min(0. they lie in the range (0-4) So.0.3.0.25 Rule 10: min(0.25)=0.66.0)=0 Rule 16: min(0. membership in low=0.5)=0.0.

So. in the rule base evaluation step we can see that Rule no. current cycle time will be short and will fall in the range (5-15). with the help of the above graph. So.7. we can say that the current cycle time is 8. 31 . DEFUZZIFICATION: The technique used for defuzzification is Center of Gravity.42 seconds. 6 will be applied according to the Center of gravity approach. according to rule 6.4.2 Rule Viewer Graph and Output So. The exact value can be shown by the graph below: Figure 7.

e. and the cycle time for both lanes is same.42)/20)*100 = 58% Hence. Although this time is very large for only 3 cars to pass. the cars behind the green light is low. we can see that the waiting time is reduced using the fuzzy logic approach. So. 32 . Now. % reduction in waiting time =(waiting time using((present day technique-fuzzy logic approach)/present day technique)))*100.42 seconds. So. i.5 COMPARISON WITH THE PRESENT DAY TECHNIQUE: In present day technique. % reduction in waiting time in the given example = ((20-8. according to our given input. the rest of the time is wasted in this case. let’s prefix the cycle time to 20 seconds for all the four sides.7.. 20 sec. Cars behind the green light=3 Cars behind the red light=6 As we can see that. So. the waiting time for cars behind the red light would only be 8. the cycle time is kept fixed. the waiting time for cars behind the red light is 20 seconds. If the fuzzy logic approach would be used. according to our assumption.

i. If two or more cars are made to move parallel across the lane. 33 . FUTURE WORK: The further work could be to remove the limitation of the project.2. to find a technique such that it works well if two or more cars are passed parallely. . this is a proposed theoretical paper which has only been implemented as a prototype. The maximum and minimum limits of the cars behind the red and green lights can be altered without having the need to change the structure of the rule set [2]. Another work could be to use the image capturing technique to count the number of cars instead of sensors.e. Presently. then only one car would be sensed by the sensor. In our proposed model. It is able to count the exact number of cars if two or more cars are passed parallely.1. This would require the study of digital image processing. the cars are made to run in a serial manner.CHAPTER 8 CONCLUSION The system proposed here is very flexible. 8. 8. LIMITATION OF OUR PROJECT: 1. This is very promising application of fuzzy logic in practical areas and can reduce the average waiting time relative to the conventional traffic light controller.

[5]. (2003). [6].com. www.com/datasheet/T/TSOP17. Fuzzy logic and Genetic Algorithms. Case Study: A Smart Traffic Light Controller. Ganesh: Introduction to fuzzy logic and fuzzy sets.datasheetcatalog. Neural Networks.http://www. 202-219. M. Levinson. 11-1:75-87. D. [8].Synthesis and Applications.atmel. [7].asp?category_id=163&fami ly_id=607&subfamily_id=760&part_id=2004&ListAllAttributes=1. Shahariz Abdul Aziz and Jeyakody Parthiban. Fuzzy control of traffic lights. Data Sheet Search Site: www. [9].com/dyn/products/product_parameters. Transportation Research Part C: Emerging Technologies. The value of advanced traveller information systems for route choice. Robert Hoyer and Ultrich Jumar: Institute for Automation and Communication Madgeburg Bahnhofstr. www.alldatasheet. 169-199 [3].com.atmel. [2].REFERENCES [1]. 34 . [4].

APPENDIX Source code for counting the cars: #include<avr/io. char cur_out = PINB & 0x02. char pre_out = 1. 35 . unsigned int car_out = 0.h> int main(void) { DDRD = 0xFF. //set port B for input DDRC = 0xFF. //set port D for output DDRB = 0x00. PORTD = 0x00. PORTC = 0xFF. char cur_in = PINB & 0x01. char diff = 0. char pre_in = 1. while(1) { //for counting the incoming car if(pre_in == 1 && cur_in == 0) { car_in++. unsigned int car_in = 0. pre_in = 0.

PORTC = 0x01. if(cur_out == 2) { pre_out = 1. PORTC = 0x00. //tranfering the difference to the output port //i.PORTC = 0x00. } cur_in = PINB & 0x01. pre_out = 0. } 36 . PORTD PORTD = diff. } //calculating the number of cars diff = car_in . } //for counting the outgoing car if(pre_out == 1 && cur_out == 0) { car_out++.e. PORTC = 0x01.car_out. } return 0. if(cur_in == 1) { pre_in = 1. } cur_out = PINB & 0x02.

Snapshot: Figure 7.3 Hardware circuitry 37 .

sy2. void signal(char .h> #define RED 1 #define GREEN 0 typedef struct RULE{ float val.dew.size1. char st.wy1. errorcode.sy1.int .wx2.y1.e.medium etc int density_EW(int . //st: status output i.sx1.char).x1.y.ny2. int main() { /* request auto detection */ int gdriver = DETECT.sizens2.int).w.y2. int density_NS(int. int n.sx2.nx2. int defuzzy(RULE[]). float min(float. float fuzzy(int).h> #include <conio.midx. ymax. short. 38 .wy2.e.ny1. int xmax.gap=10.h> #include<dos.char). int gettime(int .x. gmode.x2.h> #include<alloc.dns.nx1.sizens1.midy.currtm.j.k.h> #include <stdio.s.h> #include <stdlib.char .Source code of simulation: #include <graphics. }.wx1.float).int).h> #include<math.i. unsigned int size.

. delay(1000).... cleardevice(). exit(1). "c:\\tc\\bgi"). 39 . grapherrormsg(errorcode))..void *imageew.. getch()... printf("\n\n\n \t\t\tLOADING..... setbkcolor(5)... /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n". delay(1000).*imagens... /* initialize graphics and local variables */ initgraph(&gdriver. setbkcolor(10)."). printf(". } setcolor(getmaxcolor())... char status.. printf("... printf("\n\n\n\n \t\t\tPRESS ANY KEY TO CONTINUE... delay(1000). printf("Press any key to halt:").... /* read result of initialization */ errorcode = graphresult().1). gotoxy(1."). &gmode...").").. getch(). printf(".. delay(1000).. xmax = getmaxx().\n").

midx-30.s).2). //VERTICAL LINE line(midx-40.0.midy.midx+40.midy-30).midx+40. setlinestyle(0.midy).midx-40.midy+40.&n.3. dew=density_EW(e. midx=xmax/2.midy+40.ymax).3). midy=ymax/2. status=getch(). line(0.ymax = getmaxy().midx-40.midx.midy-40).w).midy+40.midy).3.midx.midy. if(dew>10 || dns>10) 40 .midy-40).&s.midy-40.3). line(0.midx-40.midx-40. line(midx+40.ymax). dns=density_NS(n. line(midx+40. scanf("%d%d%d%d".xmax. line(midx+40.2. line(0.midy+30.midy-40. printf("ENTER STATUS OF NORTH SOUTH g/r").ymax).&e.midy+40. setlinestyle(2. line(midx. line(midx+30.0. line(midx-40.midy-40).midy+40). line(midx+40. //HORIZONTOL LINE setlinestyle(0.0.xmax.&w).midy-40).midy+40). line(midx. printf("\nENTER THECARS NORTH SOUTH EAST WEST").xmax.

} if(status=='g') printf("\ncycle time for NORTH SOUTH %d". currtm=gettime(dns. else printf("\ncycle time for SOUTH WEST IS %d".dew. } else { printf("\nENTER THE VALID STATUS"). x1=midx+40. exit(0). // move the cursor to other side if(status=='g') { printf("\nNORTH SOUTH STATUS :-> GREEN ").currtm).'r'.{ printf("\nENTER THE NUMBER OF VEHICHLE \nIN BETWEEN 0 AND 10 :"). //currtm is the current cycle time } else if(status=='r') { printf("\nNORTH SOUTH STATUS :-> RED").'r'. /* logic for moving the car */ size=(midx-70)/5.dew.dns). currtm=gettime(dew.dns. 41 // vehichle size forEAST WEST . sizens1=(midy-70)/5.currtm).'g'). } printf("\t\t\t\tdew=%d dns=%d". exit(0).'g').

x2=midx+40+size. setcolor(14). y1=midy+5.y1. ny2=midy-40.i<e. wy1=midy-30. getimage(x1-4.midy+25).midy+15. y2=midy+25. wx1=midx-40-size.imageew). // sets current fill style and fill color rectangle(x1.midy+5. // fill the area in which x y are bounded by RED size1 =imagesize(x1. nx2=midx+25. ny1=midy-40-size.14).y1+50).y2+3.x1+50. //saving the squre region in memory in which circle lies for(i=0.y1-4. //NORTH DIRECTION nx1=midx+5. sy2=midy+40+size. wx2=midx-40. //SOUTH DIRECTION sx1=midx-25. sy1=midy+40. imageew=malloc(size1). setfillstyle(1. floodfill((x1+x2)/2. sx2=midx-5. wy2=midy-10.x2+4.RED).i++) 42 .x2.

i<s.imagens. // FOR NORTH SOUTH gap=15.'n').imagens).ny2). for(i=0.OR_PUT). // MOVING THE CARS for(k=0.i++) putimage(sx1-5. 43 //saving the squre region in .i++) putimage(midx+5.COPY_PUT).i++) putimage(wx1-i*(size+gap). rectangle(midx+5. getch().sy1+i*(sizens1+gap).14). while(!kbhit()) { gap=15.ny2+4. // fill the area in which x y are bounded by RED sizens2 =imagesize(nx1.i<w.OR_PUT). for(i=0.k++) { if(status=='g') { signal('g'.nx2+4. imagens=malloc(sizens2).midy-30-size.(ny1+ny2)/2.i<n.imageew. floodfill((nx1+nx2)/2.ny1.k<3.y1-4.nx1+50.imagens.wy1.COPY_PUT).midx+25.ny1-4.imageew. getimage(nx1-4. memory in which circle lies for(i=0.putimage(x1+i*(size+gap).ny1-5-i*(sizens1+gap).ny1+50).

while(!kbhit()) { gap=10.imageew.wy1. for(i=0.OR_PUT).i++) putimage(wx1-i*(size+gap).'e').imagens.imagens.i<w.XOR_PUT).XOR_PUT).sy1+i*(sizens1+gap). ny1=ny1+sizens1/2. // Waits for few Seconds. delay(1000). // clears the Image from Screen 44 .imageew. // clears the Image from Screen sy1=sy1-sizens1/2. for(i=0.i++) putimage(midx+5.i<e.XOR_PUT). for(i=0.imagens.ny1-5i*(sizens1+gap).i++) putimage(midx+5. } status='p'.sy1+i*(sizens1+gap).OR_PUT).imagens. getch().i<n.for(i=0. for(i=0.i<s.ny1-5i*(sizens1+gap). } else { signal('r'.i++) putimage(sx15.i++) putimage(sx15.i++) putimage(x1+i*(size+gap).XOR_PUT).y1-4.i<s. for(i=0.i<n.

// Waits for few Seconds. } status='g'. } int gettime(int dns.int w) { return (e+w). delay(1000). char stns.i<w.x1=x1-size/2+10.OR_PUT).y14.OR_PUT).imageew. wx1=wx1+size/2-10.int s) { return (n+s).wy1. } } return 0.i<e. } int density_NS(int n. // Puts the image on screen. } int density_EW(int e.int dew. for(i=0.i++) putimage(x1+i*(size+gap).char stew) { 45 . for(i=0. getch().i++) putimage(wx1i*(size+gap).imageew.

0.rfm=0.rflo).0)/4.gfz.0)/4.rfm. 46 //gfz->in g: red f:fuzzy z: zero //rfz->in r: red f:fuzzy z: zero . rfla=(dns-7.gflo=0. printf(" \ndns\tlow=%f\n\t medium=%f\n".0.rfla=0.gfla=0.0.rfz. rfm=(dns-4.0.0-dew)/3.0-dew)/4. printf("\ndns\t medium=%f\n \tlarge=%f\n". } if(dns>7 && dns<=10) { rfm=(10.0.0-dns)/4.0. gflo=(dew-0.0)/3. } if(dew>0 && dew<=4 ) { gfz=(4. RULE r[15].0.0.rfm).rflo.0. } if( dns>4 && dns<=7 ) { rflo=(7.gfz=0.gfm=0. rflo=(dns-0.gflo). printf("\ndns \t zero=%f\n \tlow=%f\n".0.0)/3.rflo=0.0-dns)/3. } if( dew>4 && dew<=7 ) { gflo=(7. printf("\ndew\tzero=%f\n \tlow=%f\n".0.0.0.0.0-dns)/3.0. if(dns>0 && dns<=4 ) { rfz=(4.0.0.int ct=0. float rfz=0.rfla).

r[14].gfla).val=min(rfz.st='l'.gfla). r[1].st='m'.st='s'.r[6]. r[16]. printf("\ndew \t medium=%f\n large=%f\n". ct=defuzzy(r). r[2].val=min(rflo.gfm). r[7]. r[13].0. gfla=(dew-7.gfla). r[5]. r[12]. r[14].0)/3.gflo).gfla). r[12].gfm). r[9]. r[3]. r[5].st='v'. r[8]. r[3]. //now defuzzyfication using fuzzy OR operation 47 .st='s'.val= min(rflo.val= min(rfm.gfm).val= min(rflo.val= min(rfla.gfm).st='s'. r[15]. r[16]. } if(dew>7 && dew<=10) { gfm=(10. r[10].val= min(rfz. r[9].val= min(rfm. r[10]. } getch().gflo). r[1].0.st='s'.gfz).st='m'.gflo.gfz).st='m'.gfz).st='s'.gfz).gfm=(dew-4.0-dew)/3. r[2].val= min(rfla.val= min(rflo.gfm).val= min(rfla. r[7]. r[8]. printf("\ndew \tlow=%f\n medium=%f\n".0)/3.0.val= min(rfla.st='m'.st='s'.gflo). r[4]. r[6].val= min(rfm. r[11].st='l'. r[15]. r[11]. r[4].st='l'.st='s'.gfla).val= min(rfz. r[13].val= min(rfz.gfm.gflo).st='m'.val= min(rfm.

cg=0.x1=0.0. y=0.0.0. x=0. } float min(float value1.0. ct=0.h2=0.val.0. for(i=1. p=0.0.i<=16.0.val.0 . } } p=r[index].0 .0. float value2) { if(value1>=value2) return value2.0 .x2=0.st=='l') { 48 //ct cycle time // use algorithm to find the (FUZZY OR) maximum . x3=0.st=='m' || r[index]. //return ( (value1 < value2) ? value1 : value2). } int defuzzy(RULE r[]) { int i.0.index. max=r[i]. else return value1.i++) { if(r[i].val>max) { index=i. } float fuzzy(int density) { return 0. h1=0. float max=0.h3=0.return ct.0 . if(r[index].

0.0*h2+h3).st=='l') { x=15.0.if(r[index]. x3=h3/3. } if(r[index]. y=25. } //slope=2/(y-x) if(r[index]. printf("\n\n\n\n\n\nfuzzy output:-> s").0.y=35. } if(r[index]. } h1=p*(y-x)*.0. printf("\n\n\n\n\n\nfuzzy output:-> l").0.st=='s') { x=5. cg=(h1*x1+2.0. h3=p*(y-x)*.5.y=15.0. x2=h2*0.0.st=='m') { x=5.5.0.0.0*h2*x3+h3*x3)/(h1+2.st=='v') { x=25.5.0-p)*(y-x).st=='v') { if(r[index]. printf("\n\n\n\n\n\nfuzzy output:-> m"). 49 . y=35.0/3. x1=h1*2. h2=(1.st=='s' || r[index].

ct=(int)ceil(cg). } void signal(char sign. midx=getmaxx()/2.5. if(r[index]. h2=p*(y-x).0. } if(r[index]. cg=(2.0*h1+h2).0*h1*x1+h2*x2)/(2. if(r[index]. x2=h2/3. } h1=(1. midy=getmaxy()/2.printf("\n\n\n\n\n\nfuzzy output:-> v").0.0.midy.st=='v') cg=cg+25. // 2 for green signal 4 for red signal if(sign=='g') { 50 //signal is either green or red dir is the direction //slope=1/(y-x) .0. if(r[index].st=='m') cg=cg+5.st=='l') cg=cg+15. x1=h1*0.0.0-p)*(y-x).char dir) { int midx.st=='s') cg=cg+5. return ct.

setfillstyle(1. floodfill(midx-50.midy+50.RED). circle(midx+50. floodfill(midx-50.midy-50. floodfill(midx+50.10). circle(midx-50.midy+50.midy-50. floodfill(midx+50.4). floodfill(midx-50. } 51 . setfillstyle(1.10).midy-50.14).setfillstyle(1.midy-50.midy+50.midy-50.14). } else { setfillstyle(1.2). circle(midx-50. circle(midx-50.10).14).4).10). //NORTH SOUTH DIRECTION circle(midx+50.midy-50.midy+50. } setcolor(14). circle(midx+50. circle(midx-50. circle(midx+50.10). floodfill(midx+50.midy-50.midy+50.10).midy+50.midy+50. setfillstyle(1. floodfill(midx+50.10).14).14).14). floodfill(midx-50.2).14).10).midy-50.midy+50.14).