コード例 #1
0
def test_centroid_deviation_cost_term_nonzero_interior_node_y():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [0, 0],
        [0, 6],
        [0, 20],
        [0, 30]
    ]).reshape((-1, 1))

    expected_cost = 20  # (10-6)^2 = 16 for [0, 6] + (18-20)^2 = 4 for [0, 20]
    common_1 = 6 - (20 + 0) / 2  # y - mean(y_neighbors)
    common_2 = 20 - (30 + 6) / 2  # y - mean(y_neighbors)
    expected_grad = np.array([
        [0, -common_1],
        [0, 2*common_1 - common_2],
        [0, 2*common_2 - common_1],
        [0, -common_2]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #2
0
def test_centroid_deviation_cost_term_nonzero_interior_node_xy():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [10, 10],
        [16, 24],
        [30, 30],
        [40, 40]
    ]).reshape((-1, 1))

    expected_cost_x = 20  # (20-16)^2 = 16 for [16, 24] + (30-28)^2 = 4 for [30, 30]
    expected_cost_y = 20  # (20-24)^2 = 16 for [16, 24] + (30-32)^2 = 4 for [30, 30]
    expected_cost = expected_cost_x + expected_cost_y

    common_x_1 = 16 - (30 + 10) / 2  # x - mean(x_neighbors)
    common_x_2 = 30 - (40 + 16) / 2  # x - mean(x_neighbors)
    common_y_1 = 24 - (30 + 10) / 2  # y - mean(y_neighbors)
    common_y_2 = 30 - (40 + 24) / 2  # y - mean(y_neighbors)
    expected_grad = np.array([
        [-common_x_1, -common_y_1],
        [2*common_x_1 - common_x_2, 2*common_y_1 - common_y_2],
        [2*common_x_2 - common_x_1, 2*common_y_2 - common_y_1],
        [-common_x_2, -common_y_2]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #3
0
def test_centroid_deviation_cost_term_nonzero_interior_node_x():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [40, 0],
        [30, 0],
        [16, 0],
        [10, 0]
    ]).reshape((-1, 1))

    expected_cost = 20  # (28-30)^2 = 4 for [30, 0] + (20-16)^2 = 16 for [16, 0]
    common_1 = 30 - (40 + 16) / 2  # y - mean(y_neighbors)
    common_2 = 16 - (30 + 10) / 2  # y - mean(y_neighbors)
    expected_grad = np.array([
        [-common_1, 0],
        [2*common_1 - common_2, 0],
        [2*common_2 - common_1, 0],
        [-common_2, 0]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #4
0
def test_centroid_deviation_cost_term_nonzero_end_node_xy():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [6, 14],
        [16, 16],
        [22, 22],
        [28, 28]
    ]).reshape((-1, 1))

    expected_cost = 8  # Only node [16, 16] will incur cost (deviation of 2 in x, -2 in y)
    common_x = 16 - (22 + 6) / 2  # x - mean(x_neighbors)
    common_y = 16 - (22 + 14) / 2  # y - mean(y_neighbors)
    expected_grad = np.array([
        [-common_x, -common_y],
        [2*common_x, 2*common_y],
        [-common_x, -common_y],
        [0, 0]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #5
0
def test_centroid_deviation_cost_term_nonzero_end_node_y():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [0, 16],
        [0, 10],
        [0, 8],
        [0, 6]
    ]).reshape((-1, 1))

    expected_cost = 4  # Only node [0, 10] will incur cost (deviation of -2 in y)
    common = 10 - (16 + 8) / 2  # y - mean(y_neighbors)
    expected_grad = np.array([
        [0, -common],
        [0, 2*common],
        [0, -common],
        [0, 0]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #6
0
def test_centroid_deviation_cost_term_nonzero_end_node_x():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [4, 0],
        [10, 0],
        [12, 0],
        [14, 0]
    ]).reshape((-1, 1))

    expected_cost = 4  # Only node [10, 0] will incur cost (deviation of 2 in x)
    common = 10 - (4 + 12) / 2  # x - mean(x_neighbors)
    expected_grad = np.array([
        [-common, 0],
        [2*common, 0],
        [-common, 0],
        [0, 0]
    ]).flatten()

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
コード例 #7
0
def make_cost_terms(g, node_indices, neighbors, settings):
    c1 = top.NeighboringDistance(g,
                                 node_indices,
                                 neighbors,
                                 settings,
                                 internal=True)
    c2 = top.NeighboringDistance(g,
                                 node_indices,
                                 neighbors,
                                 settings,
                                 internal=False)
    c3 = top.NonNeighboringDistance(g, node_indices, neighbors, settings)
    c4 = top.CentroidDeviation(g, node_indices, neighbors, settings)
    c5 = top.RightAngleDeviation(g, node_indices, neighbors, settings)
    c6 = top.HorizontalDeviation(g, node_indices, neighbors, settings)

    return [c1, c2, c3, c4, c5, c6]
コード例 #8
0
def test_centroid_deviation_cost_term_zero():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(centroid_deviation_weight=1)

    x = np.array([
        [0, 0],
        [1, 0],
        [2, 0],
        [3, 0]
    ]).reshape((-1, 1))

    expected_cost = 0
    expected_grad = np.zeros(8)

    ct = top.CentroidDeviation(g, node_indices, neighbors, settings)
    cost, grad = ct.evaluate(top.make_costargs(x))

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)