2019-08-26 21:49:10 +00:00
|
|
|
from casadi import *
|
|
|
|
import matplotlib.pyplot as plt
|
2019-08-27 09:38:04 +00:00
|
|
|
import math
|
|
|
|
import operator
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
N = 7 # number of enclosed circles
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
# this function reads and processes data for optimal circle packaging obtained form packomania.com
|
|
|
|
def read_circle_data(N):
|
|
|
|
coords_raw = open('cci/cci{}.txt'.format(N))
|
|
|
|
radii_raw = open('cci/radii.txt'.format(N))
|
|
|
|
|
|
|
|
coords_raw = coords_raw.readlines()
|
|
|
|
coords_raw = [c.split() for c in coords_raw if c[0] != '#']
|
|
|
|
coords = {}
|
|
|
|
for c in coords_raw:
|
|
|
|
coords[int(c[0])] = (float(c[1]), float(c[2]))
|
|
|
|
|
|
|
|
coords = sort_ccw(coords, (0,0))
|
|
|
|
|
|
|
|
radii_raw = radii_raw.readlines()
|
|
|
|
radii_raw = [r.split() for r in radii_raw if r[0] != '#']
|
|
|
|
radii = {}
|
|
|
|
for r in radii_raw:
|
|
|
|
radii[int(r[0])] = float(r[1])
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
return radii[N], coords
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
# this function sorts enclosed circle coordinates counter-clockwise w.r.t. the center point
|
|
|
|
# TODO: there is a problem when circles are present that are not touching the boundary of the enclosing circle (e.g. N = 7)
|
|
|
|
def sort_ccw(coords, center):
|
|
|
|
a = {}
|
|
|
|
for c in coords:
|
|
|
|
a[c] = math.atan2(coords[c][1] - center[1], coords[c][0] - center[0])
|
|
|
|
a_sort = sorted(a.items(), key=operator.itemgetter(1))
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
coords_sort = []
|
|
|
|
for a in a_sort:
|
|
|
|
coords_sort.append(coords[a[0]])
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
return coords_sort
|
|
|
|
|
|
|
|
# read radius and center coordinates for enclosed circles
|
|
|
|
rtilde, coords = read_circle_data(N)
|
|
|
|
|
|
|
|
c = (0.0, 0.0) # center of big circle
|
|
|
|
R = 1.0 # radius of big circle
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
plt.xlim((-1, 1))
|
|
|
|
plt.ylim((-1, 1))
|
2019-08-26 21:49:10 +00:00
|
|
|
plt.gca().set_aspect('equal', 'box')
|
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
plt.ion()
|
|
|
|
plt.show()
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
for p in coords:
|
2019-08-26 21:49:10 +00:00
|
|
|
plt.plot(p[0], p[1], 'o')
|
|
|
|
circle = plt.Circle(p, rtilde, fill=False)
|
|
|
|
plt.gca().add_artist(circle)
|
|
|
|
circle = plt.Circle(c, R, fill=False)
|
|
|
|
plt.gca().add_artist(circle)
|
|
|
|
|
|
|
|
plt.plot(c[0], c[1], 'o')
|
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
for k in range(0, N):
|
|
|
|
p1 = coords[k]
|
|
|
|
p2 = coords[(k+1) % N]
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
# midpoint between center of two circles
|
|
|
|
m = np.mean([p1, p2], axis=0)
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
# vector in direction of midpoint
|
|
|
|
v = m - np.array(c)
|
|
|
|
v = v/np.linalg.norm(v)
|
|
|
|
plt.plot(m[0], m[1], 'o')
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
opti = casadi.Opti()
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
r = opti.variable(1) # radius of new circle
|
|
|
|
p = opti.variable(2) # center of new circle
|
|
|
|
lamb = opti.variable(1)
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
opti.minimize(-r)
|
|
|
|
opti.subject_to(p == c + v * lamb)
|
|
|
|
opti.subject_to((p[0] - p1[0])**2 + (p[1] - p1[1])**2 >= (rtilde + r)**2)
|
|
|
|
opti.subject_to(R == lamb + r)
|
|
|
|
opti.subject_to(r >= 0)
|
|
|
|
opti.subject_to(r <= R)
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
opti.solver('ipopt')
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
init_r = 0.1
|
|
|
|
init_lamb = R - init_r
|
|
|
|
init_p = c + v * init_lamb
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
opti.set_initial(r, init_r)
|
|
|
|
opti.set_initial(p, init_p)
|
|
|
|
opti.set_initial(lamb, init_lamb)
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
sol = opti.solve()
|
2019-08-26 21:49:10 +00:00
|
|
|
|
2019-08-27 09:38:04 +00:00
|
|
|
p = sol.value(p)
|
|
|
|
r = sol.value(r)
|
|
|
|
lamb = sol.value(lamb)
|
|
|
|
|
|
|
|
print("p = {}".format(p))
|
|
|
|
print("r = {}".format(r))
|
|
|
|
print("lambda = {}".format(lamb))
|
|
|
|
print("v = {}".format(v))
|
|
|
|
|
|
|
|
plt.plot(p[0], p[1], 'o')
|
|
|
|
circle = plt.Circle(p, r, fill=False)
|
|
|
|
plt.gca().add_artist(circle)
|
2019-08-26 21:49:10 +00:00
|
|
|
|
|
|
|
pass
|