def test_smart_start_polygon_boundary(): xs_ref = [1.6, 1.6, 3.6] ys_ref = [1.6, 3.7, 2.3] x = np.arange(0, 5.1, 0.1) y = np.arange(0, 5.1, 0.1) YY, XX = np.meshgrid(y, x) ZZ = np.sin(XX) + np.sin(YY) min_spacing = 2.1 tf = xy3tb.get_tf(constraints=[ SpacingConstraint(min_spacing), XYBoundaryConstraint([(0, 0), (5, 3), (5, 5), (0, 5)], 'polygon') ]) tf.smart_start(XX, YY, ZZ) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, ZZ, 100) plt.plot(tf.xy_boundary[:, 0], tf.xy_boundary[:, 1], 'k') for x, y in tf.turbine_positions: circle = plt.Circle((x, y), min_spacing / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(x, y, 'rx') plt.axis('equal') plt.show() npt.assert_array_almost_equal(tf.turbine_positions, np.array([xs_ref, ys_ref]).T)
def test_spacing(): tf = xy3tb.get_tf(constraints=[SpacingConstraint(2)]) tf.optimize() tb_pos = tf.turbine_positions[:, :2] tf.plot_comp.show() tol = 1e-4 assert sum((tb_pos[2] - tb_pos[0])**2) > 2**2 - tol # check min spacing
def test_smart_start(): xs_ref = [1.6, 1.6, 3.7] ys_ref = [1.6, 3.7, 1.6] x = np.arange(0, 5, 0.1) y = np.arange(0, 5, 0.1) YY, XX = np.meshgrid(y, x) ZZ = np.sin(XX) + np.sin(YY) min_spacing = 2.1 tf = xy3tb.get_tf(constraints=[SpacingConstraint(min_spacing)]) tf.smart_start(XX, YY, ZZ, seed=0) try: npt.assert_array_almost_equal(tf.turbine_positions, np.array([xs_ref, ys_ref]).T) except AssertionError: # wt2 and wt3 may switch npt.assert_array_almost_equal(tf.turbine_positions, np.array([ys_ref, xs_ref]).T) if 0: import matplotlib.pyplot as plt plt.contourf(XX, YY, ZZ, 100) for x, y in tf.turbine_positions: circle = plt.Circle((x, y), min_spacing / 2, color='b', fill=False) plt.gcf().gca().add_artist(circle) plt.plot(x, y, 'rx') plt.axis('equal') plt.show()
def testDummyCostPlotComp(): if os.name == 'posix' and "DISPLAY" not in os.environ: pytest.xfail("No display") tf = xy3tb.get_tf(plot_comp=DummyCostPlotComp(xy3tb.desired)) tf.evaluate() tf.optimize()
def test_TopFarmProblem(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[]) cost, state, _ = tf.optimize() npt.assert_almost_equal(cost, 0) npt.assert_array_almost_equal(state['x'], xy3tb.desired[:, 0]) npt.assert_array_almost_equal(state['y'], xy3tb.desired[:, 1])
def test_TopFarmProblemSpacingConstraint(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[SpacingConstraint(2)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_array_equal(tf['wtSeparationSquared'], [32, 1, 25]) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [2.5, 7, 4.5]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1])
def test_TopFarmProblemXYBoundaryConstraintPolygon(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[XYBoundaryConstraint(xy3tb.boundary, 'polygon')]) # constraint violated tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1])
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_TopFarmProblemLimits(): tf = xy3tb.get_tf(design_vars={'x': (xy3tb.initial[:, 0], -3, 3), 'y': (xy3tb.initial[:, 1], [-4, -3, -2], [2, 3, 4])}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), max_iter=100), constraints=[]) tf.evaluate() desvars = tf.driver._designvars npt.assert_equal(desvars['indeps.x']['lower'], -3) npt.assert_equal(desvars['indeps.x']['upper'], 3) npt.assert_array_equal(desvars['indeps.y']['lower'], [-4, -3, -2]) npt.assert_array_equal(desvars['indeps.y']['upper'], [2, 3, 4])
def test_spacing_as_penalty(): tf = xy3tb.get_tf(constraints=[SpacingConstraint(2)], driver=SimpleGADriver()) # check normal result if spacing constraint is satisfied assert tf.evaluate()[0] == 45 # check penalized result if spacing constraint is not satisfied assert tf.evaluate({ 'x': [3, 7, 4.], 'y': [-3., -7., -3.], 'z': [0., 0., 0.] })[0] == 1e10 + 3
def test_TopFarmProblemXYBoundaryPenaltyAndLimits(): tf = xy3tb.get_tf(design_vars={'x': ([3, 7, 4], -1, 5), 'y': ([-3, -7, -3], -9, -1)}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), 10), constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) desvars = tf.driver._designvars npt.assert_equal(desvars['indeps.x']['lower'], 0) npt.assert_equal(desvars['indeps.x']['upper'], 5) npt.assert_array_equal(desvars['indeps.y']['lower'], -9) npt.assert_array_equal(desvars['indeps.y']['upper'], -1)
def test_capacity_as_penalty(): tf = xy3tb.get_tf(design_vars={topfarm.type_key: ([0, 0, 0], 0, 2)}, constraints=[ CapacityConstraint( 5, rated_power_array=[100, 10000, 10]) ], driver=EasySimpleGADriver(), plot_comp=None) # check normal result that satisfies the penalty assert tf.evaluate()[0] == 141.0 # check penalized result if capacity constraint is not satisfied assert tf.evaluate({'type': np.array([0, 1, 1])})[0] == 1e10 + 15.1
def test_TopFarmProblemXYBoundaryConstraint(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1]) desvars = tf.driver._designvars for xy in 'xy': for lu in ['lower', 'upper']: npt.assert_equal(desvars['indeps.' + xy][lu], np.nan)
def test_optimize_3tb(): """Optimize 3-turbine layout and check final positions The farm boundaries and min spacing are chosen such that the desired turbine positions are not within the boundaries or constraints. """ dec_prec = 4 # decimal precision for comparison tf = xy3tb.get_tf() tf.optimize() tb_pos = tf.turbine_positions tol = 1e-6 uta.assertGreater(sum((tb_pos[2] - tb_pos[0])**2), 2**2 - tol) # check min spacing uta.assertLess(tb_pos[1][0], 6 + tol) # check within border np.testing.assert_array_almost_equal(tb_pos[:, :2], xy3tb.optimal, dec_prec)
def test_AEPMaxLoadCostModelComponent_as_penalty(): tf = xy3tb.get_tf(design_vars={ 'x': ([0]), 'y': ([0]) }, cost_comp=AEPMaxLoadCostModelComponent( input_keys='xy', n_wt=1, aep_load_function=lambda x, y: (-np.sin(np.hypot(x, y)), np.hypot(x, y)), max_loads=3), constraints=[], driver=EasySimpleGADriver(), plot_comp=None) # check normal result that satisfies the penalty assert tf.evaluate({'x': np.pi / 2})[0] == 1 # check penalized result if capacity constraint is not satisfied for x, y in [(4, 0), (0, 4)]: assert tf.evaluate({'x': x, 'y': y})[0] == 1e10 + 1
def test_AEPMaxLoadCostModelComponent_as_penalty_multi_wt(): tf = xy3tb.get_tf(design_vars={ 'x': ([0, 1]), 'y': ([0, 0]) }, cost_comp=AEPMaxLoadCostModelComponent( input_keys='xy', n_wt=2, output_keys=["AEP", ('loads', [3, 3])], aep_load_function=lambda x, y: (-np.sin(np.hypot(x, y)).sum(), np.hypot(x, y)), max_loads=[3, 3]), constraints=[], driver=EasySimpleGADriver(), plot_comp=None) # check normal result that satisfies the penalty assert tf.evaluate({'x': [np.pi / 2, -np.pi / 2]})[0] == 2 # check penalized result if capacity constraint is not satisfied for x, y in [([4, 0], [0, 0]), ([0, 0], [4, 0]), ([4, 4], [0, 0])]: assert tf.evaluate({'x': x, 'y': y})[0] == 1e10 + 1
def test_TopFarmProblemXYBoundaryConstraint(): tf = xy3tb.get_tf(design_vars={ 'x': [3, 7, 4], 'y': [-3, -7, -3] }, constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1]) desvars = tf.driver._designvars if tuple(map(int, scipy.__version__.split("."))) < (1, 5, 0): for xy in 'xy': for lu in ['lower', 'upper']: npt.assert_equal(desvars['indeps.' + xy][lu], np.nan) else: for i, xy in enumerate('xy'): for lu, func in zip(['lower', 'upper'], (np.min, np.max)): npt.assert_equal(desvars['indeps.' + xy][lu], func(xy3tb.boundary[:, i]))
def testTopFarmProblem_turbine_positions(): tf = xy3tb.get_tf() np.testing.assert_array_equal(tf.turbine_positions, xy3tb.initial)
def get_topfarm_problem(id, plot=False): # setup topfarm problem with max 3 slsqp iterations tf = xy3tb.get_tf(plot=plot, driver=EasyScipyOptimizeDriver(maxiter=3, disp=False)) # Shuffle position via smartstart, XX and YY is posible starting point coordinates tf.smart_start(XX=np.linspace(0, 6, 10), YY=np.linspace(-10, 0, 10), random_pct=100) return tf