Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

37
Case project report 2013 Title: Artificial Neural Network and Fuzzy Logic in forecasting short-term Temperature Candidates: Chao Xi (120295) Erihe (120293) Telemark University College Faculty of Technology TFver.0.8

Transcript of Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

Page 1: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

Case project report 2013

Title: Artificial Neural Network and Fuzzy Logic in

forecasting short-term Temperature

Candidates: Chao Xi (120295) Erihe (120293)

Telemark University College Faculty of Technology TFver.0.8

Page 2: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

2

Page 3: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

3

Contents Preface .......................................................................................................................................................... 5

1 Introduction ............................................................................................................................................... 6

1.1Background .......................................................................................................................................... 6

1.2 weather station ................................................................................................................................... 6

1.3 Problem definition and Task overview ............................................................................................... 7

2 Artificial Neural Networks .......................................................................................................................... 7

2.1 Theory part: Artificial Neural Networks algorithm and model ........................................................... 7

2.1.1 BP neural networks ...................................................................................................................... 7

2.1.2 Building a temperature predicting neural network ..................................................................... 9

2.2 Practice part ...................................................................................................................................... 10

2.2.1 Data preprocessing .................................................................................................................... 10

2.2.2 Training the Neural Network ..................................................................................................... 10

3 Fuzzy logic ................................................................................................................................................ 21

3.1 Introduction ...................................................................................................................................... 21

3.2Fuzzy logic design ............................................................................................................................... 21

4 Conclusion ................................................................................................................................................ 33

Reference .................................................................................................................................................... 34

Appendix A .................................................................................................................................................. 35

Appendix B .................................................................................................................................................. 36

Page 4: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

4

List of figures

Figure 1: structure of weather station. ......................................................................................................... 6

Figure 2: system model with four inputs (Wind flow, Relative Humidity, and Temperature) and one

output (temperature). ................................................................................................................................ 11

Figure 3: A two-layer neural network. ........................................................................................................ 12

Figure 4: A simulation result of changing error as different hidden neurons chosen for training network

(1-(10 5), 4-(10 10), 3-(10 20), 4-(20 20)). ................................................................................................... 13

Figure 5: Training performance as different hidden neurons chose for training network (1-(10 5), 4-(10

10), 3-(10 20), 4-(20 20)). ............................................................................................................................ 14

Figure 6: A simulation result of curve fitting as different hidden neurons chose for training network (1-

(10 5), 4-(10 10), 3-(10 20), 4-(20 20)). ....................................................................................................... 15

Figure 7: A simulation result of curve fitting as different days or hidden neurons chose for training

network. ...................................................................................................................................................... 17

Figure 8: the regression plots in different options. .................................................................................... 18

Figure 9: changing curve of error in different options ................................................................................ 19

Figure 10: Training performance in different options ................................................................................ 20

Figure 11: Example of historical data from www.yr.no. ............................................................................. 22

Figure 12: Structure of Fuzzy system. ......................................................................................................... 24

Figure 13: Membership function of input variable “Min”. ......................................................................... 25

Figure 14: Membership function of input variable “Average”. .................................................................. 25

Figure 15: Membership function of input variable “Max” .......................................................................... 26

Figure 16: Membership function of output variable “Temperature”. ........................................................ 26

Figure 17: Surface viewer............................................................................................................................ 29

Figure 18: Rule viewer. ............................................................................................................................... 30

Figure 19: temperature in 17th January and 18th January from www.yr.no. ............................................ 31

Figure 20: result of prediction. ................................................................................................................... 31

Figure 21: temperature in 20th January and 21th January from www.yr.no. ............................................ 32

Figure 22: result of prediction. ................................................................................................................... 32

Page 5: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

5

Preface

This objective is to use neural network topologies to predict hour-by-hour

temperature in one or several days based on the historical weather data.

This paper presents research on weather forecast through the historical database

which implemented in the local weather station at Telemark University College.

Due to non-linearity in climatic physics, neural networks are suitable to predict

these meteorological processes. Back Propagation algorithm using gradient descent

method is the most important algorithm to train a neural network for weather

forecasting. The neural network training was done using MATLAB, and BP

method was also programmed by MATLAB. Also in chapter 3 will use fuzzy logic

for temperature forecasting.

Page 6: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

6

1 Introduction

1.1Background

Weather is a continuous, data-intensive, dynamic and chaotic process. The

parameters required to predict weather are enormously complex such that there is

uncertainty in prediction even for a short period. The property of artificial neural

networks that they not only analyze the data but also learn from it for future

predictions makes them suitable for weather forecasting.

Inspired by the brain, neural networks are an interconnected network of processing

elements called neurons. One of the most popular training algorithms in the

domain of neural networks used so far, for weather forecasting is the back

propagation algorithm. It is a gradient descent method. The algorithm suffers from

many problems.

1.2 weather station

Telemark University College

(TUC) has established a

Weather System for

presenting the weather at

Telemark University College.

The system is showing the

weather data from the weather

station located at TUC. The

system shall be used to

create weather models and forecasting. The weather system has a SDK/API that

makes it possible to retrieve data in different manners, including Web Services.

APIs have also been created for the following languages: C#, LabVIEW,

MATLAB and Python. An OPC API is also available in these 4 languages. These

APIs make it possible to retrieve data from the system and create your own

weather models used in forecasting and weather prediction.

Figure 1: structure of weather station.

Page 7: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

7

1.3 Problem definition and Task overview

All the work is leading to one goal-predicting the temperature for the coming days.

We have historical data of one month, and we are going to make a forecasting for

one day behind the month.

The remainder of the article is organized as follows. Chapter 2 and chapter 3

introduce the back propagation algorithm and fuzzy logic theory respectively. The

details of the integrated BP/Fuzzy Logic technique for weather forecasting model

are shown in chapter 4, followed by results in chapter. Finally, conclusions are

summarized in chapter 6.

2 Artificial Neural Networks

2.1 Theory part: Artificial Neural Networks algorithm and model

In this part we will firstly give a brief introduction to neural networks algorithm,

and then go into the specific task of how neural networks can be trained to perform

a predicting role.

2.1.1 BP neural networks

The Back propagation Algorithm was proposed by Rumelhart, McClelland in

1985[1]. It has been one of the most studied and used algorithms for neural

networks learning ever since. The Back propagation method uses output error in

the output layer to estimate the error in the direct leading layer, and use the leading

layer’s error to estimate the previous layer and further to input layer. In the

learning process, each neuron changes its joint weights according to specific rules

and finally makes output closer to the expected output.

Standard BP algorithm:

In the standard BP algorithm, k is the number of iterations, correction of weight

value and the threshold value according to the following formula for each layer.

)()()1( kgkxkx (1)

Page 8: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

8

Where: x (k) is the connection weights vector or threshold vector for the kth

iteration between the various levels.

)(

)()(

kx

kEkg

is the gradient vector of the neural network output error for each

weight value or threshold values of the kth

iteration. A negative sign indicates the

opposite direction of the gradient descent direction, i.e. the speed gradient.

is the learning rate, a constant in the training process. In MATLAB neural

network toolbox, the default value is 0.01, can be set by changing the training

parameters.

Ek is the total error of the network output performance function of the kth

iteration.

in MATLAB neural network toolbox, the default value of the BP network error

performance function is MSE (mean square error), a two-layers BP network, for

example, given only one input sample,

2

1

222

2

2 )]([1

)]([)(s

i

ii kats

keEkE (2)

})]()))()((()([{

})]()()([{)(

2 1

2

1

2

1

1112

,

2

1

212

,

22

s

j

i

s

j

iiiji

s

j

iijii

kbkibpkifkf

kbkakfka

(3)

The specified procedure as below:

Step 0 Arbitrary initializing values are given to the weights and biases in the

network.

Set each weights value between(-1,1),select error function; give desired

error and the maximum learning rate.

Step 1 randomly select the k input samples and the corresponding desired output

Step 2 calculate each of the hidden layer neurons in the input and output

Page 9: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

9

Step 3 use desired output and the actual output, and calculate the partial derivative

of the error function for each neuron of the output layer.

Step 4 use the hidden layer to the output layer connection weights, the output layer

and the hidden layer output error function of the partial derivatives of the hidden

layer neurons

,2

1

1

N

k

kEN

E : Accuracy (4)

Step 5 use the output layer neurons and the output of the hidden layer neurons to

fix the connection weights.

Step 6 the neurons in the hidden layer and the input layer neurons in the input

correction connection weights. Until:

0

ij

ijijw

Eww (5)

Step 7 calculate the global error

Steps 8 see if the network error meets the requirements or not. When the error

reaches the preset accuracy or learning the maximum number of times greater than

the set, the algorithm ends. Otherwise, select the next learning sample and the

corresponding desired output, return to the third step, learning to the next round.

2.1.2 Building a temperature predicting neural network

The MATLAB code for implementing this BP method can be seen as Appendix A.

A neural Network consists of 3 inputs, two hidden layers with a varying hidden

neurons and 1 output neuron.

Tangent sigmoid function, ‘tansig’, will be used in the hidden layers and the output

layer. ‘Traingdx’ is a network training function that updates weight and bias values

according to gradient descent momentum and an adaptive learning rate.

Page 10: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

10

2.2 Practice part

2.2.1 Data preprocessing

(1) Weather parameters and raw data

The daily weather parameters collected from TUC weather station are shown

in Table I along with their units of measurement. The parameters chosen for

prediction in this setup are mean air temperature (ºC), relative humidity (%)

and wind speed (Km/h) by hour. There is no particular reason behind this

choice of weather parameters. The choice is made just to predict the

temperature variable in one or two days. Seven days data (month of January,

2013) have been used in this research. Different day’s data will be used for

training and different day’s data will be used for testing purposes.

TABLE I

No. Meteorological

Variables

Unit

1 Temperature ºC

2 Relative Humidity %

3 Wind Speed Km/h

(2) Normalization of Data

After the collection of data and selection of the weather parameters, next

issue is normalization of data. Neural networks generally provide improved

performance with normalized data. The use of original data to train the

neural network may cause convergence problem. All the weather data sets

were, therefore, transformed into values between 0 and 1 through dividing

the difference of actual and minimum values by the difference of maximum

and minimum values.

2.2.2 Training the Neural Network

(1) Defining the inputs and outputs

Two layer neural network will be considered for this case. Further neural network

has three inputs and a output as shown in figure 1.1 , where hyperbolic tangent

sigmoid function( tansig) are used correspond to the two hidden layers.

Page 11: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

11

Figure 2: system model with four inputs (Wind flow, Relative Humidity, and Temperature) and one output (temperature).

One external variable to be used as input so that one input to network further 5

neurons are used for hidden layer. There must be one neuron in the output layer

because there is only one target value associated with each input. Proper

parameters (weight and biases) are selected by training the network and in this case

back-propagation technique was used. Using the MATLAB code in Appendix-1,

neural network was trained and results are shown below. To train the network

following steps were used.

Create the network.

Configure the network.

Initialize the weights and biases.

Train the network.

(2) Choosing the architecture of the neural network

Here we are going to only discuss number of hidden layers and number of nodes of

hidden layers. This is because these two characteristics determine the size of neural

network. And the size highly correlated to the performance of the network. Bigger

the size, more are the free parameters. And finally this comes to a problem of

overfit. For the learning rate we choose 0.01 in this case, as here we want to make

sure a stable training system.

Page 12: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

12

Figure 3: A two-layer neural network.

How many hidden layers and how many nodes in the hidden layers [2]?

As based on improving convergence: When begin the model building, error on the

side of more nodes in the hidden layer. Generally, the number of hidden neurons

primarily depends on the number of training samples (more training samples

implies that more neurons can be used) and the complexity of the output function

(more complex output functions often require more neurons)[3]. The best way is

still to test the different topologies and find the one minimizing the error.

It is very difficult to choose the number of neurons in a hidden layer, and to choose

the number of hidden layers in your neural network. Usually, for most applications,

one hidden layer is enough. Also, the number of neurons in that hidden layer

should be between the number of inputs and the number of outputs. But the best

way to choose the number of neurons and hidden layers is experimentation. Train

several neural networks with different numbers of hidden layers and hidden

neurons, and measure the performance of those networks using cross-validation.

You can stick with the number that yields the best performing network.

We are going to explore the appropriate number of neurons in hidden layer in this

experiment. Based on 7 days (24*7 samples each variable as one mean value in

one hour) weather samples and to predict 1 day (hour by hour) behind. We choose

the data as the estimation will be more precise if we narrow the time period.

110 neurons in first layer and 5 neurons in the second

210 neurons in first layer and 10 neurons in the second

310 neurons in first layer and 20 neurons in the second

Page 13: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

13

4 20 neurons in first layer and 20 neurons in the second

Figure shows the change of error for the four options. the most

concurrencies of predicted error is about 0.5℃ in 1, and 0.5℃, 0.3℃,

0.2℃We can find as the number of neurons increase, the error is decreased.

4 is relatively better than others, no error bigger than 1℃ and small

concurrencies of the bigger error.

Figure 4: A simulation result of changing error as different hidden neurons chosen for training network (1-(10 5), 4-(10 10), 3-(10 20), 4-(20 20)).

1 2

3 4

Page 14: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

14

Figure shows the training performance of the four options. it’s easy to find

the best MSE stays at 0.001 for all of them. 1 shows a little better

convergence and 2 reaches its best performance at epoch 78. While at least

2000 epochs are needed to achieve the same performance for 3 and 4.

Apparently, more neurons used, longer time and of higher complexity the

training network can be. Yet it’s really shortage of information here.

Figure 5: Training performance as different hidden neurons chose for training network (1-(10 5), 4-(10 10), 3-(10 20), 4-(20 20)).

1 2

3 4

Page 15: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

15

Figure 6 shows more neurons chose, better curve fitting.

Figure 6: A simulation result of curve fitting as different hidden neurons chose for training network (1-(10 5), 4-(10 10), 3-(10 20), 4-(20 20)).

1

4 3

Page 16: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

16

By analyzing these figures, we can come to some conclusions like below.

* More neurons, less predicted error.

* More neurons used, longer time and of higher complexity the training

network can be.

*MSE shows a low correlation with number of hidden neurons. To some

degree, to step over a specified order of magnitude.

* More neurons, higher predictive ability.

From the experiments, we come to end that given small size of samples, less

neurons will be preferred to be put into use. Yet that also depends on such as

which algorithm used, the size of samples, inputs and outputs, etc.

(3) Training the network

In the last section, no matter how many neurons we try, the best MSE always stay

at about 10-2

compared to the true order of magnitude 10-5

. So in this section, we

are going to use less or more historical data to find the fittest mode in this

forecasting.

1. one day as training data and to predict 24 hours in one day (N1=5, N2=10)

2. three days as training data and to predict 24 hours in one day(N1=5, N2=10)

3. thirty days as training data and to predict 24 hours in one day(N1=40,

N2=20)

4. thirty days as training data and to predict 24 hours in one day(N1=40,

N2=20)

Page 17: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

17

Figure 7: A simulation result of curve fitting as different days or hidden neurons chose for training network.

Figure 7 shows that 3 is quite better fit than others. Comparing 1, 2, and 3, we can

find that more samples used, more exact the forecasting. Comparing 3 and 4, we

can see it’s not possible to predict two day’s weather by using only data in one

month.

3 4 1 2

3 4

Page 18: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

18

Figure 8: the regression plots in different options.

Figure 8 shows that regression R Values measure the correlation between outputs

and targets. All the plots have a high value better than 0.99. The outputs of the

training network are quite close to the targets. So the network model has a good

training performance.

3 4

1 2

Page 19: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

19

Figure 9: changing curve of error in different options

Figure 9 shows the changing curve of error. This error is calculated between the

measured data and the predicted data when using the test set. Compare 1 , 2,

and 3, the largest error occurred is about 6 oC(4 times), 2.2

oC(2 times), and 1.1

oC(2 times) respectively; the mean error is about 2

oC, 0

oC,0

oC. So, the output

error is increased when increasing the number of days. This is as stated due to

the complexity of the weather system and the fact that the weather conditions

on a given day is not independent of the weather conditions the day before.

Compare 3(one day was tested) and 4(two days were tested), apparently, error

is quite large in 4. For the test set, when the number of days is increased, the

error is increased.

3 4

1 2

Page 20: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

20

Figure 10: Training performance in different options

Figure 10 shows when forecasting temperature in one day, the network can

come to the desired error at epoch 517, a fast convergence speed. And network

can be convergent with a range of initial weights. By Comparison of these

images, we can know that a faster speed of network training also accelerates the

convergence rate of the error. Hence the network here is working well in this

case.

By analyzing these figures, we can increase the performance (reduce error) by

these techniques.

Increase the number of neurons in hidden layer

Train it again

Get larger training data set

3 4

Page 21: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

21

3 Fuzzy logic

3.1 Introduction

1. Objective

The weather affects us all, from the simple act of raising an umbrella to a

university changing its courses to avoid the snowstorm, especially in Norway.

Weather has always been elemental to the course of human history.

In this part, we are mainly concerning about the effect of temperature, which is the

most important parameter of all. The aim is to predict the temperature tomorrow by

using the data today, which is representative and effective.

Due to the weather is an environment of uncertainty and imprecision, this is

different from classical system, while the fuzzy logic system can aim at modeling

the imprecise models of reasoning and deal with approximate rather than precise

model. Therefore, the fuzzy logic is chosen as a suitable method for weather

forecasting. The prediction model is built by Matlab Fuzzy Logic Toolbox.

2. Weather Station

The data used in Fuzzy Logic are from the weather station located in Skien

municipality, 136m.a.s.I.It is the closest official weather station, 7.3km away from

Porsgrunn. The station was established in October 1962.The station measures

temperature and wind. There are no normal for precipitation.

3.2Fuzzy logic design

1. Design of Fuzzy Systems from Input-Output Data

To design this fuzzy system, we view the rules of temperature changing as a black

box and measure the inputs and outputs that is we can collect a set of input-output

data pairs. In this way, the rule can be transformed into a set of input-output pairs;

hence, a problem of fundamental importance is to construct fuzzy systems from

input-output pairs.

2. Input-Output

Inputs and outputs are defined following:

Input:

Min-minimum value of the current day

Average-average value of the current day

Max-maximum value of the current day

Output:

T:-Temperature measured at 13:00 tomorrow

Page 22: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

22

Figure 11: Example of historical data from www.yr.no.

All the data used in this model come from the website (www.yr.no).

The temperature parameters, min, average, max on current day are used to predict

the temperature next day.

Table 2: collection of Temperature from 1st January to 15

th January

Date Min Average Max T

01-01 -1.6 0.4 5.2 0.9

01-02 -4.5 -2.7 -0.8 -1.8

01-03 -2.8 1.3 4.5 -0.9

01-04 -0.8 2.3 5.9 2.3

01-05 -2.1 -1.1 1.2 0.3

01-06 -4.5 -3.4 1.2 -2.1

01-07 -5.4 -1.9 -0.7 -1.3

01-08 -3.1 -2.4 -0.5 -1.9

01-09 -4.9 -2.8 -1.9 -2.4

01-10 -3.0 -0.9 1.0 0.7

01-11 -6.4 -4.1 0.0 -3.8

01-12 -10.1 -8.7 -5.6 -6.9

01-13 -13.4 -11.7 -9.8 -11.3

01-14 -11.9 -10.5 -9.4 -10.7

01-15 -9.4 -6.9 -4.9 -5.4

Page 23: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

23

Table 3: dataset of Fuzzy logic Input-Output pairs

Sample Input Output

Min Average Max Temperature

(1) -1.6 0.4 5.2 -1.8

(2) -4.5 -2.7 -0.8 -0.9

(3) -2.8 1.3 4.5 2.3

(4) -0.8 2.3 5.9 0.3

(5) -2.1 -1.1 1.2 -2.1

(6) -4.5 -3.4 1.2 -1.3

(7) -5.4 -1.9 -0.7 -1.9

(8) -3.1 -2.4 -0.5 -2.4

(9) -4.9 -2.8 -1.9 0.7

(10) -3.0 -0.9 1.0 -3.8

(11) -6.4 -4.1 0.0 -6.9

(12) -10.1 -8.7 -5.6 -11.3

(13) -13.4 -11.7 -9.8 -10.7

(14) -11.9 -10.5 -9.4 -5.4

3. Fuzzy model

The fuzzy inference technique we used is Mamdani method which was proposed

by Mamdani and Assilian. In Mamdani’s model the fuzzy implication is modeled

by Mamdani’s minimum operator, the conjunction operator is min, the t-norm from

compositional rule is min and for the aggregation of the rules the max operator is

used.

Page 24: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

24

Figure 12: Structure of Fuzzy system.

Table 4: Configuration of fuzzy logic

Configuration

And method Min

Or method Max

Implication Min

Aggregation Max

Defuzzification Centroid

5. Membership function

Define range, fuzzy set and membership function:

Min∈[-15,0]

Fuzzy set very cold cold Little cold

Params [-21 -15 -9] [-13.5 -7.5 -1.5] [-6 0 6]

Table2-4: Membership function of Min

Average∈[-15,5]

Fuzzy set Less normal normal beyond normal

Params [-23 -15 -7] [-13 -5 3] [-3 5 13]

Table2-5: Membership function of Average

Max∈[-10,10]

Fuzzy set Less hot hot Very hot

Params [-18 -10 -2] [-8 0 8] [2 10 18]

Table2-6: Membership function of Max

Temperature∈[-12,3]

Fuzzy set freezing cozy warm

Params [-18 -12 -6] [-10.5 -4.5 1.5] [-3 3 9]

Page 25: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

25

Table2-7: Membership function of Min

6. Rule

Rules can be generated by calculating the membership value of input-output pair.

For example:

Sample (1):

Figure 13: Membership function of input variable “Min”.

Figure 14: Membership function of input variable “Average”.

Page 26: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

26

Figure 15: Membership function of input variable “Max”

Figure 16: Membership function of output variable “Temperature”.

Finally, obtain a fuzzy IF-THEN rule as:

If Min is little cold and Average is beyond normal and Max is very hot then

Temperature is cozy.

According to this method, every input-output pair can generate one rule.

1. If (Min is little_cold) and (Average is beyond_normal) and (Max is

very_hot) then (Temperature is cozy)

2. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

3. If (Min is little_cold) and (Average is beyond_normal) and (Max is hot)

then (Temperature is warm) (1)

4. If (Min is little_cold) and (Average is beyond_normal) and (Max is

very_hot) then (Temperature is warm) (1)

Page 27: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

27

5. If (Min is little_cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

6. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

7. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

8. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

9. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is warm) (1)

10. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

11. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

12. If (Min is cold) and (Average is normal) and (Max is less_hot) then

(Temperature is freezing) (1)

13. If (Min is very_cold) and (Average is less_normal) and (Max is less_hot)

then (Temperature is freezing) (1)

14. If (Min is very_cold) and (Average is less_normal) and (Max is less_hot)

then (Temperature is cozy) (1)

Rules (2,6,7,8) are repeated.

2. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

6. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

7. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

8. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

10. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

11. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

Page 28: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

28

Some rules are conflicting.

1. If (Min is little_cold) and (Average is beyond_normal) and (Max is

very_hot) then (Temperature is cozy)

4. If (Min is little_cold) and (Average is beyond_normal) and (Max is

very_hot) then (Temperature is warm) (1)

13. If (Min is very_cold) and (Average is less_normal) and (Max is less_hot)

then (Temperature is freezing) (1)

14. If (Min is very_cold) and (Average is less_normal) and (Max is less_hot)

then (Temperature is cozy) (1)

Since there are 14 input-output pairs and with each pair generating one

rule,therefore,there are conflicting rules,that is,rules with the same IF parts but

different THEN parts.To resolve this conflict,we assign a degree to each

generated rule and keep only one rule from a conflicting group that has the

maximum degree.In this way not only is the conflict problem resolved,but also

the number of rules is greatly reduced.

The degree of a rule is defined as follows:

( )

D(1)=little cold(-1.6)×beyond normal(0.4)×very hot(5.2)×cozy(-1.8)=0.18

D(4)= little cold(-0.8)×beyond normal(2.3)×very hot(5.9)×freezing(0.3)=-0.29

D(1)>D(4)

D(13)=very cold(-13.4)×less normal(-11.7) ×less hot(-9.8) ×freezing(-

10.7)=0.28

D(14)= very cold(-11.9)×less normal(-10.5) ×less hot(-9.4) ×cozy(-5.4)=0.17

D(13)>D(14)

Hence we choose the Rule(1) and Rule(13).

Page 29: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

29

Final fuzzy rule base

1. If (Min is little_cold) and (Average is beyond_normal) and (Max is

very_hot) then (Temperature is cozy) (1)

2. If (Min is cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

3. If (Min is little_cold) and (Average is beyond_normal) and (Max is hot)

then (Temperature is warm) (1)

4. If (Min is little_cold) and (Average is normal) and (Max is hot) then

(Temperature is cozy) (1)

5. If (Min is cold) and (Average is normal) and (Max is less_hot) then

(Temperature is freezing) (1)

6. If (Min is very_cold) and (Average is less_normal) and (Max is less_hot)

then (Temperature is freezing) (1)

Constructing the fuzzy system based on the fuzzy rule base

To use Matlab Fuzzy logic toolbox to construct the weather forcasting model.

Surface viewer:

Figure 17: Surface viewer.

Page 30: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

30

Rule viewer:

Figure 18: Rule viewer.

Validation:

We use the two group of datas from different days to validate this model.

Using data in 17th

January as testing sample to predict the temperature in 18th

January,the prediction of fuzzy logic model is -5.98℃,which is closed to the

real value,-7.1℃.

Using data in 20th January as testing sample to predict the temperature in 21th

January,t the prediction of fuzzy logic model is -4.5℃,which is also closed to

real value,-6.2℃.

As can be seen,the difference is not too large.The weather forcasting itself

depends on many other parameters,such as humidity,wind

speed,etc.Moreover,there are a lot of uncertainy parameter which can influence

weather and temperatur,the error at measurement is less than 2℃ ,so the

prediction seems acceptable.

Page 31: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

31

Figure 19: temperature in 17th January and 18th January from www.yr.no.

Figure 20: result of prediction.

Page 32: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

32

Figure 21: temperature in 20th January and 21th January from www.yr.no.

Figure 22: result of prediction.

Page 33: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

33

4 Conclusion

The simulation results show the training algorithm performs well in the process of

convergence characteristics, and improve the convergence rate, a satisfactory

approximation.

However, there are still many hard handle problems,

(1) BP algorithm convergence in accordance with the direction of the mean

square error gradient descent, there are many local and global minimum

mean square error gradient, which makes neural network is easy to fall

into local minimum (local minima);

(2) BP learning algorithm convergence rate is very slow, and may waste a lot

of time;

(3) The selection of the number of hidden nodes of the network is still a lack

of unified and complete theoretical guidance;

(4) Generalization ability of the learning network is poor.

Page 34: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

34

Reference

[1] Håkon Viumdal. Developing membership functions for fuzzy-neural

applications. Tel-Tek, 2013.

[2]W.Tan, H.J.Marquez, T.Chen. IMC design for unstable processes with

time delays[J].J.Process control.2003,13,203-213.

[3]Acess: http://stackoverflow.com/questions/10565868/what-is-the-criteria-for-

choosing-number-of-hidden-layers-and-nodes-in-hidden-la

[4]Acess:http://aerostudents.com/files/knowledgeBasedControlSystems/artificialN

euralNetworks.pdf

[5] C.E.Garcia,M.Morari. Internal model control[M]. Ind.Eng.Chem.Proc.Des.

Dev.,1982.

[6] [2] Jerry M. Mendel. Fuzzy Logic Systems for Engineering: A tutorial.

Proceedings of The IEEE, Vol. 83. No 3, March 1995.

[7] Thomas Herpel et al. Multi Sensor Data Fusion in Automotive Applications.

Proceedings of 3rd International Conference on Sensing Technology (ICST 2008),

Tainan,Taiwan, Dec. 2008.

Page 35: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

35

Appendix A

MATLAB code for the BP neural network method.

%======input data========

px=xlsread('E:\learning in telemark\notes exercises\soft ensor\weather forecasting\three days for one

day.xlsx','sheet1');

p=px';

%===========target data===

tx=xlsread('E:\learning in telemark\notes exercises\soft ensor\weather forecasting\three days for one

day.xlsx','sheet2');

t=tx';

%===========test data=======

ptestx=xlsread('E:\learning in telemark\notes exercises\soft ensor\weather forecasting\three days for one

day.xlsx','sheet3');

ptest=ptestx';

ptesty=xlsread('E:\learning in telemark\notes exercises\soft ensor\weather forecasting\three days for one

day.xlsx','sheet4');

ptesy=ptesty';

%===========plot samples=======

plot(p, t ,'r*');

title('training data set');

xlabel('ipnut vector p');

ylabel('output vector t');

%===========training neural networks=======

[pn,minp,maxp,tn,mint,maxt]=premnmx(p,t); %Normalization of Data(scale

%inputs and targets so that they fall in the range [-1,1]).

NodeNum1 = 10; % number of nodes of the first layer in hidden layer

NodeNum2 = 20; % number of nodes of the second layer in hidden layer

TypeNum = 1; % output vector dimensions

TF1 = 'tansig'; %transfer function for the 1st layer

TF2 = 'tansig'; %transfer function for the 2st layer

TF3 = 'tansig'; %transfer function for the 3st layer

net=newff(minmax(pn),[NodeNum1,NodeNum2,TypeNum],{TF1 TF2 TF3},'traingdx');

%Creat network 'traingdm'

net.trainParam.show=50;

net.trainParam.epochs=5000; %Number of training times

net.trainParam.goal=1e-5; %accuracy of training

net.trainParam.lr=0.01; %learning rate

net=train(net,pn,tn);

%==use the trained network to simulate the tset data=====

p2n=tramnmx(ptest,minp,maxp);%Normalization of test data

an=sim(net,p2n);

[a]= postmnmx(an,mint,maxt); %Reverse normalization of Data, also the desired result.

%==plot the comparison of measured and forecasting temperature=====

figure;

Page 36: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

36

plot(1:length(ptesty),ptesty,'bo',1:length(ptesty),a,'r*');

title('Seven days used for training 1st - 7th of Jan. ');

xlabel('hours');

ylabel('Temperature( ^{o}C) in 8th of Jan');

legend('measured temperature', 'predicted temperature');

grid on;

%==plot the change of error=====

m=length(a); %The length of vector a

%t1=[ptesty];

error=ptesty'-a; %error vector

figure;

hist(error,length(error)-1)

legend('Foracasting errors temperature')

xlabel('Error in (^{o}C)')

ylabel('Number of occurances')

title('Forecasting change of error')

grid on

%======END========

Appendix B

Code of Fuzzy logic system:

[System]

Name='CaseProject'

Type='mamdani'

Version=2.0

NumInputs=3

NumOutputs=1

NumRules=6

AndMethod='min'

OrMethod='max'

ImpMethod='min'

AggMethod='max'

DefuzzMethod='centroid'

[Input1]

Name='Min'

Range=[-15 0]

NumMFs=3

MF1='very_cold':'trimf',[-21 -15 -9]

MF2='cold':'trimf',[-13.5 -7.5 -1.5]

MF3='little_cold':'trimf',[-6 0 6]

[Input2]

Name='Average'

Range=[-15 5]

Page 37: Title: Artificial Neural Network and Fuzzy Logic in forecasting short ...

37

NumMFs=3

MF1='less_normal':'trimf',[-23 -15 -7]

MF2='normal':'trimf',[-13 -5 3]

MF3='beyond_normal':'trimf',[-3 5 13]

[Input3]

Name='Max'

Range=[-10 10]

NumMFs=3

MF1='less_hot':'trimf',[-18 -10 -2]

MF2='hot':'trimf',[-8 0 8]

MF3='very_hot':'trimf',[2 10 18]

[Output1]

Name='Temperature'

Range=[-12 3]

NumMFs=3

MF1='freezing':'trimf',[-18 -12 -6]

MF2='cozy':'trimf',[-10.5 -4.5 1.5]

MF3='warm':'trimf',[-3 3 9]

[Rules]

3 3 3, 2 (1) : 1

2 2 2, 2 (1) : 1

3 3 2, 3 (1) : 1

3 2 2, 2 (1) : 1

2 2 1, 1 (1) : 1

1 1 1, 1 (1) : 1