Exemple #1
0
    def __init__(self, problem=None, config=None,
                 population_size=10, maximum_generation=5, crossover_list=None, mutation_list=None,
                 mutation_probability=0.1, reproduction_num=1, replacement_num=1, replacement_policy='total_elitism',
                 roulette_constant=2.5, initial_population=None):
        self.problem = problem
        self.config = config
        self.population_size = population_size
        self.maximum_generation = maximum_generation
        self.crossover_list = crossover_list
        self.mutation_list = mutation_list
        self.mutation_probability = mutation_probability
        self.reproduction_num = reproduction_num
        self.replacement_num = replacement_num
        self.roulette_constant = roulette_constant
        self.population = initial_population

        self.evaluator = EvaluatorASRS(self.population, self.config)
        self.selector = ParentSelectorASRS(self.population_size, self.reproduction_num, self.roulette_constant)
        self.offspring_maker = OffspringMakerASRS(self.crossover_list, self.reproduction_num, self.config, self.problem)
        self.offspring_changer = OffspringChangerASRS(self.mutation_list, self.config, self.problem)
        self.offspinrg_enhancer = OffspringEnhancerASRS(self.config, self.evaluator)
        self.generation_maker = GenerationMakerASRS(replacement_policy, self.replacement_num, self.reproduction_num)
Exemple #2
0
class GaSolverASRS(object):
    # problem and configuration information
    problem = None
    config = None

    # GA basic parameters
    population_size = 0
    maximum_generation = 0
    crossover_list = {}
    mutation_list = {}
    mutation_probability = 0.0
    reproduction_num = 0
    replacement_num = 0
    roulette_constant = 0.0

    # ga operators
    evaluator = None
    selector = None
    offspring_maker = None
    offspring_changer = None
    offspinrg_enhancer = None
    generation_maker = None

    # ga objects
    population = None

    # ga variables
    generation_num = 1

    # for log
    evolution_history = None

    def __init__(self, problem=None, config=None,
                 population_size=10, maximum_generation=5, crossover_list=None, mutation_list=None,
                 mutation_probability=0.1, reproduction_num=1, replacement_num=1, replacement_policy='total_elitism',
                 roulette_constant=2.5, initial_population=None):
        self.problem = problem
        self.config = config
        self.population_size = population_size
        self.maximum_generation = maximum_generation
        self.crossover_list = crossover_list
        self.mutation_list = mutation_list
        self.mutation_probability = mutation_probability
        self.reproduction_num = reproduction_num
        self.replacement_num = replacement_num
        self.roulette_constant = roulette_constant
        self.population = initial_population

        self.evaluator = EvaluatorASRS(self.population, self.config)
        self.selector = ParentSelectorASRS(self.population_size, self.reproduction_num, self.roulette_constant)
        self.offspring_maker = OffspringMakerASRS(self.crossover_list, self.reproduction_num, self.config, self.problem)
        self.offspring_changer = OffspringChangerASRS(self.mutation_list, self.config, self.problem)
        self.offspinrg_enhancer = OffspringEnhancerASRS(self.config, self.evaluator)
        self.generation_maker = GenerationMakerASRS(replacement_policy, self.replacement_num, self.reproduction_num)

    def run_ga(self):
        for i in range(0, self.population_size):
            chromosome = self.population.chromosome_list[i]
            optimized_chromosome = self.offspinrg_enhancer.cycle_optimize(chromosome)
            self.population.chromosome_list[i] = optimized_chromosome
        # print '3'
        self.evaluator.evaluate_population(self.population)
        while self.generation_num < self.maximum_generation:
            previous_best = self.population.best_fitness
            parents_pair_indices_dic = self.selector.get_parents(self.population)
            offspring_list = self.offspring_maker.get_offsprings(self.population.chromosome_list,
                                                                 parents_pair_indices_dic)
            for i in range(0, len(offspring_list)):
                offspring = offspring_list[i]
                while np.random.rand() < self.mutation_probability:
                    self.offspring_changer.location_change_mutation(offspring)
                    mutant = self.offspring_maker.repair_chromosome(offspring)
                    offspring_list[i] = mutant
            for i in range(0, len(offspring_list)):
                offspring = offspring_list[i]
                optimized_offspring = self.offspinrg_enhancer.cycle_optimize(offspring)
                self.evaluator.evaluate_chromosome(optimized_offspring)
                # self.evaluator.evaluate_chromosome(offspring)
                offspring_list[i] = optimized_offspring

            self.generation_maker.evolve_to_next_generation(self.population, offspring_list, parents_pair_indices_dic)

            current_best = self.population.best_fitness
            # print "%d th gen : " % self.generation_num + str(current_best) + ", " + str(self.population.worst_fitness)
            if current_best < previous_best:
                print "\t%d th generation: best = %f, worst = %f" % (self.generation_num, self.population.best_fitness,
                                                                     self.population.worst_fitness)
            self.generation_num += 1
        # print self.problem.rack.cell_info.__str__()
        # print self.problem.rack.cell_info.__str__()
        print "\t" + self.population.chromosome_list[0].__str__()
        print ""
        # self.run_simulation(self.population.chromosome_list[0])

    def run_simulation(self, chromosome):
        rack = self.problem.rack
        print "initial rack status"
        print rack.cell_info.__str__()
        print " "
        for gene_idx in chromosome.gene_idx_list:
            gene = chromosome.idx_gene_dic.get(gene_idx)
            print gene.__str__()
            print "location : " + rack.cell_info[gene.x, gene.y, gene.z]
            if gene.task_type == "S":
                rack.cell_info[gene.x, gene.y, gene.z] = gene.item_type
            else:
                rack.cell_info[gene.x, gene.y, gene.z] = "empty"
            print "changed rack status"
            print rack.cell_info.__str__()
            print " "