質問編集履歴
2
追加説明
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,16 +5,65 @@
|
|
5
5
|
ネット上に乗っていたコードをそのまま実行してもエラーが出るので,
|
6
6
|
実行できるようにプログラムを変更しています.
|
7
7
|
|
8
|
+
まず初めに
|
9
|
+
プログラム内
|
10
|
+
x = Variable(n, T + 1)
|
11
|
+
u = Variable(m, T)
|
12
|
+
だと以下のエラーが出たので
|
13
|
+
|
14
|
+
```
|
15
|
+
PS C:\Users\N\Desktop> python test.py
|
16
|
+
Simulation start
|
17
|
+
Traceback (most recent call last):
|
18
|
+
File "test.py", line 21, in <module>
|
19
|
+
x = Variable(n, T + 1)
|
20
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\variable.py", line 75, in __init__
|
21
|
+
raise TypeError("Variable name %s must be a string." % name)
|
22
|
+
TypeError: Variable name 51 must be a string.
|
23
|
+
```
|
24
|
+
x = Variable((n, T + 1))
|
25
|
+
u = Variable((m, T))
|
26
|
+
に変更しました.
|
27
|
+
|
28
|
+
そして実行すると
|
8
|
-
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出て
|
29
|
+
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出てたのですが,他のサイト等を調べても同様に書かれているのでどこが違うのかわかりません.
|
9
30
|
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]を記述することで制約条件を2つ追加できるそうなのですが....
|
31
|
+
|
32
|
+
載せているソースコードは
|
33
|
+
x = Variable((n, T + 1))
|
34
|
+
u = Variable((m, T))
|
35
|
+
に変更したものを載せています.
|
10
36
|
### 発生している問題・エラーメッセージ
|
11
37
|
|
12
38
|
```
|
39
|
+
PS C:\Users\N\Desktop> python test.py
|
13
40
|
Simulation start
|
14
41
|
Traceback (most recent call last):
|
15
42
|
File "test.py", line 32, in <module>
|
16
|
-
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
|
43
|
+
prob.constraints += [x[:, T] == 0, x[:, 0] == x_0]
|
44
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
45
|
+
return binary_op(self, other)
|
46
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 575, in __eq__
|
47
|
+
return Equality(self, other)
|
48
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\constraints\zero.py", line 114, in __init__
|
49
|
+
self._expr = lhs - rhs
|
50
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
51
|
+
return binary_op(self, other)
|
52
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 464, in __sub__
|
53
|
+
return self + -other
|
54
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 46, in cast_op
|
55
|
+
return binary_op(self, other)
|
56
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\expressions\expression.py", line 452, in __add__
|
57
|
+
return cvxtypes.add_expr()([self, other])
|
58
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\affine\add_expr.py", line 33, in __init__
|
59
|
+
super(AddExpression, self).__init__(*arg_groups)
|
60
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\atom.py", line 45, in __init__
|
61
|
+
self._shape = self.shape_from_args()
|
62
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\atoms\affine\add_expr.py", line 41, in shape_from_args
|
63
|
+
return u.shape.sum_shapes([arg.shape for arg in self.args])
|
64
|
+
File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxpy\utilities\shape.py", line 49, in sum_shapes
|
65
|
+
len(shapes)*" %s" % tuple(shapes))
|
17
|
-
|
66
|
+
ValueError: Cannot broadcast dimensions (4,) (4, 1)
|
18
67
|
```
|
19
68
|
|
20
69
|
### 該当のソースコード
|
@@ -27,43 +76,75 @@
|
|
27
76
|
print("Simulation start")
|
28
77
|
|
29
78
|
np.random.seed(1)
|
30
|
-
n =
|
79
|
+
n = 4 # state size
|
31
|
-
m =
|
80
|
+
m = 2 # input size
|
32
|
-
T = 50 #number of horizon
|
81
|
+
T = 50 # number of horizon
|
33
82
|
|
34
|
-
#simulation parameter
|
83
|
+
# simulation parameter
|
35
84
|
alpha = 0.2
|
36
85
|
beta = 5.0
|
37
86
|
|
38
87
|
# Model Parameter
|
39
|
-
A = np.eye(n) + alpha*np.random.randn(n,n)
|
88
|
+
A = np.eye(n) + alpha * np.random.randn(n, n)
|
40
|
-
B = np.random.randn(n,m)
|
89
|
+
B = np.random.randn(n, m)
|
41
|
-
x_0 = beta*np.random.randn(n,1)
|
90
|
+
x_0 = beta * np.random.randn(n, 1)
|
42
91
|
|
43
|
-
x = Variable((n, T+1))
|
92
|
+
x = Variable((n, T + 1))
|
44
93
|
u = Variable((m, T))
|
45
94
|
|
46
95
|
states = []
|
47
96
|
for t in range(T):
|
48
|
-
cost = sum_squares(x[:,t+1]) + sum_squares(u[:,t])
|
97
|
+
cost = sum_squares(x[:, t + 1]) + sum_squares(u[:, t])
|
49
|
-
constr = [x[:,t+1] == A*x[:,t] + B*u[:,t],
|
98
|
+
constr = [x[:, t + 1] == A * x[:, t] + B * u[:, t],
|
50
|
-
norm(u[:,t], 'inf') <= 1]
|
99
|
+
norm(u[:, t], 'inf') <= 1]
|
51
|
-
states.append(
|
100
|
+
states.append(Problem(Minimize(cost), constr))
|
52
101
|
# sums problem objectives and concatenates constraints.
|
53
102
|
prob = sum(states)
|
54
|
-
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]
|
103
|
+
prob.constraints += [x[:, T] == 0, x[:, 0] == x_0]
|
55
104
|
|
56
105
|
start = time.time()
|
57
|
-
result=prob.solve(verbose=True)
|
106
|
+
result = prob.solve(verbose=True)
|
58
107
|
elapsed_time = time.time() - start
|
59
|
-
print ("calc time:{0}".format(elapsed_time)
|
108
|
+
print ("calc time:{0}".format(elapsed_time) + "[sec]")
|
60
109
|
|
61
110
|
if result == float("inf"):
|
62
111
|
print("Cannot optimize")
|
63
112
|
import sys
|
64
113
|
sys.exit()
|
114
|
+
# return
|
65
115
|
|
116
|
+
#以下グラフの表示
|
117
|
+
f = plt.figure()
|
118
|
+
# Plot (u_t)_1.
|
119
|
+
ax = f.add_subplot(211)
|
120
|
+
u1 = np.array(u[0, :].value[0, :])[0].tolist()
|
121
|
+
u2 = np.array(u[1, :].value[0, :])[0].tolist()
|
122
|
+
plt.plot(u1, '-r', label="u1")
|
123
|
+
plt.plot(u2, '-b', label="u2")
|
124
|
+
plt.ylabel(r"$u_t$", fontsize=16)
|
125
|
+
plt.yticks(np.linspace(-1.0, 1.0, 3))
|
126
|
+
plt.legend()
|
127
|
+
plt.grid(True)
|
66
128
|
|
129
|
+
# Plot (u_t)_2.
|
130
|
+
plt.subplot(2, 1, 2)
|
131
|
+
x1 = np.array(x[0, :].value[0, :])[0].tolist()
|
132
|
+
x2 = np.array(x[1, :].value[0, :])[0].tolist()
|
133
|
+
x3 = np.array(x[2, :].value[0, :])[0].tolist()
|
134
|
+
x4 = np.array(x[3, :].value[0, :])[0].tolist()
|
135
|
+
plt.plot(range(T + 1), x1, '-r', label="x1")
|
136
|
+
plt.plot(range(T + 1), x2, '-b', label="x2")
|
137
|
+
plt.plot(range(T + 1), x3, '-g', label="x3")
|
138
|
+
plt.plot(range(T + 1), x4, '-k', label="x4")
|
139
|
+
plt.yticks([-25, 0, 25])
|
140
|
+
plt.ylim([-25, 25])
|
141
|
+
plt.ylabel(r"$x_t$", fontsize=16)
|
142
|
+
plt.xlabel(r"$t$", fontsize=16)
|
143
|
+
plt.grid(True)
|
144
|
+
plt.legend()
|
145
|
+
plt.tight_layout()
|
146
|
+
plt.show()
|
147
|
+
|
67
148
|
```
|
68
149
|
|
69
150
|
### 試したこと
|
1
追加説明
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
実行できるようにプログラムを変更しています.
|
7
7
|
|
8
8
|
プログラム上のprob.constraints += [x[:,T] == 0, x[:,0] == x_0]に対してエラーが出ているのですが,他のサイト等を調べても同様に書かれているのでどこが違うのかわかりません.
|
9
|
+
prob.constraints += [x[:,T] == 0, x[:,0] == x_0]を記述することで制約条件を2つ追加できるそうなのですが....
|
9
10
|
### 発生している問題・エラーメッセージ
|
10
11
|
|
11
12
|
```
|