-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_cvxpy.py
121 lines (100 loc) · 2.37 KB
/
3_cvxpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import numpy as np
import matplotlib.pyplot as plt
import cvxpy as cp
#Input
n = 4
m = 2
A = np.array([
[ 0.95, 0.16, 0.12, 0.01],
[-0.12, 0.98, -0.11, -0.03],
[-0.16, 0.02, 0.98, 0.03],
[-0. , 0.02, -0.04, 1.03],
])
B = np.array([
[ 0.8 , 0. ],
[ 0.1 , 0.2],
[ 0. , 0.8],
[-0.2 , 0.1],
])
x_init = np.ones(n)
T = 100
T_list = []
for i in range(T):
T_list.append(i)
#Variables
x = cp.Variable((n,T))
u = cp.Variable((m,T))
#Contraints
constraints = []
for i in range(T-1):
constraints += [x[:,i+1] == A@(x[:,i]) + B@(u[:,i])]
constraints += [x[:,0] == x_init]
constraints += [x[:,T-1] == 0]
#Objective
objective1 = 0
objective2 = 0
objective3 = 0
objective4 = 0
for i in range(T):
objective1 += (cp.norm(u[:,i]))**2
objective2 += cp.norm(u[:,i])
if cp.norm(u[:,i]) >= objective3:
objective3 = cp.norm(u[:,i])
objective4 += cp.norm1(u[:,i])
obj1 = cp.Minimize(objective1)
obj2 = cp.Minimize(objective2)
obj3 = cp.Minimize(objective3)
obj4 = cp.Minimize(objective4)
#Problem
prob1 = cp.Problem(obj1, constraints)
prob2 = cp.Problem(obj2, constraints)
prob3 = cp.Problem(obj3, constraints)
prob4 = cp.Problem(obj4, constraints)
#Problem 1
prob1.solve()
u_norm_list = []
for i in range(T):
u_norm_list.append(cp.norm((u.value)[:,i]))
plt.figure(1)
plt.plot(T_list, (u.value)[0,:])
plt.plot(T_list, (u.value)[1,:])
plt.plot(T_list, u_norm_list)
plt.title("Part a")
plt.legend("u1","u2","||u||2")
plt.show()
#Problem 2
prob2.solve()
u_norm_list = []
for i in range(T):
u_norm_list.append(cp.norm((u.value)[:,i]))
plt.figure(2)
plt.plot(T_list, (u.value)[0])
plt.plot(T_list, (u.value)[1])
plt.plot(T_list, u_norm_list)
plt.title("Part b")
plt.legend("u1","u2","||u||2")
plt.show()
#Problem 3
prob3.solve()
u_norm_list = []
for i in range(T):
u_norm_list.append(cp.norm((u.value)[:,i]))
plt.figure(3)
plt.plot(T_list, (u.value)[0])
plt.plot(T_list, (u.value)[1])
plt.plot(T_list, u_norm_list)
plt.title("Part c")
plt.legend("u1","u2","||u||2")
plt.show()
#Problem 4
prob4.solve()
u_norm_list = []
for i in range(T):
u_norm_list.append(cp.norm((u.value)[:,i]))
plt.figure(4)
plt.plot(T_list, (u.value)[0])
plt.plot(T_list, (u.value)[1])
plt.plot(T_list, u_norm_list)
plt.title("Part d")
plt.legend("u1","u2","||u||2")
plt.show()