def generate_paretos(self): already_visited = [] CURRENT = self.evaluator.get_paretos_instances() PREVIOUS = [] while self.N < self.budget: self.N += 1 PREVIOUS = deepcopy(CURRENT) to_copy = random.choice(self.evaluator.get_paretos_instances()) while to_copy in already_visited: to_copy = random.choice(self.evaluator.get_paretos_instances()) already_visited.append(to_copy) for i in xrange(len(self.instance)): for j in xrange(i+1, len(self.instance)): neighbor = deepcopy(to_copy) neighborhood.swap(neighbor, i, j) c = tsp.cost(neighbor, self.objectives) self.evaluator.update(neighbor, tuple(c.values())) self.evaluator.clean_dominated() CURRENT = self.evaluator.get_paretos_instances() with open('results.txt', 'w') as f: for res in CURRENT: if res: f.write(';'.join(map(str, res))) f.write('\n') yield self.evaluator.get_paretos_costs()
def generate_paretos(self): already_visited = [] CURRENT = self.evaluator.get_paretos_instances() PREVIOUS = [] while self.ratio[0] <= 1.0: print 'Looking for ratios: %s' % (' '.join(map(str, self.ratio))) PREVIOUS = deepcopy(CURRENT) cnt = 0 n = len(self.evaluator.get_paretos_instances()) to_copy = random.choice(self.evaluator.get_paretos_instances()) while to_copy in already_visited: cnt += 1 if cnt > n: break to_copy = random.choice(self.evaluator.get_paretos_instances()) already_visited.append(to_copy) ct = tsp.cost(to_copy, self.objectives) stop = False while not stop: best_neighbor = None best_cost = float('inf') # best neighbor for i in xrange(len(self.instance)): for j in xrange(i+1, len(self.instance)): neighbor = deepcopy(to_copy) neighborhood.swap(neighbor, i, j) c = tsp.cost(neighbor, self.objectives) cr = self.proportional_score(c) if cr < best_cost: best_neighbor = neighbor best_cost = cr if best_cost < self.proportional_score(ct): to_copy = best_neighbor ct = tsp.cost(to_copy, self.objectives) else: stop = True self.evaluator.update(to_copy, tuple(ct.values())) CURRENT = self.evaluator.get_paretos_instances() with open('results.txt', 'w') as f: for res in CURRENT: if res: f.write(';'.join(map(str, res))) f.write('\n') self.update_ratios() yield self.evaluator.get_paretos_costs()