I-Shou University Institutional Repository:Item 987654321/12981
全文
(2)
(3) 義 守 大 學 電子工程學系碩士班 碩 士 論 文 粒子尋優演算法應用於旋轉式倒單擺之控制 Control for the Rotational Inverted Pendulum System with Particle Swarm Optimization Algorithm. 指導教授: 洪惠陽 凌衍雷 研究生: 簡伯丞. 中華民國 一百 年 七 月.
(4) 摘要 本論文主要是針對一個典型不穩定、非線性且存在著不確定性的控制 平台—旋轉式倒單擺系統。本論文主要目的在於利用 Matlab Simulink 配合 模糊控制器及粒子尋優演算法(Particle Swarm Optimization),使旋轉式倒單 擺系統的擺錘甩上直立起來。而這個系統常被使用來驗證各種控制理論, 因它本身是一個非線性的系統。擺錘自然下垂時的位置是一個穩定平衡 點,當它甩上至直立位置是屬於不穩定平衡點。利用模糊控制理論來設計 系統的甩上、直立及定位控制器,使得擺錘由自然下垂甩上到直立,最後 橫桿到達目標位置。 利用模糊控制器中的資料庫,能使倒單擺系統即使受到外力的干擾 時,也能迅速回復到直立定位的功能,因此證明模糊控制系統的強健性、 適應性與實用性。 關鍵字:粒子群聚最佳化、模糊控制、旋轉式倒單擺系統。. II.
(5) ABSTARCT This paper is mainly aimed at a typical nonlinear and unstable Systems -Rotational Inverted Pendulum System. The main purpose of this paper is to use Matlab Simulink with Fuzzy Controller and Particle Swarm Optimization, so that the Rotational inverted pendulum system is thrown on the pendulum stand upright. And this system is often used to validate a variety of control theory, because it is a nonlinear system. Using Fuzzy controller database, can be inverted pendulum system quickly return to upright positioning function, even if the interference of external forces, so that the fuzzy control system robustness, adaptability and practicality. Keywords: Particle Swarm Optimization, Fuzzy Control, Rotational Inverted Pendulum System.. III.
(6) 誌謝 在這兩年的時光裏,本人非常的感謝指導教授洪惠陽博士的殷切指 導,在學業與論文研究上能給予細心的指導和引領,使得本人的學習研究 得以順利進行,並能順利完成本論文。 其次,十分感謝指導教授凌衍雷博士的教學與指導,使得本人對研究架構 與構思助益良多。除此之外,特別感謝 謝哲光教授、孫允平教授口試委員, 於百忙之中撥冗指導,對於論文的指正與寶貴意見,使得本論文得以更為 完備,在此由衷的感謝。 最後感謝三位學長碩夫、炮哥、彥翔,在我求學研究過程中能勉勵我 指導我,尤其是碩夫,他對我不論是課業上還是生活上都對我有非常大的 幫助,本篇論文得以完成他幫助甚多,本人再次百萬分的感激他。此外, 我也要感謝好友黑鬼、皓呆等,謝謝你們的支援及協助,皓呆、炮哥及黑 鬼天天上演實驗室無間道,他們總是帶給我每天研究的動力助我完成這二 年的研究生活,也讓整間實驗室特別歡樂、特別有凝聚力。 3905 的各位好朋友我們每天一起做研究、一起共苦、一起歡樂、一起 享受豐碩的成果,我相信這都讓我們彼此留下難忘的回憶。 最後,我要特別感謝我的母親及弟弟,有了他們在背後的默默鼓勵、關心 與支持,使本人於課業繁重的壓力下,找到往前推進的動力,使得本人能 夠順利完成碩士學位,最後僅以本論文獻給我的家人與朋友,願與大家分. IV.
(7) 享這份榮耀與成果。. V.
(8) 目錄 封面 審定書 中文摘要 英文摘要 誌謝 目錄 圖表索引 第1章. 緒論............................................................................................ 1. 1.1 前言............................................................................................ 1 1.2 研究動機.................................................................................... 3 1.3 論文架構.................................................................................... 3 第2章. 模糊控制簡介............................................................................ 4. 2.1 模糊控制理論 .......................................................................... 4 2.2 模糊系統架構............................................................................ 7 第3章. 旋轉式倒單擺系統分析及軟體之架構.................................. 20. 3.1 動態方程式.............................................................................. 20 第4章. 粒子群演算法.......................................................................... 24. 4.1 演算法介紹.............................................................................. 24. VI.
(9) 4.2 速度更新公式.......................................................................... 25 4.3 演算法流程介紹...................................................................... 26 第5章. 模糊控制器設計...................................................................... 30. 5.1 平行分散補償設計.................................................................. 30 5.2 模糊控制器設計...................................................................... 31 5.3 模擬結果.................................................................................. 35 第6章. 結論.......................................................................................... 47. 第7章. 參考文獻.................................................................................. 48. 附錄(一) 旋轉式倒單擺系統 s-function ................................................ 52 附錄(二) PSO 主程式 ............................................................................. 55 附錄(三) PSO 參數程式 ......................................................................... 66. VII.
(10) 圖表索引 圖 2.1. 模糊控制器................................................................................ 6. 圖 2.2. 模糊邏輯控制器之基本架構.................................................... 7. 圖 2.3. 歸屬函數圖形............................................................................ 9. 圖 2.4. Mamdani 模糊模式示意圖 ..................................................... 14. 圖 2.5. Sugeno 模糊模式示意圖......................................................... 15. 圖 2.6. 重心法示意圖.......................................................................... 16. 圖 2.7. 最大值之平均值投影圖.......................................................... 17. 圖 2.8. 最大值之平均值示意圖.......................................................... 18. 圖 3.1. 旋轉示倒單擺系統之力系分析.............................................. 21. 圖 4.1. PSO 演化流程圖...................................................................... 27. 圖 4.2. 搜尋過程-最初狀態 ................................................................ 28. 圖 4.3. 搜尋過程-經過 10 代 .............................................................. 28. 圖 4.4. 搜尋過程-經過 50 代 .............................................................. 29. 圖 4.5. 搜尋過程-經過 500 代............................................................. 29. 圖 5.1. 歸屬函數圖.............................................................................. 31. 圖 5.2. Simulink 模擬圖 ...................................................................... 32. 圖 5.3. 控制器模擬圖.......................................................................... 32. 圖 5.4. PDC 控制器設計圖................................................................. 33. VIII.
(11) 圖 5.5. 2000 代 PSO 模擬圖 ............................................................... 34. 圖 5.6. 45 度(0.8754 rad)倒單擺角度 X1 ............................................ 35. 圖 5.7. 45 度(0.8754 rad)旋轉盤角度 X3 ............................................ 36. 圖 5.8. -45 度(-0.8754 rad)倒單擺角度 X1.......................................... 37. 圖 5.9. -45 度(0.8754 rad)旋轉盤角度 X3 ........................................... 28. 圖 5.10. 135 度(2.3562 rad)倒單擺角度 X1 .......................................... 39. 圖 5.11. 135 度(2.3562 rad)旋轉盤角度 X3 .......................................... 40. 圖 5.12 -135 度(-2.3562 rad)倒單擺角度 X1........................................ 41 圖 5.13. -135 度(2.3562 rad)旋轉盤角度 X3 ......................................... 42. 圖 5.14. 180 度(3.1416 rad)倒單擺角度 X1 .......................................... 43. 圖 5.15 180 度(3.1416 rad)旋轉盤角度 X3 .......................................... 44 圖 5.16. 模擬倒單擺角度 X1 ................................................................. 45. 圖 5.17. 模擬旋轉盤角度 X3 ................................................................. 46. 表 2.1. 旋轉式倒單擺系統之參數設定.............................................. 23. IX.
(12) 第 1 章 緒論 1.1. 前言 近十幾年來,模糊理論[1]有了相當成功的實際應用。如引擎的 轉速控制,鍋爐的溫度控制及家電產品應用上不勝枚舉,甚至最近日 本發明汽車自動倒車入停車位,也是應用到模糊控制理論而發展出 來。在模糊邏輯的觀念上,它是以一種近似理論為基礎的邏輯觀念, 由加州大學 Zadeh 教授於 1965 年所提出。它將語意上的變數機率視 為傳統數值機率的模糊特性,也就是將傳統人工操作系統,藉由有經 驗的操作者以口語化的定性敘述,來達成控制與校正目的的方式。將 擷取出此口語化的敘述轉變成模糊或然率的定量詞,使之直接成為一 模糊規則,進而擴充至一專屬之規則庫,如此,便能以人為主的觀念 來達成所預期的控制目的[2]。 本篇論文將使用旋轉式倒單擺系統來驗證模糊控制理論的實用 性及簡單設計程序[3]~[8],並透過 MATLAB 的 Simulink 中拉出所需 的 Block 與之接線,即可完成系統控制。本論文主要目的在於利用粒 子尋優演算法(Particle Swarm Optimization)[9]配合模糊控制器,使旋 轉式倒單擺系統的擺錘甩上直立起來。而這個系統常被使用來驗證各 種控制理論,因它本身是一個非線性的系統。擺錘自然下垂時的位置 1.
(13) 是一個穩定平衡點,當它甩上至直立位置是屬於不穩定平衡點。 近年來美國發明家 Dean Kamen 發明一種兩輪代步的電動車─賽 格威(Segway),建立在平行分立兩側的兩輪交通工具。當人站在上 面時,可以自動平衡,時時保持直立,而且會根據整體重心的改變而 產生前進或後退的行動模式。另外推出以賽格威底座為主,可載運物 品的兩輪移動運平台,稱為“RMP”(The Segway RoboticMobility Platform) 。在 1994 年,日本學者 Yunsu Ha 和 Shin’ichi Yuta 發表了 一篇論文[10],能自我平衡與軌跡追蹤的移動式倒單擺機器人,利用 Lagrange Motion Equation 求得系統狀態方程式,控制車身整體平 衡。在 2002 年的輪倒單擺機器人 Joe[11],此篇論文採用陀螺儀所得 的資訊當作車身傾斜角度與角速度,利用兩輪回授編碼值精確得出車 身目前位置與方向,將這些資訊套入求得的狀態方程式,控制車身前 進、後退、旋轉等動作。 因此,由以上實例驗證了旋轉式倒單擺是一個非常有代表性的實 驗系統,許多的實際控制問題,如機器人平衡應用、精密物體的運輸 控制等等[12]~[22]。. 2.
(14) 1.2. 研究動機 旋轉式倒單擺系統這個系統常被使用來驗證各種控制理論,因它 本身是一個非線性的系統。利用模糊控制理論來設計系統的甩上、直 立及定位控制器,使得擺錘由自然下垂甩上到直立,最後橫桿到達目 標位置,證明模糊控制系統的強健性、適應性與實用性。. 1.3. 論文架構 第一章『緒論』:闡述研究背景、文獻回顧以及研究動機。 第二章『模糊控制簡介』:本章節介紹模糊理論內容。 第三章『旋轉式倒單擺控制系統建立』:參考先前文章,得到滑 車倒單擺的動態方程式,並設計控制器。接著便把滑車倒單擺動態方 程式轉為 Takagi-Sugeno(T-S)模糊系統型式。 第四章『粒子尋優演算法』:本章節介紹粒子尋優演算法,並加 入粒子群聚最佳化概念,求得最佳回授值。 第五章『模擬結果與分析』:主要以程式模擬來觀察結果。 第六章『結論與未來方向』:對本論文的研究成果作總結,並提 出本論文未來可進行研究的方向。. 3.
(15) 第2章 章. 模糊控制簡介 2.1 模糊控制理論 美國加州大學柏克萊分校的 L. A. Zadeh 教授於 1965 年提出模 糊集合理論。此後在多位研究者共同努力下,建構出模糊邏輯(Fuzzy Logic)等理論,Fuzzy 理論就是以此為中心而發展而成的。模糊理 論(Fuzzy theory)包含有模糊集合、模糊邏輯、模糊量測(Fuzzy measure)、模糊推論系統(Fuzzy inference system)、模糊數(Fuzzy numbers)及模糊積分等之研究領域。 以往傳統科學在描述及解決所有數學與科學的問題時,所根據的 是明確集合(Crisp Set),即1和0的二元邏輯概念。然而在日常生 活當中有很多事物的界定是無法明確定義的。對於這種高度複雜的非 線性系統或是無法取得正確的數學模式的系統,使用模糊控制器 (Fuzzy Logic Control)可以獲得不錯的控制結果。 模糊控制的研究開始於 1970 年代中期,在 1980 年代進入實用 化。模糊控制是把人類對於受控系統的操控經驗及直覺歸納,而得到 一組用語言變數來表達一些不精確的條件語句與決定規則,本論文所 使用的模糊控制法則是依據 Sugeno Type 利用 IF-THEN 條件命題式 的「語言性控制(linguistic control)」法則。再藉由模糊集合理論構 4.
(16) 成模糊控制規則、模糊推論及解模糊化,並輸出受控系統(Plant)的 輸出值。 一個模糊控制器的設計流程一般可分成兩個部分[23],如(圖 2.1) 所示。針對一個非線性系統而言,我們可以透過數學模型,再利用 T-S 模糊模式完成控制器的設計;若無法取得正確的數學方程式,則 利用輸入輸出資料的系統鑑別方法,一樣可以求得 Takagi-Sugeno 模 糊模式,再設計出受控系統所需要的控制器。. 5.
(17) 非線性系統 (Nonlinear system). 輸入輸出資料的系統鑑別. 實體模式 (Physical model). (Identification using input-output data). T-S 模糊模型 (Takagi-Sugeno fuzzy model). 平行分散補償 Parallel Distributed Compensation 模糊控制器 Fuzzy controller. 圖 2.1 模糊控制器[23]. 6.
(18) 2.2 模糊系統架構 一個基本的模糊邏輯控制系統[24]~[26]可由(圖 2.2)來觀察到, 主要的組成部分有四部分: 一. 模糊化(Fuzzifier) 二. 模糊規則庫(Fuzzy Rule Base) 三. 模糊推論器(Fuzzy Inference Engine) 四. 解模糊化(Defuzzifier). 圖 2.2 模糊邏輯控制器之基本架構. 當系統藉由感測器(Sensor)把明確的外界資訊輸入(X),藉 由模糊化將之轉化為模糊資訊,經由控制器的模糊推論器,根據所得 到的模糊資訊,以及在模糊規則庫中預先存放設計者主觀擬定的法 7.
(19) 則,模擬人類思考決策的模式,解決其問題。最後解模糊化則將模糊 推論器所推論出的模糊資訊,轉化為外界所能接受的明確資訊(u)。. 2.2.1 模糊化 對於受控目標系統而言,由感測器所量測到的資訊通常是一個明 確的數值。然而模糊邏輯控制器則是採用語言化的條件式規則為控制 策略。因此,為了能讓受控系統與語言化的條件式規則資訊相結合, 模糊化處理將是量測資訊進入模糊邏輯控制器所必要的步驟。 為了達到模糊化的功能,首先應根據受控系統輸出/入變數的變 動範圍決定語言變數的確定論域。接著再由設計者依據專家知識自行 決定論域的模糊分割(Fuzzy Partition)數量。確定了模糊分割的數量 後,便能將語言變數的論域適當地切割成數個模糊集合。以控制系統 為例,對於一個控制系統而言,通常是以誤差量(e)作為輸入語言 變數,其論域中模糊集合的標識可定義為: NB(Negative Big)、 NM(Negative Medium)、NS(Negative Small)、ZO(Zero)、PS (Positive Small)、PM(Positive Medium)、PB(Positive Big)。 而這些模糊集合可以採用的歸屬函數種類很多,如(圖 2.3)所 示 。一般較 常使用 的有三角 形歸屬 函數( Triangular Membership Function)、高斯歸屬函數(Gaussian Membership Function)和梯形 歸屬函數(Trapezoidal Membership Function)。由於三角形歸屬函數 8.
(20) 具有較快的計算速度而且模糊化的效果與高斯函數相當,因此,三角 形的歸屬函數最常被採用。. 圖 2.3 歸屬函數圖形. 在決定語言變數的歸屬函數時,必須注意以下的因素: (1) 嚴謹性︰ 不同形狀所定義的歸屬函數,所產生的控制效果,會有很大的差 別的。換言之,如果控制者要求較嚴謹,則必須選擇斜率較陡的,以 便於當論域稍微有變動時能作明顯的表現。 (2) 涵蓋性︰ 9.
(21) 當我們在考慮歸屬函數涵蓋的程度時,原則上是讓論域中的每一 元素,所對應的歸屬值不可太低,如此方能考慮到每一元素。. (3) 重疊性︰ 對於歸屬函數之間的重疊程度,一般而言重疊的程度越大時,模 糊控制器對系統的行為變化將有較好的適應性。但原則上仍不可過於 重疊以免兩個歸屬函數的值很難被區分。. 2.2.2 模糊規則庫 模糊規則庫是由一群以 IF-THEN 型式,所組成的條件式語句模 糊規則,並以語句之方式來命令受控系統,使其達到所須控制的目 的,而建立模糊規則庫須注意事項如下: (一) 完全性(Complete):即每次輸入的語句變數之模糊集合,至少要 觸發一條規則;不可沒有規則可觸發,否則會產生不正常的模 糊關係。 (二) 一致性(Consistent):即表示如有兩條規則其前件部相同,則其後 件部也要相同,如後件部不相同則表示不符合一致性的要求。. 10.
(22) (三) 連續性(Continuous):每相鄰的兩條規則之後件部,須有重疊的 區域, 則此稱為規則庫的連續性。 將許多的模糊規則組合成一個規則庫,而其中 IF X 1 is A1 的部分 稱為前件部,而 THEN Y is B1 部分則稱為後件部。其模糊規則庫表 示方式如下: R1 :IF X 1 is A1 ,THEN Y is B1 R 2 :IF X 1 is A2 ,THEN Y is B 2 ︴. Rn−1 :IF X 1 is An −1 ,THEN Y is B n −1 ; R n :IF X 1 is An ,THEN Y is B n 。. 2.2.3 模糊推論器 Fuzzy推論(Fuzzy Inference) [24]~[26]是根據知識庫保存的規則和 給予的事實推導出新的結論。而在模糊邏輯推論最常用的有以下三 種: [1]前向推論(Forward Reasoning): 規則:IF X is A THEN Y is B 事實:X is A’. 11.
(23) 結論:Y is B’ [2]逆向推論(Backward Reasoning): 規則:IF X is A THEN Y is B 事實:Y is B’. 結論:X is A’ [3]假設三段式(Generalized Hypothetical Syllogism): 規則:IF X is A THEN Y is B 事實:Y is B’, THEN Z is C. 結論:X is A, THEN Z is C’ 而模糊規則庫中有許多條規則被整合後看成一個“系統或工 場”此系統即稱為模糊規推論工場,常用之兩個式子如下: (一) Mamdani 整合(Combination):其中 QM 即表示模糊推論工 m. 場, ∪ 代 表s–基準 ( R (1) ∪ R ( 2) ∪ ⋯ R ( m ) ) : l =1. m. ∆. ( Q = ∪ R (l ) = QM ) 。. (2.1). l =1. m. (二) Godel 整合(Combination):其中 QG 即表示模糊推論工場, ∩. l =1. 代表. 12.
(24) t–基準(T-norms) ( R (1) ∩ R ( 2) ∩ ⋯ R ( m ) ) : m. ∆. ( Q = ∩ R ( l ) = QG )。. (2.2). l =4. 將語句變數輸入後,運用模糊推論工場,觸發規則庫的若干規則 並整合為一個適當之模糊集合,其中 A 表示語句變數, Q 即表示模糊 推論工場, B 表示觸發規則庫後並整合成為適當之模糊集合,其公式 如下: ( B = A Q , ( Q = Q M 或 QG ) ) 。. (2.3). 2.2.4 模糊推論工場 模糊推論工場[24]~[26]是將輸入模糊集合經由模糊規則庫,取得 適當的模糊輸出集合,一般常用的模糊近似推理非常多,比較常見的 模糊模式分別是 Mamdani 模式及 Sugeno 模式,以下做一些簡單的介 紹:. 1. Mamdani 模式 Mamdani 模糊模式推論之方式是採用邏輯積(Min)演算,找到前 件部的適合度,而後件部的模糊集合則用邏輯積演算取得結論。將每 條規則經由模糊推論工場,取得適當的模糊集合,再進行解模糊化後 而得到明確的輸出值。如下圖 2.4 所示,利用 t–基準運算,求出模糊 輸出集合並進行解模糊化。 13.
(25) 圖 2.4 Mamdani 模糊模式示意圖. 2. Sugeno 模式 模糊推論工場Sugeno 模糊模式是從已知的輸入-輸出資料中,有 系統地求出所需要的IF-THEN 規則之前件部與後件部。即從已知的 輸入-輸出資料中,有系統性地取得模糊規則的一種方法。且Sugeno 模式在前件部IF-THEN規則中,與Mamdani 模式架構相同,在後件 部則是採用函數轉換表示,Sugeno 模糊模式如圖2.5. 14.
(26) 圖 2.5 Sugeno 模糊模式示意圖. 2.2.5 解模糊化 解模糊化[24]~[26]與模糊化恰好相反,解模糊化是將經由模糊推 論工場後的模糊集合轉為明確的數值。而解模糊化則由以下三個準則 決定: (1) 合理性:明確數值其所代表的模糊集合應是合理的,或其歸屬度 值為較高的。 (2) 計算簡單:便利於模糊控制問題上之使用。 (3) 連續性:模糊推論工場輸出的模糊集合之形狀,即使有稍許變 化,明確值的位置變化也不會太大。. 常見的方法有重心法(Center of Area Defuzzification ;COA)、最 大平均法(Mean of maxima Defuzzification;MOD)及中心平均值解. 15.
(27) 模糊化法(Center Average Defuzzification)。. (1) 在模糊控制中解模糊化常用重心法的計算式: r. ∑y u(y ) i. y(x) =. i. i=1 r. (2.4). ∑. u( yi ). i=1. 式中, , u( yi ) 為第 i 個規則輸出集合的歸屬函數, yi 第 i 個規則的輸出 值, r為規則總數。重心法如圖2.6所示:. 圖 2.6 重心法示意圖. (2) 最大值之平均值(Middle of Maxima Defuzzification) 因為 B' ( y ) 往往是不規則形的,我們從左邊開始沿著 B' ( y ) 之山幾向右 攀爬,當爬到第一個最高點時,此點在 Y 軸上之投影即為 y * 值,如 圖 2.7。 y* = min { y ∈ Y B( y ) = h(B)} ∆ y* F y∈Y. =. 16. (2.5).
(28) 其中 h(B) 叫做模糊集合 B' ( y ) 之高度。 當爬到最後一個最高點時,此點在 Y 軸上之投影即為 y * 值,如圖 2.7。 y * = max{y ∈ Y B' ( y )= h(B' )}∆ y * L =. y∈Y. (2.6). 所謂的最大值之平均解模糊化法即為(2.11)式及(2.12)式之平均值即 可,如圖 2.8。 y*F + y*L y = 2 *. (2.7). 最後一個最高點. 第一個最高點. yF. ∗. yL. ∗. 圖 2.7 最大值之平均值投影圖. 17.
(29) y*. 圖 2.8 最大值之平均值示意圖. (3) 中心平均值解模糊化法(Center Average Defuzzification). 此法或稱為高度解模糊化法(Height Defuzzification),特別針對的形狀 為正規且對稱的,非常常用的解模糊化法之一。 m. ∫ q h' (B' ) l. y = *. l. l =1 m. (2.8). ∫ h' (B' ) l. l =1. 其中 h( B 'l ) 表示每個 B 'l 之高度。ql 表示在未經砍頭或未經矮化前之最 中心點之 y 值(如圖 2.9)。. 18.
(30) h( B2 ). h( B1 ) p1. y*. p2. 圖 2.9 中心平均值示意圖. 19.
(31) 第3章 章. 旋轉式倒單擺系統分析及軟體之 架構 3.1. 動態方程式 動態方程式 如圖 3-1 所示為旋轉式倒單擺數學模型,其中 G 為轉動中心或質 心(Mass Center),g 為重力加速度,M 為旋轉臂之重量,m 為倒 單擺桿之重量,L 為旋轉臂之一半長度,l 為倒單擺桿之ㄧ半長度, I L 為旋轉臂之質量慣性矩(Mass of inertia)及馬達轉動時對旋轉臂所造 成之輸入扭矩 (Torque)以 µ 代表。以此系統來分析描述其動能(Kinetic Energy, T)及位能(Potential Energy, VP),再將所求得之動能、位能. 代入 Lagrange’s function:La=T-VP 後,依序對倒單擺桿之角度 α、旋 轉臂之角度 θ 以 Lagrange’s equation 計算之,進而獲得所要的非線性 動態方程式(nonlinear dynamics equation)。 為了進一步分析此系統動態特性,必須先建立系統的數學模式, 根據參考文獻[27],可得(3.1)、(3.2)滑車倒單擺非線性方程式:. 20.
(32) 圖 3.1 旋轉式倒單擺系統之力系分析. 此旋轉臂之轉動慣量為 IL =. [. 1 2 M a 2 + (2 L ) 12. ]. 在此,a、L、M 分別為旋轉臂之寬度、一半長度及質量。則此系統 之動能、位能描述如下: T = TL + Tl =. VP =. (. ). 1 ɺ2 1 1 I Lθ + m L2θɺ 2 + 2lLθɺ cos ααɺ + l 2αɺ 2 + ml 2αɺ 2 2 2 6 . 1 mg (2l ) cos α = mgl cos α 2. 其中 TL 旋轉臂之轉動動能,Tl 倒單擺桿之轉動動能,g 為重力加速度 (Gravitational Acceleration) 。 依 據 Lagrange’s function 可 得 La = T − V P =. (. ). 1 ɺ2 1 1 I Lθ + m L2θɺ 2 + 2lLθɺ cos ααɺ + l 2αɺ 2 + ml 2αɺ 2 − mgl cos α 2 2 6. 依序對 α、θ 軸求基本形式(fundamental form)之 Lagrange’s equation 可 21.
(33) 得 ∂L d ∂La ( )− a =u dt ∂αɺ ∂α ∂L d ∂La ( )− a =0 dt ∂θɺ ∂θ. 在此為此實驗所提供之 DC 馬達所提供之輸入扭矩(input torque),由 此將可得系統之動態方程式. (I. L. ). + mL2 θɺɺ + mlL cos ααɺɺ − mlL sin ααɺ 2 = u. mlLθɺɺ cos α +. 4 2 ml αɺɺ − mgl sin α = 0 3. 經整理後,可得倒單擺之動態系統如下: αɺɺ = f (α ) + g (α )u θɺɺ =. (3-1). g 4l tan α − sec ααɺɺ L 3L. (3-2). T T T ɺ 其中 α = (α , αɺ ) ≡ (α 1 , α 2 ) , θ = (θ ,θ ) ≡ (θ1 ,θ 2 ) ,在此 T. f (α ) =. g (α ) =. − ( I L + mL2 ) g sin α + mlL2 sin α cos ααɺ 2 4 − l ( I L + mL2 ) + mlL2 cos 2 α 3. L cos α 4 − l ( I L + mL2 ) + mlL2 cos 2 α 3. 明顯的由式子可看出此倒單擺系統有兩個輸入狀態分別為倒單 擺桿角度 α 與旋轉臂角度 θ 而單一輸入狀態為扭矩 µ。因此如何利用 單一輸入 µ 值來同時控制兩個狀態,使得 α→ 0、θ→ 0 將是一具挑戰 性的問題。但由動態方程式中,可以分別發現倒單擺桿角度 α 與控制 輸入扭矩 µ 和旋轉臂角度 θ 與倒單擺桿角度 α 間之動態關係,可以知. 22.
(34) 道倒單擺桿角度 α、倒單擺桿角速度 α'和旋轉臂角度 θ、旋轉臂角速 度 θ'是相關的,兩者並不是獨立互不相關,因此本論文將試著從這方 面出發來解決倒單擺系統直立與定位控制的問題。. 表 2.1 旋轉式倒單擺系統之參數設定 系統參數. 設定數值. 單位. 旋轉臂之重量 M. 0.09. Kg. 倒單擺之重量 m. 0.15. Kg. 0.4. m. 倒單擺桿之ㄧ半長度 l. 0.095. m. 旋轉臂之寬度 a. 0.07. m. 重力 g. 9.8. m / sec. 旋轉臂長度. L. 23.
(35) 第4章 章. 粒子群演算法 粒子群演算法 (Particle Swarm Optimization) [9]簡稱 PSO,為 Kennedy 與 Eberhart 於 1995 年所提出,為受到鳥群的覓食行為所啟. 發。PSO 的概念源自於群體行為理論,每一個體經過特別的傳遞訊息 方式,將所得到的資訊提供給群體並做交流,使整個團體朝同一方向 而去,藉此搜尋到食物。 一群鳥再隨機搜尋食物。這個區域裡只有一塊食物。所有的鳥都 不知道食物再哪裡,但他們知道目前距離食物還有多遠,那麼找到食 物的最佳策略是什麼?最簡單的方法就是找尋距離食物最近的鳥之 周圍區域及根據自己本身飛行的經驗判斷食物的所在。. 4.1. 演算法介紹 粒子群演算法與基因演算法類似,為一種基於迭代演算的最 佳化搜尋工具,屬於演化式計算與隨機搜尋法中的一種。PSO 演算法 初始化為一群隨機粒子(隨機解),然後通過迭代找到群體最佳解。在 每一次演化迭代中,粒子透過兩個最佳值-個體最佳值(Pid)與群體最佳 值(Pgd)來更新自己。. 24.
(36) I. 演算法特性 .每個問題解都想像成一隻鳥,稱為“Particle”。 .每個 Particle 都有一個 fitness function 判斷目前位置的好壞。 .每一個 Particle 能記得所搜尋到最佳位置。 .每一個 Particle 還有一個速度以決定飛行的距離與方向。. 4.2. 速度更新公式 在 PSO 演化的程序中每個粒子都有一個速度以決定移動的距離 與方向,而每個粒子都能判斷目前位置的好壞,並且能記得所搜尋到 的最佳位置,經過特別的傳遞訊息方式,將所得到的資訊提供給群體 並做交流,藉此搜尋到最佳解。速度的更新公式如下:. xid = xid + Vid. (4-1). Vid = W × Vid + c1 × Rand () × ( pid − xid ) + c2 × Rand () × ( pgd − xid ). (4-2). 其中. Vid :每一 Particle 在第 d 維之速度 i:Particle 之編號 、 d:維度 W :Inertia Weight. c1 、 c2 :學習常數 Rand( ):一介於 0 至 1 的亂數. pid :每一 Particle 到目前為止,所求得的最佳解 25.
(37) pgd :所有 Particle 到目前為止,所求得的最佳解. xid :每一 Particle 目前之所在 本論文採用初始族群為 10,W 為 0.9, c1 、 c2 為 1.5 及 0.5 [29]。 0 < ( c1 + c2 ) < 4. (4-3). ( c1 + c2 )/2 - 1 < W < 1. (4-4). 如能滿足公式(4-3)及(4-4),則 PSO 演算法將能穩定收歛。. 4.3. 演算法流程介紹[28] 演算法流程介紹 1. Initial:. 將群族做初始化,以隨機的方式求出每一 Particle 之初始位置與 速度。 2. Evaluation:. 依據 fitness function 計算出 fitness value 以作為判斷每 Particle 之 好壞 xid 。 3. Fine the pid :. 找出每一 Particle 到目前為止的搜尋過程中最佳解,這個最佳解 我們將之稱為 pid (Pbest)。. 4. Fine the pgd :. 找出所有 Particle 到目前為止所搜尋到的整體最佳解,此最佳解 26.
(38) 我們稱之為 pgd (Gbest)。 5. Update the Velocity:. 依據(4-1)與(4-2)更新每一個 Particle 之速度與位置。回到步驟 2. 繼續執行,直到獲得一個令人滿意的結果或符合終止條件為止。. 圖 4.1 PSO 演化流程圖. 27.
(39) 演算法流程示意:. 圖 4.2 搜尋過程-最初狀態. 圖 4.1 搜尋過程-經過 10 代. 28.
(40) 圖 4.2 搜尋過程-經過 50 代. 圖 4.5 搜尋過程-經過 500 代. 29.
(41) 第 5 章 模糊控制器設計 5.1 平行分散補償設計 平行分散補償設計 針對旋轉式倒單擺控制的模式,我們根據 T-S 模糊模型來定義模 糊規則,並採用五條規則來做設計 [30]~[33]。但考慮到規則的對稱 性,將原本的五個對應的回授增益值 F 縮減為三個,其規則如下:. Rule 1: If x1 ( t ) is " about 0o " then u (t ) = F1 x(t ) Rule 2 : If x1 ( t ) is " about -45o " then u (t ) = F2 x(t ) Rule 3 : If x1 ( t ) is " about 45o " then u (t ) = F2 x(t ) Rule 4 : If x1 ( t ) is " about -90o " then u (t ) = F3 x(t ) Rule 5 : If x1 ( t ) is " about 90o " then u (t ) = F3 x(t ). 30.
(42) 圖 5.1 歸屬函數圖. 5.2 模糊控制器設計 此控制器會以倒單擺角度為控制的準則,當角度大於 90 至 180 度或小於-90 至-180 度時,則使系統變不穩定做甩上的動作;而角度介 於 90 度與-90 度之間則利用平行分散補償(PDC)做直立定位的動作。. 31.
(43) 圖 5.2 Simulink 模擬圖. 圖 5.3 控制器模擬圖 Gin3 及 Gin4 值同為[1 -2 3 4]. 32.
(44) 圖 5.4 PDC 控制器設計圖. 33.
(45) 圖 5.5 2000 代 PSO 模擬圖. F1=[-64.839 -6.0179 -14.809 -8.1682] F2=[-123.49 -5.3274 34.483 5.9411] F3=[11.575 9.1775 -280.04 211.19]. 34.
(46) 5.3 模擬結果 模擬結果. X1 為角度 α. 1. 0.8. 角角(rad). 0.6. 0.4. 0.2. 0. -0.2. -0.4. 0. 0.5. 1. 1.5. 2. 2.5 3 時 時 (sec). 3.5. 4. 圖 5.6 45 度(0.7854 rad)倒單擺角度 X1. 35. 4.5. 5.
(47) X3 為角度 θ. 1.2. 1. 角角(rad). 0.8. 0.6. 0.4. 0.2. 0. -0.2. 0. 0.5. 1. 1.5. 2. 2.5 3 時 時 (sec). 3.5. 4. 圖 5.7 45 度(0.7854 rad)旋轉盤角度 X3. 36. 4.5. 5.
(48) 0.4. 0.2. 角角(rad). 0. -0.2. -0.4. -0.6. -0.8. 0. 0.5. 1. 1.5. 2. 2.5 3 時 時 (sec). 3.5. 4. 圖 5.8 -45 度(-0.7854 rad)倒單擺角度 X1. 37. 4.5. 5.
(49) 0.2. 0. 角角(rad). -0.2. -0.4. -0.6. -0.8. -1. -1.2. 0. 0.5. 1. 1.5. 2. 2.5 3 時時 (sec). 3.5. 4. 圖 5.9 -45 度(-0.7854 rad)旋轉盤角度 X3. 38. 4.5. 5.
(50) 12. 10. 角角(rad). 8. 6. 4. 2. 0. -2 0. 2. 4. 6. 8. 10 12 時 時 (sec). 14. 16. 圖 5.10 135 度(2.3562 rad)倒單擺角度 X1. 39. 18. 20.
(51) 5. 0. 角角(rad). -5. -10. -15. -20. 0. 2. 4. 6. 8. 10 12 時 時 (sec). 14. 16. 圖 5.11 135 度(2.3562 rad)旋轉盤角度 X3. 40. 18. 20.
(52) 2. 0. 角角(rad). -2. -4. -6. -8. -10. -12 0. 2. 4. 6. 8. 10 12 時 時 (sec). 14. 16. 圖 5.12 -135 度(-2.3562 rad)倒單擺角度 X1. 41. 18. 20.
(53) 20. 15. 角角(rad). 10. 5. 0. -5. 0. 2. 4. 6. 8. 10 12 時 時 (sec). 14. 16. 圖 5.13 -135 度(-2.3562 rad)旋轉盤角度 X3. 42. 18. 20.
(54) 4 3.5 3. 角角(rad). 2.5 2 1.5 1 0.5 0 -0.5. 0. 0.5. 1. 圖 5.14. 1.5. 2. 2.5 3 時 時 (sec). 3.5. 4. 180 度(3.1416 rad)倒單擺角度 X1. 43. 4.5. 5.
(55) 0.6. 0.4. 角角(rad). 0.2. 0. -0.2. -0.4. -0.6. -0.8. 0. 0.5. 1. 圖 5.15. 1.5. 2. 2.5 3 時 時 (sec). 3.5. 4. 180 度(3.1416 rad)旋轉盤角度 X3. 44. 4.5. 5.
(56) 圖 5.16. 模擬倒單擺角度 X1. 45.
(57) 圖 5.17 模擬旋轉盤角度 X3. 46.
(58) 第 6 章結論 本論文是針對一個典型不穩定、非線性且存在著不確定的控制平 台 - 旋轉式倒單擺系統。初步結果是利用粒子尋優演算法 (Particle Swarm Optimization Algorithm)結合 Simulink 以及模糊控制來設計系. 統的控制器使的單擺能夠穩定直立。 未來可以使用 PSO 尋找出最好 T-S model 模糊控制器,再使用線 性矩陣不等式(linear matrix inequality,LMI)探討整體系統的穩定性、 及強健性。. 47.
(59) 第 7 章參考文獻 [1]. L. A. Zadeh, ”Fuzzy Sets,” Inform. And control, Vol. 8, pp. 338-353, 1965.. [2]. 王文俊,"認識 Fuzzy(第三版)",全華科技圖書股份有限公司, 民國八十八年。. [3]. 陳重誠,"旋轉式倒單擺動作控制之再設計",國立中央大學碩 士論文,中華民國八十九年。. [4]. 孫宗瀛、楊英魁,"Fuzzy 控制:理論、實作與應用",全華科技 圖書股份有限公司,民國八十八年。. [5]. 鈦思科技股份有限公司編著,"視覺化建模環境 Simulink 入門 與進階",鈦思科技股份有限公司, 中華民國九十年。. [6]. 莊鈞閔,"模糊控制系統設計與實現",淡江大學碩士論文,中 華民國九十一年。. [7]. 王建雄,"模糊控制器之設計思維",國立台北科技大學碩士論 文,中華民國九十一年。. [8]. 周烜達,"二輪自走車之設計與實現",國立中央大學碩士論文, 中華民國九十六年。. [9]. J. Kennedy and R. Eberhart, Particle swarm optimization, IEEE International Conference of Neural Networks, 1995,. pp.. 1942-1948. [10] Y. Ha and S. Yuta, “Trajectory Tracking Control for Navigation of Self-Contained Mobile Inverse Pendulum,” IEEE/RSJ/GI Int. Conf. on Advanced Robotic Systems and the Real World', Vol. 3, pp. 12-16, September 1994. 48.
(60) [11]. F. Grasser, A. D'Arrigo, S. Colombi and A.C. Rufer, “JOE: a mobile, inverted pendulum,” IEEE Transactions on Industrial Electronics, Vol. 39, No. 1, pp. 107-114, Feb. 2002.. [12] G. Y. Park and P.H. Seong, “Towards increasing the learning speed of gradient descent method in fuzzy system,” Fuzzy Sets and Systems, Vol.77, pp.229-313, 1996. [13] K. M. Chow and A.B. Rad, “System identification via a virtual higher-resolution fuzzy model,” Intell. Automat. Soft-Comput. , Vol. 6 No. 4, pp. 243–259 , 2000. [14] K. M. Chew and A.B. Rad, “On-line fuzzy identification using genetic algorithms,” Fuzzy Sets and Systems, Vol. 132, pp. 147-171, 2002. [15] L. A. Zadeh, “Fuzzy sets and systems,” in: Proc. Sysmp. Systems Theory, Polytech. Inst. Brooklyn, pp. 29-37, 1965. [16] L.-X. Wang and J.M. Mendel, “Back-propagation fuzzy system as non-linear dynamic system identifiers,” IEEE Internat. Cnf. on Fuzzy Systems, San Diego, CA, pp. 1409–1412, 1992. [17] Li-Xin Wang, “Design and analysis of fuzzy identifiers of nonlinear dynamic systems,” IEEE Trans. Automat. Control, Vol. 40, pp. 11-23, 1995. [18] T. Sugeno and K. Tanaka, “Successive identification of a fuzzy model and its applications to prediction of a complex system,” Fuzzy Sets Syst., Vol. 42, pp. 315–334, 1991. [19] R. Rovatti and R. Guerrieri, “Fuzzy sets of rules for system identification,” IEEE Trans. Fuzzy Systems, Vol. 4, pp. 89-102, May 1996. [20]. S. G. Cao and N.W. Rees, “Identification of dynamic fuzzy models, ” Fuzzy Sets and Systems, Vol. 74, pp. 307-320, 1995.. [21] W. F. Xie and A.B. Rad, “Fuzzy on-line identification of SISO 49.
(61) nonlinear system ”Fuzzy Sets and Systems,” Vol. 107, pp. 323-334, 1999. [22]. S. G. Cao and N. W. Rees, "Identification of dynamic fuzzy models," Fuzzy Sets and Systems, Vol. 74, pp. 307-320, 1995.. [23] K. Tanaka and H. O. Wang, Fuzzy Control Systems Analysis and Design: A Linear Matrix Inequality Approach, New York: Wiley, 2001. [24] 李賢良,"模糊控制應用於平移振盪台車系統",義守大學電子. 工程研究所碩士論文,中華民國九十四年。 [25]. 孫宗瀛、楊英魁,"Fuzzy 控制:理論、實作與應用",全華科 技圖書出版,中華民國八十四年。. [26]. 楊英魁,"Fuzzy 控制",全華科技圖書出版,中華民國八十一 年。. [27] 周晏鈴,"應用動態模糊系統於旋轉式倒單擺系統之分析與控制 ",大葉大學碩士論文,民國九十三年。 [28] 黃智樑,"微量群聚法之最佳化 LQ 控制器設計",國立高雄海. 洋科技大學輪機工程研究所碩士論文,中華民國九十五年。 [29]. R. E. Perez and K. Behdinan. "Particle swarm approach for structural design optimization," Computers and Structures, Vol. 85, pp. 1579-88, 2007.. [30] 賴彥翔,"粒子群聚最佳化於滑車倒單擺系統之控制",義守大. 學電子工程研究所碩士論文,中華民國九十九年。 [31] 許碩夫,"粒子群聚最佳化應用於倒車控制系統",義守大學電. 子工程研究所碩士論文,中華民國九十九年。 [32] 簡伯丞、洪惠陽、凌衍雷、許碩夫,"粒子尋優演算法應用於旋. 轉式倒單擺之控制",三軍官校學術研討會,中華民國一百年五 月二十七日。 50.
(62) [33] 簡伯丞、洪惠陽、凌衍雷、許碩夫,"粒子尋優演算法應用於旋. 轉式倒單擺之控制",義守大學電子工程技術研討會,中華民國 一百年六月五日。. 51.
(63) 附錄 (一) 旋轉式倒單擺系統 s-function function [sys,x0,str,ts] = back(t,x,u,flag) switch flag, %%%%%%%%%%%%%%%%%% % Initialization % %%%%%%%%%%%%%%%%%% case 0 [sys,x0,str,ts] = mdlInitializeSizes; %%%%%%%%%%%%%%% % Derivatives % %%%%%%%%%%%%%%% case 1, sys = mdlDerivatives(t,x,u); %sys=[]; %%%%%%%%%%% % Outputs % %%%%%%%%%%% case 3 sys = mdlOutputs(t,x,u); %%%%%%%%%%%%% % Terminate % %%%%%%%%%%%%% case { 2, 4, 9 } % Unused flags sys = []; otherwise error(['Unhandled flag = ',num2str(flag)]);. 52.
(64) end % %=================================================== ========================== % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the S-function. %=================================================== ========================== % function [sys,x0,str,ts] = mdlInitializeSizes sizes = simsizes; sizes.NumContStates = 4; sizes.NumDiscStates = 0; sizes.NumOutputs = 4; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; sys = simsizes(sizes); x0 = [45*pi/180;0;0;0]; str = []; ts = [0 0]; % end mdlInitializeSizes % %=================================================== ========================== %=================================================== ========================== % function sys = mdlDerivatives(t,x,u) M=0.09; 53.
(65) m=0.15; l=0.095; L=0.4; g=9.8; a=0.07; I=0.414; f=(-(I+m*(L^2))*g*sin(x(1))+(m*l*L^2*sin(x(1)))*cos(x(1))*(x(2)^2))/( (-4/3)*l*I+m*(L^2)+m*l*(L^2)*((1+cos(2*x(1)))/2)); G=(L*cos(x(1)))/((-(4/3)*l*(I+m*(L^2))+(m*l*(L^2)*((1+cos(2*x(1)))/2 )))); A=((g/L)*tan(x(1)))-(((4*l)/(3*L))*sec(x(1))*(f+G*u)); sys=[x(2);f+G*u;x(4);A]; % %=================================================== ========================== % mdlOutputs % Return the output vector for the S-function %=================================================== ========================== % function sys = mdlOutputs(t,x,u) sys(1) =x(1); sys(2) =x(2); sys(3) =x(3); sys(4) =x(4);. % end mdlOutputs. 54.
(66) (二) PSO 主程式 function [xOpt,fval,exitflag,output,population,scores] = ... pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB,nonlcon,options) global K % Find the minimum of a function using Particle Swarm Optimization. % % This is an implementation of Particle Swarm Optimization algorithm using % the same syntax as the Genetic Algorithm Toolbox, with some additional % options specific to PSO. Allows code-reusability when trying different % population-based optimization algorithms. Certain GA-specific parameters % such as cross-over and mutation functions will not be applicable to the % PSO algorithm. Demo function included, with a small library of test % functions. Requires Optimization Toolbox. % % In development, new features will be added regularly until this is made % redundant by an official MATLAB PSO toolbox. % % S. Chen. Version 20100125. % Available from http://www.mathworks.com/matlabcentral/fileexchange/25986 % Distributed under BSD license. % % Syntax: % psodemo % pso % x = pso(fitnessfcn,nvars) % x = pso(fitnessfcn,nvars,Aineq,bineq) % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq) % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB) % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB,nonlcon) % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB,nonlcon,options) % x = pso(problem) % [x, fval] = pso(...) 55.
(67) % [x, fval,exitflag] = pso(...) % [x, fval,exitflag,output] = pso(...) % [x, fval,exitflag,output,population] = pso(...) % [x, fval,exitflag,output,population,scores] = pso(...) % % Description: % psodemo % Runs a demonstration of the PSO algorithm using test function specified % by user. % % pso % Runs a default demonstration using Rosenbrock's banana function. % % x = pso(fitnessfcn,nvars) % Runs the particle swarm algorithm with no constraints and default % options. fitnessfcn is a function handle, nvars is the number of % parameters to be optimized, i.e. the dimensionality of the problem. % % x = pso(fitnessfcn,nvars,Aineq,bineq) % Linear constraints, such that Aineq*x <= bineq. Soft boundaries only. % Aineq is a matrix of size nconstraints x nvars, while b is a column % vector of length nvars. % % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq) % Linear equality constraints, such that Aeq*x = beq. Soft boundaries only. % If no inequality constraints exist, set Aineq and bineq to []. % % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB) % Lower and upper bounds definted as LB and UB respectively. Use empty % arrays [] for A, b, Aeq, or beq if no linear constraints exist. % % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB,nonlcon) % Non-linear constraints. Nonlinear inequality constraints in the form c(x) % <= 0 have now been implemented using 'soft' boundaries only. See the 56.
(68) % Optimization Toolbox documentation for the proper syntax for defining % nonlinear constraints. % % x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB,nonlcon,options) % Default algorithm parameters replaced with those defined in the options % structure: % Use >> options = psooptimset('Param1,'value1','Param2,'value2',...) to % generate the options structure. Type >> psooptimset with no input or % output arguments to display a list of available options and their % default values. % % NOTE: the swarm will perform better if the PopInitRange option is defined % so that it closely bounds the expected domain of the feasible region. % This is automatically done for lower and upper bound constraints, but not % for linear and nonlinear constraints. % % x = pso(problem) % Parameters imported from problem structure. Should work, but no error % checking yet. % % [x, fval] = pso(...) % Returns the fitness value of the best solution. % % [x, fval,exitflag] = pso(...) % Returns information on outcome of pso run. Should match exitflag values % for GA where applicable, for code reuseability. % % [x, fval,exitflag,output] = pso(...) % The structure output contains more detailed information about the PSO % run. Should match output fields of GA, where applicable. % % [x, fval,exitflag,output,population] = pso(...) 57.
(69) % A matrix population of size PopulationSize x nvars, with the locations of % particles across the rows. % % [x, fval,exitflag,output,population,scores] = pso(...) % Final scores of the particles in population. % % See also: % PSODEMO, PSOOPTIMSET. if ~nargin % Rosenbrock's banana function by default, as a demonstration fitnessfcn = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2 ; nvars = 2 ; LB = [-1.5,-2] ; UB = [2,2] ; options.PopInitRange = [[-2;4],[-1;2]] ; options.PlotFcns = {@psoplotbestf,@psoplotswarmsurf} ; options.Generations = 50 ; options.DemoMode = 'on' ; options.KnownMin = [1 1] ; elseif isstruct(fitnessfcn) nvars = fitnessfcn.nvars ; Aineq = fitnessfcn.Aineq ; bineq = fitnessfcn.bineq ; Aeq = fitnessfcn.Aeq ; beq = fitnessfcn.beq ; LB = fitnessfcn.LB ; UB = fitnessfcn.UB ; nonlcon = fitnessfcn.nonlcon ; if ischar(nonlcon) && ~isempty(nonlcon) nonlcon = str2func(nonlcon) ; end options = fitnessfcn.options ; fitnessfcn = fitnessfcn.fitnessfcn ; elseif nargin < 2 msg = 'PSO requires at least two input arguments' ; error('%s, or a problem structure. Type >> help pso for details',... msg) 58.
(70) end % if ~nargin if ~exist('options','var') % Set default options options = struct ; end % if ~exist options = psooptimset(options) ; if ~exist('Aineq','var'), Aineq = [] ; end if ~exist('bineq','var'), bineq = [] ; end if ~exist('Aeq','var'), Aeq = [] ; end if ~exist('beq','var'), beq = [] ; end if ~exist('LB','var'), LB = [] ; end if ~exist('UB','var'), UB = [] ; end if ~exist('nonlcon','var'), nonlcon = [] ; end % Change this when nonlcon gets fully implemented: if ~isempty(nonlcon) && strcmpi(options.ConstrBoundary,'reflect') msg = 'Non-linear constraints don''t have ''reflect'' boundaries' ; warning('pso:main:nonlcon',... '%s implemented. Changing options.ConstrBoundary to ''soft''.',... msg) options.ConstrBoundary = 'soft' ; end % Is options.PopInitRange reconcilable with LB and UB constraints? % ------------------------------------------------------------------------% Resize PopInitRange in case it was given as one range for all dimensions if size(options.PopInitRange,2) == 1 && nvars > 1 options.PopInitRange = repmat(options.PopInitRange,1,nvars) ; end % Check initial population with respect to bound constraints % Is this really desirable? Maybe there are some situations where the user % specifically does not want an uniform inital population covering all of % LB and UB? if ~isempty(LB) || ~isempty(UB) 59.
(71) options.LinearConstr.type = 'boundconstraints' ; options.PopInitRange = ... psocheckpopulationinitrange(options.PopInitRange,LB,UB) ; end % ------------------------------------------------------------------------% Generate swarm initial state (this line must not be moved) state = psocreationuniform(options,nvars) ; % Check initial population with respect to linear and nonlinear constraints % ------------------------------------------------------------------------if ~isempty(Aeq) || ~isempty(Aineq) || ~isempty(nonlcon) options.LinearConstr.type = 'linearconstraints' ; if ~isempty(nonlcon) options.LinearConstr.type = 'nonlinearconstraints' ; end if strcmpi(options.ConstrBoundary,'reflect') options.ConstrBoundary = 'soft' ; msg = sprintf('Constraint boundary behavior ''reflect''') ; msg = sprintf('%s is not yet supported for linear constraints.',... msg) ; msg = sprintf('%s Switching boundary behavior to ''soft''.',msg) ; warning('pso:mainfcn:constraintbounds',... '%s',msg) end [state,options] = psocheckinitialpopulation(state,... Aineq,bineq,Aeq,beq,... LB,UB,... nonlcon,... options) ; end % ------------------------------------------------------------------------n = options.PopulationSize ; itr = options.Generations ;. 60.
(72) if ~isempty(options.PlotFcns) close(findobj('Tag','Swarm Plots','Type','figure')) state.hfigure = figure('NumberTitle','off',... 'Name','PSO Progress',... 'Tag','Swarm Plots') ; end % if ~isempty fprintf('\nSwarming...') exitflag = 0 ; % Default exitflag, for max iterations reached. flag = 'init' ; % Iterate swarm state.fitnessfcn = fitnessfcn ; state.LastImprovement = 1 ; state.ParticleInertia = 0.9 ; % Initial inertia % alpha = 0 ; for k = 1:itr state.Score = inf*ones(n,1) ; % Reset fitness vector state.Generation = k ; state.OutOfBounds = false(options.PopulationSize,1) ; % Check bounds before proceeding % --------------------------------------------------------------------if ~all([isempty([Aineq,bineq]), isempty([Aeq,beq]), ... isempty([LB;UB]), isempty(nonlcon)]) state = psocheckbounds(options,state,Aineq,bineq,Aeq,beq,... LB,UB,nonlcon) ; end % if ~isempty % --------------------------------------------------------------------% Evaluate fitness, update the local bests % --------------------------------------------------------------------if strcmpi(options.Vectorized,'off') for i = setdiff(1:n,find(state.OutOfBounds)) state.Score(i) = fitnessfcn(state.Population(i,:)) ; end % for i else % Vectorized fitness function state.Score(setdiff(1:n,find(state.OutOfBounds))) = ... 61.
(73) fitnessfcn(state.Population(setdiff(1:n,... find(state.OutOfBounds)),:)) ; end % if strcmpi betterindex = state.Score < state.fLocalBests ; state.fLocalBests(betterindex) = state.Score(betterindex) ; state.xLocalBests(betterindex,:) = ... state.Population(betterindex,:) ; % --------------------------------------------------------------------% Update the global best and its fitness, then check for termination % --------------------------------------------------------------------[minfitness, minfitnessindex] = min(state.Score) ; % % % %. alpha = alpha + (1/k) * ... ((1/n)*sum((state.Velocities*state.Velocities')^2) ./ ... ((1/n)*sum(state.Velocities*state.Velocities')).^2) ; tempchk = alpha <= 1.6 ; if minfitness < state.fGlobalBest state.fGlobalBest(k) = minfitness ; state.xGlobalBest = state.Population(minfitnessindex,:) ; state.LastImprovement = k ; imprvchk = k > options.StallGenLimit && ... (state.fGlobalBest(k - options.StallGenLimit) - ... state.fGlobalBest(k)) / (k - options.StallGenLimit). < ... options.TolFun ; if imprvchk exitflag = 1 ; flag = 'done' ; elseif state.fGlobalBest(k) < options.FitnessLimit exitflag = 2 ; flag = 'done' ; end % if k else % No improvement from last iteration state.fGlobalBest(k) = state.fGlobalBest(k-1) ; end % if minfitness. 62.
(74) stallchk = k - state.LastImprovement >= options.StallGenLimit ; if stallchk % No improvement for StallGenLimit generations exitflag = 3 ; flag = 'done' ; end % --------------------------------------------------------------------% Update flags, state and plots before updating positions % --------------------------------------------------------------------if k == 2 flag = 'iter' ; elseif k == itr flag = 'done' ; exitflag = 0 ; end if ~isempty(options.PlotFcns) && ~mod(k,options.PlotInterval) % Exit gracefully if user has closed the figure if isempty(findobj('Tag','Swarm Plots','Type','figure')) exitflag = -1 ; break end % if isempty % Find a good size for subplot array rows = floor(sqrt(length(options.PlotFcns))) ; cols = ceil(length(options.PlotFcns) / rows) ; % Cycle through plotting functions if strcmpi(flag,'init') haxes = zeros(length(options.PlotFcns),1) ; end % if strcmpi for i = 1:length(options.PlotFcns) if strcmpi(flag,'init') haxes(i) = subplot(rows,cols,i,... 'Parent',state.hfigure) ; else subplot(haxes(i)) end % if strcmpi state = options.PlotFcns{i}(options,state,flag) ; 63.
(75) end % for i drawnow end % if ~isempty if ~isempty(options.OutputFcns) && ~mod(k,options.PlotInterval) for i = 1:length(options.Output) state = options.OutputFcns{i}(options,state,flag) ; end % for i end % if ~isempty if strcmpi(flag,'done') break end % if strcmpi % --------------------------------------------------------------------% Update the particle velocities and positions state = psoiterate(state,options) ; end % for k % Assign output variables and generate output % ------------------------------------------------------------------------xOpt = state.xGlobalBest ; fval = state.fGlobalBest(k) ; % Best fitness value % Final population: (hopefully very close to each other) population = state.Population ; scores = state.Score ; % Final scores (NOT local bests) output.generations = k ; % Number of iterations performed clear state output.message = psogenerateoutputmessage(options,output,exitflag) ; fprintf('\n\n%s\n',output.message) ; % ------------------------------------------------------------------------% Check for hybrid function, run if necessary % ------------------------------------------------------------------------if ~isempty(options.HybridFcn) && exitflag ~= -1 [xOpt,fval] = psorunhybridfcn(fitnessfcn,xOpt,Aineq,bineq,... Aeq,beq,LB,UB,nonlcon,options) ; 64.
(76) end % ------------------------------------------------------------------------% Wrap up % ------------------------------------------------------------------------if exitflag == -1 fprintf('\nBest point found: %s\n\n',mat2str(xOpt,5)) else fprintf('\nFinal best point: %s\n\n',mat2str(xOpt,5)) end if ~nargout, clear all, end % -----------------------------------------------------------. 65.
(77) (三) PSO 參數程式 function options = psooptimset(varargin) % Creates an options structure for pso. % % Syntax: % psooptimset % options = psooptimset % options = psooptimset(@pso) % options = psooptimset(@psomultiobj) % options = psooptimset('param1',value1,'param2',value2,...) % options = psooptimset(oldopts,'param1',value1,...) % options = psooptimset(oldopts,newopts) % % Description: % psooptimset with no input or output arguments displays a complete list of % parameters with their valid values. % % options = psooptimset (with no input arguments) creates a structure % called options that contains the options, or parameters, for the pso % algorithm and sets parameters to [], indicating default values will be % used. % % options = psooptimset(@pso) creates a structure called options that % contains the default options for the genetic algorithm. % % options = psooptimset(@psomultiobj) creates a structure called options % that contains the default options for psomultiobj. Not yet implemented % % options = psooptimset('param1',value1,'param2',value2,...) creates a % structure options and sets the value of 'param1' to value1, 'param2' to % value2, and so on. Any unspecified parameters are set to their default % values. Case is ignored for parameter names. % % options = psooptimset(oldopts,'param1',value1,...) creates a copy of 66.
(78) % oldopts, modifying the specified parameters with the specified values. % % options = psooptimset(oldopts,newopts) combines an existing options % structure, oldopts, with a new options structure, newopts. Any parameters % in newopts with nonempty values overwrite the corresponding old % parameters in oldopts. % % Again, type psooptimset with no input arguments to display a list of % options which may be set. % NOTE regarding the ConstrBoundary option: % A 'soft' boundary allows particles to leave problem bounds, but sets % their fitness scores to Inf if they do. Other acceptable options are % 'reflect' and 'absorb', which prevents them from travelling outside the % problem bounds at all. % % See also: % pso, psodemo % Default options options.CognitiveAttraction = 0.5 ; options.ConstrBoundary = 'soft' ; options.DemoMode = 'off' ; options.FitnessLimit = -inf ; options.Generations =2000; options.HybridFcn = [] ; options.InitialPopulation = [] ; options.InitialVelocities = [] ; options.KnownMin = [] ; options.OutputFcns = [] ; options.PlotFcns = {@psoplotbestf} ; options.PlotInterval = 1 ; options.PopInitRange = [-10;10] ; options.PopulationSize = 10 ; options.SocialAttraction = 1.5 ; options.StallGenLimit = inf ; options.TolCon = 1e-6; options.TolFun = 1e-6; 67.
(79) options.Vectorized = 'off' ; options.VelocityLimit = [] ; if ~nargin && ~nargout fprintf('\n') fprintf('CognitiveAttraction: [Positive scalar | {%g}]\n',... options.CognitiveAttraction) ; fprintf(' ConstrBoundary: [soft | reflect | absorb | {%s}]\n',... options.ConstrBoundary) ; fprintf(' DemoMode: [fast | pretty | on | off | {%s}]\n',... options.DemoMode) ; fprintf(' FitnessLimit: [Scalar | {%g}]\n',... options.FitnessLimit) ; fprintf(' Generations: [Positive integer | {%g}]\n',... options.Generations) ; msg = sprintf(' HybridFcn: [@fminsearch | @patternsearch |'); fprintf('%s @fminunc | @fmincon | {[]}]\n',msg) fprintf(' InitialPopulation: [nxnvars matrix | {[]}]') fprintf(' InitialVelocities: [nxnvars matrix | {[]}]') % PlotFcns, a bit tricky to turn into a string: % --------------------------------------------------------------------if ~isempty(options.PlotFcns) msg = '{' ; for i = 1:length(options.PlotFcns) msg = sprintf('%s@%s, ',msg,func2str(options.PlotFcns{i})) ; end % for i msg = sprintf('%s\b\b}',msg) ; else msg = '{}' ; end fprintf(' PlotFcns: [Cell array of fcn handles | {%s}]\n',... msg) ; % --------------------------------------------------------------------fprintf(' PlotInterval: [Positive integer | {%g}]\n',... options.PlotInterval) ; 68.
(80) fprintf(' PopInitRange: [2x1 vector | 2xnvars matrix | {%s}]\n',... mat2str(options.PopInitRange)) ; fprintf(' PopulationSize: [Positive integer | {%g}]\n',... options.PopulationSize) ; fprintf(' SocialAttraction: [Positive scalar | {%g}]\n',... options.SocialAttraction) ; fprintf(' StallGenLimit: [Positive integer | {%g} ]\n',... options.StallGenLimit) ; fprintf(' TolFun: [Positive scalar | {%g}]\n',... options.TolFun) ; fprintf(' TolCon: [Positive scalar | {%g}]\n',... options.TolCon) ; fprintf(' Vectorized: [on | off | {%s}]\n',... options.Vectorized) ; fprintf(' VelocityLimit: [Positive scalar | {[]}]\n'); fprintf('\n') clear options return end if ~nargin || isequal(varargin{1},@pso) return elseif isstruct(varargin{1}) oldoptions = varargin{1} ; fieldsprovided = fieldnames(oldoptions) ; end requiredfields = fieldnames(options) ; % Find any input arguments that match valid field names. If they exist, % replace the default values with them. for i = 1:size(requiredfields,2) idx = find(cellfun(@(varargin)strcmpi(varargin,requiredfields{i,1}),... varargin)) ; if ~isempty(idx) options.(requiredfields{i,1}) = varargin(idx(end) + 1) ; 69.
(81) options.(requiredfields{i,1}) = options.(requiredfields{i,1}){:} ; elseif exist('fieldsprovided','var') fieldidx = find(cellfun(@(fieldsprovided)strcmp(fieldsprovided,... requiredfields{i,1}),... fieldsprovided)) ; if ~isempty(fieldidx) options.(requiredfields{i,1}) = ... oldoptions.(fieldsprovided{fieldidx}) ; end end % if ~isempty end % for i % Some robustness if isequal(size(options.PopInitRange),[0 1]) options.PopInitRange = options.PopInitRange' ; end. 70.
(82)
數據
相關文件
Abstract - The main purpose of this study is applying TRIZ theory to construct the Green Supply Chain management (GSCM) strategies for the international tourist hotel.. Based on
The main purpose of this study is applying TRIZ theory to construct the Green Supply Chain management (GSCM) strategies for the international tourist hotel1. Based on the
The main purpose of this paper is using Java language with object-oriented and cross platform characteristics and Macromedia Dreamweaver MX to establish a JSP web site with
The purpose of this research is to develop an approach that uses the triangular distribution with the Fractile Method to estimate the optimistic and pessimistic duration of
The neural controller using an asymmetric self-organizing fuzzy neural network (ASOFNN) is designed to mimic an ideal controller, and the robust controller is designed to
The purpose of this study is to use schematic video and animation to help students with LD solve real-life mathematical word problems.. The single-subject
The purpose of this paper is to use data mining method in semiconductor production to explore the relation of engineering data and wafer accept test.. In this paper, we use two
The main purpose of this study is to explore the status quo of the food quality and service quality for the quantity foodservice of the high-tech industry in Taiwan;