|
13 | 13 | ] |
14 | 14 | }, |
15 | 15 | { |
16 | | - "cell_type": "code", |
17 | | - "execution_count": 9, |
18 | | - "metadata": { |
19 | | - "collapsed": false |
20 | | - }, |
21 | | - "outputs": [ |
22 | | - { |
23 | | - "data": { |
24 | | - "text/plain": [ |
25 | | - "1" |
26 | | - ] |
27 | | - }, |
28 | | - "execution_count": 9, |
29 | | - "metadata": {}, |
30 | | - "output_type": "execute_result" |
31 | | - } |
32 | | - ], |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
33 | 18 | "source": [ |
34 | | - "from fuzzy.functions import alpha, singleton, constant\n", |
35 | | - "\n", |
| 19 | + "## Usage\n", |
36 | 20 | "\n", |
37 | | - "a = alpha(0.2, 0.8)\n", |
38 | | - "s = singleton(5)\n", |
39 | | - "\n" |
| 21 | + "1. Define a Domain and specify the measurable range & resolution.\n", |
| 22 | + "2. Define some fuzzysets (\"adjectives\") as attributes that describe ranges within\n", |
| 23 | + "the domain using Set(). Each set is defined by its membership function func(REAL) -> [0,1]\n", |
| 24 | + "3. You can combine fuzzysets of the same domain using fuzzy logic operators (~, &, |, +, *) to\n", |
| 25 | + "define derived fuzzysets.\n", |
| 26 | + "4. Now suppose you have a value x. Call the fuzzyset(x) directly to produce a\n", |
| 27 | + "membership value or call the domain with it to get a dictionary of memberships.\n", |
| 28 | + "5. To really put these to work, you will want to map membership values of one or\n", |
| 29 | + "more domains onto another domain, producing \"real world\" values again that can\n", |
| 30 | + "be used to control things. This is called inference (combining domains) and\n", |
| 31 | + "defuzzification (turning membership values back into measurables)." |
40 | 32 | ] |
41 | 33 | }, |
42 | 34 | { |
43 | 35 | "cell_type": "code", |
44 | | - "execution_count": 3, |
| 36 | + "execution_count": null, |
45 | 37 | "metadata": { |
46 | 38 | "collapsed": false |
47 | 39 | }, |
48 | | - "outputs": [ |
49 | | - { |
50 | | - "ename": "TypeError", |
51 | | - "evalue": "'>=' not supported between instances of 'function' and 'float'", |
52 | | - "output_type": "error", |
53 | | - "traceback": [ |
54 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
55 | | - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
56 | | - "\u001b[0;32m<ipython-input-3-7ecf006a5990>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mnumbers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDomain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"numbers\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhigh\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mclose_to_10\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0malpha\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0.8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtriangular\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mclose_to_10\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
57 | | - "\u001b[0;32m/media/sf_Google_Drive/jupyter/fuzzy/fuzzy/functions.py\u001b[0m in \u001b[0;36mf_value\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mf_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 92\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mceiling\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 93\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mceiling\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 94\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mfloor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
58 | | - "\u001b[0;31mTypeError\u001b[0m: '>=' not supported between instances of 'function' and 'float'" |
59 | | - ] |
60 | | - } |
61 | | - ], |
| 40 | + "outputs": [], |
62 | 41 | "source": [ |
| 42 | + "%matplotlib inline\n", |
63 | 43 | "from fuzzy.classes import Domain, Set\n", |
64 | 44 | "from fuzzy.functions import alpha, triangular\n", |
| 45 | + "from fuzzy.hedges import plus, minus\n", |
65 | 46 | "\n", |
66 | | - "numbers = Domain(\"numbers\", low=0, high=20)\n", |
67 | | - "close_to_10 = Set(numbers, alpha(0.2,0.8)(triangular(0, 20)))\n", |
68 | | - "close_to_10.plot()" |
| 47 | + "numbers = Domain(\"numbers\", low=0, high=20, res=0.1)\n", |
| 48 | + "close_to_10 = Set(numbers, alpha(0.2, 0.8, \n", |
| 49 | + " triangular(0, 20)\n", |
| 50 | + " ))\n", |
| 51 | + "close_to_5 = Set(numbers, triangular(1,10))\n", |
| 52 | + "foo = plus(minus(close_to_5))\n", |
| 53 | + "bar = close_to_10\n", |
| 54 | + "bar.plot()\n", |
| 55 | + "foo.plot()\n", |
| 56 | + "baz = foo + bar\n", |
| 57 | + "baz.plot()" |
69 | 58 | ] |
70 | 59 | }, |
71 | 60 | { |
72 | | - "cell_type": "code", |
73 | | - "execution_count": null, |
74 | | - "metadata": { |
75 | | - "collapsed": false |
76 | | - }, |
77 | | - "outputs": [], |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
78 | 63 | "source": [ |
79 | | - "%matplotlib inline\n", |
80 | | - "from fuzzy.classes import Domain, Set, Rule\n", |
81 | | - "from fuzzy.functions import S, R\n", |
| 64 | + "## Rules\n", |
82 | 65 | "\n", |
83 | | - "zeit = Domain(\"time\", low=0, high=24, res=0.1)\n", |
84 | | - "morgen = Set(zeit, S(0, 8))\n", |
85 | | - "abend = Set(zeit, R(18, 24))\n", |
86 | | - "mittag = ~morgen & ~abend\n", |
87 | | - "\n", |
88 | | - "morgen.plot()\n", |
89 | | - "abend.plot()\n", |
90 | | - "mittag.plot()\n" |
| 66 | + "Rules aka Fuzzy Inference is done when domains and sets are defined to combine sets of different domains. " |
91 | 67 | ] |
92 | 68 | }, |
93 | 69 | { |
94 | 70 | "cell_type": "code", |
95 | | - "execution_count": null, |
| 71 | + "execution_count": 7, |
96 | 72 | "metadata": { |
97 | 73 | "collapsed": false |
98 | 74 | }, |
99 | | - "outputs": [], |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "ename": "AttributeError", |
| 78 | + "evalue": "'NotImplementedType' object has no attribute 'copy'", |
| 79 | + "output_type": "error", |
| 80 | + "traceback": [ |
| 81 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 82 | + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", |
| 83 | + "\u001b[0;32m<ipython-input-7-4a94e0f36941>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0md1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtemp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m32\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# {'temperature.hot': 1}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0md2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# {'distance.close': 0}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0md1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# need to merge the results of the Domains\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# for py3.5: https://www.python.org/dev/peps/pep-0448/\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# min(1, 0)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 84 | + "\u001b[0;31mAttributeError\u001b[0m: 'NotImplementedType' object has no attribute 'copy'" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
100 | 88 | "source": [ |
101 | 89 | "from fuzzy.classes import Domain, Set, Rule\n", |
102 | | - "from fuzzy.functions import S, R\n", |
103 | | - "\n", |
104 | | - "dist = Domain(\"distance\", low=0, high=1000, res=0.1)\n", |
105 | | - "near = Set(dist, S(0, 100))\n", |
106 | | - "\n", |
107 | | - "standing = Domain(\"standing\", low=0, high=100)\n", |
108 | | - "friendly = Set(standing, R(0, 100))\n", |
109 | | - "\n", |
110 | | - "reaction = Domain(\"reaction\", low=0, high=10)\n", |
111 | | - "aggro = IF(near & ~friendly, reaction)" |
| 90 | + " \n", |
| 91 | + "temp = Domain(\"temperature\", 0, 100)\n", |
| 92 | + "temp.hot = Set(temp, lambda x: 1)\n", |
| 93 | + "dist = Domain(\"distance\", 0, 300)\n", |
| 94 | + "dist.close = Set(dist, lambda x: 0)\n", |
| 95 | + "r = Rule(min, [\"distance.close\", \"temperature.hot\"])\n", |
| 96 | + "d1 = temp(32) # {'temperature.hot': 1}\n", |
| 97 | + "d2 = dist(5) # {'distance.close': 0}\n", |
| 98 | + "d = d1.copy() # need to merge the results of the Domains\n", |
| 99 | + "d.update(d2) # for py3.5: https://www.python.org/dev/peps/pep-0448/\n", |
| 100 | + "r(d) # min(1, 0)" |
112 | 101 | ] |
113 | 102 | }, |
114 | 103 | { |
|
0 commit comments