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
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
def train(**kwargs): for k_, v_ in kwargs.items(): setattr(opt, k_, v_) if opt.vis is True: 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(), #change value to (0,1) tv.transforms.Lambda(lambda x: x * 255) ]) #change value to (0,255) dataset = tv.datasets.ImageFolder(opt.data_root, transforms) dataloader = data.DataLoader(dataset, opt.batch_size) #value is (0,255) transformer = TransformerNet() if opt.model_path: transformer.load_state_dict( t.load(opt.model_path, map_location=lambda _s, _: _s)) vgg = VGG16().eval() for param in vgg.parameters(): param.requires_grad = False optimizer = t.optim.Adam(transformer.parameters(), opt.lr) style = utils.get_style_data(opt.style_path) vis.img('style', (style[0] * 0.225 + 0.45).clamp(min=0, max=1)) if opt.use_gpu: transformer.cuda() style = style.cuda() vgg.cuda() style_v = Variable(style.unsqueeze(0), volatile=True) features_style = vgg(style_v) gram_style = [Variable(utils.gram_matrix(y.data)) for y in features_style] style_meter = tnt.meter.AverageValueMeter() content_meter = tnt.meter.AverageValueMeter() for epoch in range(opt.epoches): content_meter.reset() style_meter.reset() for ii, (x, _) in tqdm.tqdm(enumerate(dataloader)): optimizer.zero_grad() if opt.use_gpu: x = x.cuda() #(0,255) x = Variable(x) y = transformer(x) #(0,255) y = utils.normalize_batch(y) #(-2,2) x = utils.normalize_batch(x) #(-2,2) features_y = vgg(y) features_x = vgg(x) #calculate the content loss: it's only used relu2_2 # i think should add more layer's result to calculate the result like: w1*relu2_2+w2*relu3_2+w3*relu3_3+w4*relu4_3 content_loss = opt.content_weight * F.mse_loss( features_y.relu2_2, features_x.relu2_2) content_meter.add(content_loss.data) style_loss = 0 for ft_y, gm_s in zip(features_y, gram_style): gram_y = utils.gram_matrix(ft_y) style_loss += F.mse_loss(gram_y, gm_s.expand_as(gram_y)) style_meter.add(style_loss.data) style_loss *= opt.style_weight total_loss = content_loss + style_loss total_loss.backward() optimizer.step() if (ii + 1) % (opt.plot_every) == 0: if os.path.exists(opt.debug_file): ipdb.set_trace() vis.plot('content_loss', content_meter.value()[0]) vis.plot('style_loss', style_meter.value()[0]) vis.img('output', (y.data.cpu()[0] * 0.225 + 0.45).clamp(min=0, max=1)) vis.img('input', (x.data.cpu()[0] * 0.225 + 0.45).clamp(min=0, max=1)) vis.save([opt.env]) t.save(transformer.state_dict(), 'checkpoints/%s_style.pth' % epoch)