How should I Test a Genetic Algorithm

前端 未结 10 668
心在旅途
心在旅途 2021-01-29 22:00

I have made a quite few genetic algorithms; they work (they find a reasonable solution quickly). But I have now discovered TDD. Is there a way to write a genetic algorithm (whic

相关标签:
10条回答
  • 2021-01-29 22:22

    All of your functions should be completely deterministic. This means that none of the functions you are testing should generate the random number inside the function itself. You will want to pass that in as a parameter. That way when your program is making decisions based on your random numbers, you can pass in representative numbers to test the expected output for that number. The only thing that shouldn't be deterministic is your actual random number generator, which you don't really need to worry too much about because you shouldn't be writing this yourself. You should be able to just assume it works as long as its an established library.

    That's for your unit tests. For your integration tests, if you are doing that, you might look into mocking your random number generation, replacing it with an algorithm that will return known numbers from 0..n for every random number that you need to generate.

    0 讨论(0)
  • 2021-01-29 22:22

    I wrote a C# TDD Genetic Algorithm didactic application: http://code.google.com/p/evo-lisa-clone/

    Let's take the simplest random result method in the application: PointGenetics.Create, which creates a random point, given the boundaries. For this method I used 5 tests, and none of them relies on a specific seed:

    http://code.google.com/p/evo-lisa-clone/source/browse/trunk/EvoLisaClone/EvoLisaCloneTest/PointGeneticsTest.cs

    The randomness test is simple: for a large boundary (many possibilities), two consecutive generated points should not be equal. The remaining tests check other constraints.

    0 讨论(0)
  • 2021-01-29 22:26

    I would test random functions by testing them a number of times and analyzing whether the distribution of return values meets the statistical expectations (this involves some statistical knowledge).

    0 讨论(0)
  • 2021-01-29 22:28

    If you're talking TDD, I would say definitely start out by picking a constant number and growing your test suite from there. I've done TDD on a few highly mathematical problems and it helps to have a few constant cases you know and have worked out by hand to run with from the beginning.

    W/R/T your 4th point, moving nondeterministic code out of the GA, I think this is probably an approach worth considering. If you can decompose the algorithm and separate the nondeterministic concerns, it should make testing the deterministic parts straightforward. As long as you're careful about how you name things I don't think that you're sacrificing much here. Unless I am misunderstanding you, the GA will still delegate to this code, but it lives somewhere else.

    As far as links to very good books on (developer) testing my favorites are:

    • Test Driven by Lasse Kosela
    • Working Effectively with Legacy Code by Michael Feathers
    • XUnit Test Patterns by Gerard Meszaros
    • Next Generation Java™ Testing: TestNG and Advanced Concepts by Cédric Beust & Hani Suleiman
    0 讨论(0)
  • 2021-01-29 22:28

    I would highly suggest looking into using mock objects for your unit test cases (http://en.wikipedia.org/wiki/Mock_object). You can use them to mock out objects that make random guesses in order to cause you to get expected results instead.

    0 讨论(0)
  • 2021-01-29 22:35

    You could write a redundant neural network to analyze the results from your algorithm and have the output ranked based on expected outcomes. :)

    Break your method down as much as your can. Then you can also have a unit test around just the random part to check the range of values. Even have the test run it a few times to see if the result changes.

    0 讨论(0)
提交回复
热议问题