Пример #1
0
def count_inside(w_geom,
                 v,
                 u,
                 platform_index,
                 w_means=None,
                 w_std=None,
                 grids=None):
    if np.ndim(w_geom) == 1:
        w_geom = w_geom.reshape((1, -1))
    if w_means is not None:
        w_geom = w_geom * w_std + w_means
    if w_geom.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_geom, v, u, w_geom.shape[0])
    if grids.size <= 2:
        grids = grids.reshape(1, 2)
    else:
        grids = grids.reshape(int(grids.size / 2), 2)
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)

    count = 0
    inside_index = []
    for isamp in [0]:
        ind_use = np.arange(u * 2) + platform_index * u * 2
        points = w_geom[isamp, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)
        for i in range(grids.shape[0]):
            pole = Point(grids[i, :])
            if pole.within(poly):
                count += 1
                inside_index.append(i)
    return count, inside_index
Пример #2
0
def which_poles_in(w_, grids, garden_index, v=5, u=8):
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))

    if w_.shape[1] == ((u + 3) * v - 2):
        w_ = ut.convert_toxyz(w_, v, u, w_.shape[0])
    if grids.size <= 2:
        grids = grids.reshape(1, 2)
    else:
        grids = grids.reshape(int(grids.size / 2), 2)
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)
    good_ind = []
    for pole in range(grids.shape[0]):
        point = Point(grids[pole, :])
        for i in range(v):
            ind_use = np.arange(u * 2) + i * u * 2
            points = w_[garden_index, ind_use].reshape(u, -1)
            points = np.concatenate([points, points[0, :].reshape(1, 2)],
                                    axis=0)
            poly = Polygon(points)
            is_in = in_polygon(point, poly)
            if is_in:
                good_ind.append(pole)
                break

    return good_ind
Пример #3
0
def count_inside(w_geom,
                 v,
                 u,
                 garden_index,
                 w_means=None,
                 w_std=None,
                 grids=None):
    count = 0
    inside_index = []
    h_max = 12
    # sort platforms by height
    platforms = w_geom[garden_index, :]
    h_index = [50, 51, 52]
    heights = platforms[h_index]
    sorted_args = np.argsort(heights)
    sorted_args += 1
    sorted_indices = np.zeros(5)
    sorted_indices[0] = 0
    sorted_indices[4] = 4
    sorted_indices[1:4] = sorted_args
    pole_heights = np.zeros(grids.shape[0])
    count_per_platform = np.zeros(5)

    if np.ndim(w_geom) == 1:
        w_geom = w_geom.reshape((1, -1))
    if w_means is not None:
        w_geom = w_geom * w_std + w_means
    if w_geom.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_geom, v, u, w_geom.shape[0])
    if grids.size <= 2:
        grids = grids.reshape(1, 2)
    else:
        grids = grids.reshape(int(grids.size / 2), 2)
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)

    for sorted_plat in sorted_indices:
        sorted_plat = int(sorted_plat)
        ind_use = np.arange(u * 2) + sorted_plat * u * 2
        points = w_geom[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)
        for i in range(grids.shape[0]):
            pole = Point(grids[i, :])
            #if pole_heights[i] <= h_max:
            if pole.within(poly):
                count_per_platform[sorted_plat] += 1
                if sorted_plat == 0:
                    pole_heights[i] = 4.5
                elif sorted_plat == 4:
                    pole_heights[i] = 12
                else:
                    index = sorted_plat - 1
                    pole_heights[i] = heights[index]

    return count_per_platform
Пример #4
0
def calc_distance_to_platform(w_geom,
                              v,
                              u,
                              garden_index,
                              w_means=None,
                              w_std=None,
                              grids=None):
    mpt_g = convex_hull_garden(w_geom, garden_index)

    if np.ndim(w_geom) == 1:
        w_geom = w_geom.reshape((1, -1))
    if w_means is not None:
        w_geom = w_geom * w_std + w_means
    if w_geom.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_geom, v, u, w_geom.shape[0])

    if grids.ndim < 2:
        grids = grids.reshape(11, 2)

    distances_matrix = np.zeros((grids.shape[0], v))
    inside_dist = []
    outside_dist = []
    pole_in_one_poly = np.zeros((grids.shape[0]))
    for iw in range(v):
        ind_use = np.arange(u * 2) + iw * u * 2
        points = w_geom[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)

        for pole_index in range(grids.shape[0]):
            pole = Point(grids[pole_index, :])
            if pole.within(mpt_g.convex_hull):
                if pole.within(poly):
                    dist = poly.exterior.distance(pole)
                    distances_matrix[pole_index, iw] = dist
                    inside_dist.append(dist)
                    pole_in_one_poly[pole_index] = 1
                else:
                    dist = poly.boundary.distance(pole)
                    distances_matrix[pole_index, iw] = dist
                    outside_dist.append(dist)
            else:
                distances_matrix[pole_index, iw] = 5
    count = 0
    loss = 0
    for pole_index in range(grids.shape[0]):
        if pole_in_one_poly[pole_index] == 0:
            count += 1
    if count >= 5:
        loss += count * 0.1

    return distances_matrix, np.array(inside_dist), np.array(
        outside_dist), loss
Пример #5
0
def give_linear_rings(w_,v,u,garden_index):
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))

    if w_.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_, v, u, w_.shape[0])

    points_list = []
    for i in range(v):
        ind_use = np.arange(u * 2) + i * u * 2
        points = w_geom[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = LinearRing(points)
        points_list.append(poly)
    return points_list
Пример #6
0
def convex_hull_garden(w_, garden_index, v=5, u=8):
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))

    if w_.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_, v, u, w_.shape[0])

    points_list = []
    for i in range(v):
        ind_use = np.arange(u * 2) + i * u * 2
        points = w_geom[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)
        points_list.append(poly)
    mtp = MultiPolygon(points_list)
    #cvx = mtp.convex_hull
    return mtp
Пример #7
0
def pole_to_pole_distances(w_geom,
                           v,
                           u,
                           w_means=None,
                           w_std=None,
                           samp_to_plot=None,
                           grids=None):
    if np.ndim(w_geom) == 1:
        w_geom = w_geom.reshape((1, -1))
    if w_means is not None:
        w_geom = w_geom * w_std + w_means
    if w_geom.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_geom, v, u, w_geom.shape[0])

    if grids.ndim < 2:
        grids = grids.reshape(grids.shape[0] / 11, 2)
    if samp_to_plot is None:
        vec_rand_samples = np.random.permutation(w_geom.shape[0])
    else:
        vec_rand_samples = [samp_to_plot]
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)
    count = 0
    inside_index = []
    pole_to_pole = np.zeros((grids.shape[0], grids.shape[0]))

    for isamp in vec_rand_samples[:1]:
        for iw in range(v):
            ind_use = np.arange(u * 2) + iw * u * 2
            points = w_geom[isamp, ind_use].reshape(u, -1)
            points = np.concatenate([points, points[0, :].reshape(1, 2)],
                                    axis=0)
            poly = Polygon(points)

            for i in range(grids.shape[0]):
                # pole1 = Point(grids[i,:])
                for j in range(grids.shape[0]):
                    # pole2 = Point(grids[j,:])
                    # if pole1.within(poly) and pole2.within(poly):
                    # dist = pole1.distance(pole2)
                    dist = np.linalg.norm(grids[i, :] - grids[j, :])
                    pole_to_pole[i, j] = dist

    return pole_to_pole
Пример #8
0
def get_polygon(w_, garden_index):
    # returns five polygons in a list
    u = 8
    v = 5
    poly_list = []
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))
    if w_.shape[1] == ((u + 3) * v - 2):
        w_ = ut.convert_toxyz(w_, v, u, w_.shape[0])

    for platform_index in range(5):
        ind_use = np.arange(u * 2) + platform_index * u * 2
        points = w_[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        #plt.plot(points[:,0],points[:,1])
        #plt.show()
        poly = Polygon(points)
        poly_list.append(poly)

    return poly_list
Пример #9
0
def calc_distance_to_platform(w_geom,
                              v,
                              u,
                              w_means=None,
                              w_std=None,
                              grids=None):
    if np.ndim(w_geom) == 1:
        w_geom = w_geom.reshape((1, -1))
    if w_means is not None:
        w_geom = w_geom * w_std + w_means
    if w_geom.shape[1] == ((u + 3) * v - 2):
        w_geom = ut.convert_toxyz(w_geom, v, u, w_geom.shape[0])

    if grids.ndim < 2:
        grids = grids.reshape(11, 2)

    distances_matrix = np.zeros((grids.shape[0], v))
    inside_dist = []
    outside_dist = []

    for isamp in [0]:
        for iw in range(v):
            ind_use = np.arange(u * 2) + iw * u * 2
            points = w_geom[isamp, ind_use].reshape(u, -1)
            points = np.concatenate([points, points[0, :].reshape(1, 2)],
                                    axis=0)
            poly = Polygon(points)

            for pole_index in range(grids.shape[0]):
                pole = Point(grids[pole_index, :])
                if pole.within(poly):
                    dist = poly.exterior.distance(pole)
                    distances_matrix[pole_index, iw] = dist
                    inside_dist.append(dist)
                else:
                    dist = poly.boundary.distance(pole)
                    distances_matrix[pole_index, iw] = dist
                    outside_dist.append(dist)

    return distances_matrix, np.array(inside_dist), np.array(outside_dist)
Пример #10
0
def random_search_in_platform(platform_ind, w_, garden_index, grids):
    u = 8
    v = 5
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))
    if w_.shape[1] == ((u + 3) * v - 2):
        w_ = ut.convert_toxyz(w_, v, u, w_.shape[0])
    if grids.size <= 2:
        grids = grids.reshape(1, 2)
    else:
        grids = grids.reshape(int(grids.size / 2), 2)
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)
    for platform_index in platform_ind:
        ind_use = np.arange(u * 2) + platform_index * u * 2
        points = w_[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)
        count, _ = count_inside(w_,
                                v,
                                u,
                                platform_index,
                                garden_index,
                                grids=grids)
        while count < 3:
            print("COUNT IN EXTERNAL LOOP {}".format(count))
            rand_point = np.array(get_random_point_in_polygon(poly)).reshape(
                1, 2)
            print(rand_point)
            tmp = grids
            tmp = np.append(tmp, rand_point, axis=0)
            pole_platform, _ = poles_platform_distance_constraint(
                tmp, w_, garden_index)
            pole_pole, _ = all_poles_dist_constraint(tmp)
            c = max(pole_pole, pole_platform)
            if c <= 0:
                count += 1
                grids = np.append(grids, rand_point, axis=0)
    return grids
Пример #11
0
def bo_in_platform(platform_ind, w_, garden_index, grids):
    u = 8
    v = 5
    if np.ndim(w_) == 1:
        w_ = w_.reshape((1, -1))
    if w_.shape[1] == ((u + 3) * v - 2):
        w_ = ut.convert_toxyz(w_, v, u, w_.shape[0])
    if grids.size <= 2:
        grids = grids.reshape(1, 2)
    else:
        grids = grids.reshape(int(grids.size / 2), 2)
    if grids.ndim < 2:
        grids = grids.reshape(11, 2)

    grids = np.random.uniform(low=0, high=1, size=(15, 2))
    for platform_index in platform_ind:
        ind_use = np.arange(u * 2) + platform_index * u * 2
        points = w_[garden_index, ind_use].reshape(u, -1)
        points = np.concatenate([points, points[0, :].reshape(1, 2)], axis=0)
        poly = Polygon(points)

        minx, miny, maxx, maxy = poly.bounds
        bounds = [{
            'name': 'p1_1',
            'type': 'continuous',
            'domain': (minx, maxx)
        }, {
            'name': 'p1_2',
            'type': 'continuous',
            'domain': (miny, maxy)
        }, {
            'name': 'p2_1',
            'type': 'continuous',
            'domain': (minx, maxx)
        }, {
            'name': 'p2_2',
            'type': 'continuous',
            'domain': (miny, maxy)
        }, {
            'name': 'p3_1',
            'type': 'continuous',
            'domain': (minx, maxx)
        }, {
            'name': 'p3_2',
            'type': 'continuous',
            'domain': (miny, maxy)
        }]
        poles = np.random.uniform(low=minx, high=maxx, size=(3, 2))
        _, _, Y_init = all_constraints(w_,
                                       garden_index,
                                       poles,
                                       final_check=True)
        poles = poles.reshape(1, 6)
        Y_step = np.empty((1, 1))
        Y_step[0, 0] = Y_init

        count = 0
        context = {}
        while count < 3:
            bo_step = BayesianOptimization(f=None,
                                           model="GP_MCMC",
                                           domain=bounds,
                                           X=poles,
                                           Y=Y_step,
                                           maximize=False,
                                           acquisition="LCB")
            print("COUNT IN EXTERNAL LOOP {} for platform {}".format(
                count, platform_index))

            x_next = bo_step.suggest_next_locations()
            y_next, good_poles, counts = all_constraints(w_,
                                                         garden_index,
                                                         poles,
                                                         final_check=True)
            print("good poles {}".format(good_poles))
            print("context {}".format(context))
            num_fixed_poles = len(context)
            print("num fixed poles {}".format(num_fixed_poles))
            # good_indices = list(chain.from_iterable((i, i + 1) for i in good_poles))
            fixed_poles = poles[np.argmin(Y_step), :]  # [good_indices]
            for p in good_poles:
                context["p{}_{}".format(p, 1)] = fixed_poles[p]
                context["p{}_{}".format(p, 2)] = fixed_poles[p + 1]
                count += 1
            y_tmp = np.empty((1, 1))
            y_tmp[0, 0] = y_next
            poles = np.vstack((poles, x_next))
            Y_step = np.vstack((Y_step, y_tmp))
            loss = y_tmp[0, 0]
            if loss <= 0:
                count += 1

        results = poles[np.argmin(Y_step), :].reshape(3, 2)
        grids[platform_index * 3:platform_index * 3 + 3, :] = results

    return grids