예제 #1
0
def MakeSign2D(sign_type, scale):
    """
    いろんな種類の標識作成
    sign_type: 
    0: 半径0.3mの円
    1: 1辺0.8mの正三角形
    2: 1辺0.9mの正方形
    3. 1辺0.45mのひし形(てか正方形)
    4. 1辺が0.05~1のどれかの長方形

    scale: sign_typeのスケールを標準とした倍率
    """

    # 標識作成
    if sign_type == 0:
        r = 0.3 * scale
        fig = F2.circle([0, 0, r])
        fig_type = 0
        AABB = np.array([-0.35, 0.35, -0.35, 0.35]) * scale

    elif sign_type == 1:
        r = 0.8 / np.sqrt(3) * scale
        fig = F2.tri([0, 0, r, Random(0, np.pi * 2 / 3)])
        fig_type = 1
        AABB = np.array([-0.45, 0.45, -0.45, 0.45]) * scale

    elif sign_type == 2:
        r = 0.9 * scale
        fig = F2.rect([0, 0, r, r, Random(0, np.pi / 2)])
        fig_type = 2
        l = 0.9 * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale

    elif sign_type == 3:
        r = 0.45 * scale
        fig = F2.rect([0, 0, r, r, Random(0, np.pi / 2)])
        fig_type = 2
        l = 0.45 * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale

    else:
        w = Random(0.5, 2)
        h = Random(0.5, 2)
        fig = F2.rect([0, 0, w, h, Random(0, np.pi / 2)])
        fig_type = 2
        l = np.sqrt(w**2 + h**2) * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale

    return fig, fig_type, AABB
예제 #2
0
파일: MakeDataset.py 프로젝트: Suke-H/CSG
def MakeSign2D(sign_type, scale):

    # 標識作成
    if sign_type == 0:
        r = 0.3 * scale
        fig = F.circle([0, 0, r])
        fig_type = 0
        AABB = np.array([-0.35, 0.35, -0.35, 0.35]) * scale

    elif sign_type == 1:
        r = 0.8 / np.sqrt(3) * scale
        fig = F.tri([0, 0, r, Random(0, np.pi * 2 / 3)])
        fig_type = 1
        AABB = np.array([-0.45, 0.45, -0.45, 0.45]) * scale

    elif sign_type == 2:
        r = 0.9 * scale
        fig = F.rect([0, 0, r, r, Random(0, np.pi / 2)])
        fig_type = 2
        l = 0.9 * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale
    elif sign_type == 3:
        r = 0.45 * scale
        fig = F.rect([0, 0, r, r, Random(0, np.pi / 2)])
        fig_type = 2
        l = 0.45 * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale
    else:
        w = Random(0.5, 2)
        h = Random(0.5, 2)
        fig = F.rect([0, 0, w, h, Random(0, np.pi / 2)])
        fig_type = 2
        l = np.sqrt(w**2 + h**2) * np.sqrt(2) / 2
        AABB = np.array([-l * 1.1, l * 1.1, -l * 1.1, l * 1.1]) * scale

    # X, Y = Disassemble2d(sign_points)
    # plt.plot(X, Y, marker=".", linestyle='None', color="red")
    # plt.show()

    return fig, fig_type, AABB
예제 #3
0
파일: MakeDataset.py 프로젝트: Suke-H/CSG
def RandomRectangle(x_min=-100,
                    x_max=100,
                    y_min=-100,
                    y_max=100,
                    w_min=0,
                    w_max=20,
                    h_min=0,
                    h_max=20):
    x = Random(x_min, x_max)
    y = Random(y_min, y_max)
    w = Random(w_min, w_max)
    h = Random(h_min, h_max)
    t = Random(0, np.pi / 2)

    return F.rect([x, y, w, h, t])
예제 #4
0
파일: GA.py 프로젝트: Suke-H/CSG
def Crossover2(parents, fig, max_p, min_p, l, cross_rate):

    if np.random.rand() >= cross_rate:
        return np.random.choice(parents)

    # n: パラメータの数, x: n+1人の親のパラメータのリスト
    n = len(parents[0].figure.p)
    x = np.array([parents[i].figure.p for i in range(n + 1)])

    # g: xの重心
    g = np.sum(x, axis=0) / n

    alpha = np.sqrt(n + 2)

    # p, cを定義
    p, c = np.empty((0, n)), np.empty((0, n))
    p = np.append(p, [g + alpha * (x[0] - g)], axis=0)
    c = np.append(c, [[0 for i in range(n)]], axis=0)

    for i in range(1, n + 1):
        r = Random(0, 1)**(1 / i)
        p = np.append(p, [g + alpha * (x[i] - g)], axis=0)
        c = np.append(c, [r * (p[i - 1] - p[i] + c[i - 1])], axis=0)
        #print(r, p[i], c[i])

    # 子のパラメータはp[n]+c[n]となる
    child = p[n] + c[n]

    # パラメータが範囲外ならやり直し
    # if CheckIB(child, fig, max_p, min_p, l):
    #     break

    # パラメータが範囲外なら子は生成しない
    # if not CheckIB(child, fig, max_p, min_p, l):
    #     return None

    # パラメータをpersonクラスに代入する

    if fig == 0:
        figure = F.circle(child)
    elif fig == 1:
        figure = F.tri(child)
    elif fig == 2:
        figure = F.rect(child)

    return person(fig, figure)
예제 #5
0
파일: GA.py 프로젝트: Suke-H/PFD_withPython
def CreateRandomPerson(fig_type, max_p, min_p, l):
    """ 個体をランダム生成 """

    # 円
    if fig_type == 0:
        #print("円")
        # min_p < x,y < max_p
        x = Random(min_p[0], max_p[0])
        y = Random(min_p[1], max_p[1])
        # 0 < r < 2/3*l
        r = Random(0.2 * l, 2 / 3 * l)

        figure = F.circle([x, y, r])

    # 正三角形
    elif fig_type == 1:
        #print("正三角形")
        # min_p < x,y < max_p
        x = Random(min_p[0], max_p[0])
        y = Random(min_p[1], max_p[1])
        # 0 < r < 2/3*l
        r = Random(0.2 * l, 2 / 3 * l)
        # 0 < t < pi*2/3
        t = Random(0, np.pi * 2 / 3)

        figure = F.tri([x, y, r, t])

    # 長方形
    elif fig_type == 2:
        #print("長方形")
        # min_p < x,y < max_p
        x = Random(min_p[0], max_p[0])
        y = Random(min_p[1], max_p[1])
        # 0 < w,h < l
        w = Random(0.2 * l, l)
        h = Random(0.2 * l, l)
        # 0 < t < pi
        t = Random(0, np.pi)

        figure = F.rect([x, y, w, h, t])

    return person(fig_type, figure)
예제 #6
0
파일: GA.py 프로젝트: Suke-H/PFD_withPython
def Crossover(parents, fig, max_p, min_p, l, cross_rate):
    """
    交叉を実装
    シンプレックス(SPX)交叉を採用
    """

    if np.random.rand() >= cross_rate:
        return np.random.choice(parents)

    # n: パラメータの数, x: n+1人の親のパラメータのリスト
    n = len(parents[0].figure.p)
    x = np.array([parents[i].figure.p for i in range(n + 1)])

    # g: xの重心
    g = np.sum(x, axis=0) / n

    alpha = np.sqrt(n + 2)

    # p, cを定義
    p, c = np.empty((0, n)), np.empty((0, n))
    p = np.append(p, [g + alpha * (x[0] - g)], axis=0)
    c = np.append(c, [[0 for i in range(n)]], axis=0)

    for i in range(1, n + 1):
        r = Random(0, 1)**(1 / i)
        p = np.append(p, [g + alpha * (x[i] - g)], axis=0)
        c = np.append(c, [r * (p[i - 1] - p[i] + c[i - 1])], axis=0)

    # 子のパラメータはp[n]+c[n]となる
    child = p[n] + c[n]

    # パラメータをpersonクラスに代入する
    if fig == 0:
        figure = F.circle(child)
    elif fig == 1:
        figure = F.tri(child)
    elif fig == 2:
        figure = F.rect(child)

    return person(fig, figure)
예제 #7
0
파일: GAtest.py 프로젝트: Suke-H/CSG
def check_exam(fig_type,
               i,
               dir_path="data/dataset/tri/",
               out_path="data/GAtest/tri/"):
    # データセット読み込み
    fig_list = np.load(dir_path + "fig.npy")
    AABB_list = np.load(dir_path + "AABB.npy")
    outArea_list = np.load(dir_path + "outArea.npy")

    print("fig:{}".format(np.array(fig_list).shape))
    print("AABB:{}".format(np.array(AABB_list).shape))
    print("outArea:{}".format(np.array(outArea_list).shape))

    # points, outPointsはまずパスを読み込み
    points_paths = sorted(glob(dir_path + "points/**.npy"),\
                        key=lambda s: int(re.findall(r'\d+', s)[len(re.findall(r'\d+', s))-1]))
    outPoints_paths = sorted(glob(dir_path + "outPoints/**.npy"),\
                        key=lambda s: int(re.findall(r'\d+', s)[len(re.findall(r'\d+', s))-1]))

    print("epoch:{}".format(i))

    # points, outPoints読み込み
    points = np.load(points_paths[i])
    outPoints = np.load(outPoints_paths[i])

    # 他も参照
    fig_p = fig_list[i]
    AABB = AABB_list[i]
    outArea = outArea_list[i]

    if fig_type == 0:
        fig = F.circle(fig_p)
    elif fig_type == 1:
        fig = F.tri(fig_p)
    else:
        fig = F.rect(fig_p)
예제 #8
0
def calc_score1_2(points, out_contour, out_area, figure, flag=False):
    # AABB内にあるのかチェック
    max_p, min_p, _, l, AABB_area = buildAABB2d(points)

    # print(max_p, min_p)

    if len(figure.p) == 3:
        fig = 0
    elif len(figure.p) == 4:
        fig = 1
    else:
        fig = 2

    if not CheckIB2(figure.p, fig, max_p, min_p, l):
        return -100

    ########################################################

    # figureの2倍の図形を作成し、それを外枠とする
    if fig == 0:
        out_p = figure.p[:]
        out_p[2] *= 1.2
        out_figure = F.circle(out_p)

    elif fig == 1:
        out_p = figure.p[:]
        out_p[2] *= 1.2
        out_figure = F.tri(out_p)

    else:
        out_p = figure.p[:]
        out_p[2] *= 1.2
        out_p[3] *= 1.2
        out_figure = F.rect(out_p)

    # W >= 0(-図形の内部)のインデックスを取り出し、
    # その数をCinとして保存
    X, Y = Disassemble2d(points)
    W = figure.f_rep(X, Y)
    in_index = np.where(W >= 0)
    Cin = len(in_index[0])

    # Ain = 推定図形の面積
    Ain = figure.CalcArea()

    # Cout = outの点群数
    # (外枠内の点群数 - inの点群数)
    X2 = np.delete(X, in_index)
    Y2 = np.delete(Y, in_index)
    W2 = out_figure.f_rep(X2, Y2)
    out_index = np.where(W2 >= 0)
    Cout = len(out_index[0])

    # Aout = outの面積 - Ain
    Aout = out_figure.CalcArea() - Ain

    if flag == True:
        X = X[in_index]
        Y = Y[in_index]
        plt.plot(X, Y, marker=".", linestyle="None", color="blue")
        X2 = X2[out_index]
        Y2 = Y2[out_index]
        plt.plot(X2, Y2, marker=".", linestyle="None", color="red")

        fig1 = ContourPoints(figure.f_rep,
                             AABB=AABB,
                             grid_step=1000,
                             epsilon=0.01)
        Xin, Yin = Disassemble2d(fig1)
        plt.plot(Xin, Yin, marker=".", linestyle="None", color="blue")
        fig2 = ContourPoints(out_figure.f_rep,
                             AABB=AABB,
                             grid_step=1000,
                             epsilon=0.01)
        Xout, Yout = Disassemble2d(fig2)
        plt.plot(Xout, Yout, marker=".", linestyle="None", color="red")

        plt.show()

        print(points.shape)
        print("{}/{} - {}/{}".format(Cin, Ain, Cout, Aout))

    return Cin / Ain - Cout / Aout
예제 #9
0
파일: MakeDataset.py 프로젝트: Suke-H/CSG
def RandomRectangle2(w, h, x_min=-100, x_max=100, y_min=-100, y_max=100):
    x = Random(x_min, x_max)
    y = Random(y_min, y_max)
    t = Random(0, np.pi / 2)

    return F.rect([x, y, w, h, t])
예제 #10
0
파일: GAtest.py 프로젝트: Suke-H/CSG
def use_dataset(fig_type,
                num,
                dir_path="data/dataset/tri/",
                out_path="data/GAtest/tri/"):
    # データセット読み込み
    fig_list = np.load(dir_path + "fig.npy")
    AABB_list = np.load(dir_path + "AABB.npy")
    outArea_list = np.load(dir_path + "outArea.npy")

    print("fig:{}".format(np.array(fig_list).shape))
    print("AABB:{}".format(np.array(AABB_list).shape))
    print("outArea:{}".format(np.array(outArea_list).shape))

    # points, outPointsはまずパスを読み込み
    points_paths = sorted(glob(dir_path + "points/**.npy"),\
                        key=lambda s: int(re.findall(r'\d+', s)[len(re.findall(r'\d+', s))-1]))
    outPoints_paths = sorted(glob(dir_path + "outPoints/**.npy"),\
                        key=lambda s: int(re.findall(r'\d+', s)[len(re.findall(r'\d+', s))-1]))

    # print(points_paths)
    # print(outPoints_paths)

    for i in range(num):
        print("epoch:{}".format(i))

        # points, outPoints読み込み
        points = np.load(points_paths[i])
        outPoints = np.load(outPoints_paths[i])

        # print("points{}:{}".format(i, points.shape))
        # print("outPoints{}:{}".format(i, outPoints.shape))

        # 他も参照
        fig_p = fig_list[i]
        AABB = AABB_list[i]
        outArea = outArea_list[i]

        # print("p:{}".format(fig_p))
        # print("AABB:{}".format(AABB))
        # print("outArea:{}".format(outArea))

        if fig_type == 0:
            fig = F.circle(fig_p)
        elif fig_type == 1:
            fig = F.tri(fig_p)
        else:
            fig = F.rect(fig_p)

        # GAにより最適パラメータ出力

        # step1
        # best0 = EntireGA(points, outPoints, outArea, CalcIoU0, out_path+"score0/"+str(i)+".png")
        # best1 = EntireGA(points, outPoints, outArea, CalcIoU1, out_path+"score1/"+str(i)+".png")
        # best2 = EntireGA(points, outPoints, outArea, CalcIoU2, out_path+"score2/"+str(i)+".png")
        # best3 = EntireGA(points, outPoints, outArea, CalcIoU3, out_path+"score3/"+str(i)+".png")

        # step1.5
        #best0, n = EntireGA(points, outPoints, outArea, CalcIoU3, out_path+str(i)+".png")

        # step2
        # best0, alt0 = EntireGA(points, outPoints, outArea, CalcIoU1, out_path+"score0/"+str(i)+".png", fig_type)
        # best1, alt1 = EntireGA(points, outPoints, outArea, CalcIoU1, out_path+"score1/"+str(i)+".png", fig_type,
        #                 half_reset_num=15, all_reset_num=9)
        # best2, alt2 = EntireGA(points, outPoints, outArea, CalcIoU1, out_path+"score2/"+str(i)+".png", fig_type,
        #                 add_num=30)
        start = time.time()
        best3, alt3 = EntireGA(points,
                               outPoints,
                               outArea,
                               CalcIoU1,
                               out_path + "score3/" + str(i) + ".png",
                               fig_type,
                               tournament_size=25,
                               n_epoch=600,
                               add_num=30,
                               half_reset_num=15,
                               all_reset_num=9)
        mid1 = time.time()
        best4, alt4 = EntireGA(points,
                               outPoints,
                               outArea,
                               calc_score2,
                               out_path + "score4/" + str(i) + ".png",
                               fig_type,
                               tournament_size=25,
                               n_epoch=600,
                               add_num=30,
                               half_reset_num=15,
                               all_reset_num=9)
        mid2 = time.time()
        best5, alt5 = EntireGA(points,
                               outPoints,
                               outArea,
                               calc_score1_5,
                               out_path + "score5/" + str(i) + ".png",
                               fig_type,
                               tournament_size=25,
                               n_epoch=600,
                               add_num=30,
                               half_reset_num=15,
                               all_reset_num=9)
        mid3 = time.time()
        best6, alt6 = EntireGA(points,
                               outPoints,
                               outArea,
                               calc_score1_2,
                               out_path + "score6/" + str(i) + ".png",
                               fig_type,
                               tournament_size=25,
                               n_epoch=600,
                               add_num=30,
                               half_reset_num=15,
                               all_reset_num=9)
        end = time.time()
        print("score3:{}s, score4:{}s, score5:{}s, score6:{}s".format(
            (mid1 - start), (mid2 - mid1), (mid3 - mid2), (end - mid3)))

        rec_list = []

        # IoU0 = LastIoU(fig, best0.figure, AABB, out_path+"IoU0/"+str(i)+".png")
        # IoU1 = LastIoU(fig, best1.figure, AABB, out_path+"IoU1/"+str(i)+".png")
        # IoU2 = LastIoU(fig, best2.figure, AABB, out_path+"IoU2/"+str(i)+".png")
        IoU3 = LastIoU(fig, best3.figure, AABB,
                       out_path + "IoU3/" + str(i) + ".png")
        IoU4 = LastIoU(fig, best4.figure, AABB,
                       out_path + "IoU4/" + str(i) + ".png")
        IoU5 = LastIoU(fig, best5.figure, AABB,
                       out_path + "IoU5/" + str(i) + ".png")
        IoU6 = LastIoU(fig, best6.figure, AABB,
                       out_path + "IoU6/" + str(i) + ".png")

        # rec_list.append(IoU0)
        # if IoU0 == -1:
        #     rec_list.append(LastIoU(fig, alt0.figure, AABB, out_path+"IoU0/"+str(i)+"re.png"))
        # rec_list.append(IoU1)
        # if IoU1 == -1:
        #     rec_list.append(LastIoU(fig, alt1.figure, AABB, out_path+"IoU1/"+str(i)+"re.png"))
        # rec_list.append(IoU2)
        # if IoU2 == -1:
        #     rec_list.append(LastIoU(fig, alt2.figure, AABB, out_path+"IoU2/"+str(i)+"re.png"))
        rec_list.append(IoU3)
        if IoU3 == -1:
            rec_list.append(
                LastIoU(fig, alt3.figure, AABB,
                        out_path + "IoU3/" + str(i) + "re.png"))
        rec_list.append(IoU4)
        if IoU4 == -1:
            rec_list.append(
                LastIoU(fig, alt4.figure, AABB,
                        out_path + "IoU4/" + str(i) + "re.png"))
        rec_list.append(IoU5)
        if IoU5 == -1:
            rec_list.append(
                LastIoU(fig, alt5.figure, AABB,
                        out_path + "IoU5/" + str(i) + "re.png"))
        rec_list.append(IoU6)
        if IoU5 == -1:
            rec_list.append(
                LastIoU(fig, alt6.figure, AABB,
                        out_path + "IoU6/" + str(i) + "re.png"))
        print(rec_list)

        with open(out_path + "circle_k.csv", 'a', newline="") as f:
            writer = csv.writer(f)
            writer.writerow(rec_list)