Everybody, thank you very much for your previous help. I believe that I am now very close to conquering this!
I have gotten the information needed to be able to continue this project.
I now have everything working except the actual calling of the program that I receive a pointer to.
The call is successful because the returned values seem reasonable however the variable "NrOfFitnessCases" gets clobbered after the call.
I believe that I have not defined Inputs or Outputs correctly or I am not passing them properly.
One clue that I have is that it gets changed to the address Outputs(0)? Very strange to me.....
Can anyone offer any suggestions?
Thank you in advance for any more wonderful suggestion,
Dennis
[This message has been edited by Dennis Martell (edited November 19, 2006).]
I have gotten the information needed to be able to continue this project.
I now have everything working except the actual calling of the program that I receive a pointer to.
Code:
//////////////////////////////////////////////////////////////////////////////////////// // CalculateTrainingFitness // // Calculates the fitness of an individual having direct access to the genetic // program itself, allowing you to do with it whatever you want. // // Parameters: // pGeneticProgram - Pointer to the genetic program. This is how you call it: // (*pGeneticProgram) (Inputs, pOutputs) // // Inputs is an array of floats used as the input of the program. The size of // this array must be equal to the number of inputs for the genetic program, // set in varable NrOfInputs. // // pOutputs is a pointer to an array that contains the outputs of the // program after the function has been called. The size of the array will be // NrOfOutputs. // // Return Value: // The fitness value of the program. The lower the fitness value, the better // the program. //////////////////////////////////////////////////////////////////////////////////////// float CalculateTrainingFitness(FUNCTION_PTR pGeneticProgram, long Length) { // Init inputs. Note: Inputs[1] to Inputs[3] are never // changed, so they are constants for the GP program float Inputs[4]={ 0,0,0.5,1}; // Pointer to evolved program output float *Outputs[8]; float Error; float ErrorSum=0; int NrOfFitnessCases=0; for (float x=-2;x<2;x+=0.2f) // The stepsize here is 0.2 { // Set the input Inputs[0]=x; // Call evolved program (*pGeneticProgram)(Inputs,Outputs); // Calculate error Error=(float)fabs(*Outputs[0]-(x*x*x+x*x+x)); // We want to evolve x^3 + x^2 + x... // Square the error Error*=Error; // Sum up errors ErrorSum+=Error; // Count the number of fitness cases we tested NrOfFitnessCases++; }; // Return average squared error return ErrorSum/NrOfFitnessCases; } ********************************************************** +++++++++++++ PowerBasic Version ++++++++++++++++++++ ********************************************************** DECLARE FUNCTION CallGP CDECL( BYVAL Inputs AS SINGLE PTR, BYVAL Outputs AS SINGLE PTR) FUNCTION GPcalcVF CDECL ALIAS "CalculateValidationFitness" ( _ BYVAL aGP AS DWORD PTR ,_ BYVAL Length AS LONG ) EXPORT AS SINGLE DIM pGP AS DWORD pGP = aGP DIM Inputs(3) AS SINGLE Inputs(0)=0 : Inputs(1)=0 : Inputs(2)=.5 : Inputs(3)=1 DIM Outputs(7) AS SINGLE PTR DIM ER!, ErrorSum!, NrOfFitnessCases&, x!, n1&, n2&, n3&, n4& ER=0 : ErrorSum=0 : NrOfFitnessCases=0 n1 = NrOfFitnessCases FOR x=-2 TO 1.999 STEP 0.2 ' // The stepsize here is 0.2 Inputs(0)=x n2 = NrOfFitnessCases CALL DWORD aGP USING CallGP(VARPTR(Inputs(0)),VARPTR(Outputs(0))) n3 = NrOfFitnessCases ER= ABS(@Outputs(0)-(x*x*x+x*x+x)) ' // We want to evolve x^3 + x^2 + x... ER=ER*ER ErrorSum=ErrorSum+ER NrOfFitnessCases=NrOfFitnessCases+1 MSGBOX("n1 "+STR$(n1)+" n2 "+STR$(n2)+" n3 "+STR$(n3)+" outptr "+STR$(Outputs(0))) NEXT x FUNCTION = ErrorSum/NrOfFitnessCases END FUNCTION
The call is successful because the returned values seem reasonable however the variable "NrOfFitnessCases" gets clobbered after the call.
I believe that I have not defined Inputs or Outputs correctly or I am not passing them properly.
One clue that I have is that it gets changed to the address Outputs(0)? Very strange to me.....
Can anyone offer any suggestions?
Thank you in advance for any more wonderful suggestion,
Dennis
[This message has been edited by Dennis Martell (edited November 19, 2006).]
Comment