Using sample sets to determine gp functions

Apr 23, 2012 at 6:02 AM
Edited Apr 24, 2012 at 12:30 AM

I've come up with an example to pass in sample sets instead of functions.  Here is an example of one used to come up with the pythagorean theorom.  The target is the last number of the set.  It would get evaluated with the following:

 

  case '5':
                    // Population.SetRandomSeed(0);
                    var samples = new [,] {{3, 4, 5}, {8, 6, 10}, {1, 1, 1.4142}, {12,5,13}, {9,40,41}}; 
                    population = SetupFindFunctionTest(samples);
                    break;

 

 

Here is the code to process it:

 

 

       private static Population SetupFindFunctionTest(double[,] samplesets)
        {
            var variableString = "abcdefghijklmnopqrstuvwxyz";
            Variable[] variables = new Variable[26];
            var length = samplesets.GetUpperBound(1) + 1; // get length of each set
            
            Population population = new Population();
 
            population.GenomeDefinition.MaxSize = 50;
 
            population.GenomeDefinition.MaxDepth = 7;
 
            population.GenomeDefinition.AddOperators(
                new Add(), 
                new Div(), 
                new Sub(), 
                new Mul(), 
                new SquareRoot());  // added square root in here as additional operation
 
            for (int i = 0; i < length - 1; i++)
            {
                variables[i] = population.GenomeDefinition.AddOperator(new Variable(variableString[i].ToString(), 0.1));
            }
 
            population.GenomeDefinition.MaxDepth = 5;
 
            population.PopulationSize = 5000;
 
            population.ParsimonyPressure = 0.0;
 
            population.MaxGenerations = 50000;
 
            population.TargetFitness = 0.001;
 
            population.FitnessFunction =
                individual =>
                    {
                        var localVariables = new VariableInstance[length - 1];
                        for (int i = 0; i < length - 1; i++)
                        {
                            localVariables[i] = Variable.GetVariable(individual, variableString[i].ToString());
                        }
 
                        int tests = 0;
                        double errorSum = 0;
 
                        // this is total number of tests
                        int numberOfTests = samplesets.GetUpperBound(0) + 1;
 
 
                        while (tests < numberOfTests)
                        {
                            for (int i = 0; i < length - 1; i++ )
                                localVariables[i].Value = samplesets[tests, i];
 
                            // the last element in the set is the target value
                            double targetValue = samplesets[tests, length - 1];
 
 
                            double error = targetValue - individual.Evaluate();
 
                            errorSum += error*error;
 
                            tests++;
                        }
 
 
                        return errorSum/numberOfTests;
                    };
 
 
            return population;
        }