본문 바로가기

카테고리 없음

지능제어(2-2) Homework code1 - sin func as input

728x90
반응형

 

 

1. 문제

 

(Question) 입력 값이 sin함수일 경우, 어떻게 할 것인가. 

 

 

2. 코드 

 

<Main.py>

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

# 범위 설정
x = np.arange(0, 6.29, 0.1)
y = np.arange(-1, 1, 0.1)


# 입출력 설정. membership function 설정
x_lo = fuzz.trimf(x, [0, 1.57, 3.14])
x_hi = fuzz.trimf(x, [3.14, 4.72, 6.28])
y_lo = fuzz.trimf(y, [-1, -0.5, 0])
y_hi = fuzz.trimf(y, [0, 0.5, 1])


fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(8,9))

ax0.plot(x, x_lo, 'b', linewidth=1.5, label='Low')
ax0.plot(x, x_hi, 'g', linewidth=1.5, label='High')
ax0.set_title('input')
ax0.legend()

ax1.plot(y, y_lo, 'b', linewidth=1.5, label='Low')
ax1.plot(y, y_hi, 'g', linewidth=1.5, label='High')
ax1.set_title('output')
ax1.legend()

# Rule application
x_level_lo = fuzz.interp_membership(x, x_lo, 4)
x_level_hi = fuzz.interp_membership(x, x_hi, 4)

# RULE 1
y_activation_lo = np.fmin(x_level_hi, y_lo)

# RULE 2
y_activation_hi = np.fmin(x_level_lo, y_hi)

f0 = np.zeros_like(y)
# vis
fig, ax0 = plt.subplots(figsize=(8, 3))

ax0.fill_between(y, f0, y_activation_lo, facecolor='g', alpha=0.7)
ax0.plot(y, y_lo, 'g', linewidth=0.5, linestyle='--', )
ax0.fill_between(y, f0, y_activation_hi, facecolor='r', alpha=0.7)
ax0.plot(y, y_hi, 'r', linewidth=0.5, linestyle='--', )
ax0.set_title('Output membership activity')

aggregated = np.fmax(y_activation_lo, y_activation_hi)

f = fuzz.defuzz(y, aggregated, 'centroid')
f_activation = fuzz.interp_membership(y, aggregated, f)

fig, ax0 = plt.subplots(figsize=(8, 3))

ax0.plot(y, y_lo, 'b', linewidth=0.5, linestyle='--', )
ax0.plot(y, y_hi, 'g', linewidth=0.5, linestyle='--')

ax0.fill_between(y, f0, aggregated, facecolor='Orange', alpha=0.7)
ax0.plot([f, f], [0, f_activation], 'k', linewidth=1.5, alpha=0.9)
ax0.set_title('Aggregated membership and result (line)')

plt.show()

ㅇㅇ

 

 

3. 실행 화면

 

실행

 

 

 

728x90
반응형