Beispiel #1
0
def testLoop(hexa, lnk, viz):
    if viz:
        viz = Visualizer(hexa);
    
    hexa.setWalking(1., 0., 0.);
    while True:
        hexa.tick();
        if lnk:
            lnk.tick();
        if viz:
            viz.tick();
Beispiel #2
0
def controlLoop(hexa, lnk, viz):
    if viz:
        viz = Visualizer(hexa);
    
    with ControlSource(hexa) as control:
        while True:
            control.tick();
            hexa.tick();
            lnk.tick();
            if viz:
                viz.tick();
Beispiel #3
0
def k_means_clustering(data, featureWeightMap, show=False):
    SimilarityCalc = similarityCalculator.SimilarityCalculator(featureWeightMap)
    attribute_clusters = _compute_k_means_clusters(data, SimilarityCalc.simiarity_according_to_attributes, 5)
    attribute_and_friendship_clusters = _compute_k_means_clusters(data, SimilarityCalc.simiarity_according_to_attributes_and_friendship, 10)
    weighted_attribute_and_friendship_clusters = _compute_k_means_clusters(data, SimilarityCalc.similarity_weighted_attributes_friendship, 3.5)
    
    if show:
        visualizer = Visualizer()
        for personID in data.persons.getOriginalPersons():
            visualizer.visualizeClusters( attribute_clusters[personID] )
            visualizer.visualizeClusters( attribute_and_friendship_clusters[personID] )

    return attribute_clusters, attribute_and_friendship_clusters, weighted_attribute_and_friendship_clusters
    def __init__(self, maze, **settings):
        self.maze = maze
        self.ants = []

        for name, value in settings.items():
            setattr(self, name, value)

        if self.visualize:
            self.visualizer = Visualizer(maze)
            self.visualizer.save('0_initial.png')

        if self.multiprocessing:
            self.pool = multiprocessing.Pool()
Beispiel #5
0
            print stat_testing_values, '='

            for model, name in zip(models, model_names):

                clf = model
                clf.fit(stat_training_attrs, stat_training_values)
                stat_pred = clf.predict(stat_training_attrs)
                print stat_pred
                diff =[]
                for trueValue, predValue in zip(stat_testing_values, stat_pred):
                    diff.append(abs(trueValue - predValue))
                print 'Errors for model', name, ':', sum(diff), 'on value set', value_set

    # Visualize data
    if args.v:
        visualizer = Visualizer()
        visualizer.visualize(data, EDGE_FUNCS[args.edge], split=args.split,
                save=args.save, show=args.show)

    if args.p:
        # Select prediction method
        if args.p == 'kmeans':
            print 'Using k-means clustering metric.'
            attribute_clusters, attribute_and_friendship_clusters, weighted_attribute_and_friendship_clusters = k_means_clustering(data, featureWeightMap, args.show)
            
            attribute_clusters = _convert_kmeans_format(attribute_clusters)
            attribute_and_friendship_clusters = _convert_kmeans_format(attribute_and_friendship_clusters)
            weighted_attribute_and_friendship_clusters = _convert_kmeans_format(weighted_attribute_and_friendship_clusters)
            
            real_training_data = 'real_training_data.csv'
            kmeans_attrs = 'kmeans_attrs.csv'
Beispiel #6
0
def train(**kwargs):
    for k_, v_ in kwargs.items():
        setattr(opt, k_, v_)

    device=t.device('cuda') if opt.gpu else t.device('cpu')
    if opt.vis:
        from visualize import Visualizer
        vis = Visualizer(opt.env)

    # 数据
    transforms = tv.transforms.Compose([
        tv.transforms.Resize(opt.image_size),
        tv.transforms.CenterCrop(opt.image_size),
        tv.transforms.ToTensor(),
        tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    dataset = tv.datasets.ImageFolder(opt.data_path, transform=transforms)
    dataloader = t.utils.data.DataLoader(dataset,
                                         batch_size=opt.batch_size,
                                         shuffle=True,
                                         num_workers=opt.num_workers,
                                         drop_last=True
                                         )

    # 网络
    netg, netd = NetG(opt), NetD(opt)
    map_location = lambda storage, loc: storage
    if opt.netd_path:
        netd.load_state_dict(t.load(opt.netd_path, map_location=map_location))
    if opt.netg_path:
        netg.load_state_dict(t.load(opt.netg_path, map_location=map_location))
    netd.to(device)
    netg.to(device)


    # 定义优化器和损失
    optimizer_g = t.optim.Adam(netg.parameters(), opt.lr1, betas=(opt.beta1, 0.999))
    optimizer_d = t.optim.Adam(netd.parameters(), opt.lr2, betas=(opt.beta1, 0.999))
    criterion = t.nn.BCELoss().to(device)

    # 真图片label为1,假图片label为0
    # noises为生成网络的输入
    true_labels = t.ones(opt.batch_size).to(device)
    fake_labels = t.zeros(opt.batch_size).to(device)
    fix_noises = t.randn(opt.batch_size, opt.nz, 1, 1).to(device)
    noises = t.randn(opt.batch_size, opt.nz, 1, 1).to(device)

    errord_meter = AverageValueMeter()
    errorg_meter = AverageValueMeter()


    epochs = range(opt.max_epoch)
    for epoch in iter(epochs):
        for ii, (img, _) in tqdm.tqdm(enumerate(dataloader)):
            real_img = img.to(device)

            if ii % opt.d_every == 0:
                # 训练判别器
                optimizer_d.zero_grad()
                ## 尽可能的把真图片判别为正确
                output = netd(real_img)
                error_d_real = criterion(output, true_labels)
                error_d_real.backward()

                ## 尽可能把假图片判别为错误
                noises.data.copy_(t.randn(opt.batch_size, opt.nz, 1, 1))
                fake_img = netg(noises).detach()  # 根据噪声生成假图
                output = netd(fake_img)
                error_d_fake = criterion(output, fake_labels)
                error_d_fake.backward()
                optimizer_d.step()

                error_d = error_d_fake + error_d_real

                errord_meter.add(error_d.item())

            if ii % opt.g_every == 0:
                # 训练生成器
                optimizer_g.zero_grad()
                noises.data.copy_(t.randn(opt.batch_size, opt.nz, 1, 1))
                fake_img = netg(noises)
                output = netd(fake_img)
                error_g = criterion(output, true_labels)
                error_g.backward()
                optimizer_g.step()
                errorg_meter.add(error_g.item())

            if opt.vis and ii % opt.plot_every == opt.plot_every - 1:
                ## 可视化
                if os.path.exists(opt.debug_file):
                    ipdb.set_trace()
                fix_fake_imgs = netg(fix_noises)
                vis.images(fix_fake_imgs.detach().cpu().numpy()[:64] * 0.5 + 0.5, win='fixfake')
                vis.images(real_img.data.cpu().numpy()[:64] * 0.5 + 0.5, win='real')
                vis.plot('errord', errord_meter.value()[0])
                vis.plot('errorg', errorg_meter.value()[0])

        if (epoch+1) % opt.save_every == 0:
            # 保存模型、图片
            tv.utils.save_image(fix_fake_imgs.data[:64], '%s/%s.png' % (opt.save_path, epoch), normalize=True,
                                range=(-1, 1))
            t.save(netd.state_dict(), 'checkpoints/netd_%s.pth' % epoch)
            t.save(netg.state_dict(), 'checkpoints/netg_%s.pth' % epoch)
            errord_meter.reset()
            errorg_meter.reset()
class ACO(object):
    '''
    Perform ACO on the maze.
    '''

    iterations = 15

    evaporation = 0.1

    # Initialize Q to high value
    Q = 10000

    # update Q using the minimum path length  as value.
    update_Q = False

    ant_count = 10

    # Number of steps an ant may wander before it is terminated for that
    # iterations.
    ant_max_steps = 10000
    update_max_steps = False

    # Wether or not to optimize the trails of the ants after they found the end.
    optimize_ants = True

    visualize = True
    quiet = False

    multiprocessing = True

    do_reconnaissance = 4000

    maze_elimination = True

    def __init__(self, maze, **settings):
        self.maze = maze
        self.ants = []

        for name, value in settings.items():
            setattr(self, name, value)

        if self.visualize:
            self.visualizer = Visualizer(maze)
            self.visualizer.save('0_initial.png')

        if self.multiprocessing:
            self.pool = multiprocessing.Pool()

    def delta_matrix(self, ant):
        delta_tau = np.zeros((self.maze.height, self.maze.width))

        unique_positions = list(set(ant.position_list))
        delta_tau_k = self.Q / len(unique_positions)

        for x, y in unique_positions:
            delta_tau[y][x] += delta_tau_k

        return delta_tau

    def reconnaissance(self, iterations=1):
        maze = self.maze
        if self.do_reconnaissance < 1:
            return maze

        print 'performing reconnaissance with %d ants for %d steps in %d iterations' % (
            self.ant_count, self.do_reconnaissance, iterations
        )

        disabled = set()
        start_time = time.time()
        for iteration in range(iterations):
            ants = []
            for i in range(self.ant_count):
                ants.append(Ant(maze, maze.start))

            results = self.pool.map_async(
                ant_loop_apply, itertools.izip(ants, [self.do_reconnaissance] * self.ant_count)
            ).get(999999)

            for ant in results:
                for disable in ant.disable_positions:
                    maze.disable_at(disable)
                    disabled.add(disable)

        print 'Reconnaissance done, %d cells disabled in %0.2fs' % (
            len(disabled),
            time.time() - start_time
        )
        return maze

    def run(self):
        if not self.quiet:
            print 'starting with ACO with %d ants for %d iterations' % (
                self.ant_count, self.iterations
            )
        maze = self.maze

        self.iteration_best_trail = []

        # initialize ants
        for k in range(self.ant_count):
            self.ants.append(Ant(maze, maze.start))

        global_best = iteration_best = None
        for i in range(self.iterations):
            if not self.quiet:
                print '\nIteration: %d, Q: %d, max_steps: %d' % (i, self.Q, self.ant_max_steps)

            if self.multiprocessing:
                # Make ants do their steps.
                self.ants = self.pool.map_async(
                    ant_loop_apply, itertools.izip(self.ants, [self.ant_max_steps] * self.ant_count)
                ).get(9999999)
            else:
                print 'Stepping...'
                for ant in self.ants:
                    i = 0
                    while not ant.done and len(ant.trail) < self.ant_max_steps:
                        ant.step()

                        i += 1
                        if i % 1000 == 1:
                            self.visualizer.update('stepping: %d' % i)

                    if not ant.done:
                        print 'moving to next ant, this one stuck in', ant.position

            done_ants = [a for a in self.ants if a is not None and a.done]

            if not self.quiet:
                print '%d out of %d ants finished within %d steps.' % (
                    len(done_ants),
                    self.ant_count,
                    self.ant_max_steps
                )

            if self.optimize_ants:
                # optimize the trails for these ants
                opts = []
                for ant in done_ants:
                    opts.append(ant.optimize_trail(quiet=self.quiet))
                if not self.quiet:
                    print 'Optimisation reduced trail langth with an average of', mean(opts)

            # disable the dead ends found by the ant
            if self.maze_elimination:
                for ant in self.ants:
                    if ant is not None:
                        for p in ant.disable_positions:
                            self.maze.disable_at(p)

            # select the best ant:
            if len(done_ants) > 0:
                iteration_best = min(done_ants)

                # if global_best becomes invalid, forget it.
                # if global_best is not None:
                #     global_best.maze = self.maze
                #     if not global_best.is_valid():
                #         global_best = None
                #         if not self.quiet:
                #             print 'Forgot global best!'

                if global_best is None:
                    global_best = iteration_best.clone()
                else:
                    global_best = min([iteration_best, global_best]).clone()

            # update pheromone in the maze, for unique positions
            deltas = np.zeros((self.maze.height, self.maze.width))
            if global_best is not None:
                deltas = self.delta_matrix(global_best)

            if iteration_best is not None and global_best is not iteration_best:
                deltas += self.delta_matrix(iteration_best)

            # only update if iteration returned something.
            if iteration_best is not None:
                self.iteration_best_trail.append(len(iteration_best.trail))
            else:
                self.iteration_best_trail.append(None)

            maze.update_tau(delta_tau=deltas, evaporation=self.evaporation)

            # update ant_max_steps to the max value of this iteration
            if len(done_ants) > 3:
                if self.update_max_steps:
                    try:
                        self.ant_max_steps = min(
                            self.ant_max_steps,
                            max(len(x.trail) for x in done_ants if len(x.trail) < self.ant_max_steps)
                        )
                    except:
                        pass
                if self.update_Q:
                    self.Q = min(min(len(x.trail) for x in self.ants), self.Q)

            if not self.quiet:
                if iteration_best is not None and global_best is not None:
                    print 'Best ant: %d, iteration best: %d' % (
                        len(global_best.trail),
                        len(iteration_best.trail)
                    )
                else:
                    print 'None of the ants finished stepping'

            # reset ants
            for ant in self.ants:
                ant.reset(maze)

            if self.visualize:
                self.visualizer.update('Pheromone level iteration %d' % i)
                self.visualizer.save('%dth_iteration.png' % i)

        if self.multiprocessing:
            self.interrupt()

        self.global_best = global_best
        return global_best

    def interrupt(self):
        if self.multiprocessing:
            self.pool.close()
            self.pool.join()

    def get_first_iteration_with_best_trail(self):
        trail_length = len(self.global_best.trail)

        for i, val in enumerate(self.iteration_best_trail):
            if val == trail_length:
                return i