1
2
3 """Example controller which uses a prelearned neural network
4 for controlling the inverted pendulum."""
5
6 import math
7
8 try:
9 import numpy
10
11
12
14
15 return numpy.array(weights, numpy.float64).transpose()
16
18
19 return numpy.array(data,numpy.float64)
20
22
23 return 1.0/(1.0+numpy.exp(-1.0*numpy.dot(data,weights)))
24
25 except ImportError:
26
27 try:
28
29 import Numeric
30
31
32
34
35 return Numeric.transpose(Numeric.array(weights, Numeric.Float64))
36
38
39 return Numeric.array(data,Numeric.Float64)
40
42
43 return 1.0/(1.0+Numeric.exp(-1.0*Numeric.matrixmultiply(data,weights)))
44
45 except ImportError:
46
47
48
50 """transpose a matrix"""
51 return zip(*matrix)
52
54 """multiply vector with matrix.
55 b must be transposed"""
56 return [sum([a__*b__ for (a__,b__) in zip(a,b_)]) for b_ in b]
57
59 """multiply two matrices.
60 b must be transposed"""
61 return [vectormatrixmultiply(a_,b) for a_ in a]
62
64 """apply function to all elements of vector"""
65 return [f(x) for x in vector]
66
68 """apply function to all elements of matrix"""
69 return [applyfuncvector(f,vector) for vector in matrix]
70
71
72
74 """build array and transpose"""
75 return transpose(weights)
76
78 """build vector from list"""
79 return data
80
82 """sigmoidal function."""
83 return 1.0/(1.0+math.exp(-1.0*x))
84
89
90
91 input_hidden1 = prepareWeightMatrices((
92
93 [0,0,0,0,0],
94 [1.3523004, -67.2762222, -2.7101681, -1.2252374, -0.1924101],
95 [3.5358984, -3.0979257, 11.8105240, -2.0400195, -5.6440673],
96 [2.2998035, -6.4394584, -0.4456880, 13.2240639, -1.9416124],
97 [-5.3507094, -25.7565060, -0.8581331, -2.4438772, 0.3309670],
98 [-11.8540325, -27.4872322, -0.4254195, 2.7986245, 0.8198755],
99 [-2.3312118, -0.4025910, -2.0389884, 3.8539076, 3.2488785],
100 [-14.8030014, -19.3887348, -2.1227808, -2.5157831, -1.3637272],
101 [-21.9581738, -38.1970100, -0.7618715, 2.2844830, 1.3290895]
102 ))
103
104
105 hidden1_hidden2 = prepareWeightMatrices((
106
107 [0,0,0,0,0,0,0,0,0],
108 [-0.2244532, 6.8226333, 5.7899127, 1.6202815, 8.7983570, -15.6285400, 3.5351822, -8.2750082, -6.9833350],
109 [-14.1263733, -9.2765779, 12.6322460, 0.0562423, -7.2784958, -4.2336187, 6.9077992, 3.8148420, -4.8503165],
110 [-1.7320144, -2.2005868, -0.0205769, 2.0873408, 3.7128136, -3.4949446, 2.3434448, 7.4144168, -9.0030613],
111 [-13.3777761, 14.2169991, 6.5337262, -1.3075531, -8.6651535, -24.3653679, 2.9219866, -10.9699783, -7.3186345]
112 ))
113
114
115 hidden2_output = prepareWeightMatrices((
116
117 [2.6952314, -2.1971807, -1.7768925, -1.1596987, -3.1659257],
118 ))
119
120
121 arctanh_0_8 = 1.098612
122
123
124 from simulation import Controller
126 """Neural net controller. Trained from data gained
127 with the fuzzy controller."""
128
130
131 try:
132 Phi = input["Phi"]
133 except KeyError:
134 Phi = 90.0
135 try:
136 dPhi_dT = input["dPhi_dT"]
137 except KeyError:
138 dPhi_dT = 0.0
139 try:
140 X = input["X"]
141 except KeyError:
142 X = 0.0
143 try:
144 dX_dT = input["dX_dT"]
145 except KeyError:
146 dX_dT = 0.0
147
148 input_ = makeVector((1.0,
149 (Phi-180.0) *2.0/(380.0-(-20.0)),
150 math.tanh(dPhi_dT/500.0 * arctanh_0_8),
151 math.tanh(X/50.0 * arctanh_0_8),
152 math.tanh(dX_dT/20.0 * arctanh_0_8)
153 ))
154
155 hidden1 = feedForwardStep(input_,input_hidden1)
156 hidden1[0] = 1.0
157
158 hidden2 = feedForwardStep(hidden1,hidden1_hidden2)
159 hidden2[0] = 1.0
160
161 output_ = feedForwardStep(hidden2,hidden2_output)
162
163 output["a"] = (output_[0]*60.0)-30.0
164
165 return output
166