Example #1
0
def test_nonneighboring_distance_cost_close():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(minimum_dist_others=10,
                                     others_weight=1)

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

    expected_cost = 50  # 2 nodes * (D - sqrt( (x2-x1)^2 + (y2-y1)^2 ))^2 per node

    common = 2 * -2 * (10 - 5) / 5  # 2 nodes * (-2 * (D - norm) / norm) per node
    expected_grad = np.array([
        [common * -3, common * -4],
        [0, 0],
        [0, 0],
        [common * 3, common * 4]
    ]).flatten()

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

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
Example #2
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]
Example #3
0
def test_nonneighboring_distance_cost_far():
    g, node_indices, neighbors = make_one_component_engine_data()
    settings = top.OptimizerSettings(minimum_dist_others=10,
                                     others_weight=1)

    x = np.array([
        [0, 0],
        [0, 100],
        [100, 100],
        [100, 200]
    ]).reshape((-1, 1))

    expected_cost = 0
    expected_grad = np.zeros(8)

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

    assert cost == expected_cost
    np.testing.assert_equal(expected_grad, grad)
Example #4
0
def test_nonneighboring_distance_cost_multiple_nodes_close():
    g, node_indices, neighbors = make_two_component_engine_data()
    settings = top.OptimizerSettings(minimum_dist_others=20,
                                     others_weight=1)

    x = np.array([
        [0, 0],    # 1
        [100, 0],  # c1.1
        [103, 4],  # c2.1
        [106, 1],  # c1.2
        [109, 5],  # c2.2
        [200, 0]   # 2
    ]).reshape((-1, 1))

    cost_11_21 = 2 * (20 - 5) ** 2
    cost_11_22 = 2 * (20 - np.sqrt(106)) ** 2
    cost_12_21 = 2 * (20 - np.sqrt(18)) ** 2
    cost_12_22 = 2 * (20 - 5) ** 2
    expected_cost = cost_11_21 + cost_11_22 + cost_12_21 + cost_12_22

    # 2 nodes * (-2 * (D - norm) / norm) per node
    common_11_21 = 2 * -2 * (20 - 5) / 5
    common_11_22 = 2 * -2 * (20 - np.sqrt(106)) / np.sqrt(106)
    common_12_21 = 2 * -2 * (20 - np.sqrt(18)) / np.sqrt(18)
    common_12_22 = 2 * -2 * (20 - 5) / 5

    expected_grad = np.array([
        [0, 0],
        [common_11_21 * -3 + common_11_22 * -9, common_11_21 * -4 + common_11_22 * -5],
        [common_11_21 * 3 + common_12_21 * -3, common_11_21 * 4 + common_12_21 * 3],
        [common_12_21 * 3 + common_12_22 * -3, common_12_21 * -3 + common_12_22 * -4],
        [common_11_22 * 9 + common_12_22 * 3, common_11_22 * 5 + common_12_22 * 4],
        [0, 0]
    ]).flatten()

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

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