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

lindorl發表於2015-10-15

雖說是不限程式語言,但如果要用例項進行說明就不能不用具體的程式語言。

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

#! /usr/bin/python

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

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

# Code segment 3: F(X) + I = L ;
def Func1(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

# Code segment 4: |K(L) - M| <= N ;
def Func2(L, M = 128, N = 16):
    while True:
        t = L.mean() - M
        if abs(t) < N:
            # Print data
            print 'D-value -> abs(t): ', abs(t)
            return L
        else:
            SS = int(N * 0.618)
            L += [SS, -SS][t > 0]

# Code segment 5: main(), to call some modules and print messages
def main():
    X = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), np.uint8)
    while True:
        time.sleep(DELAY_SIZE)
        L = Func1(X)
        O = Func2(L)
        X = O

        # Print data
        print '-------------------------------'
        print 'Element array:'
        print O.dtype, O.shape
        print O
        print '-------------------------------'
        print '===============================\n'
        img = np.zeros((SCREEN_SIZE[0], SCREEN_SIZE[1], 3), dtype=np.uint8)
        img[:,:,0] = O.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        img[:,:,1] = O.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        cv2.imshow('Intelligence Element 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:

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

引入numpy數學處理庫、opencv的python庫cv2和time模組,在執行該程式時需要確保系統已經安裝了numpy和python-opencv。

Code segment 2:

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

設定一些全域性常量,SCREEN_SIZE表示視訊顯示尺寸,DELAY_SIZE表示迴圈延時(單位s),EVOLUTION_SPEED表示進步速度(體現在Func1(X)中),VARIATION_SPEED表示變異速度(體現在Func1(X))。

Code segment 3:

# Code segment 3: F(X) + I = L ;
def Func1(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

Func1(X)函式用以實現公式中的F(X) + I = L,其中" F = X + np.array([EVOLUTION_SPEED] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), np.uint8) "表示為集合(這裡用矩陣表示)中的某個元素新增一個常數用以構成公式中的 F(X) 運算,其中" I = np.array(np.random.randint(0, VARIATION_SPEED, size=(SCREEN_SIZE[0] * SCREEN_SIZE[1])), dtype=np.uint8) "表示為公式中 I 的得到方法(取一個元素在0到7之間的隨機矩陣,元素數量與 X 相同),最好得到 L = F + I 並輸出。

Code segment 4:

# Code segment 4: |K(L) - M| <= N ;
def Func2(L, M = 128, N = 16):
    while True:
        t = L.mean() - M
        if abs(t) < N:
            # Print data
            print 'D-value -> abs(t): ', abs(t)
            return L
        else:
            SS = int(N * 0.618)
            L += [SS, -SS][t > 0]

Func2(L, M=128, N=16)用以實現公式中的|K(L) - M| <= N,其中引數L、M、N分別對應公式中的L、M、N。其中 " t = L.mean() - M "用以得到L的平均值並用這個平均值減去M,這裡的K(L)就指的是L的平均值,通過while迴圈進行矯正使得 t 的絕對值小於N,然後像螢幕列印此時t的絕對值 abs(t) 並輸出 L 同時結束該段程式。

Code segment 5:

# Code segment 5: main(), to call some modules and print messages
def main():
    X = np.array([128,] * (SCREEN_SIZE[0] * SCREEN_SIZE[1]), np.uint8)
    while True:
        time.sleep(DELAY_SIZE)
        L = Func1(X)
        O = Func2(L)
        X = O

        # Print data
        print '-------------------------------'
        print 'Element array:'
        print O.dtype, O.shape
        print O
        print '-------------------------------'
        print '===============================\n'
        img = np.zeros((SCREEN_SIZE[0], SCREEN_SIZE[1], 3), dtype=np.uint8)
        img[:,:,0] = O.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        img[:,:,1] = O.reshape((SCREEN_SIZE[0], SCREEN_SIZE[1]))
        cv2.imshow('Intelligence Element Simulation...', img)
        KEY = cv2.waitKey(1) & 0xFF
        if 27 == KEY: break
        elif 32 == KEY: cv2.waitKey(0)
    cv2.destroyAllWindows()

main()呼叫了Func1()和Func2()用來實現這個公式的運算並使之迴圈,同時也列印出相關資訊和輸出視訊影像。

相關文章