/
placement.py
executable file
·180 lines (146 loc) · 5.04 KB
/
placement.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Figures 8.15-17, pages 435 and 436.
# Linear, quadratic and fourth-order placement.
#
# The problem data are different from the example in the book.
import pickle
from cvxopt import lapack, solvers, matrix, spmatrix, sqrt, mul
from cvxopt.modeling import variable, op
#solvers.options['show_progress'] = False
try: import pylab, numpy
except ImportError: pylab_installed = False
else: pylab_installed = True
data = pickle.load(open("placement.bin", "rb"))
Xf = data['X'] # M by n matrix with coordinates of M fixed nodes
M = Xf.size[0]
E = data['E'] # list of edges
L = len(E) # number of edges
N = max(max(e) for e in E) + 1 - M # number of free nodes; fixed nodes
# have the highest M indices.
# arc-node incidence matrix
A = matrix(0.0, (L,M+N))
for k in range(L): A[k, E[k]] = matrix([1.0, -1.0], (1,2))
# minimize sum h( sqrt( (A1*X[:,0] + B[:,0])**2 +
# (A1*X[:,1] + B[:,1])**2 ) for different h
A1 = A[:,:N]
B = A[:,N:]*Xf
# Linear placement: h(u) = u.
#
# minimize 1'*t
# subject to [ ti*I (A[i,:]*[x,y] + B)' ]
# [ A[i,:]*[x,y] + B ti ] >= 0,
# i = 1, ..., L
#
# variables t (L), x (N), y (N).
novars = L + 2*N
c = matrix(0.0, (novars,1))
c[:L] = 1.0
G = [ spmatrix([], [], [], (9, novars)) for k in range(L) ]
h = [ matrix(0.0, (3,3)) for k in range(L) ]
for k in range(L):
# coefficient of tk
C = spmatrix(-1.0, [0,1,2], [0,1,2])
G[k][C.I + 3*C.J, k] = C.V
for j in range(N):
# coefficient of x[j]
C = spmatrix(-A[k,j], [2, 0], [0, 2])
G[k][C.I + 3*C.J, L+j] = C.V
# coefficient of y[j]
C = spmatrix(-A[k,j], [2, 1], [1, 2])
G[k][C.I + 3*C.J, L+N+j] = C.V
# constant
h[k][2,:2] = B[k,:]
h[k][:2,2] = B[k,:].T
sol = solvers.sdp(c, Gs=G, hs=h)
X1 = matrix(sol['x'][L:], (N,2))
# Quadratic placement: h(u) = u^2.
#
# minimize sum (A*X[:,0] + B[:,0])**2 + (A*X[:,1] + B[:,1])**2
#
# with variable X (Nx2).
Bc = -B
lapack.gels(+A1, Bc)
X2 = Bc[:N,:]
# Fourth order placement: h(u) = u^4
#
# minimize g(AA*x + BB)
#
# where AA = [A1, 0; 0, A1]
# BB = [B[:,0]; B[:,1]]
# x = [X[:,0]; X[:,1]]
# g(u,v) = sum((uk.^2 + vk.^2).^2)
#
# with variables x (2*N).
AA = matrix(0.0, (2*L, 2*N))
AA[:L, :N], AA[L:,N:] = A1, A1
BB = matrix(B, (2*L,1))
def F(x=None, z=None):
if x is None:
return 0, matrix(0.0, (2*N,1))
y = AA*x + BB
d = y[:L]**2 + y[L:]**2
f = sum(d**2)
gradg = matrix(0.0, (2*L,1))
gradg[:L], gradg[L:] = 4*mul(d,y[:L]), 4*mul(d,y[L:])
g = gradg.T * AA
if z is None: return f, g
H = matrix(0.0, (2*L, 2*L))
for k in range(L):
H[k,k], H[k+L,k+L] = 4*d[k], 4*d[k]
H[[k,k+L], [k,k+L]] += 8 * y[[k,k+L]] * y[[k,k+L]].T
return f, g, AA.T*H*AA
sol = solvers.cp(F)
X4 = matrix(sol['x'], (N,2))
if pylab_installed:
# Figures for linear placement.
pylab.figure(1, figsize=(10,4), facecolor='w')
pylab.subplot(121)
X = matrix(0.0, (N+M,2))
X[:N,:], X[N:,:] = X1, Xf
pylab.plot(Xf[:,0], Xf[:,1], 'sw', mec = 'k')
pylab.plot(X1[:,0], X1[:,1], 'or', ms=10)
for s, t in E: pylab.plot([X[s,0], X[t,0]], [X[s,1],X[t,1]], 'b:')
pylab.axis([-1.1, 1.1, -1.1, 1.1])
pylab.axis('equal')
pylab.title('Linear placement')
pylab.subplot(122)
lngths = sqrt((A1*X1 + B)**2 * matrix(1.0, (2,1)))
pylab.hist(list(lngths), [.1*k for k in range(15)])
x = pylab.arange(0, 1.6, 1.6/500)
pylab.plot( x, 5.0/1.6*x, '--k')
pylab.axis([0, 1.6, 0, 5.5])
pylab.title('Length distribution')
# Figures for quadratic placement.
pylab.figure(2, figsize=(10,4), facecolor='w')
pylab.subplot(121)
X[:N,:], X[N:,:] = X2, Xf
pylab.plot(Xf[:,0], Xf[:,1], 'sw', mec = 'k')
pylab.plot(X2[:,0], X2[:,1], 'or', ms=10)
for s, t in E: pylab.plot([X[s,0], X[t,0]], [X[s,1],X[t,1]], 'b:')
pylab.axis([-1.1, 1.1, -1.1, 1.1])
pylab.axis('equal')
pylab.title('Quadratic placement')
pylab.subplot(122)
lngths = sqrt((A1*X2 + B)**2 * matrix(1.0, (2,1)))
pylab.hist(list(lngths), [.1*k for k in range(15)])
x = pylab.arange(0, 1.5, 1.5/500)
pylab.plot( x, 5.0/1.5**2 * x**2, '--k')
pylab.axis([0, 1.5, 0, 5.5])
pylab.title('Length distribution')
# Figures for fourth order placement.
pylab.figure(3, figsize=(10,4), facecolor='w')
pylab.subplot(121)
X[:N,:], X[N:,:] = X4, Xf
pylab.plot(Xf[:,0], Xf[:,1], 'sw', mec ='k')
pylab.plot(X4[:,0], X4[:,1], 'or', ms=10)
for s, t in E: pylab.plot([X[s,0], X[t,0]], [X[s,1],X[t,1]], 'b:')
pylab.axis([-1.1, 1.1, -1.1, 1.1])
pylab.axis('equal')
pylab.title('Fourth order placement')
pylab.subplot(122)
lngths = sqrt((A1*X4 + B)**2 * matrix(1.0, (2,1)))
pylab.hist(list(lngths), [.1*k for k in range(15)])
x = pylab.arange(0, 1.5, 1.5/500)
pylab.plot( x, 6.0/1.4**4 * x**4, '--k')
pylab.axis([0, 1.4, 0, 6.5])
pylab.title('Length distribution')
pylab.show()