示例#1
0
def tests_smart_start():
    xs_ref = [
        1.6, 14.1, 1.6, 7.9, 14.1, 7.9, 19.9, 19.9, 7.8, 5.8, 14.2, 5.8, 1.5,
        16.2, 16.2, 1.6, 3.7, 14.1, 7.9, 3.7
    ]
    ys_ref = [
        1.6, 1.6, 7.9, 1.6, 7.9, 7.9, 1.6, 7.9, 5.8, 7.8, 5.8, 1.5, 5.8, 7.8,
        1.5, 3.7, 1.6, 3.7, 3.7, 7.9
    ]
    N_WT = 20
    min_space = 2.1

    XX, YY, val = egg_tray_map()
    xs, ys = smart_start(XX, YY, val, N_WT, min_space, seed=0)

    if 0:
        import matplotlib.pyplot as plt
        plt.contourf(XX, YY, val, 100)
        for i in range(N_WT):
            circle = plt.Circle((xs[i], ys[i]),
                                min_space / 2,
                                color='b',
                                fill=False)
            plt.gcf().gca().add_artist(circle)
            plt.plot(xs[i], ys[i], 'rx')
        print(np.round(xs, 1).tolist())
        print(np.round(ys, 1).tolist())

        plt.axis('equal')
        plt.show()
    npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref])
示例#2
0
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)
示例#3
0
def tests_smart_start():
    xs_ref = [
        1.6, 14.1, 1.6, 7.9, 14.1, 7.9, 19.9, 19.9, 7.8, 5.8, 14.2, 5.8, 1.5,
        16.2, 16.2, 1.6, 3.7, 14.1, 7.9, 3.7
    ]
    ys_ref = [
        1.6, 1.6, 7.9, 1.6, 7.9, 7.9, 1.6, 7.9, 5.8, 7.8, 5.8, 1.5, 5.8, 7.8,
        1.5, 3.7, 1.6, 3.7, 3.7, 7.9
    ]

    x = np.arange(0, 20, 0.1)
    y = np.arange(0, 10, 0.1)
    YY, XX = np.meshgrid(y, x)
    val = np.sin(XX) + np.sin(YY)
    N_WT = 20
    min_space = 2.1
    np.random.seed(0)
    xs, ys = smart_start(XX, YY, val, N_WT, min_space)
    npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref])

    if 0:
        import matplotlib.pyplot as plt
        plt.contourf(XX, YY, val, 100)
        for i in range(N_WT):
            circle = plt.Circle((xs[i], ys[i]),
                                min_space / 2,
                                color='b',
                                fill=False)
            plt.gcf().gca().add_artist(circle)
            plt.plot(xs[i], ys[i], 'rx')
        print(np.round(xs, 1).tolist())
        print(np.round(ys, 1).tolist())

        plt.axis('equal')
        plt.show()
示例#4
0
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()
示例#5
0
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])
示例#6
0
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])
示例#7
0
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])
示例#8
0
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)
示例#9
0
def testCostModelComponentAdditionalOutput():
    def aep_cost(x, y):
        opt_x, opt_y = optimal.T
        return -np.sum((x - opt_x)**2 + (y - opt_y)**2), {'add_out': sum(x)}

    tf = get_tf(
        AEPCostModelComponent(['x', 'y'],
                              4,
                              aep_cost,
                              aep_gradients,
                              additional_output=[('add_out', 0)]))
    _, state, _ = tf.optimize()
    npt.assert_array_almost_equal(tf.turbine_positions[:, :2],
                                  optimal_with_constraints, 5)
    npt.assert_equal(sum(state['x']), state['add_out'])
示例#10
0
def tests_smart_start_random():
    xs_ref = [
        1.7, 13.9, 1.4, 7.7, 14.4, 7.6, 19.7, 19.7, 8.7, 19.4, 15.8, 12.4, 7.7,
        9.8, 14.1, 1.8, 9.7, 6.6, 13.6, 3.5
    ]
    ys_ref = [
        7.9, 1.4, 1.7, 1.3, 7.9, 8.4, 1.7, 8.7, 6.4, 6.6, 2.3, 7.1, 3.5, 1.6,
        5.8, 5.8, 8.3, 6.5, 3.5, 1.4
    ]

    N_WT = 20
    min_space = 2.1
    XX, YY, val = egg_tray_map()
    np.random.seed(0)

    with pytest.raises(expected_exception=AssertionError):
        xs, ys = smart_start(XX,
                             YY,
                             val,
                             N_WT,
                             min_space,
                             random_pct=101,
                             seed=0)
    xs, ys = smart_start(XX, YY, val, N_WT, min_space, random_pct=1, seed=0)

    if 0:
        import matplotlib.pyplot as plt
        plt.contourf(XX, YY, val, 100)
        plt.plot(XX, YY, ',k')
        for i in range(N_WT):
            circle = plt.Circle((xs[i], ys[i]),
                                min_space / 2,
                                color='b',
                                fill=False)
            plt.gcf().gca().add_artist(circle)
            plt.plot(xs[i], ys[i], 'rx')
        print(np.round(xs, 1).tolist())
        print(np.round(ys, 1).tolist())

        plt.axis('equal')
        plt.show()
    npt.assert_array_almost_equal([xs, ys], [xs_ref, ys_ref])
示例#11
0
def test_WindResource():
    f = [
        0.035972, 0.039487, 0.051674, 0.070002, 0.083645, 0.064348, 0.086432,
        0.117705, 0.151576, 0.147379, 0.10012, 0.05166
    ]
    A = [
        9.176929, 9.782334, 9.531809, 9.909545, 10.04269, 9.593921, 9.584007,
        10.51499, 11.39895, 11.68746, 11.63732, 10.08803
    ]
    k = [
        2.392578, 2.447266, 2.412109, 2.591797, 2.755859, 2.595703, 2.583984,
        2.548828, 2.470703, 2.607422, 2.626953, 2.326172
    ]
    ti = np.zeros_like(f) + .1
    wr = WindResource(f, A, k, ti)

    wdir, ws, ti, weight = wr([0], [0], [4, 5])
    npt.assert_array_almost_equal(
        weight,
        np.array([[0.071381703], [0.088361194]]) * 0.035972 * (12 / 360))
示例#12
0
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]))