質問編集履歴
2
追加説明
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,25 +12,123 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
+
まず初めに
|
16
|
+
|
17
|
+
プログラム内
|
18
|
+
|
19
|
+
x = Variable(n, T + 1)
|
20
|
+
|
21
|
+
u = Variable(m, T)
|
22
|
+
|
23
|
+
だと以下のエラーが出たので
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
PS C:\Users\N\Desktop> python test.py
|
30
|
+
|
31
|
+
Simulation start
|
32
|
+
|
33
|
+
Traceback (most recent call last):
|
34
|
+
|
35
|
+
File "test.py", line 21, in <module>
|
36
|
+
|
37
|
+
x = Variable(n, T + 1)
|
38
|
+
|
39
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\variable.py", line 75, in __init__
|
40
|
+
|
41
|
+
raise TypeError("Variable name %s must be a string." % name)
|
42
|
+
|
43
|
+
TypeError: Variable name 51 must be a string.
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
x = Variable((n, T + 1))
|
48
|
+
|
49
|
+
u = Variable((m, T))
|
50
|
+
|
51
|
+
に変更しました.
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
そして実行すると
|
56
|
+
|
15
|
-
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出て
|
57
|
+
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出てたのですが,他のサイト等を調べても同様に書かれているのでどこが違うのかわかりません.
|
16
58
|
|
17
59
|
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]を記述することで制約条件を2つ追加できるそうなのですが....
|
18
60
|
|
61
|
+
|
62
|
+
|
63
|
+
載せているソースコードは
|
64
|
+
|
65
|
+
x = Variable((n, T + 1))
|
66
|
+
|
67
|
+
u = Variable((m, T))
|
68
|
+
|
69
|
+
に変更したものを載せています.
|
70
|
+
|
19
71
|
### 発生している問題・エラーメッセージ
|
20
72
|
|
21
73
|
|
22
74
|
|
23
75
|
```
|
24
76
|
|
77
|
+
PS C:\Users\N\Desktop> python test.py
|
78
|
+
|
25
79
|
Simulation start
|
26
80
|
|
27
81
|
Traceback (most recent call last):
|
28
82
|
|
29
83
|
File "test.py", line 32, in <module>
|
30
84
|
|
31
|
-
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
|
85
|
+
prob.constraints += [x[:, T] == 0, x[:, 0] == x_0]
|
86
|
+
|
32
|
-
|
87
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
88
|
+
|
89
|
+
return binary_op(self, other)
|
90
|
+
|
91
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 575, in __eq__
|
92
|
+
|
93
|
+
return Equality(self, other)
|
94
|
+
|
95
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\constraints\zero.py", line 114, in __init__
|
96
|
+
|
97
|
+
self._expr = lhs - rhs
|
98
|
+
|
99
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
100
|
+
|
101
|
+
return binary_op(self, other)
|
102
|
+
|
103
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 464, in __sub__
|
104
|
+
|
105
|
+
return self + -other
|
106
|
+
|
107
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
108
|
+
|
109
|
+
return binary_op(self, other)
|
110
|
+
|
111
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 452, in __add__
|
112
|
+
|
113
|
+
return cvxtypes.add_expr()([self, other])
|
114
|
+
|
115
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\affine\add_expr.py", line 33, in __init__
|
116
|
+
|
117
|
+
super(AddExpression, self).__init__(*arg_groups)
|
118
|
+
|
119
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\atom.py", line 45, in __init__
|
120
|
+
|
121
|
+
self._shape = self.shape_from_args()
|
122
|
+
|
123
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\affine\add_expr.py", line 41, in shape_from_args
|
124
|
+
|
125
|
+
return u.shape.sum_shapes([arg.shape for arg in self.args])
|
126
|
+
|
127
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\utilities\shape.py", line 49, in sum_shapes
|
128
|
+
|
129
|
+
len(shapes)*" %s" % tuple(shapes))
|
130
|
+
|
33
|
-
|
131
|
+
ValueError: Cannot broadcast dimensions (4,) (4, 1)
|
34
132
|
|
35
133
|
```
|
36
134
|
|
@@ -56,15 +154,15 @@
|
|
56
154
|
|
57
155
|
np.random.seed(1)
|
58
156
|
|
59
|
-
n =
|
157
|
+
n = 4 # state size
|
60
|
-
|
158
|
+
|
61
|
-
m =
|
159
|
+
m = 2 # input size
|
62
|
-
|
160
|
+
|
63
|
-
T = 50 #number of horizon
|
161
|
+
T = 50 # number of horizon
|
64
|
-
|
65
|
-
|
66
|
-
|
162
|
+
|
163
|
+
|
164
|
+
|
67
|
-
#simulation parameter
|
165
|
+
# simulation parameter
|
68
166
|
|
69
167
|
alpha = 0.2
|
70
168
|
|
@@ -74,15 +172,15 @@
|
|
74
172
|
|
75
173
|
# Model Parameter
|
76
174
|
|
77
|
-
A = np.eye(n) + alpha*np.random.randn(n,n)
|
175
|
+
A = np.eye(n) + alpha * np.random.randn(n, n)
|
78
|
-
|
176
|
+
|
79
|
-
B = np.random.randn(n,m)
|
177
|
+
B = np.random.randn(n, m)
|
80
|
-
|
178
|
+
|
81
|
-
x_0 = beta*np.random.randn(n,1)
|
179
|
+
x_0 = beta * np.random.randn(n, 1)
|
82
|
-
|
83
|
-
|
84
|
-
|
180
|
+
|
181
|
+
|
182
|
+
|
85
|
-
x = Variable((n, T+1))
|
183
|
+
x = Variable((n, T + 1))
|
86
184
|
|
87
185
|
u = Variable((m, T))
|
88
186
|
|
@@ -92,29 +190,29 @@
|
|
92
190
|
|
93
191
|
for t in range(T):
|
94
192
|
|
95
|
-
cost = sum_squares(x[:,t+1]) + sum_squares(u[:,t])
|
193
|
+
cost = sum_squares(x[:, t + 1]) + sum_squares(u[:, t])
|
96
|
-
|
194
|
+
|
97
|
-
constr = [x[:,t+1] == A*x[:,t] + B*u[:,t],
|
195
|
+
constr = [x[:, t + 1] == A * x[:, t] + B * u[:, t],
|
98
|
-
|
196
|
+
|
99
|
-
norm(u[:,t], 'inf') <= 1]
|
197
|
+
norm(u[:, t], 'inf') <= 1]
|
100
|
-
|
198
|
+
|
101
|
-
states.append(
|
199
|
+
states.append(Problem(Minimize(cost), constr))
|
102
200
|
|
103
201
|
# sums problem objectives and concatenates constraints.
|
104
202
|
|
105
203
|
prob = sum(states)
|
106
204
|
|
107
|
-
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
|
205
|
+
prob.constraints += [x[:, T] == 0, x[:, 0] == x_0]
|
108
206
|
|
109
207
|
|
110
208
|
|
111
209
|
start = time.time()
|
112
210
|
|
113
|
-
result=prob.solve(verbose=True)
|
211
|
+
result = prob.solve(verbose=True)
|
114
212
|
|
115
213
|
elapsed_time = time.time() - start
|
116
214
|
|
117
|
-
print ("calc time:{0}".format(elapsed_time)
|
215
|
+
print ("calc time:{0}".format(elapsed_time) + "[sec]")
|
118
216
|
|
119
217
|
|
120
218
|
|
@@ -126,7 +224,71 @@
|
|
126
224
|
|
127
225
|
sys.exit()
|
128
226
|
|
129
|
-
|
227
|
+
# return
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
#以下グラフの表示
|
232
|
+
|
233
|
+
f = plt.figure()
|
234
|
+
|
235
|
+
# Plot (u_t)_1.
|
236
|
+
|
237
|
+
ax = f.add_subplot(211)
|
238
|
+
|
239
|
+
u1 = np.array(u[0, :].value[0, :])[0].tolist()
|
240
|
+
|
241
|
+
u2 = np.array(u[1, :].value[0, :])[0].tolist()
|
242
|
+
|
243
|
+
plt.plot(u1, '-r', label="u1")
|
244
|
+
|
245
|
+
plt.plot(u2, '-b', label="u2")
|
246
|
+
|
247
|
+
plt.ylabel(r"$u_t$", fontsize=16)
|
248
|
+
|
249
|
+
plt.yticks(np.linspace(-1.0, 1.0, 3))
|
250
|
+
|
251
|
+
plt.legend()
|
252
|
+
|
253
|
+
plt.grid(True)
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
# Plot (u_t)_2.
|
258
|
+
|
259
|
+
plt.subplot(2, 1, 2)
|
260
|
+
|
261
|
+
x1 = np.array(x[0, :].value[0, :])[0].tolist()
|
262
|
+
|
263
|
+
x2 = np.array(x[1, :].value[0, :])[0].tolist()
|
264
|
+
|
265
|
+
x3 = np.array(x[2, :].value[0, :])[0].tolist()
|
266
|
+
|
267
|
+
x4 = np.array(x[3, :].value[0, :])[0].tolist()
|
268
|
+
|
269
|
+
plt.plot(range(T + 1), x1, '-r', label="x1")
|
270
|
+
|
271
|
+
plt.plot(range(T + 1), x2, '-b', label="x2")
|
272
|
+
|
273
|
+
plt.plot(range(T + 1), x3, '-g', label="x3")
|
274
|
+
|
275
|
+
plt.plot(range(T + 1), x4, '-k', label="x4")
|
276
|
+
|
277
|
+
plt.yticks([-25, 0, 25])
|
278
|
+
|
279
|
+
plt.ylim([-25, 25])
|
280
|
+
|
281
|
+
plt.ylabel(r"$x_t$", fontsize=16)
|
282
|
+
|
283
|
+
plt.xlabel(r"$t$", fontsize=16)
|
284
|
+
|
285
|
+
plt.grid(True)
|
286
|
+
|
287
|
+
plt.legend()
|
288
|
+
|
289
|
+
plt.tight_layout()
|
290
|
+
|
291
|
+
plt.show()
|
130
292
|
|
131
293
|
|
132
294
|
|
1
追加説明
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,6 +13,8 @@
|
|
13
13
|
|
14
14
|
|
15
15
|
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出ているのですが,他のサイト等を調べても同様に書かれているのでどこが違うのかわかりません.
|
16
|
+
|
17
|
+
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]を記述することで制約条件を2つ追加できるそうなのですが....
|
16
18
|
|
17
19
|
### 発生している問題・エラーメッセージ
|
18
20
|
|