廣義智慧集理論實踐——智慧集構建

lindorl發表於2015-10-15

以下Python程式碼構建廣義智慧集中的智慧集。

#! /usr/bin/python

# Code segment 1: import library
import numpy as np
import cv2
import time
import random

# Code segment 2: Configure
SCREEN_SIZE = (400, 400)
DELAY_SIZE = 0.1
EVOLUTION_SPEED = 8
VARIATION_SPEED = 7

# Code segment 3: IntelligenceElement_A
def FuncA_1(X):
    F = X + np.array([EVOLUTION_SPEED] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), np.uint8)
    I = np.array(np.random.randint(0, VARIATION_SPEED, size=(SCREEN_SIZE[0] * SCREEN_SIZE[1])), dtype=np.uint8)
    L = F + I
    return L

def FuncA_2(L, M=128, N=16):
    while True:
        t = L.mean() - M
        if abs(t) < N:
            return t, L
        else:
            SS = int(N * 0.618)
            L += [SS, -SS][t > 0]

def FuncA(X, M=128):
    L = FuncA_1(X)
    T, O = FuncA_2(L, M)
    return T, O

# Code segment 4: IntelligenceElement_B
def FuncB_1(X):
    F = X + np.array([EVOLUTION_SPEED] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), np.uint8)
    I = np.array(np.random.randint(0, VARIATION_SPEED, size=(SCREEN_SIZE[0] * SCREEN_SIZE[1])), dtype=np.uint8)
    L = F + I
    return L

def FuncB_2(L, M=128, N=16):
    while True:
        t = L.mean() - M
        if abs(t) < N:
            return t, L
        else:
            SS = int(N * 0.618)
            L += [SS, -SS][t > 0]

def FuncB(X, M=128):
    L = FuncB_1(X)
    T, O = FuncB_2(L, M)
    return T, O

# Code segment 5: IntelligenceSet
def main():
    X_A = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)
    X_B = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)
    M_A = M_B = 128
    LIMIT = 0.618 * 32
    while True:
        for i in range(64):
            time.sleep(DELAY_SIZE)
            T_A, X_A = FuncA(X_A, M_A)
            T_B, X_B = FuncB(X_B, M_B)
            T = T_A + T_B
            if abs(T) <= LIMIT:
                # Print data
                print 'T:', T
                print "T_A: %f, T_B: %f" % (T_A,T_B)
                print '--------------------------------'
                print "M_A: %f, M_B: %f" % (M_A,M_B)
                print '--------------------------------'
                print 'Set array:'
                print X_A
                print X_B
                print '--------------------------------'
                print '================================\n'
                break
            print 'The abs(T) more than LIMIT'
            if T > 0:
                if T_A > 0: M_A += random.random()
                if T_B > 0: M_B += random.random()
            else:
                if T_A < 0: M_A -= random.random()
                if T_B < 0: M_B -= random.random()
        else:
            print '\n================================\n'
            print 'END OF WORLD'
            print '\n================================\n'
            X_A = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)
            X_B = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)

        # Show data
        img = np.zeros((SCREEN_SIZE[0], SCREEN_SIZE[1] * 3, 3), dtype=np.uint8)
        OA = X_A.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        OB = X_B.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        img[:,:SCREEN_SIZE[1],0] = OA
        img[:,SCREEN_SIZE[1] * 2:,1] = OB
        img[:,SCREEN_SIZE[1]:SCREEN_SIZE[1] * 2,0] = OA
        img[:,SCREEN_SIZE[1]:SCREEN_SIZE[1] * 2,1] = OB

        cv2.imshow('Intelligence Set Simulation...', img)
        KEY = cv2.waitKey(1) & 0xFF
        if 27 == KEY: break
        elif 32 == KEY: cv2.waitKey(0)

    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

# End of code

程式碼說明

Code segment 1-4:

其中的FuncA_1(X)、FuncA_2(L, M=128, N=16)、FuncB_1(X)、FuncB_2(zL, M=128, N=16)都是前面用於構建智慧元的函式,值得注意的是除卻之前的功能,在這裡FuncA_2(L, M=128, N=16)和FuncB_2(L, M=128, N=16)還多反饋一個引數——差值 t,FuncA與FuncB用以整合前面的FuncA_1(X)、FuncA_2(L, M=128, N=16)、FuncB_1(X)、FuncB_2(zL, M=128, N=16),有點類似前面的程式碼中的main函式的功能。

Code segment 5:

    X_A = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)
    X_B = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), dtype=np.uint8)
    M_A = M_B = 128
    LIMIT = 0.618 * 32

初始化兩個不同的集合X_A、X_B,兩個智慧元定義公式中的M_A, M_B,LIMIT相當於智慧集定義公式中的N。

    while True:
        for i in range(64):
            time.sleep(DELAY_SIZE)
            T_A, X_A = FuncA(X_A, M_A)
            T_B, X_B = FuncB(X_B, M_B)
            T = T_A + T_B
            if abs(T) <= LIMIT:
                # Print data
                print 'T:', T
                print "T_A: %f, T_B: %f" % (T_A,T_B)
                print '--------------------------------'
                print "M_A: %f, M_B: %f" % (M_A,M_B)
                print '--------------------------------'
                print 'Set array:'
                print X_A
                print X_B
                print '--------------------------------'
                print '================================\n'
                break
            print 'The abs(T) more than LIMIT'
            if T > 0:
                if T_A > 0: M_A += random.random()
                if T_B > 0: M_B += random.random()
            else:
                if T_A < 0: M_A -= random.random()
                if T_B < 0: M_B -= random.random()

相當於智慧集定義公式中的 | K(L) - M | < N 用以修正引數M_A、 M_B,起到反饋的作用。

相關文章