def tests_smart_start(): xs_ref = [ 1.6, 14.1, 1.6, 7.9, 14.1, 7.9, 19.9, 19.9, 7.8, 5.8, 14.2, 5.8, 1.5, 16.2, 16.2, 1.6, 3.7, 14.1, 7.9, 3.7 ] ys_ref = [ 1.6, 1.6, 7.9, 1.6, 7.9, 7.9, 1.6, 7.9, 5.8, 7.8, 5.8, 1.5, 5.8, 7.8, 1.5, 3.7, 1.6, 3.7, 3.7, 7.9 ] N_WT = 20 min_space = 2.1 XX, YY, val = egg_tray_map() xs, ys = smart_start(XX, YY, val, N_WT, min_space, seed=0) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, val, 100) for i in range(N_WT): circle = plt.Circle((xs[i], ys[i]), min_space / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(xs[i], ys[i], 'rx') print(np.round(xs, 1).tolist()) print(np.round(ys, 1).tolist()) plt.axis('equal') plt.show() npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref])
def smart_start(self, XX, YY, ZZ, radius=None): assert XX.shape == YY.shape if len(XX.shape) == 1: XX, YY = np.meshgrid(XX, YY) ZZ_is_func = hasattr(ZZ, '__call__') spacing_comp_lst = [ c for c in self.model.constraint_components if isinstance(c, SpacingComp) ] if len(spacing_comp_lst) == 1: min_spacing = spacing_comp_lst[0].min_spacing else: min_spacing = 0 X, Y = XX.flatten(), YY.flatten() if not ZZ_is_func: Z = ZZ.flatten() else: Z = ZZ for comp in self.model.constraint_components: if isinstance(comp, BoundaryBaseComp): dist = comp.distances(X, Y) if len(dist.shape) == 2: dist = dist.min(1) mask = dist >= 0 X, Y = X[mask], Y[mask] if not ZZ_is_func: Z = Z[mask] x, y = smart_start(X, Y, Z, self.n_wt, min_spacing, radius) self.update_state({topfarm.x_key: x, topfarm.y_key: y}) return x, y
def tests_smart_start_no_feasible(): XX, YY, val = egg_tray_map() N_WT = 20 min_space = 5.1 with pytest.raises(Exception, match="No feasible positions for wt 8"): xs, ys = smart_start(XX, YY, val, N_WT, min_space)
def tests_smart_start(): xs_ref = [ 1.6, 14.1, 1.6, 7.9, 14.1, 7.9, 19.9, 19.9, 7.8, 5.8, 14.2, 5.8, 1.5, 16.2, 16.2, 1.6, 3.7, 14.1, 7.9, 3.7 ] ys_ref = [ 1.6, 1.6, 7.9, 1.6, 7.9, 7.9, 1.6, 7.9, 5.8, 7.8, 5.8, 1.5, 5.8, 7.8, 1.5, 3.7, 1.6, 3.7, 3.7, 7.9 ] x = np.arange(0, 20, 0.1) y = np.arange(0, 10, 0.1) YY, XX = np.meshgrid(y, x) val = np.sin(XX) + np.sin(YY) N_WT = 20 min_space = 2.1 np.random.seed(0) xs, ys = smart_start(XX, YY, val, N_WT, min_space) npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref]) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, val, 100) for i in range(N_WT): circle = plt.Circle((xs[i], ys[i]), min_space / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(xs[i], ys[i], 'rx') print(np.round(xs, 1).tolist()) print(np.round(ys, 1).tolist()) plt.axis('equal') plt.show()
def tests_smart_start_random(): xs_ref = [ 1.7, 13.9, 1.4, 7.7, 14.4, 7.6, 19.7, 19.7, 8.7, 19.4, 15.8, 12.4, 7.7, 9.8, 14.1, 1.8, 9.7, 6.6, 13.6, 3.5 ] ys_ref = [ 7.9, 1.4, 1.7, 1.3, 7.9, 8.4, 1.7, 8.7, 6.4, 6.6, 2.3, 7.1, 3.5, 1.6, 5.8, 5.8, 8.3, 6.5, 3.5, 1.4 ] N_WT = 20 min_space = 2.1 XX, YY, val = egg_tray_map() np.random.seed(0) with pytest.raises(expected_exception=AssertionError): xs, ys = smart_start(XX, YY, val, N_WT, min_space, random_pct=101, seed=0) xs, ys = smart_start(XX, YY, val, N_WT, min_space, random_pct=1, seed=0) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, val, 100) plt.plot(XX, YY, ',k') for i in range(N_WT): circle = plt.Circle((xs[i], ys[i]), min_space / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(xs[i], ys[i], 'rx') print(np.round(xs, 1).tolist()) print(np.round(ys, 1).tolist()) plt.axis('equal') plt.show() npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref])
def smart_start(self, XX, YY, ZZ): min_spacing = [ c for c in self.model.constraint_components if isinstance(c, SpacingComp) ][0].min_spacing X, Y, Z = XX.flatten(), YY.flatten(), ZZ.flatten() for comp in self.model.constraint_components: if isinstance(comp, BoundaryBaseComp): mask = (comp.distances(X, Y).min(1) >= 0) X, Y, Z = X[mask], Y[mask], Z[mask] x, y = smart_start(X, Y, Z, self.n_wt, min_spacing) self.update_state({topfarm.x_key: x, topfarm.y_key: y}) return x, y
def tests_smart_start_no_feasible(): x = np.arange(0, 20, 0.1) y = np.arange(0, 10, 0.1) YY, XX = np.meshgrid(y, x) val = np.sin(XX) + np.sin(YY) N_WT = 20 min_space = 5.1 xs, ys = smart_start(XX, YY, val, N_WT, min_space) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, val, 100) for i in range(N_WT): circle = plt.Circle((xs[i], ys[i]), min_space / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(xs[i], ys[i], 'rx') plt.axis('equal') plt.show() print(xs) assert np.isnan(xs).sum() == 12