SS Weasel

I’ve written a genetic algorithm, which I have posted below, which implements single-step selection.

The relevant line for single-step selection is line 38:

fitness == 28 ? fitness : 0

  1. Have I successfully coded a genetic algorithm?

  2. Is this a version of the Weasel program?

  3. Does it demonstrate the power of cumulative selection?


#!/usr/bin/env ruby -w

#
# step_weasel.rb
#
# A simple GA in Ruby using single-step selection
#

TARGET_PHRASE = "METHINKS IT IS LIKE A WEASEL".chars
TARGET_LENGTH = TARGET_PHRASE.length

CHAR_SET = [' '] + ('A'..'Z').to_a

POPULATION_SIZE = 100
LENGTH_OF_RUN = 14_444

class WeaselFactory
  def self.create
    Array.new(TARGET_LENGTH).map {CHAR_SET.sample}
  end
end

def mutation_operator candidate
  candidate.map! do |locus|
    if rand(100) > 98
      CHAR_SET.sample
    else
      locus
    end
  end
end

def fitness_function candidate
  fitness = 0
  candidate.each_index do |idx|
    fitness += 1 if candidate[idx] == TARGET_PHRASE[idx]
  end
  fitness == 28 ? fitness : 0
end

def genetic_algorithm
  population = Array.new(POPULATION_SIZE).map {WeaselFactory.create}
  generation = 0
  most_fit_member = []

  while most_fit_member != TARGET_PHRASE
    temp_pop = population.map {|weasel| mutation_operator(weasel).dup}
    most_fit_member = temp_pop.sort_by {|weasel| fitness_function(weasel)}.last
    population = temp_pop.map {most_fit_member.dup}

    puts "#{generation}:" + most_fit_member.join
    generation += 1
    if generation == LENGTH_OF_RUN
      puts "Failed to Weasel"
      exit
    end
  end
end
genetic_algorithm
puts "It's a Weasel!"

I say I have coded both a Weasel and a GA. Now there’s no longer any need for keiths to quote-mine my comments.

Given this is a GA that implements single-step selection, we ought to now have a baseline for comparison against a GA that demonstrates the power of cumulative selection, but how would that work?

277 thoughts on “SS Weasel

  1. petrushka: I could be wrong about this, but I believe there is a way to compute the minimum possible value, in which case you could halt when some arbitrary percentage of this value is reached.

    Yes, Google says:

    The best algorithms can now routinely solve TSP instances with tens of thousands of nodes. (The record at the time of writing is the pla85900 instance in TSPLIB, a VLSI application with 85,900 nodes. For certain instances with millions of nodes, solutions have been found guaranteed to be within 1% of an optimal tour

    https://developers.google.com/optimization/routing/tsp
    You can use google maps to solve the TSP!

  2. OMagain: And here target is something like ‘better solutions are shorter’.

    That is not a target. It is the behavior of the selection algorithm. One can Humpty-Dumpty this, but behavior of a program is not a target.

    As I said, one can add an arbitrary halting condition, but the program will run indefinitely without a halting condition.

    It really is conceptually equivalent to LTEE.

  3. A simple Dawkins algorithm can reach a point wher fitness never increases.

    A TSP program evaluating thousands of nodes will never, in the life of the universe, reach a point where fitness cannot increase.

  4. petrushka: That is not a target. It is the behavior of the selection algorithm. One can Humpty-Dumpty this, but behavior of a program is not a target.

    Yes, that’s sort of what I was trying to get at. The program can be run with and without that behaviour, but each time it outputs solutions to the TSP.

  5. Mung: Such as?

    You want me to provide an operational definition of cumulative selection, something which Dawkins himself did not do?

    What do you object to in Dawkins’ definition?

  6. OMagain: No, it’s precisely because of people like you with agendas.

    It amazes me that atheists are so afraid of common English words. The recent history of this site is one case after another of constantly trying to run from simple definitions.

    I can understand the frustration

    It’s almost as if all of language is conspiring against you guys. 😉

    peace

  7. OMagain: For each possible solution to a given TSP each ‘target’ is simply the length of the solution.

    no the target is the shortest possible length a solution can have

    peace

  8. fifthmonarchyman: no the target is the shortest possible length a solution can have

    Then you have established that such programs do not have targets, because that value is not coded in the program.

  9. fmm: no the target is the shortest possible length a solution can have

    It is often unknown what the shortest possible length a solution can have. Hence it cannot be a target – how can the unknown be a target? How can you know when you have hit that target? If you decide to stop on a particular solution, how do you know the next one along would not have been shorter?

    fmm, after studying programming now for almost a year you should be able to write a simple solver for the TSP. Why don’t you do so?

  10. fifthmonarchyman: no the target is the shortest possible length a solution can have
    peace

    Sorry, but the program works fine without knowing that value, and even if it knows th value, it will never reach it. It doesn’t need to know anything at all about goals.

  11. petrushka: Lenski’s cultures continue to evolve without targets.

    So? Has someone here claimed that Lenski’s cultures demonstrate the power of cumulative selection?

  12. petrushka: Then you have established that such programs do not have targets, because that value is not coded in the program.

    And it’s absurd to think that some value must be hard-coded into a proram for there to be any target.

  13. If every candidate solution is a solution then every candidate target is a target and Dawkins’s Weasel program tells us nothing at all about cumulative selection.

    And if TSP have nothing to tell us about the power of cumulative selection they are irrelevant to the subject under discussion.

    How is the power of cumulative selection demonstrated in TSP programs? If you cannot say how then the whole line is just a red herring.

  14. See walto, how easy it is to dismiss my argument, because everyone simply knows that I am wrong, lol.

  15. Mung,

    Seems to me you are arguing about the efficacy and relevance of computer models. I’d suggest it’s a case of good enough or better models of whatever you want to model.

  16. fmm: no the target is the shortest possible length a solution can have
    peace

    How does the programmer sneak in the target in?

  17. Mung:

    See walto, how easy it is to dismiss my argument, because everyone simply knows that I am wrong, lol.

    That’s right. And after explaining it to you a dozen or so times, we start to lose interest.

  18. Mung:
    Richard Dawkins’s Weasel Program Is Bad

    “But Dawkins waves off these shortcoming by saying the little program is merely for illustrative purposes and suggesting that more sophisticated programs will be designed soon enough that properly mimic natural selection while still illustrating how wonderfully productive the evolutionary process can be.

    Three decades later, we’re still waiting.”

    No, they’re not still waiting. What they wanted has been provided countless times. Best example I know of is Avida.

    They are deliberate liars on evolutionnews.

  19. Mung: If every candidate solution is a solution then every candidate target is a target and Dawkins’s Weasel program tells us nothing at all about cumulative selection.

    You can demonstrate the power of cumulative selection both WITH and WITHOUT targets.

    So no, it is plain false to say it tells us nothing at all about cumulative selection.

    How is the power of cumulative selection demonstrated in TSP programs?

    By showing that fitness iteratively increases over generations. Because there’s a hill to climb. That’s what cumulative selection is, climbing hills in the fitness landscape.

    How many times must we answer this question? How many times will you ignore it?

  20. Mung: They hate targets, but they need them. Like teleology in general.

    Nobody NEEDS targets to show how cumulative selection works. There are no targets, for example, in BoxCar2D. Yet BoxCar2D still manages to capture the effect of cumulative selection.

    Programs with targets are made because they are comparatively easy to make. It doesn’t take a lot of code, you don’t have to program a simulated environment.

  21. Yes. Targets are just very simple stand-ins for the environment. We do not have the computational resources to emulate biochemistry and ecosystems.

    What even the simplest GAs model is the effects of differential rates of reproductive success.

  22. Mung: How is the power of cumulative selection demonstrated in TSP programs? If you cannot say how then the whole line is just a red herring.

    It has cumulative selection. It’s called an “example”. There is selection, because the GA prefers shorter solutions, and ir is cumulative, because it gets shorter and shorter ones, ultimately finding solutions much shorter than random ones.

    What part of “example” don’t you understand?

  23. Rumraket: You can demonstrate the power of cumulative selection both WITH and WITHOUT targets.

    That’s the claim.

    Yet people can’t even explain how to do so with a simple program that has a single target, much less one that has multiple targets, or no targets at all.

  24. Rumraket: That’s what cumulative selection is, climbing hills in the fitness landscape.

    If there’s only one hill? Can we call that a target? If there’s more than one hill, can we call those targets? And if there are no hills/targets?

    A simple hill climbing algorithm is all that’s needed?

  25. Rumraket: By showing that fitness iteratively increases over generations.

    How can we tell that fitness is is iteratively increasing over generations?

    Is there a minimum number of generations? A maximum?

    Is there a minimum fitness? A maximum? How do we measure the fitness?

    If fitness declines is that uncumulative selection? If fitness stops increasing is there no longer cumulative selection? How do you know that fitness is no longer increasing?

  26. This comment from more than a week ago still applies:

    Mung,

    Let’s cut to the chase. What spooks you about Weasel is that it bolsters the case for unguided biological evolution. You are looking for ways to undermine that case.

    Your fixation with targets (as in the comment Joe quoted above) is an example. Biological evolution has no specified targets. If you could show that cumulative selection requires such targets, then you could argue that it is irrelevant to biology and that Weasel therefore demonstrates nothing of relevance to biological evolution.

    Hence your attempts to portray cumulative selection as requiring a target, as in your previous OP:

    So there you have it. You need a target and a fitness function that rewards proximity to the target.

    This is just Mungish confusion. Weasel has a target, and so its fitness function rewards proximity to that target. That does not mean that cumulative selection requires a target, as is obvious to those of us who actually understand the concept. The target and the proximity-rewarding fitness function are characteristics of Weasel, not of cumulative selection generally.

    Another example was your challenge regarding what would happen if we changed Weasel’s target phrase in the middle of a run. Biological evolution operates in changing fitness landscapes, so if you could somehow show that cumulative selection was thwarted by such changes, you could argue that it’s insufficient as a selective mechanism for biological evolution.

    But as anyone who understands Weasel could predict, it simply starts tracking toward the new target, and I even provided a feature in my Weasel that allows you to experiment with this. Your weird single-character latching program, by contrast, can’t handle changes to the target phrase, because any characters it has already latched are latched for good.

    Since the target gambit failed, you are now dicking around with weird fitness functions that are designed to impede convergence while still (you hope) qualifying as instances of cumulative selection. As Joe and I have pointed out, that tactic also fails.

    Your list of Weasel-related failures just grows longer and longer. What gambit will you try next?

  27. Mung, evolution, either real or simulated, is cumulative because it starts each generation from the result of the previous generation.

    You can take, say a GA that finds routes in a TSP, and make it noncumulative by simply, after each generation of selection, resetting the population to the original initial population. We would save the best solution found in all those generations of noncumulative selection. We would compare those to the solutions found by allowing selection to be cumulative.

    The power would be shown by the results of a bet we would have with you, with you betting that the result of cumulative selection is not better than noncumulative selection.

    That way we would avoid wasting time on fussing about language of specifications and formal definitions.

  28. Mung: Rumraket: You can demonstrate the power of cumulative selection both WITH and WITHOUT targets.

    That’s the claim.

    Yet people can’t even explain how to do so with a simple program that has a single target

    Sure we can, and have done. Dawkins WEASEL is one such program. So is yours.

    Mung: much less one that has multiple targets, or no targets at all.

    Sure we can. If there are multiple targets, you just evaluate the “fit” of the organism to each of the targets. The best matchs to any one of the targets is copied to the next generation.

    If there’s no target, you need to start doing actual simulations of the organisms in an environment and have them compete in some way. Like in the BoxCar2D program. Here the fitness is not how well a string matches another target-sequence, rather the fitness of the “organism” is how far the car can make it in the landscape before stopping. So there’s no set target.

    You can make even more complicated simulations where even the process of reproduction and metabolism is simulated, so there’s actual competition to produce more offspring that consumes limited resources. That would be the Avida program.

  29. Mung: Rumraket: That’s what cumulative selection is, climbing hills in the fitness landscape.

    If there’s only one hill? Can we call that a target?

    No. Targets are something you set up beforehand and then try to hit as accurately as possible.

    There can be hills without them being set up beforehand with the intent of hitting them.

    If there’s more than one hill, can we call those targets?

    Only if they were deliberately set up so something is trying to hit them.

    And if there are no hills/targets?

    If there are no hills cumulative selection is impotent. If there are no targets there are no targets. There can still be hills without there being targets.

    A simple hill climbing algorithm is all that’s needed?

    To show the power of cumulative selection, yes.

  30. Mung: Rumraket: By showing that fitness iteratively increases over generations.

    How can we tell that fitness is is iteratively increasing over generations?

    By observation. For example, in your own program, compare the sequences at arbitrary generations and see the degree of match to the target phrase.

    Is there a minimum number of generations? A maximum?

    Yes there’s a minimum: 0.
    There is no maximum.

    Is there a minimum fitness?

    Depends on how the program works. For example in your own program, the fitness is the number of sites that matchs to the target phrase. So the minimum fitness is 0, since there can be a string with 0 matches.

    A maximum?

    Depends on how the program works. For example in your own program, the fitness is the number of sites that matchs to the target phrase. So the maximum fitness is 28, since there can be a string with no more than 28 matches, because the target phrase is 28 sites in length.

    How do we measure the fitness?

    Depends on how the program works. For example in your own program, the fitness is the number of sites that matchs to the target phrase. So we just count the number of matches.

    If fitness declines is that uncumulative selection?

    Fitness can decline while cumulative selection is in operation, but that would entail selection isn’t strong enough to overcome whatever force it is that works against it.

    If fitness stops increasing is there no longer cumulative selection?

    There can still be cumulative selection when fitness stops increasing, but that would just imply you’ve reached the peak of the local hill. In your program, for example, you would have reached the target sequence.

    How do you know that fitness is no longer increasing?

    By determining an arbitrary length of generations over which you measure fitness at various intervals. Obviously if selection is slow enough, you might actually be in one of those situations where it looks like there is no increase, but the number of generations between fitness increases is too great for you to detect it in the amount of time you used.

  31. Joe Felsenstein: Mung, evolution, either real or simulated, is cumulative because it starts each generation from the result of the previous generation.

    You can take, say a GA that finds routes in a TSP, and make it noncumulative by simply, after each generation of selection, resetting the population to the original initial population. We would save the best solution found in all those generations of noncumulative selection. We would compare those to the solutions found by allowing selection to be cumulative.

    Thanks for that extremely clear statement of what cumulativity is and what a test of it amounts to.

  32. Mung:

    Rumraket: You can demonstrate the power of cumulative selection both WITH and WITHOUT targets.

    That’s the claim.

    Yet people can’t even explain how to do so with a simple program that has a single target, much less one that has multiple targets, or no targets at all.

    It’s been explained to you repeatedly. Here’s Dawkins’ definition again:

    “The essential difference between single-step selection and cumulative selection is this. In single-step selection the entities selected or sorted, pebbles or whateverthay are, are sorted once and for all. In cumulative slelection, on the other hand, they ‘reproduce’; or in some other way the results of one sieving process are fed into a subsequent sieving, which is fed into into . . . and so on. The entities are subjected to selection or sorting over many ‘generations’ in succession. The end-product of one genration of selection is the starting point for the next generation of selection, and so on for many generations.”

    Cumulative selection is observed in his Weasel program, with its target, and in other EAs like those that evolve solutions to the traveling salesman problem, without targets.

    Do you find this definition unsatisfactory for some reason?

  33. Patrick: “The essential difference between single-step selection and cumulative selection is this. In single-step selection the entities selected or sorted, pebbles or whateverthay are, are sorted once and for all.

    Examine the code in the OP. Does it fail as an example of single step selection? If it fails as an example of single step selection, why so?

    In single-step selection the entities selected or sorted, pebbles or whateverthay are, are sorted once and for all.

    What does that even mean? Sorted once and for all? How do we test for “sorted once and for all”?

  34. Mung:

    What do you object to in Dawkins’ definition?

    Is it an operational definition, and if not, why not?

    I think it’s sufficiently detailed to be useful:

    “The essential difference between single-step selection and cumulative selection is this. In single-step selection the entities selected or sorted, pebbles or whateverthay are, are sorted once and for all. In cumulative slelection, on the other hand, they ‘reproduce’; or in some other way the results of one sieving process are fed into a subsequent sieving, which is fed into into . . . and so on. The entities are subjected to selection or sorting over many ‘generations’ in succession. The end-product of one genration of selection is the starting point for the next generation of selection, and so on for many generations.”

    You can step through the vast majority of EAs, including Weasel and its variants, generation by generation and determine that each successive generation builds upon the previous one, with changes driven by selection pressures. That meets Dawkins’ definition of cumulative selection.

    Do you disagree?

  35. Patrick: Thanks for the Dawkins quote. It is entirely consistent with my statement (above) that

    Mung, evolution, either real or simulated, is cumulative because it starts each generation from the result of the previous generation.

    You can take, say a GA that finds routes in a TSP, and make it noncumulative by simply, after each generation of selection, resetting the population to the original initial population. We would save the best solution found in all those generations of noncumulative selection. We would compare those to the solutions found by allowing selection to be cumulative.

    Starting each generation from the results of the previous one is of course a basic property of organisms that reproduce.

  36. Cumulative change is a better term, since selection can imply truncation or differential selection. According to Moran and company, truncation accounts for more change at the molecular level than selection.

    I believe keiths supported this by allowing the software user to set a minimal criterion for survival, but no particular goal.

  37. Patrick: You can step through the vast majority of EAs, including Weasel and its variants, generation by generation and determine that each successive generation builds upon the previous one, with changes driven by selection pressures. That meets Dawkins’ definition of cumulative selection.

    Do you disagree?

    Yes, I disagree. The “definition” you cite fails to capture an essential characteristic of cumulative selection. If we use that definition of cumulative selection the Weasel program is irrelevant. Do you disagree?

    The Weasel program adds some other element to cumulative selection that is not reflected in that definition. Can you tell us what that is, Patrick?

  38. Joe Felsenstein: Starting each generation from the results of the previous one is of course a basic property of organisms that reproduce.

    If that’s all that cumulative selection means, what was the point of the Weasel program?

    Does my SS Weasel program NOT start each generation from the results of the previous one?

  39. Mung, to Joe:

    If that’s all that cumulative selection means, what was the point of the Weasel program?

    To demonstrate the power of cumulative selection.

    Does my SS Weasel program NOT start each generation from the results of the previous one?

    I addressed that already:

    Since the target gambit failed, you are now dicking around with weird fitness functions that are designed to impede convergence while still (you hope) qualifying as instances of cumulative selection. As Joe and I have pointed out, that tactic also fails.

    And:

    The problem (for Mung) is stark and obvious: the fact that cumulative selection can fail in some cases does not mean that it fails in all, and it doesn’t mean that Weasel fails to demonstrate the power of cumulative selection.

    And as Joe asks:

    Has anyone claimed that a weasel-like program will demonstrate the power of cumulative selection with every possible fitness function?

  40. keiths: To demonstrate the power of cumulative selection.

    You don’t need a Weasel program for that. After all, “starting each generation from the results of the previous one is of course a basic property of organisms that reproduce.”

    I addressed that already:

    No, you didn’t. Here’s the question again: Does my SS Weasel program NOT start each generation from the results of the previous one? It does. So why not just admit it?

  41. Christ, Mung.

    Your “SS Weasel” doesn’t demonstrate the power of cumulative selection. Weasel does.

  42. keiths: And as Joe asks:

    “Has anyone claimed that a weasel-like program will demonstrate the power of cumulative selection with every possible fitness function?”

    The answer is yes. Weasel-like programs demonstrate the power of cumulative selection else they are not Weasel-like. Even the keiths Drift-Weasel is Weasel-like.

  43. keiths: Christ, Mung.

    Your “SS Weasel” doesn’t demonstrate the power of cumulative selection. Weasel does.

    According to the definition given by Richard Dawkins [as quoted by Patrick and endorsed by Joe F.], my program implements cumulative selection. If you think otherwise, please say why.

    Now if you agree my program does in fact implement cumulative selection according to the definition given by Richard Dawkins [as quoted by Patrick and endorsed by Joe F.] but you think this is not sufficient to “demonstrate the power of cumulative selection,” then you are in fact agreeing with me, for taht is exactly what I have said.

Leave a Reply