Exemplo n.º 1
0
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

        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

        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=EasyScipyOptimizeDriver(),
            plot_comp=plot_comp)
        tf.optimize()
        plot_comp.show()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
def test_turbine_Type_multistart_XYZ_optimization():
    plot_comp = DummyCostPlotComp(optimal, delay=.5)
    plot_comp = NoPlot()
    xyz = [(0, 0, 0), (1, 1, 1)]

    p1 = DummyCost(optimal_state=optimal,
                   inputs=['x', 'y', 'z', 'type'])

    p2 = TurbineXYZOptimizationProblem(
        cost_comp=p1,
        turbineXYZ=xyz,
        min_spacing=2,
        boundary_comp=get_boundary_comp(),
        plot_comp=plot_comp,
        driver=EasyScipyOptimizeDriver(disp=True, optimizer='COBYLA', maxiter=10))
    p3 = InitialXYZOptimizationProblem(
        cost_comp=p2,
        turbineXYZ=xyz, min_spacing=2,
        boundary_comp=get_boundary_comp(),
        driver=DOEDriver(ListGenerator([[('x', [0, 4]), ('y', [2, 2]), ('z', [4, 1])]])))
    tf = TurbineTypeOptimizationProblem(
        cost_comp=p3,
        turbineTypes=[0, 0], lower=0, upper=1,
        driver=DOEDriver(FullFactorialGenerator(1)))

    case_gen = tf.driver.options['generator']
    cost, state, recorder = tf.optimize()
    print(cost)
    # print (state)
    print(recorder.get('type'))
    print(recorder.get('cost'))
    best_index = np.argmin(recorder.get('cost'))
    initial_xyz_recorder = recorder['recorder'][best_index]
    xyz_recorder = initial_xyz_recorder.get('recorder')[0]
    npt.assert_almost_equal(xyz_recorder['cost'][-1], cost)
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
def test_turbineType_and_XYZ_optimization():
    plot_comp = DummyCostPlotComp(optimal)
    plot_comp = NoPlot()
    cost_comp = DummyCost(
        optimal_state=optimal,
        inputs=['x', 'y', 'z', 'type'])
    xyz_opt_problem = TurbineXYZOptimizationProblem(
        cost_comp,
        turbineXYZ=[(0, 0, 0), (1, 1, 1)],
        min_spacing=2,
        boundary_comp=get_boundary_comp(),
        plot_comp=plot_comp,
        driver=EasyScipyOptimizeDriver(disp=False))
    tf = TurbineTypeOptimizationProblem(
        cost_comp=xyz_opt_problem,
        turbineTypes=[0, 0], lower=0, upper=1,
        driver=DOEDriver(FullFactorialGenerator(2)))
    cost = tf.optimize()[0]
    npt.assert_almost_equal(cost, 0)
Exemplo n.º 6
0
def get_tf(plot=False, **kwargs):
    k = {
        'cost_comp':
        DummyCost(desired[:, :2], [topfarm.x_key, topfarm.y_key]),
        'design_vars': {
            topfarm.x_key: initial[:, 0],
            topfarm.y_key: initial[:, 1]
        },
        'driver':
        EasyScipyOptimizeDriver(disp=False, tol=1e-8),
        'plot_comp':
        NoPlot(),
        'constraints': [
            SpacingConstraint(2),
            XYBoundaryConstraint(boundary),
            CapacityConstraint(**capacit)
        ]
    }
    if plot:
        k['plot_comp'] = DummyCostPlotComp(desired)
    k.update(kwargs)
    return TopFarmProblem(**k)
Exemplo n.º 7
0
def main():
    if __name__ == '__main__':
        # ------------------------ INPUTS ------------------------

        # 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

        try:
            import matplotlib.pyplot as plt
            plt.gcf()
            plot_comp = DummyCostPlotComp(desired)
            plot = True
        except RuntimeError:
            plot_comp = NoPlot()
            plot = False
        # ------------------------ OPTIMIZATION ------------------------

        # create the wind farm and run the optimization

        tf = TopFarmProblem(design_vars={
            'x': initial[:, 0],
            'y': initial[:, 1]
        },
                            cost_comp=DummyCost(desired, ['x', 'y']),
                            constraints=[
                                XYBoundaryConstraint(boundary),
                                SpacingConstraint(min_spacing)
                            ],
                            plot_comp=plot_comp,
                            driver=EasyScipyOptimizeDriver())
        cost, state, recorder = tf.optimize()
        tf.plot_comp.show()

        # final position
        final_x, final_y = state['x'], state['y']

        # get the positions tried during optimization from the recorder
        rec_x, rec_y = recorder['x'], recorder['y']

        # get the final, optimal positions
        optimized = tf.turbine_positions

        # ------------------------ PLOT (if possible) ------------------------

        if plot:

            # initialize the figure and axes
            fig = plt.figure(1, figsize=(7, 5))
            plt.clf()
            ax = plt.axes()

            # plot the boundary and desired locations
            ax.add_patch(
                Polygon(boundary, closed=True, fill=False,
                        label='Boundary'))  # boundary
            plt.plot(desired[:, 0],
                     desired[:, 1],
                     'ok',
                     mfc='None',
                     ms=10,
                     label='Desired')  # desired positions

            # plot the history of each turbine
            for i_turb in range(rec_x.shape[1]):
                l, = plt.plot(rec_x[0, i_turb],
                              rec_y[0, i_turb],
                              'x',
                              ms=8,
                              label=f'Turbine {i_turb+1}')  # initial
                plt.plot(rec_x[:, i_turb], rec_y[:, i_turb],
                         c=l.get_color())  # tested values
                plt.plot(rec_x[-1, i_turb],
                         rec_y[-1, i_turb],
                         'o',
                         ms=8,
                         c=l.get_color())  # final

            # make a few adjustments to the plot
            ax.autoscale_view()  # autoscale the boundary
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                       loc=3,
                       ncol=4,
                       mode='expand',
                       borderaxespad=0.)  # add a legend
            plt.tight_layout()  # zoom the plot in
            plt.axis('off')  # remove the axis

            # save the png
            folder, file = os.path.split(__file__)
            fig.savefig(folder + "/figures/" + file.replace('.py', '.png'))