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, distance_prefer_constant=2.0, 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.distance_prefer_constant = distance_prefer_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 = OffspringMakerASRSwithPandas(self.crossover_list, self.reproduction_num,
                                                            self.distance_prefer_constant, self.config, self.problem)
        self.offspring_changer = OffspringChangerASRS(self.mutation_list, self.distance_prefer_constant,
                                                      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 " "
Exemple #3
0
class GaSolverASRSwithPandas(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
    distance_prefer_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, distance_prefer_constant=2.0, 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.distance_prefer_constant = distance_prefer_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 = OffspringMakerASRSwithPandas(self.crossover_list, self.reproduction_num,
                                                            self.distance_prefer_constant, self.config, self.problem)
        self.offspring_changer = OffspringChangerASRS(self.mutation_list, self.distance_prefer_constant,
                                                      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 chromosome_index in range(0, self.population_size):
            # print str(chromosome_index) + " th chromosome"
            chromosome = self.population.chromosome_list[chromosome_index]
            self.evaluator.evaluate_chromosome_with_pandas(chromosome)
            self.offspinrg_enhancer.local_search_iteration(chromosome)
        self.evaluator.evaluate_population_with_pandas(self.population)
        print "\t\t\t\tinit gen : " + str(self.population.best_fitness)\
              + ", " + str(self.population.worst_fitness) + ""
        while self.generation_num < self.maximum_generation:
            previous_best = self.population.best_fitness
            parents_pair_indices_dic = self.selector.get_parents(self.population)
            # current_time = time.time()
            # print ""
            offspring_list = self.offspring_maker.get_offsprings(self.population.chromosome_list,
                                                                 parents_pair_indices_dic)
            # print "\toffspring is made : " + str(time.time() - current_time)
            # current_time = time.time()
            # print "\noffspring generate phase\n"
            for offspring_index in range(0, len(offspring_list)):
                offspring = offspring_list[offspring_index]
                self.evaluator.evaluate_chromosome_with_pandas(offspring)
                print "\t\t" + str(offspring_index) + " th offspring's initial fitness : " + str(offspring.fitness)
                while np.random.rand() < self.mutation_probability:
                    # current_time = time.time()
                    self.offspring_changer.change_offspring(offspring)
                    # print "\tmutation : " + str(time.time() - current_time)
                    # current_time = time.time()
                    # self.evaluator.evaluate_chromosome_with_pandas(offspring)
                    # print "\t\t" + str(offspring_index) + " th offspring's mutation fitness : " + str(offspring.fitness)
                    # self.offspring_maker.repair_chromosome(offspring)
                    # print "\t\t" + str(offspring_index) + " th offspring's repaired fitness : " + str(offspring.fitness)

                    # self.evaluator.evaluate_chromosome_with_pandas(offspring)
                    # print "\t" + str(offspring_index) + " th offspring's repaired fitness : " + str(offspring.fitness)
                self.offspinrg_enhancer.local_search_iteration(offspring)
                # self.evaluator.evaluate_chromosome_with_pandas(offspring)
                # print "\t" + str(offspring_index) + " th offspring's optimized fitness : " + str(offspring.fitness)
                # self.evaluator.evaluate_chromosome_with_pandas(offspring)
                # print str(offspring_index) + " th offspring's fitness : " + str(offspring.fitness) + "\n"

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

            current_best = self.population.best_fitness
            print "\t\t\t\t%d th gen : " % self.generation_num + str(current_best) + ", " + str(self.population.worst_fitness) + ""
            if current_best < previous_best:
                print "\t\t\t\t\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 " "