def main(): if __name__ == '__main__': from topfarm.cost_models.dummy import DummyCost, DummyCostPlotComp from topfarm.constraint_components.spacing import SpacingConstraint from topfarm.constraint_components.boundary import XYBoundaryConstraint from openmdao.api import view_model initial = np.array([[6, 0], [6, -8], [1, 1]]) # initial turbine layouts optimal = np.array([[2.5, -3], [6, -7], [4.5, -3]]) # optimal turbine layouts boundary = np.array([(0, 0), (6, 0), (6, -10), (0, -10)]) # turbine boundaries desired = np.array([[3, -3], [7, -7], [4, -3]]) # desired turbine layouts drivers = [ EasySimpleGADriver(max_gen=10, pop_size=100, bits={ 'x': [12] * 3, 'y': [12] * 3 }, random_state=1), EasyScipyOptimizeDriver() ] plot_comp = DummyCostPlotComp(optimal) tf = TopFarmProblem( design_vars=dict(zip('xy', initial.T)), cost_comp=DummyCost(optimal_state=desired, inputs=['x', 'y']), constraints=[XYBoundaryConstraint(boundary), SpacingConstraint(2)], driver=drivers[1], plot_comp=plot_comp) cost, _, recorder = tf.optimize() plot_comp.show()
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_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_capacity_tf(): # 15 turbines, 5 different types, 50MW max installed capacity n_wt = 15 rated_power_array_kW = np.linspace(1, 10, int(n_wt / 3)) * 1e3 inputtypes = np.tile(np.array([range(int(n_wt / 3))]), 3).flatten() tf = TopFarmProblem({'type': inputtypes}, constraints=[ CapacityConstraint( max_capacity=50, rated_power_array=rated_power_array_kW) ], driver=EasySimpleGADriver()) tf.evaluate() # case above the maximum allowed installed capacity, yes penalty assert tf["totalcapacity"] == 82.5 assert tf['penalty_comp.penalty_capacity_comp_50'] == 32.5 # set all turbines type 0, still 15 turbines and re-run the problem tf.evaluate({'type': inputtypes * 0}) # case below the maximum allowed installed capacity, no penalty assert tf["totalcapacity"] == 15 assert tf['penalty_comp.penalty_capacity_comp_50'][0] == 0.0
plot_comp=plot_comp, driver=driver, expected_cost=1.5) return _topfarm_obj @pytest.mark.parametrize('driver,tol', [ (EasyScipyOptimizeDriver(disp=False), 1e-4), (EasyScipyOptimizeDriver(tol=1e-3, disp=False), 1e-2), (EasyScipyOptimizeDriver(maxiter=14, disp=False), 1e-1), (EasyScipyOptimizeDriver(optimizer='COBYLA', tol=1e-3, disp=False), 1e-2), (EasySimpleGADriver(max_gen=10, pop_size=100, bits={ 'x': [12] * 3, 'y': [12] * 3 }, random_state=1), 1e-1), (EasyPyOptSparseIPOPT(), 1e-4), ][:]) def test_optimizers(driver, tol, topfarm_generator_scalable): if driver.__class__.__name__ == "PyOptSparseMissingDriver": pytest.xfail("Driver missing") tf = topfarm_generator_scalable(driver) tf.evaluate() cost, _, recorder = tf.optimize() tb_pos = tf.turbine_positions[:, :2] assert sum((tb_pos[2] - tb_pos[0])**2) > 2**2 - tol # check min spacing assert tb_pos[1][0] < 6 + tol # check within border