def test_qap(n = 5, i = 0): total_ind = 10 total_generations = 100 max_pareto_size = 20 op = GeneticOperators() instancias = parse_qap() flux_mats = instancias[i][:-1] dist_mat = instancias[i][-1] num_loc = len(flux_mats[0]) #nro de localidades objs = [] for cost_mat in flux_mats: objs.append(QAPObjectiveFunction(dist_mat, cost_mat)) spea = SPEA(len(objs), op, max_pareto_size) pareto_set = ParetoSet(None) for i in xrange(n): pop = [] for i in xrange(total_ind): sol = range(num_loc) random.shuffle(sol) pop.append(GaSolution(sol, objs)) spea.run(pop, total_generations) pareto_set.update(pop) pareto_front = ParetoFront(pareto_set) pareto_front.draw() return pareto_set
def run(self, P, num_generations): """ Ejecuta el algoritmo SPEA @param P: la poblacion inicial @param num_generations: el numero maximo de generaciones """ ps = ParetoSet() for i in xrange(num_generations): ps.update(P) for s in ps.solutions: if s in P: P.remove(s) if len(ps.solutions) > self.max_pareto_points: self.reduce_pareto_set(ps) self.fitness_assignment(ps, P) mating_pool = self.selection(P, ps) P = self.next_generation(mating_pool, len(P))
def test_tsp(n = 5, i = 0): total_ind = 10 total_generations = 100 max_pareto_size = 20 op = GeneticOperators() instancias = parse_tsp() cost_mats = instancias[i] objs = [] for cost_mat in cost_mats: objs.append(TSPObjectiveFunction(cost_mat)) num_cities = len(objs[0].mat) spea = SPEA(len(objs), op, max_pareto_size) pareto_set = ParetoSet(None) for i in xrange(n): pop = [] for i in xrange(total_ind): sol = range(num_cities) random.shuffle(sol) pop.append(GaSolution(sol, objs)) spea.run(pop, 100) pareto_set.update(pop) pareto_front = ParetoFront(pareto_set) pareto_front.draw() return pareto_set