import numpy as np
import matplotlib.pyplot as pp
N_g = int( round( np.random.uniform( low = 3, high = 7 ) ) )
print( N_g )
groups = []
r = 5
dr = r / 5
theta = 2 * np.pi / ( N_g - 1 )
dtheta = theta / 5
for i in range( N_g ):
if i == 0:
center = np.array( [ 0.0, 0.0 ] )
else:
curr_r = np.random.uniform( low = r - dr, high = r + dr )
curr_theta = np.random.uniform(
low = ( i - 1 ) * theta - dtheta,
high = ( i - 1 ) * theta + dtheta
)
center = curr_r * np.array( [ np.cos( curr_theta ), np.sin( curr_theta ) ] )
points = []
for j in range( int( round( np.random.uniform( low = 5, high = 15 ) ) ) ):
curr_r = np.random.uniform( low = 0, high = r / 2 )
curr_theta = np.random.uniform(
low = 0,
high = 2 * np.pi
)
p_center = center + curr_r * np.array( [ np.cos( curr_theta ), np.sin( curr_theta ) ] )
v = np.random.uniform( low = 2, high = 4 )
h = np.random.uniform( low = 2, high = 4 )
points.append( ( ( h, v ), p_center ) )
groups.append( ( center, points ) )
pp.figure( figsize = ( 10, 10 ) )
colors = [ 'blue', 'red', 'black', 'cyan', 'green', 'orange', 'gray' ]
i = 0
for c in groups:
# pp.scatter( *c[ 0 ], marker = "x", color = colors[ i ] )
for p in c[ 1 ]:
pp.gca().add_patch(
pp.Rectangle(
( p[ 1 ][ 0 ], p[ 1 ][ 1 ] ),
p[ 0 ][ 0 ] , p[ 0 ][ 1 ],
color = colors[ i ],
alpha = 0.1
)
)
# pp.plot(
# [
# p[ 1 ][ 0 ],
# p[ 1 ][ 0 ] + p[ 0 ][ 0 ],
# p[ 1 ][ 0 ] + p[ 0 ][ 0 ],
# p[ 1 ][ 0 ]
# ],
# [
# p[ 1 ][ 1 ],
# p[ 1 ][ 1 ],
# p[ 1 ][ 1 ] + p[ 0 ][ 1 ],
# p[ 1 ][ 1 ] + p[ 0 ][ 1 ]
# ],
# color = colors[ i ]
# )
# pp.scatter( *p[ 1 ], color = colors[ i ] )
i = i + 1
pp.axis( ( -10, 10, -10, 10 ) )
pp.show()
def zero_trunc( x ):
if x >= 0.0:
return x
else:
return 0.0
def O_ij( x, h, i, j ):
if x[ i ] >= x[ j ]:
return ( zero_trunc( h[ j ] ** 2.0 - ( x[ i ] - x[ j ] ) ** 2.0 ) ** 2.0 ) / ( h[ j ] ** 4.0 )
else:
return ( zero_trunc( h[ i ] ** 2.0 - ( x[ i ] - x[ j ] ) ** 2.0 ) ** 2.0 ) / ( h[ i ] ** 4.0 )
def E_O( xy, h, v ):
x = xy[ : : 2 ]
y = xy[ 1 : : 2 ]
n = int( xy.shape[ 0 ] / 2 )
retval = 0.0
for i in range( n ):
curr_xi = x[ i ]
curr_yi = y[ i ]
curr_hi = h[ i ]
curr_vi = v[ i ]
for j in range( i + 1, n ):
curr_xj = x[ j ]
curr_yj = y[ j ]
curr_hj = h[ j ]
curr_vj = v[ j ]
retval += O_ij( x, h, i, j ) * O_ij( y, v, i, j )
return 2.0 * retval / ( n * ( n + 1 ) )
def gera_xyhv( groups ):
xy = []
h = []
v = []
for g in groups:
for p in g[ 1 ]:
h.append( p[ 0 ][ 0 ] )
v.append( p[ 0 ][ 1 ] )
xy.append( p[ 1 ][ 0 ] )
xy.append( p[ 1 ][ 1 ] )
return( np.array( xy ), np.array( h ), np.array( v ) )
( xy, h, v ) = gera_xyhv( groups )
print( E_O( xy, h, v ) )
$$ \sum_{i = 0}^{n - 1}\sum_{j = i + 1}^{n - 1}1 = \sum_{i = 0}^{n - 1}n - 1 - ( i + 1 ) + 1 = \sum_{i = 0}^{n - 1}n - i - 1 $$
Seja $j = n - 1 - i$, então: $$ \sum_{i = 0}^{n - 1}\sum_{j = i + 1}^{n - 1}1 = \sum_{j = n - 1}^0 j = \sum_{j = 0}^{n - 1} j $$
def calcula_L( groups ):
n_verts = 0
for g in groups:
for p in g[ 1 ]:
n_verts += 1
L = np.eye( n_verts )
curr_idx = 0
for g in groups:
curr_n = len( g[ 1 ] )
for i in range( curr_n ):
for j in range( i + 1, curr_n ):
L[ curr_idx + i, curr_idx + j ] = -1.0 / ( curr_n - 1.0 )
L[ curr_idx + j, curr_idx + i ] = L[ curr_idx + i, curr_idx + j ]
curr_idx += curr_n
return L
L = calcula_L( groups )
delta_x = L @ xy[ : : 2 ]
delta_y = L @ xy[ 1 : : 2 ]
def E_N( xy, w ):
retval = np.linalg.norm( L @ xy[ : : 2 ] - w * delta_x ) ** 2
retval += np.linalg.norm( L @ xy[ 1 : : 2 ] - w * delta_y ) ** 2
retval *= ( L.shape[ 0 ] ** 2 ) / \
( 2.0 * ( np.linalg.norm( delta_x ) ** 2 ) + np.linalg.norm( delta_y ) ** 2 )
return retval
print( E_N( xy / 2.0, 0.5 ) )
import scipy.optimize
res = scipy.optimize.minimize( E_O, xy, ( h, v ) )
print( res.x )
pp.figure( figsize = ( 10, 10 ) )
colors = [ 'blue', 'red', 'black', 'cyan', 'green', 'orange', 'gray' ]
i = 0
j = 0
for c in groups:
for p in c[ 1 ]:
pp.gca().add_patch(
pp.Rectangle(
( res.x[ 2 * j ], res.x[ 2 * j + 1 ] ),
p[ 0 ][ 0 ] , p[ 0 ][ 1 ],
color = colors[ i ],
alpha = 1.0
)
)
j = j + 1
i = i + 1
pp.axis( ( -150, 150, -150, 150 ) )
pp.show()
print( E_O( res.x, h, v ) )
def obj_fun( z, alpha ):
# z contém xy e w
xy = z[ : -1 ]
w = z[ -1 ]
return alpha * E_N( xy, w ) + ( 1.0 - alpha ) * E_O( xy, h, v )
res = scipy.optimize.minimize( obj_fun, np.concatenate( ( xy, np.ones( ( 1, ) ) ) ), ( 0.9 ) )
pp.figure( figsize = ( 10, 10 ) )
colors = [ 'blue', 'red', 'black', 'cyan', 'green', 'orange', 'gray' ]
i = 0
j = 0
for c in groups:
for p in c[ 1 ]:
pp.gca().add_patch(
pp.Rectangle(
( res.x[ 2 * j ], res.x[ 2 * j + 1 ] ),
p[ 0 ][ 0 ] , p[ 0 ][ 1 ],
color = colors[ i ],
alpha = 1.0
)
)
j = j + 1
i = i + 1
pp.axis( ( -100, 100, -100, 100 ) )
pp.show()