Exemplo n.º 1
0
        def breed(parent_a, parent_b):
            """Breeds two parent genotypes together to produce a child genotype using
            n-point crossover.

            Returns the child genotype.
            """
            a_bulbs = list(parent_a.bulbs)
            b_bulbs = list(parent_b.bulbs)

            # Perform a n-point crossover on the parent's bulbs
            n = int(self.config.settings['n_point_crossover'])

            min_crossover_index = 0
            max_crossover_index = min(len(a_bulbs) - 1, len(b_bulbs) - 1)

            if not max_crossover_index:
                if len(parent_a.bulbs):
                    return genotype_class.Genotype(parent_a.bulbs)
                else:
                    return genotype_class.Genotype(parent_b.bulbs)

            crossover_indices = []
            rand_start = min_crossover_index
            for _ in range(n):
                if not rand_start == max_crossover_index and rand_start < max_crossover_index:
                    crossover_indices.append(
                        random.randint(rand_start, max_crossover_index))
                    rand_start = crossover_indices[-1]

            # Ensure the entire parent is copied during crossover
            crossover_indices.append(max_crossover_index + 1)

            child_bulbs = set([])
            prev_crossover_index = 0
            for crossover_index in crossover_indices:
                if random.random() < float(
                        self.config.settings['parent_selection_weight']):
                    # Choose parent_a's substring
                    for bulb in a_bulbs[prev_crossover_index:crossover_index]:
                        child_bulbs.add(bulb)

                else:
                    # Choose parent_b's substring
                    for bulb in b_bulbs[prev_crossover_index:crossover_index]:
                        child_bulbs.add(bulb)

                prev_crossover_index = crossover_index

            return genotype_class.Genotype(child_bulbs)
Exemplo n.º 2
0
    def __init__(self, config):
        """Initializes the EADriver class.
        
        Where config is a Config object. 
        """

        self.config = config

        # Initialize the seed class
        self.seed = seed_class.Seed(self.config)

        self.population_size = int(self.config.settings['mu'])
        self.offspring_pool_size = int(self.config.settings['lambda'])

        self.run_count = 1
        self.best_fit_global_genotype = genotype_class.Genotype()
        self.best_fit_global_genotype.fitness = -1 * int(
            self.config.settings['arbitrary_large_number'])

        self.init_run_variables()

        # Initialize the log file class
        self.log = log_class.Log(self.config,
                                 self.seed,
                                 self.phenotype,
                                 overwrite=True)
Exemplo n.º 3
0
    def init_run_variables(self):
        """Initializes run specific variables.

        This function should be called before each run.
        """
        def force_adj_bulbs():
            """Places bulbs around black squares where there is only one valid
            bulb placement pattern.
            """
            bulbs = set([])

            # Determine where to place bulbs
            for black_square in self.phenotype.black_squares:
                # Get the adjacent coordinates to black_square that are not black
                adj_coords = [
                    s for s in self.phenotype.get_adj_coords(black_square)
                    if not s in self.phenotype.black_squares
                ]

                if self.phenotype.black_squares[black_square] == len(
                        adj_coords):
                    # There is only one way to place bulbs around this square
                    # Place those bulbs
                    for coord in adj_coords:
                        self.phenotype.place_bulb(coord, bulbs)

            # Save bulb placements to each genotype
            for genotype in self.population:
                genotype.bulbs = copy.deepcopy(bulbs)

        def init_puzzles_with_bulbs():
            """Randomly places bulbs on each puzzle in population in a uniform manner.
            
            The number of attempted bulb placement failures is determined by config.
            """
            for genotype_index in range(len(self.population)):
                # Place bulbs until num_bulb_placement_failures failures are reached
                failure_count = 0
                while failure_count < int(
                        self.config.settings['num_bulb_placement_failures']):
                    if not self.phenotype.place_bulb_randomly(
                            self.population[genotype_index].bulbs):
                        failure_count += 1

        self.max_run_fitness = 0
        self.eval_count = 0
        self.avg_fitness_ratio = 0.0
        self.total_fitnesses_seen = 0
        self.total_fitness_ratio_sum = 0
        self.stale_fitness_count = 0
        self.prev_avg_fitness_ratio = 0.0
        self.best_fit_local_genotype = genotype_class.Genotype()

        # Create/reset the base puzzle class (phenotype)
        self.phenotype = puzzle_class.LightUpPuzzle(self.config)

        # Create/reset the puzzle population: a list genotypes
        self.population = []
        for _ in range(self.population_size):
            self.population.append(genotype_class.Genotype())

        self.parents = []
        self.children = []

        if int(self.config.settings['force_validity']):
            # Use black square adjacency heuristic to force validity
            force_adj_bulbs()

        init_puzzles_with_bulbs()
Exemplo n.º 4
0
    config_file = args.get_args()[0]

    # Setup configuration
    config = config_class.Config(config_file)

    # Initialize the EA driver and its run variables
    # Even though it will be used in performing a random search
    ea_driver = ea_driver_class.EADriver(config)

    # Perform the random search
    while ea_driver.run_count <= int(config.settings["num_experiment_runs"]):
        while ea_driver.eval_count <= int(
                config.settings['num_fitness_evaluations']):
            ea_driver.eval_count += 1

            genotype = genotype_class.Genotype()

            # Place bulbs until num_bulb_placement_failures failures are reached
            failure_count = 0
            while failure_count < int(
                    ea_driver.config.settings['num_bulb_placement_failures']):
                if not ea_driver.phenotype.place_bulb_randomly(genotype.bulbs):
                    failure_count += 1

            ea_driver.phenotype.check_valid_solution(genotype.bulbs)
            genotype.fitness = ea_driver.phenotype.get_fitness()
            genotype.fitness_ratio = genotype.fitness / (
                ea_driver.phenotype.num_rows * ea_driver.phenotype.num_cols -
                len(ea_driver.phenotype.black_squares))

            if ea_driver.best_fit_local_genotype.fitness_ratio < genotype.fitness_ratio: