def main():
    if __name__ == '__main__':
        # ------------------------ INPUTS ------------------------

        # define the conditions for the wind farm
        positions = np.array([[0, 0], [6, 6]])  # initial turbine pos
        optimal_types = np.array([[2], [6]])  # optimal layout

        # ===============================================================================
        # Setup the problem and plotting
        # ===============================================================================

        try:
            import matplotlib.pyplot as plt
            plt.gcf()
            plot_comp = TurbineTypePlotComponent(
                turbine_type_names=["Turbine %d" % i for i in range(5)],
                plot_initial=False,
                delay=0.1,
                legendloc=0)
            plot = True
        except RuntimeError:
            plot_comp = NoPlot()
            plot = False

        # create the wind farm
        tf = TopFarmProblem(
            design_vars={'type': ([0, 0], 0, 4)},
            cost_comp=DummyCost(optimal_types, ['type']),
            plot_comp=plot_comp,
            driver=FullFactorialGenerator(5),
            ext_vars={
                'x': positions[:, 0],
                'y': positions[:, 1]
            },
        )

        # ===============================================================================
        # #  Run the optimization
        # ===============================================================================
        state = {}
        cost, state, recorder = tf.optimize(state)

        # ===============================================================================
        # plot and prin the the final, optimal types
        # ===============================================================================
        print(state['type'])
        tf.evaluate(state)

        # save the figure
        if plot:
            folder, file = os.path.split(__file__)
            plt.savefig(folder + "/figures/" + file.replace('.py', '.png'))
            plt.show()
def test_TopFarmProblem_with_cirleboundary_plot():
    if os.name == 'posix' and "DISPLAY" not in os.environ:
        pytest.xfail("No display")
    optimal = np.array([(0, 0)])
    desvar = dict(zip('xy', optimal.T))
    plot_comp = PlotComp()
    b = CircleBoundaryConstraint([1, 2], 3)
    tf = TopFarmProblem(desvar,
                        DummyCost(optimal, 'xy'),
                        constraints=[b],
                        plot_comp=plot_comp)
    tf.evaluate()
示例#3
0
def test_NOJ_Topfarm(aep_calc):
    init_pos = aep_calc.wake_model.windFarm.pos
    with warnings.catch_warnings(
    ):  # suppress "warning, make sure that this position array is oriented in ndarray([n_wt, 2]) or ndarray([n_wt, 3])"
        warnings.simplefilter("ignore")
        tf = TopFarmProblem(dict(zip('xy', init_pos.T)),
                            aep_calc.get_TopFarm_cost_component(),
                            constraints=[
                                SpacingConstraint(160),
                                XYBoundaryConstraint(init_pos, 'square')
                            ])
        tf.evaluate()
    assert tf.cost == -18.90684500124578
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])
示例#5
0
def main():
    if __name__ == '__main__':
        # define the conditions for the wind farm
        boundary = [(0, 0), (6, 0), (6, -10), (0, -10)]  # turbine boundaries
        initial = np.array([[6, 0], [6, -8], [1, 1], [-1, -8]])  # initial turbine pos
        desired = np.array([[3, -3], [7, -7], [4, -3], [3, -7]])  # desired turbine pos
        optimal = np.array([[2.5, -3], [6, -7], [4.5, -3], [3, -7]])  # optimal layout
        min_spacing = 2  # min distance between turbines

        # ------------------------ OPTIMIZATION ------------------------

        # create the wind farm and run the optimization

        def wt_cost(i, x, y):
            time.sleep(0.01)
            return (desired[i, 0] - x[i])**2 + (desired[i, 1] - y[i])**2

        n_wt = len(initial)
        comps = [CostModelComponent('xy', 4,
                                    cost_function=lambda x, y, i=i:wt_cost(i, x, y),
                                    objective=False,
                                    output_key='cost%d' % i) for i in range(n_wt)]

        def sum_map(**kwargs):

            return np.sum([kwargs['cost%d' % i] for i in range(n_wt)])

        comps.append(CostModelComponent(['cost%d' % i for i in range(n_wt)], 1,
                                        cost_function=sum_map,
                                        objective=True))
        cost_comp = TopFarmParallelGroup(comps)

        tf = TopFarmProblem(
            design_vars={'x': initial[:, 0], 'y': initial[:, 1]},
            cost_comp=cost_comp,
            constraints=[XYBoundaryConstraint(boundary),
                         SpacingConstraint(min_spacing)],
#            plot_comp=DummyCostPlotComp(desired),
            plot_comp=NoPlot(),
            driver=EasyScipyOptimizeDriver()
        )
#        view_model(tf)
        #print(tf.evaluate({'x': desired[:, 0], 'y': desired[:, 1]}))
        print(tf.evaluate({'x': optimal[:, 0], 'y': optimal[:, 1]}, disp=False))
        #print(tf.evaluate({'x': initial[:, 0], 'y': initial[:, 1]}))
#        tic = time.time()
        cost, state, recorder = tf.optimize()
#        toc = time.time()
#        print('optimized in {:.3f}s '.format(toc-tic))
        tf.plot_comp.show()
示例#6
0
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
def test_turbineXYZ_optimization():
    optimal = np.array([(5, 4, 3), (3, 2, 1)])
    turbineXYZ = np.array([[0, 0, 0], [2, 2, 2]])
    design_vars = {k: v for k, v in zip('xy', turbineXYZ.T)}
    design_vars['z'] = (turbineXYZ[:, 2], 1, 4)

    xy_boundary = [(0, 0), (5, 5)]
    tf = TopFarmProblem(
        design_vars=design_vars,
        cost_comp=DummyCost(optimal, 'xyz'),
        driver=EasyScipyOptimizeDriver(disp=False),
        constraints=[XYBoundaryConstraint(xy_boundary, 'square')])

    cost, state = tf.evaluate()
    assert cost == 52
    np.testing.assert_array_equal(state['x'], [0, 2])

    cost = tf.optimize()[0]
    assert cost < 1e6
    np.testing.assert_array_almost_equal(tf.turbine_positions, optimal[:, :2],
                                         3)