본문 바로가기

LAB/지능 제어

지능제어(2) 임시

728x90
반응형

 

 

1. Review

 

퍼지 시스템이란? 입력과 출력이 있는 함수.

 

NOT, OR, AND의 공식들

 

(1) 퍼지는 '학습'에 이용된다. -> parameter를 업데이트함으로써 이루어진다. 

(2) 퍼지를 '예측'에 이용할 수 있다. -> 함수를 안다는 것은, 예측을 한다는 것. 

Data를 가지고 퍼지를 만든다. 이걸 어떻게 구현할 수 있을까. 이것들에 대해 고민해보자. data들이 있다. 이를 가지고 함수를 만들어, 이 함수를 또다른 상황에 적용하여 '의도'에 맞는 결론을 도출하는 것이다. 

 

 

 

2. Fuzzy Relations and the Extension Principle

 

퍼지 관계(Fuzzy relation)란, 수학에서 통상적으로 사용하는 '관계'라는 개념을 퍼지화한 것이다. 예를 들어 'A와 B가 사이가 좋다' 혹은 'A가 B보다 소극적이다'라는 관계를 퍼지 관계가 된다. 퍼지 관계는 퍼지추론에서 퍼지 조건문을 표현하는 중요한 방법이다. 

집합 X와 Y의 cartesian product를 X x Y라 할 때, X x Y의 부분집합 R을 '관계'라고 부른다. (x, y)가 R의 원소가 되면 x와 y는 R이라는 관계가 있고, (x, y) ∉ R이면 x와 y는 관계가 없다고 한다. 만약 관계 R이 퍼지 집합이면, R을 집합 X에서 집합 Y로의 퍼지 관계라 한다. 이때 x와 y의 관계의 정도는 소속함수 µR(x, y) 로 나타낸다. 

퍼지 관계도 퍼지그래프와 퍼지행렬로 표시할 수 있다. 퍼지그래프는 정점(vertex)과 호(arc)를 이용하여 표현하는데, 호는 관계의 강도를 의미한다.

 

(1) From classical Relations to Fuzzy Relations

 

 

 

 

 

 

 

 

<저번 시간, 서비스와 팁>

내가 가지고 있는 데이터 값을 입력받아, 그에 맞는 결과 도출.

-> 입력이 서비스와 팁이 아니라. sin(t) 라면? 할 수 있을까? 

-> (고민되는 점) sin(t)의 RULE을 찾아야 한다. M,F도 어떻게 선정해야 할까.

-> 결론, 실생활과의 매칭에서도. Rule와 M,F만 알면 퍼지 시스템을 쉽게 적용 가능하다. 

-> (?) sin(t) 입력의 Rule과 MF를 어떻게 정할 것인가 : t가 0~10이라면 5 기준으로 시간이 작다, 크다로 나누어 멤버십 함수를 적용하면 된다. 

 

(*) 데이터쌍의 개수만큼 RULE이 나온다. -> 그만큼 RULE 없으면 오류 뜸

 

<문제 : 주차하기>

제어입력 : 핸들

13쪽 : 전문가의 시범 --> 이걸 가지고 Rule을 만들 수 있다. --> 15쪽처럼 그래프 그려서 Rule 만들면, 16쪽처럼 됨.

17쪽처럼 중복되는 데이터가 있을 경우, 영향이 젤 큰것만 쓰면 된다. S2, S2,S2일 경우 젤 큰 값인 0.92 선택. 

--> 17쪽은 membership function의 값을 다 곱한 것이다. degree값을 선택하여 Rule로 사용한다. 

--> 17쪽에서 CE, SI일 때 SI와 CE라는 두 가지의 결과가 나오는데. 그냥 degree 젤 큰(0.35인) SI선택하면 됨. CE는 Rule에서 제외. 이렇게 백만개의 Rule을 다 쓰는게 아니라 필요한 것만 골라서 쓰자. 

참고로 파이는 차의 각도, 세타는 핸들이다. 

 

(*) 각 상태에 따라 membership func을 만든 다음, 세타를 만드는 것이 목적(?)이다. x와 파이 데이터를 가지고 데이터 페어로 세타를, 룰을 만들자. 

(?) membership func은 임의로 정하면 되는 것인가? low와 upper bound만 설정해주면 된다. 

-> membership func은 임의로. 포인트는 이를 기반으로 그 데이터의 '룰'을 만드는 것이다. 

-> 쉽게 쉽게 입력의 개수를 3개, 5개(즉 15개)로 만들고,, 이를 점점 잘게잘게 만들면 된다. 

 

(?) 룰 27개니??

figure()

plot(X,Y)

 

(*) 숙제 확인 : 입력 각각에 membership function이 존재한다. 따라서 출력에도 출력값과 별개로 membership function이 존재한다. 

(?) 전건부와 후건부의 관계를 어떻게 해야 하나? min or max를 하자. add는 (OR) 좀 이상해. 

(*) 곱하기는 min, OR(더하기)는 max와 동일하다. 

 

(*) 챕터 7의 3쪽을 참고하자. fuzzy는 신경망들과 달리, Rule이 있다. 데이터가 별로 없어도 Rule을 만들 수 있으면 충분히 사용 가능하다. 

(*) 챕터 9의 4쪽이 최종 결론. 분모는 각 membership function의 합. (입력이 여러 조건이면 변수 개수만큼 product해준다.) 분자는 출력값과, membership function 각각의 무게 중심을 구하는 것(?)이다. 

--> triangle 함수로도 했지만, 쉽게 나타내기 위해 가우시안 함수를 쓰자. 그렇게 Fx를 만들면 sin 함수도 만들 수 있다. 

--> Y바 : sin(t)에서 t가 언제일 때 sin값을 가진다. 이걸로 Rule을 만든다. Rule 5개만 있을 때보다 Rule 여러개일 때 더 부드러운 곡선, sin함수가 만들어질 것이다. 그때 t에 따른 각각의 출력값, 그중에서도 대푯값이 y바이다. 이 y바는 sin함수의 대푯값으로, 나머지 그 사이에 있는 값들을 얘가 만들어준다. 이 5개 or 10개의 대푯값들은 이미 학습된, 그런 데이터들이다. 그 사이의 값들을 기존의 Rule로 메꾸어주면 온전한 sin함수가 나온다.  

 

 

 

 

이건 main.py. 

import numpy as np
import skfuzzy as fuzz
from matplotlib.pyplot import *
import math
from fuzzy_theta import fuzzy_theta


X = [0]
Y = [0]
Phi = [0.1]
Theta = np.zeros(100)

for t in range(np.size(Theta)):
Theta[t] = fuzzy_theta(X[t], Phi[t])
X.append(X[t] + math.cos(Phi[t]+Theta[t])+math.sin(Theta[t])*math.sin(Phi[t]))
Y.append(Y[t] + math.sin(Phi[t]+Theta[t])-math.sin(Theta[t])*math.cos(Phi[t]))
Phi.append(Phi[t] - math.asin(2*math.sin(Theta[t])/4))

figure()
plot(X,Y)

 

이건 fuzzy_theta.py 하다 만 거

import numpy as np
import skfuzzy as fuzz
from matplotlib.pyplot import *
import matplotlib.pyplot as plt

def fuzzy_theta(x_value, pi_value):

# 범위 설정
x = np.arange(0, 21, 1)
pi = np.arange(-90, 271, 1)
theta = np.arange(-40, 41, 1)

# 입출력 설정 - 숫자 바꿔야 함.
pi_S3 = fuzz.trimf(x, [-90, -45, 0])
pi_S2 = fuzz.trimf(x, [-90, -45, 0])
pi_S1 = fuzz.trimf(x, [-90, -45, 0])
pi_CE = fuzz.trimf(x, [-90, -45, 0])
pi_B1 = fuzz.trimf(x, [-90, -45, 0])
pi_B2 = fuzz.trimf(x, [-90, -45, 0])
pi_B3 = fuzz.trimf(x, [-90, -45, 0])

x_S2= fuzz.trimf(x, [-90, -45, 0])
x_S1 = fuzz.trimf(x, [-90, -45, 0])
x_CE = fuzz.trimf(x, [-90, -45, 0])
x_B1 = fuzz.trimf(x, [-90, -45, 0])
x_B2 = fuzz.trimf(x, [-90, -45, 0])

theta_S3 = fuzz.trimf(x, [-90, -45, 0])
theta_S2 = fuzz.trimf(x, [-90, -45, 0])
theta_S1 = fuzz.trimf(x, [-90, -45, 0])
theta_CE = fuzz.trimf(x, [-90, -45, 0])
theta_B1 = fuzz.trimf(x, [-90, -45, 0])
theta_B2 = fuzz.trimf(x, [-90, -45, 0])
theta_B3 = fuzz.trimf(x, [-90, -45, 0])

#입출력 그림으로 출력
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, figsize=(8, 9))

ax0.plot(pi, pi_S3, 'b', linewidth=1.5, label='S3')
ax0.plot(pi, pi_S2, 'b', linewidth=1.5, label='S2')
ax0.plot(pi, pi_S1, 'b', linewidth=1.5, label='S1')
ax0.plot(pi, pi_CE, 'b', linewidth=1.5, label='CE')
ax0.plot(pi, pi_B1, 'b', linewidth=1.5, label='B1')
ax0.plot(pi, pi_B2, 'b', linewidth=1.5, label='B2')
ax0.plot(pi, pi_B3, 'b', linewidth=1.5, label='B3')
ax0.set_title('pi')
ax0.legend()

ax1.plot(x, x_S2, 'b', linewidth=1.5, label='S2')
ax1.plot(x, x_S1, 'b', linewidth=1.5, label='S1')
ax1.plot(x, x_CE, 'b', linewidth=1.5, label='CE')
ax1.plot(x, x_B1, 'b', linewidth=1.5, label='B1')
ax1.plot(x, x_B2, 'b', linewidth=1.5, label='B2')
ax1.set_title('x')
ax1.legend()

ax2.plot(theta, theta_S3, 'b', linewidth=1.5, label='S3')
ax2.plot(theta, theta_S2, 'b', linewidth=1.5, label='S2')
ax2.plot(theta, theta_S1, 'b', linewidth=1.5, label='S1')
ax2.plot(theta, theta_CE, 'b', linewidth=1.5, label='CE')
ax2.plot(theta, theta_B1, 'b', linewidth=1.5, label='B1')
ax2.plot(theta, theta_B2, 'b', linewidth=1.5, label='B2')
ax2.plot(theta, theta_B3, 'b', linewidth=1.5, label='B3')
ax2.set_title('theta')
ax2.legend()

#Rule application
pi_level_S3 = fuzz.interp_membership(pi, pi_S3, 4)
pi_level_S2 = fuzz.interp_membership(pi, pi_S2, 4)
pi_level_S1 = fuzz.interp_membership(pi, pi_S1, 4)
pi_level_CE = fuzz.interp_membership(pi, pi_CE, 4)
pi_level_B1 = fuzz.interp_membership(pi, pi_B1, 4)
pi_level_B2 = fuzz.interp_membership(pi, pi_B2, 4)
pi_level_B3 = fuzz.interp_membership(pi, pi_B3, 4)

x_level_S2 = fuzz.interp_membership(x, x_S2, 70)
x_level_S1 = fuzz.interp_membership(x, x_S1, 70)
x_level_CE = fuzz.interp_membership(x, x_CE, 70)
x_level_B1 = fuzz.interp_membership(x, x_B1, 70)
x_level_B2 = fuzz.interp_membership(x, x_B2, 70)

#Rules (27)
active_rule1 = np.fmin(-, -)
active_rule2 = np.fmin(-, -)
active_rule3 = np.fmin(-, -)
active_rule4 = np.fmin(-, -)
active_rule5 = np.fmin(-, -)
active_rule6 = np.fmin(-, -)
active_rule7 = np.fmin(-, -)
active_rule8 = np.fmin(-, -)
active_rule9 = np.fmin(-, -)
active_rule10 = np.fmin(-, -)
active_rule11 = np.fmin(-, -)
active_rule12 = np.fmin(-, -)
active_rule13 = np.fmin(-, -)
active_rule14 = np.fmin(-, -)
active_rule15 = np.fmin(-, -)
active_rule16 = np.fmin(-, -)
active_rule17 = np.fmin(-, -)
active_rule18 = np.fmin(-, -)
active_rule19 = np.fmin(-, -)
active_rule20 = np.fmin(-, -)
active_rule21 = np.fmin(-, -)
active_rule22 = np.fmin(-, -)
active_rule23 = np.fmin(-, -)
active_rule24 = np.fmin(-, -)
active_rule25 = np.fmin(-, -)
active_rule26 = np.fmin(-, -)
active_rule27 = np.fmin(-, -)


#active_theta.append(np.fmin(active_rule27, theta_S2))



#activate_theta.append()
aggregated_mf = []

for i in range(np.size(theta)):
aggregated_mf.append(np.max(active_theta[:, i]))

print(aggregated_mf)

#return activated_theta

 

 

 

728x90
반응형

'LAB > 지능 제어' 카테고리의 다른 글

지능제어(1) Introduction  (0) 2023.08.02
지능제어(2-1) Homework code1 - Truck Backer-Upper Control  (0) 2023.07.26
지능제어(1-1) Example code  (0) 2023.07.25