简体   繁体   中英

What would be a good Selection function in a genetic algorithm to find a target number?

I'm writing a genetic algorithm to find an expression that expresses a target number, ie if the target number is 10 a solution could be 2*5 . I'm running into scenarios where my whole population becomes one identical chromosome and I think that the fitness function is in charge for this.

The following are possible chromosomes, obeying the rules that numbers and operators appears in the string alternately, in a way that no two digits or two operators are adjacent. A legal string would start with a digit or +/- operators. The expression would be calculated from left to right as-is (ignoring the order of arithmetic operations):

  • 1/2+3+5
  • -2+4+1+8
  • -7+6*2+8
  • +2/5-1+8 2+1*2-2
  • +2*7*7+3
  • +1/2/2/6 5/5*9*1
  • +3-1+1*8 3-8+7*1

Selection():

    def selection(population):
        total_finesses = Decimal(0.0)

        # Roulette selection:
        for chromosome in population:
            total_finesses += chromosome .fitness
        # Generate random number (spin the roulette).
        pick = Decimal(random.uniform(0, float(total_finesses)))
        current = Decimal(0.0)

        for i, chrom in enumerate(population):
            current += chrom.fitness
            if current > pick:
                return population[i]

Fitness():

    def fitness_calculator(chromosome):
        current_value = get_value(chromosome) # Returns the decimal value of the chromosome.

        return Decimal(-1 * (abs(target - current_value), 5))

As you can see, the fitness is considered better as it gets closer to 0, since I'm looking for the expression that expresses the number with the lowest delta between itself and the target number.

I think I have a problem with the Selection algorithm, to choose the lowest fitted chromosomes in a roulette approach.

I answered my a similar question on the computer science stackexchange here which explains in the best way possible how to find out which is a good selection method for your problem.

However it doesn't look as thought you have implemented mutation which is responsible for maintaining diversity in the population; so you might consider implementing that in some way. Several methods can be found here .

@TomDalton also makes some fair points in the comments to your question. If having several identical solutions in your pool is causing a problem, don't allow it. Also, randomised selection is rarely a good idea and can often render the evaluation function useless. Your evaluation function ranks the solutions in the pool. Use it! Some methods say take the best solutions and mate them to make the best better; and others says take worst solutions to keep all solutions pretty decent. On top of that there are a ton of other possible selection methods you can choose from.

My final point is this (though it may not apply to your directly it might help people who end up in this thread) a fine tuned initial population size is often underestimated when looking at GAs. Make sure you try a few variations of that too.

Reading material:

Miller, BL, & Goldberg, DE (1995). Genetic algorithms, tournament selection, and the effects of noise. Complex Systems, 9(3), 193-212.

Goldberg, DE, & Deb, K. (1991). A comparative analysis of selection schemes used in genetic algorithms. Urbana, 51, 61801-2996.

Poon, PW, & Carter, JN (1995). Genetic algorithm crossover operators for ordering applications. Computers & Operations Research, 22(1), 135-147.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM