IMITATION OF FUZZY LOGIC CONTROLLER BASED ARTIFICIAL NEURAL NETWORK, AND APPLICATION OF INVERTED PENDULUM SYSTEM CONTROL

Fuzzy Logic Controllers (FLCs) are effective solutions for nonlinear and parameter variability systems, but it contains multiple mathematical operations causing the controller to react slowly. This study aims to obtain a controller that can imitate the effective control performance of the FLC, which is easy to design both in software and hardware, and has a short response time. Artificial neural networks (ANNs) provide effective solutions in system modeling. Modeling of FLC has been realized by using of ANN’s learning and parallel processing capability. The design process of the FLC and the training processes of the ANN were studied in Matlab SIMULINK environment. In the study, FLC was modelled at high similarity ratio with small ANN structure. ANN results were obtained very faster than the FLC control performance. The control performances of two controllers were observed to be very close to each other. As a result, ANN model has smaller structure than FLC, which makes it possible to implement the controller easily in terms of hardware and software.


Introduction
Today, with the development of technology, some demands in our daily activities such as speed, safety and comfort are increasing. For example, while people aim to only travel between two cities in the early ages, they are now looking for solutions to how they can perform faster, safer and more comfortably travelling. To ensure a faster, safer and more comfortable travelling, advanced and often complex control systems are required.
There is a relationship between an input signal applied to a controlled system and the system's response to this input signal. This relationship, sometimes linear or nonlinear, is known as the transfer function of the system and can be formulated with a mathematical expression.
to the error rate of the returned data and form the most suitable structure. Once this structure is established, the system can properly check for new incoming data. This method is used in many different areas.

Related works
ANN provides effective solutions in system modeling [9]. Although FLCs are effective in many applications, both of the software and hardware design of a FLC's presents a number of difficulties. In addition, the controller contains multiple mathematical operations causing the controller to react slowly. In the literature, there are only few studies regarded with modeling of FLCs by ANN in control applications.
Diaz and Soriano [10] reported that DC-DC converters are high-efficiency tools used to power different systems, have a nonlinear behavior and may affect the stability of differences in the main parameters. In this study, different control strategies, linear and nonlinear controllers applied to a Buck converter are examined and compared. There are three main control strategies discussed in the study. First, an optimal control-based design was used using Quadratic Performance Index, the second was a knowledge-based fuzzy control, and thirdly an ANN was proposed as a dynamic emulator of fuzzy control. Some comparisons have been made on the variation of several factory parameters about the systems created by the plant and the controller. In addition, the calculation time in the simulation was compared between the two intelligent controllers.
In another study, fractional order PID (PI λ D μ ) control was considered. This is a challenge for real-time applications, since the implementation of fractional-order operators is achieved with integergrade transfer functions. In the present article, mathematical difficulties are mentioned in the design stage of the PI λ D μ controller and ANN design issues that will give the same performance as the PI λ D μ controller are examined [11,12]. Önder et al., used ANN for predict of nonlinear system dynamics [13].
This study aims to imitation of the FLC by using ANN to be easy to design both in software and hardware, and has a short response time. For the imitation process ANN is used. The reason for using ANN is that it has parallel operation feature. Parallel processing enables the processing speed to be increased. This improves the control performance of the controller and allows for easy design. The design process of the FLC and the training processes of the ANN were studied in Matlab SIMULINK environment. The performance of the designed controllers has been tested on the inverted pendulum system. In the study, FLC was modelled at high similarity ratio with small ANN structure. ANN results were obtained very faster than the FLC control performance. The control performances of two controllers were observed to be very close to each other. As a result, ANN model has smaller structure than FLC, which makes it possible to implement the controller easily in terms of hardware and software.

Fuzzy logic controller
In the classical set theory, the degree of membership of an element is as crisp as 0 and 1. On the other hand, in fuzzy set theory, a different kind of set definition has been made. In fuzzy sets, membership values can be considered as infinitely real number equivalents in the closed range [0 1] instead of crisp sets. Fuzzy logic control system is actually a structure created by using the sentences we use in our daily life, such as increase the temperature, a little slow down the cart speed. FLCs also fulfills the task of control using an algorithm encoded with terms that do not express such certainty when deciding on a case. The main goal here is to think more humane and to provide a more realistic control [3]. A FLC architecture is shown in Fig. 1. In fuzzy set theory, the degree of belonging of an element to a set is realized by the membership functions. Real-life crisp quantities are passed through membership functions and fuzzy membership degrees are calculated, and these calculated membership degrees and input quantity are included in fuzzy subsets. In fuzzy sets, the degree of membership of an element is indicated by µ. The selection of membership functions on the FLC greatly affects the control performance.
Fuzzification is the process of converting crisp real-life values into fuzzy values. In any fuzzification process, quantities such as speed and temperature are called antecedent variables. These antecedent values applied to some membership functions, and so obtained fuzzy values in the closed [0 1] range. FLC designs use a varying number of membership functions for each antecedent variable. Usually an odd number such as 3, 5 and 7 membership functions are preferred. It is possible to use the same or different membership functions for input and output variables in applications. The fuzzy values from the membership functions are subjected to a number of fuzzy operations in the FLC. These are the rules for the operation of the FLC. The number of these rules is proportional to the number of membership functions. Therefore, as membership functions increase, more rules are needed. The search for the number of rules provides better control results in most cases. As a disadvantage, the increase in the number of rules increases the number of fuzzy processes and therefore makes it difficult to implement FLC both of software and hardware.
In the fuzzy inference stage, the input variable obtained by fuzzification process is applied to the fuzzy operator such as min, max by fuzzy rules. Fuzzy rules are fuzzy instructions prepared with expert experience. These instructions indicate where the fuzzified input variable is directed at the output according to the values it receives in different fuzzy subsets. The realization of a fuzzy rule is the process of subjecting it to operators in fuzzy set theory. The fuzzy result values obtained after the fuzzification and fuzzy inference processes should be converted to the actual control signal applicable to the controlled system. The process of converting fuzzy values into crisp values is called defuzzification process. There are different methods used for defuzzification. Due to the difference in the way these methods work, differences in results may occur depending on the type of study. Expert experience in FLC design may still be required here. Nowadays, the method suitable for the needs of the system in need of control can be tested by simulation and trial studies.

Artificial neural networks
ANN is a very complex nonlinear calculation tool that can model extremely complex functions. It is preferred where it is impossible or very difficult to find any given functional relationship between the sequence of inputs and the corresponding sequence of outputs. ANNs can be represented by means of an appropriately selected architecture and a successful learning result created by ANN [14]. The basic element of ANN is the cells. Cells, the main processing element of ANN, are nonlinear. In this way, it has become one of the most important tools for the solution of complex nonlinear problems and their architecture usually has parallel distributed cells. This parallel structure allows easy learning. Thanks to its generalization feature, it can produce appropriate responses for entrances not encountered. Thus, it can solve even complex problems of unknown function [7].
ANNs are composed of cells. These cells are used in groups in layers. An ANN input layer consists of at least three layers, the hidden layer and the output layer. There are no cells in the input layer. Input signals from the input layer are transmitted to the cells in the hidden layer by weights. The number of hidden layers may be multiple according to the application. The connections between the cells between the hidden layers and the cells in the output layer are carried out by means of weights. The cells in the hidden layer and the output layer are the basic information processing units within the ANN. There are three basic elements in the cell model. These are synapses, collector and activation functions. The synapses between the cells consist of directed connections, and these connections have weights. The collector is used to collect the inputs. The activation function is used to process a neuron output in a way that is necessary to update weights [15].
There are several network architectures for ANN. Structuring the neurons of a neural network is closely related to the learning algorithm used to train the network. In general, network architectures can basically define three different classes of network architecture [16].
One of these is a single layer network structure, in the simplest form it connects directly to a neuron output layer. It is the second class of a feedforward neural network. The compute nodes comprise one or more inter layers, called hidden layers, that contain inter layer cells. The function of the cells in the hidden layers is to interfere in some way between the input and the output. Adding one or more hidden layers can help generate higher-level statistics from the input of the network. A recurrent neural network separates itself from a forward neural network because it has at least one feedback loop. For example, a recurrent network may consist of a single layer of neurons that feeds the output signal of each cell to the inputs of all other neurons. There are no self-feedback loops in the network [16]. In the Fig. 2

Cart and inverted pendulum system
The inverted pendulum is a classical control problem which has been the subject of many articles in order to test the control performance of the designed a controller. As shown in Fig. 3, it consists of a cart that can move linearly along the x-axis and a rod that can move angularly in the x-y axis with a hinge. This system can be likened to a rod held upright in the palm. The inverted pendulum has a nonlinear transfer function and can therefore easily become unstable. A general representation of a cart-inverted pendulum free body diagram is shown in the Fig. 3.   Fig. 3, and Newton's second law; The dynamic equations in the x and y directions of the pendulum are as follows.
However, the x and y position coordinates of pendulum can be represented with equations as following.
In order to get rid of the nonlinearities caused by sin and cos functions in the above equations, the following approaches are made by accepting that the pendulum changes in a little range. ( The transfer functions of the pendulum and the cart from Eq. (21) and Eq. (22) are obtained as shown below.
Where q value;

Simulation studies and results
To perform simulation works, the cart-inverted pendulum model was created using Eq. (23) and Eq. (24) in Simulink. The design of the FLCs was made with Fuzzy Toolbox. The ANN design for the modelling of the FLC was made using Matlab's Neural Network Toolbox.

FLC design
In both FLCs used to control the cart-inverted pendulum, the error (e) and the change of the error (ce) were used as input variables. However, the same input membership functions and the same output membership functions were used in both FLCs. The only difference is the universal set range of variables. The universal set of input and output variables is shown in Tab. 2. The membership functions used are shown in Fig. 4  The rules established according to the membership functions in Fig. 4 are shown in Tab. 3. As can be seen from Tab. 3, there are a total of 25 rules in FLCs. The relationship between the input variables and the output variables according to the rules in Tab. 3 is shown in Fig. 5. The exact design used for FLC training is shown in Fig. 6. As shown in Fig. 6, the position controls of the cart and inverted pendulum are realized as closed loop control structure with two separate FLC units. For FLC_cart, the error of the cart position error and the change of the error of the cart position are used as the input signal, while the change of the error of the pendulum angle and the error of the pendulum angle is used as the input signal for FLC_pend. The outputs of these two FLCs were summed, and this final check mark was intended to move the cart to the desired position and to keep the pendulum upright without falling.

Training of ANN
On the FLC design made on Matlab Simulink, different reference values were given for cart and control was performed. During the control process, input variables (e and ce) and output variables (u) of FLC_cart and FLC_pendulum were transferred from Simulink to the Matlab workspace with the "to workspace" block. Thus, ANN's training set was created. e and ce values are taken as the input variables of the training set and u is taken as the output variable of the training set. The same architecture was used for both FLC_cart and FLC_pend ANNs. Feed forward network structure was used including each ANN, 2 variable input layer, two 5-cell hidden layers and one variable output layer. "tansig" function was used as the activation function in the hidden layers and "purelin" function was used as the activation function in the output layer.
The success rate of the training was measured according to the MSE performance. The training was done with the Levenberg-Marquardt algorithm called "trainlm" in the Matlab. The Levenberg-Marquardt algorithm update derived from steepest descent and Newton algorithms was given in Eq. (26). [17] Where w is the weight vector, I is the unit matrix, is the combination coefficient, J represents the Jacobian matrix in dimension (PxM)xN, e represents the error vector in dimension (PxM)x1. And P is the number of training samples, M is the number of outputs, N is the number of weights. is an adjustable variable. If this parameter is too big, the method behaves like a steepest descent method, if it is too small the method behaves like a Newton method. An adaptive structure for this parameter was given in Eq. (27). [17] In Eq. (27), k is a constant number. E shows the fitness value.
The contents of the ANN block, which ANN block placed instead of the FLC block, were shown in the Fig. 7. These contents were created by Matlab during the ANN's training and then turned into Simulink block with "gensim" function. The Feed-forward network was used as ANN structure. The ANN contains ten cells, two inputs, an output, four layers, three activation functions, collectors, and many weights. The first layer is the input and the last layer is the output. Each layer has biases and also weights coming from the previous layer. The ANN training process is as follows. The regression graph plotted during ANN training is shown in Fig. 9. Here, the collection of data on the regression curve is an indicator of a good training.  The Simulink Diagram was run with the test set, which completely different from training set. The FLC and the ANN controllers were run separately and tested. And the results obtained are as follows. Fig. 11 is a position graph of the cart and it shows the comparison between the control results of the two controllers. Here, the MSE value calculated based on the difference between the cart position controlled by the FLC and the reference position is 10.1312. Similarly, the MSE value calculated based on the difference between the cart position controlled by the ANN and the reference position is 10.1177. As can be seen, the MSE values of both controllers are very close to each other. This shows us that ANN is able to imitate FLC highly rate. Furthermore, the calculated MSE value which based on the difference between cart position control performances of these two controllers is 0.000806, which is very small.  Fig. 11 is the position graph of the cart, and it shows the comparison between the control results of two controllers. In order to make this difference more clarity, the position difference graph of Fig.  12 is obtained by subtracting the position of the cart controlled by the ANN from the position of the cart controlled by the FLC. When Fig. 12 is examined, it is seen that the biggest difference between the control results of two controllers is 15 cm and this difference closes in a short time.   13 is an angle graph of the pendulum and it shows the comparison between the control results of FLC and ANN controllers. Here, the MSE value calculated based on the difference between the pendulum angle controlled by the FLC and the reference angle (0 radian) is 0.0032098. Similarly, the MSE value calculated based on the difference between the pendulum angle controlled by the ANN and the reference angle is 0.0031916. As can be seen, the MSE values of both controllers are very close to each other as cart position. This shows us that ANN is able to imitate FLC highly rate. Furthermore, the calculated MSE value which based on the difference between pendulum angle control performances of these two controllers is 1.595*10 -5 , which is very small.  Fig. 13 is the angle graph of the pendulum, and it shows the comparison between the control results of two controllers. In order to make this difference more clarity, the angle difference graph of Fig. 14 is obtained by subtracting the angle of the pendulum controlled by the ANN from the angle of the pendulum controlled by the FLC. When Fig. 14 is examined, it is seen that the biggest difference between the control results of two controllers is 0.038 radian and this difference closes in a short time.

Conclusion
The ANN can be used for the imitation process of many systems, the FLC is one of them. The ANN has a parallel operation feature and so it runs very faster than the FLC. Further, the ANN design both in software and hardware is easier than the FLC design.
With the using of the data of the FLC's input and output, the trained ANN successfully imitated the FLC and managed to perform the control process under different reference values. According to the position values of the car, the MSE value of the difference between the FLC and the ANN controllers is 0.000806. For the angle values of the pendulum, the MSE value of the difference between the FLC and the ANN controllers was obtained as 1.595*10 -5 .
Since the operations used by ANN are easier than those used by the FLC, while a 300-second simulation was performing with 178.799 real seconds with FLC, the same simulation was performed with ANN in 2.059 real seconds. The system designed with ANN will be easier to use and design in various control applications as it is containing less complex mathematical operations. This study can be improved by using more training data, more cell numbers, more iteration or different training algorithms in the ANN training process.