def test_with_convex_boundary(get_tf, driver): tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='convex_hull', driver=driver(UniformGenerator(10, 0))) arr = tf.get_DOE_array() x, y = [arr[:, i] for i in range(2)] uta.assertGreaterEqual(x.min(), 0) # x uta.assertGreaterEqual(y.min(), 0) # y npt.assert_array_less(y, x)
def test_setup_as_constraint_z(): tf = TopFarmProblem( {'z': (initial[:, 2], 0, 2)}, DummyCost(desired[:, :2], 'z'), driver=EasyScipyOptimizeDriver(disp=False), ) tf.optimize() npt.assert_array_less(tf['z'], 2 + 1e-10)
def test_TopFarmProblem_with_cirleboundary_constraint(): optimal = np.array([(0, 0)]) desvar = dict(zip('xy', optimal.T)) b = CircleBoundaryConstraint([2, 2], 2) tf = TopFarmProblem(desvar, DummyCost(optimal, 'xy'), constraints=[b]) _, state, _ = tf.optimize() npt.assert_array_less((state['x'] - 2)**2 + (state['y'] - 2)**2, 9)
def test_with_convex_boundary_and_constrain(get_tf, driver): tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='convex_hull', min_spacing=2, driver=driver(UniformGenerator(10, 0))) arr = tf.get_DOE_array() x, y = [arr[:, i] for i in range(2)] uta.assertGreaterEqual(x.min(), 0) # x uta.assertGreaterEqual(y.min(), 0) # y npt.assert_array_less(y, x) assert all(np.sqrt(np.diff(x)**2 + np.diff(y)**2) >= 2)
def test_with_constrained_generator_polygon(get_tf, driver): tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='polygon', driver=driver) arr = tf.get_DOE_array() x, y = [arr[:, i] for i in range(2)] uta.assertGreaterEqual(x.min(), 0) # x uta.assertGreaterEqual(y.min(), 0) # y npt.assert_array_less(y, x)
def test_TopFarmProblem_with_cirleboundary_constraint_and_limits(): optimal = np.array([(0, 0)]) desvar = {'x': ([0], 1, 4), 'y': [0]} b = CircleBoundaryConstraint([2, 2], 2) tf = TopFarmProblem(desvar, DummyCost(optimal, 'xy'), constraints=[b]) _, state, _ = tf.optimize() npt.assert_array_less((state['x'] - 2)**2 + (state['y'] - 2)**2, 9) npt.assert_array_less(.9999999, state['x'])
def test_TopFarmProblemXYBoundaryPenalty(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), 10), constraints=[XYBoundaryConstraint(xy3tb.boundary)]) # spacing violated cost, _ = tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_array_less(1e10, cost) # spacing satisfied cost, _ = tf.evaluate({'x': xy3tb.optimal[:, 0], 'y': xy3tb.optimal[:, 1]}) npt.assert_equal(1.5, cost)
def test_setup_as_constraint_xyz(): desvar = dict(zip('xy', initial.T)) desvar['z'] = (initial[:, 2], 0, 2) tf = TopFarmProblem(desvar, DummyCost(desired, 'xyz'), driver=EasyScipyOptimizeDriver(disp=False), constraints=[XYBoundaryConstraint(boundary)]) tf.optimize() tb_pos = tf.turbine_positions tol = 1e-4 assert tb_pos[1][0] < 6 + tol # check within border npt.assert_array_less(tf['z'], 2 + tol) # check within height limit
def test_move_inside(): pbc = CircleBoundaryComp(1, (2, 1), 3) x0, y0 = [3, 3, 3, 12, 12, 12], [3, 5, 10, 8, 10, 12] state = pbc.satisfy({'x': x0, 'y': y0}) x, y = state['x'], state['y'] if 0: import matplotlib.pyplot as plt b = np.r_[pbc.xy_boundary, pbc.xy_boundary[:1]] plt.plot(b[:, 0], b[:, 1], 'k') for x0_, x_, y0_, y_ in zip(x0, x, y0, y): plt.plot([x0_, x_], [y0_, y_], '.-') plt.show() npt.assert_array_less((x - 2)**2 + (y - 1)**2, 3**2)
def test_TopFarmProblem_with_cirleboundary_penalty(): optimal = np.array([(0, 0)]) desvar = dict(zip('xy', optimal.T)) b = CircleBoundaryConstraint([1, 2], 3) driver = SimpleGADriver() tf = TopFarmProblem(desvar, DummyCost(optimal, 'xy'), constraints=[b], driver=driver) tf.evaluate() tf.plot_comp.show() np.testing.assert_array_almost_equal( ((b.constraintComponent.xy_boundary - [1, 2])**2).sum(1), 3**2) npt.assert_array_less(tf.evaluate({'x': [3.9], 'y': [2]})[0], 1e10) npt.assert_array_less(1e10, tf.evaluate({'x': [4.1], 'y': [2]})[0])
def test_move_inside(): pbc = ConvexBoundaryComp(1, [(0, 0), (10, 0), (10, 10)]) x0, y0 = [3, 3, 3, 12, 12, 12], [3, 5, 10, 8, 10, 12] x, y, z = pbc.move_inside(x0, y0, []) # import matplotlib.pyplot as plt # b = np.r_[pbc.xy_boundary, pbc.xy_boundary[:1]] # plt.plot(b[:, 0], b[:, 1], 'k') # for x0_, x_, y0_, y_ in zip(x0, x, y0, y): # plt.plot([x0_, x_], [y0_, y_], '.-') # plt.show() eps = 1e-10 npt.assert_array_less(y, x + eps) npt.assert_array_less(x, 10 + eps) npt.assert_array_less(y, 10 + eps) npt.assert_array_less(-x, 0 + eps) npt.assert_array_less(-y, 0 + eps)
def test_move_inside(): pbc = ConvexBoundaryComp(1, [(0, 0), (9, 0), (10, 1), (10, 10)]) x0, y0 = [3, 3, 3, 12, 12, 12], [3, 5, 10, 8, 10, 12] state = pbc.satisfy({'x': x0, 'y': y0}) x, y = state['x'], state['y'] if 0: import matplotlib.pyplot as plt b = np.r_[pbc.xy_boundary, pbc.xy_boundary[:1]] plt.plot(b[:, 0], b[:, 1], 'k') for x0_, x_, y0_, y_ in zip(x0, x, y0, y): plt.plot([x0_, x_], [y0_, y_], '.-') plt.show() eps = 1e-10 npt.assert_array_less(y, x + eps) npt.assert_array_less(x, 10 + eps) npt.assert_array_less(y, 10 + eps) npt.assert_array_less(-x, 0 + eps) npt.assert_array_less(-y, 0 + eps)
def test_random_search_driver_randomize_all_uniform(): np.random.seed(1) class Cost(): i = 0 def __call__(self, *args, **kwargs): self.i += 1 return self.i cost_comp = CostModelComponent(input_keys=['x', 'y', 'type'], n_wt=2, cost_function=Cost(), income_model=True) tf = TopFarmProblem( { 'x': ([1, 6], [0, 1], [5, 6]), 'y': ([-1., 0], -6, 0), 'type': ([3, 3], 3, 8) }, cost_comp=cost_comp, constraints=[], driver=EasyRandomSearchDriver(randomize_func=RandomizeAllUniform( ['x', 'type']), max_iter=600, disp=False), ) _, state, recorder = tf.optimize() # check that integer design variables are somewhat evenly distributed x, y, t = recorder['x'], recorder['y'], recorder['type'] for arr, l, u in [(x[:, 0], 0, 5), (x[:, 1], 1, 6), (t[:, 0], 3, 8)]: count = [(arr == i).sum() for i in range(l, u + 1)] npt.assert_equal(601, sum(count)) npt.assert_array_less(600 / len(count) * .70, count) count, _ = np.histogram(y[:, 0], np.arange(-6, 1)) npt.assert_equal(y.shape[0], sum(count)) npt.assert_array_less(600 / len(count) * .70, count)
def test_satisfy(): sc = SpacingComp(n_wt=3, min_spacing=2) state = sc.satisfy(dict(zip('xy', xy3tb.desired.T))) x, y = state['x'], state['y'] npt.assert_array_less(y, x)
@pytest.mark.parametrize('driver', [ ConstrainedGenerator(UniformGenerator(10, 0)), ConstrainedDiscardGenerator(UniformGenerator(10, 0)) ]) def test_with_convex_boundary_and_constrain(get_tf, driver): tf = get_tf(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='convex_hull', min_spacing=2, driver=driver) arr = tf.get_DOE_array() x, y = [arr[:, i] for i in range(2)] uta.assertGreaterEqual(x.min(), 0) # x uta.assertGreaterEqual(y.min(), 0) # y npt.assert_array_less(y, x) assert all(np.sqrt(np.diff(x)**2 + np.diff(y)**2) >= 2) if __name__ == '__main__': tf = get_InitialXYZOptimizationProblem(xy_boundary=[(0, 0), (10, 0), (10, 10)], xy_boundary_type='convex_hull', min_spacing=2, driver=ConstrainedDiscardGenerator( UniformGenerator(10, 0))) arr = tf.get_DOE_array() x, y = [arr[:, i] for i in range(2)] uta.assertGreaterEqual(x.min(), 0) # x uta.assertGreaterEqual(y.min(), 0) # y npt.assert_array_less(y, x)
def test_satisfy(): pbc = PolygonBoundaryComp(1, [(0, 0), (10, 0), (10, 10)]) state = pbc.satisfy({'x': [3, 3, 3], 'y': [0, 5, 10]}) x, y = state['x'], state['y'] npt.assert_array_less(y, x)
def test_move_inside(): pbc = PolygonBoundaryComp(1, [(0, 0), (10, 0), (10, 10)]) x, y, z = pbc.move_inside([3, 3, 3], [0, 5, 10], []) npt.assert_array_less(y, x)