Ejemplo n.º 1
0
def ga_main1(out='result'):
    file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1]
    optimizer = NSGA2.load(file=file)

    elite = optimizer.get_elite()
    print('elite:', len(elite))

    for epoch, population in enumerate(optimizer):
        print(epoch, end='\n')
        plt.cla()

        print([fit.rank for fit in population])
        exit()

        for i in range(1):
            front = [x for x in population if x.rank == i]
            if not front:
                continue
            x, y = np.array([x.data.value for x in front]).T
            plt.scatter(x, y, label=f'{front[0][0]:.3f}')

        plt.legend()
        if epoch < len(optimizer) - 1:
            plt.pause(0.2)
        else:
            plt.show()
Ejemplo n.º 2
0
def ga_result1(out='result'):
    file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1]
    optimizer = NSGA2.load(file=file)
    print('epoch:', len(optimizer))

    elite = optimizer.get_elite()
    print('elite:', len(elite))

    for epoch in range(0, len(optimizer) - 1, 10):
        print(epoch, end='\n')
        # plt.cla()
        population = optimizer[epoch]
        population = optimizer.calc_rank(population)

        for i in range(1, 2):
            front = [x for x in population if x.rank == i]
            # print(sorted([x.rank for x in population]))
            if not front:
                continue
            x, y = np.array([x.data.value for x in front]).T
            # print(front)
            # return
            plt.scatter(x, y, label=f'{front[0][0]:.3f}')

        # plt.legend()
        plt.xlim((0, 50))
        # plt.ylim((0, 50))
        if epoch < (len(optimizer) - 2) // 10 * 10:
            plt.pause(0.2)
        else:
            plt.show()
Ejemplo n.º 3
0
def ga_main1(out='result'):
    file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1]
    optimizer = MOEAD.load(file=file)

    elite = optimizer.get_elite()
    print('elite:', len(elite))
    imax = len(optimizer)

    for epoch, population in enumerate(optimizer):
        print(epoch, end='\n')
        plt.cla()

        # print([fit.rank for fit in population])
        # exit()

        # for i in range(1):
        #     front = [x for x in population if x.rank == i]
        #     if not front:
        #         continue
        #     x, y = np.array([x.data.value for x in front]).T
        #     plt.scatter(x, y, label=f'{front[0][0]:.3f}')
        front = population
        x, y = np.array([x.data.value for x in front]).T
        plt.scatter(x, y, label=f'{front[0][0]:.3f}')

        # plt.legend()
        # if epoch >= 10:
        #     plt.show()
        #     return

        plt.pause(10 / imax)
    plt.show()
Ejemplo n.º 4
0
def comp_main():
    def read(files):
        for file in files:
            name = re.search(r'out_\d+', file)[0]
            print(name, end=' \r')
            yield name, np.load(file)

    for d in ut.fsort(os.listdir('.')):
        loc = f'{d}/result'
        if not os.path.isdir(loc):
            continue

        try:
            with ut.chdir(loc):
                # if any(ut.iglobm('*.npz')):
                #     continue

                if any(ut.iglobm('*.npy')):
                    print(loc)

                for i in range(10):
                    out = f'out_{i:03d}.npz'
                    if os.path.isfile(out):
                        continue

                    l = i * 100
                    u = (i + 1) * 100 if i < 9 else 1001
                    files = [f'out_{j:04d}.plt.npy' for j in range(l, u)]

                    if not all(map(os.path.isfile, files)):
                        continue

                    print(f'{l}=>{u}', ' ' * 20, end='\r')
                    h = dict(read(files))

                    try:
                        np.savez_compressed(out, **h)

                    except:
                        if os.path.isfile(out):
                            os.remove(out)
                        raise

                    ck = np.load(out)
                    for nm in ck.files:
                        f = f'{nm}.plt.npy'
                        if os.path.isfile(f):
                            os.remove(f)

        except OSError:
            print('OSError')
            time.sleep(10)
            continue
Ejemplo n.º 5
0
def get_model(out):
    # モデル読み込み
    # model_cls = {'nsga2':NSGA2, 'moead':MOEAD}[model]
    files = ut.fsort(glob.glob(os.path.join(out, f'*epoch*.pkl')))
    for i, file in enumerate(files):
        print(f'[{i}]', file)
    print('select file')
    n = int(input())
    if n == -1:
        pass
    elif n < 0:
        return
    file = files[n]
    print('file:', file)
    env, optimizer, history = ut.load(file)
    return env, optimizer, history
Ejemplo n.º 6
0
def collect_result(dest):
    print('collect_result')
    odir = 'result'
    rdir = '__raw__'
    with post_base(dest):
        files = ut.fsort(glob.iglob('out_*.plt'))
        last = len(files) - 1
        if last < 1:
            return
        os.makedirs(odir, exist_ok=True)
        os.makedirs(rdir, exist_ok=True)
        for i, file in enumerate(files):
            # print(i, end='\r')
            ofile = os.path.join(odir, os.path.basename(file) + '.npy')
            if not os.path.isfile(ofile):
                data = read_plt(file)
                np.save(ofile, data)
            if i < last:
                shutil.move(file, rdir)
Ejemplo n.º 7
0
def zip_main():
    for loc in ut.fsort(ut.iglobm('**/image_*')):
        # loc = f'{d}/image_w'
        if not os.path.isdir(loc):
            continue

        with ut.chdir(loc):
            file = f'{ut.basename(loc)}.zip'
            if os.path.isfile(file):
                continue

            print(loc)
            # subprocess.run('zip image_w *.png', shell=True)

            try:

                def f_():
                    for png in ut.fsort(ut.iglobm('*.png')):
                        print(png, os.path.getsize(png), end='\r')
                        with open(png, 'rb'):
                            yield png

                pngs = list(f_())
                if not pngs:
                    continue

                with ZipFile(file, 'w', compression=ZIP_DEFLATED) as z:
                    for png in pngs:
                        z.write(png)
            except:
                if os.path.isfile(file):
                    os.remove(file)
                raise

            assert os.path.isfile(file)

            if os.path.isfile(file):
                with ZipFile(file) as z:
                    for png in z.namelist():
                        if os.path.isfile(png):
                            print(png)
                            os.remove(png)
Ejemplo n.º 8
0
def ga_main2(out='result'):
    file = ut.fsort(glob.glob(os.path.join(out, f'epoch*.pickle')))[-1]
    optimizer = NSGA2.load(file=file)

    population = optimizer[-1]
    front = [x for x in population if x.rank == 1]
    front.sort(key=attrgetter('data.value'))

    for ind in front:
        print(ind.value, ind.data.value)

    crowdings = [ind.value[1] for ind in front]

    fig, axes = plt.subplots(2)
    axes[0].plot(crowdings)

    x, y = np.array([x.data.value for x in front]).T
    im = axes[1].scatter(x, y, c=crowdings, cmap='jet')
    # plt.xlim((0, 1))
    # plt.ylim((0, 1))
    fig.colorbar(im)
    plt.show()
Ejemplo n.º 9
0
 def f_():
     for png in ut.fsort(ut.iglobm('*.png')):
         print(png, os.path.getsize(png), end='\r')
         with open(png, 'rb'):
             yield png