SCaat
SCaat
Fuzzy Set :
Fuzzy sets can be considered as an extension and gross oversimplification of classical sets.
It can be best understood in the context of set membership. Basically it allows partial
membership which means that it contain elements that have varying degrees of membership
in the set.
From this, we can understand the difference between classical set and fuzzy set. Classical set
contains elements that satisfy precise properties of membership while fuzzy set contains
elements that satisfy imprecise properties of membership.
1
Fuzzy Rule:
A fuzzy rule can be represented by a fuzzy relation R = A → B
• µR(x, y) = f(µA(x), µB(y)) where the function f, called the fuzzy implication function,
performs the task of transforming the membership degrees of x in A and y in B into those of (x,
y) in A × B.
Fuzzy logic is not always accurate, so the results are perceived based on assumption, so it
may not be widely accepted.
3
Fuzzy systems don't have the capability of machine learning as-well-as neural network
type pattern recognition
Validation and Verification of a fuzzy knowledge-based system needs extensive testing
with hardware
Setting exact, fuzzy rules and, membership functions is a difficult task
Some fuzzy time logic is confused with probability theory and the terms
Aerospace
Business
Electronics
Finance
4
Industrial Sector
6) Problem:
Fuzzy Q-Learning (FQL) for any controllers with continues states:
Implementation:
Fuzzy Q-Learning is a fuzzy extension of Q-learning algorithm. For creating an FQL model you
first need to specify the input states and their corresponding fuzzy sets and then build your Fuzzy
Inference System (FIS) to integrate with the Q-Learning algorithm. In this code, two type of
fuzzy membership functions have been implemented:
1) Triangular
2) Trapezoidal
Fuzzy sets (as many as needed) can be created, initialized, and assigned to an input state variable
to build your FIS as follows:
5
statevariable.py
class InputStateVariable(object):
fuzzy_set_list = []
self.fuzzy_set_list = args
def get_fuzzy_sets(self):
return self.fuzzy_set_list
FQL.py
import numpy as np
import FIS
import operator
import itertools
import functools
import random
import sys
import copy
class Model(object):
L = []
R = []
R_= []
M = []
Q=0
V=0
6
Error = 0
q_table = np.matrix([])
fis = FIS.Build()):
self.gamma = gamma
self.alpha = alpha
self.ee_rate = ee_rate
self.q_initial_value = q_initial_value
self.action_set_length = action_set_length
self.fis = fis
if self.q_initial_value =='random':
if self.q_initial_value == 'zero':
def CalculateTruthValue(self,state_value):
self.R = []
self.L = []
input_variables = self.fis.list_of_input_variable
X =[]
fuzzy_sets = variable.get_fuzzy_sets()
membership_value = set.membership_value(state_value[index])
7
X.append(membership_value)
self.L.append(X)
def ActionSelection(self):
self.M = []
r = random.uniform(0, 1)
max = -sys.maxsize
if r < self.ee_rate:
action_index = index
else:
self.M.append(action_index)
def InferredAction(self):
max = -sys.maxsize
max = truth_value
action = self.M[index]
return action
def CalculateQValue(self):
8
self.Q = 0
def CalculateStateValue(self):
self.V = 0
max = -sys.maxsize
max = action
if sum(self.R) == 0:
self.R[0] = 0.00001
def UpdateqValue(self):
def KeepStateHistory(self):
9
self.R_ = copy.copy(self.R)
def get_initial_action(self,state):
self.CalculateTruthValue(state)
self.ActionSelection()
action = self.InferredAction()
self.CalculateQValue()
self.KeepStateHistory()
return action
self.CalculateTruthValue(state)
self.CalculateStateValue()
self.CalculateQualityVariation(reward)
self.UpdateqValue()
self.ActionSelection()
action = self.InferredAction()
self.CalculateQValue()
self.KeepStateHistory()
return action
FuzzySet.py
class Trapeziums(object):
self.left = left
10
self.right = right
self.left_top = left_top
self.right_top = right_top
membership_value = 1.0
membership_value = 0.0
else:
membership_value = 0.0
return membership_value
class Triangles(object):
self.left = left
self.right = right
self.top = top
11
def membership_value(self, input_value):
if input_value == self.top:
membership_value = 1.0
membership_value = 0.0
return membership_value
FIS.py
class Build(object):
list_of_input_variable = []
def __init__(self,*args):
self.list_of_input_variable = args
def get_input(self):
return self.list_of_input_variable
def get_number_of_rules(self):
number_of_rules = 1
12
return number_of_rules
return len(input_variable.get_fuzzy_sets())
Environment.py
import math
class Environment(object):
action_set = []
state = []
def __init__(self):
# cart position in the horizontal direction x = state[0], and cart velocity denoted by x_ = state[1]
# vertical angle of the pole denoted by tetha = state[2], angular velocity of the pole tetha_ = state[3]
self.state = [0,0,0,0]
def apply_action(self,action):
u = self.action_set[action]
self.get_current_state(u)
reward = self.get_reward()
def get_state_variable(self,variable_name):
13
if variable_name == 'x':
return self.state[0]
return self.state[1]
return self.state[2]
else:
return self.state[3]
def set_state_variable(self,variable_name,value):
if variable_name == 'x':
self.state[0] = value
self.state[1] = value
self.state[2] = value
self.state[3] = value
def get_current_state(self,u):
mio_c = 0.000002
mio_p = 0.0005
m_big = 1
14
m = 0.1
l = 0.5
g = 9.8
x__= (u + ((m * l) *
((math.pow(self.get_state_variable('tetha_'),2)*math.sin(self.get_state_variable('tetha'))) - (theta__
* math.cos(self.get_state_variable('tetha'))))) - (mio_c *
math.copysign(self.get_state_variable('x_'),1))) / (m_big + m)
def get_reward(self):
reward = -1
else:
reward = 0
return reward
CartPole.py
15
import FuzzySet
import StateVariable
import FQL
import FIS
# Create FIS
x2 = StateVariable.InputStateVariable(FuzzySet.Triangles(-2.4,-0.5,1), FuzzySet.Triangles(-
0.5,1,2.4))
fis = FIS.Build(x1,x2,x3,x4)
# Create Model
angel_list = []
env = Environment()
16
if iteration % 100 == 0 or reward == -1:
env.__init__()
action = model.get_initial_action(env.state)
if reward != -1:
angel_list.append(state_value[2])
plt.figure(figsize=(14,3))
plt.plot(angel_list)
plt.ylabel('Pole Angel')
plt.show()
8) Output:
17
9) Conclusion:
18
19