📄 testpattern4evolvingexperiment.java
字号:
/*
* TestPatter4EvolvingExperiment.java
*
* Created on 08 December 2001, 13:56
*/
package jaga.pj.circuits.experiment;
import jaga.*;
import jaga.experiment.*;
import debug.DebugLib;
/**
*
* @author Michael Garvie
* @version
*/
public class TestPattern4EvolvingExperiment implements Experiment {
Experiment experiment;
int numInputs;
int origTestLength;
int evolvedTestLength;
double nextCircuitFitness = -1;
/** Creates new TestPatter4EvolvingExperiment */
public TestPattern4EvolvingExperiment( Experiment exp )
{
experiment = exp;
numInputs = exp.getNumOfInputs();
}
/** returns a fitness associated to a given input/output pair for
* this experiment. The fitness is a double and is in adjusted
* fitness format. From 0 to 1, 1 being perfectly fit.
*
* *WARNING* Assumes inputs were generated in same class.
*
* @param in array of inputs
* @param out array of outputs
*/
public double getFitness(SampleData[] in,SampleData[] out)
{
//int completeTestLength = 1 << numInputs;
int numOutputs = out.length;
//int evolvedTestLength = in[ 0 ].length() - completeTestLength;
int inputSampleSeparation = in[ 0 ].getSampleSeparation();
StringBuffer debugText = null;
if( debug.DebugLib.trcLogger.isLogging() )
{
debug.DebugLib.trcLogger.text( com.ibm.logging.IRecordType.TYPE_MISC_DATA, this.getClass().getName(), "getFitness", "OriginalLength: " + origTestLength + ", EvolvedLength: " + evolvedTestLength );
debugText = new StringBuffer();
}
// 1. chop in and out in two sections, one full (inc. evolved) and one evolved only.
// 2. calculate fitness for experiment with both.
// 3. calculate fitness of this test pattern based on how it lowers fitness.
/* Using trick here using previous fitness eval of whole circuit
// 1.A Complete
SampleData[] completeIn = new SampleData[ numInputs ];
SampleData[] completeOut = new SampleData[ numOutputs ];
for( int il = 0; il < numInputs; il++ )
{
completeIn[ il ] = ( SampleData )( in[ il ].clone() );
completeIn[ il ].setLength( completeTestLength );
debug.DebugLib.debug( this, "Extracted complete input " + il + " with length " + completeIn[ il ].length() + " and ss " + completeIn[ il ].getSampleSeparation() );
}
for( int ol = 0; ol < numOutputs; ol++ )
{
completeOut[ ol ] = ( SampleData )( out[ ol ].clone() );
completeOut[ ol ].setLength( completeTestLength * inputSampleSeparation );
debug.DebugLib.debug( this, "Extracted complete output " + ol + " with length " + completeOut[ ol ].length() + " and ss " + completeOut[ ol ].getSampleSeparation() );
}
*/
// 1.B Evolved
SampleData[] evolvedIn = new SampleData[ numInputs ];
SampleData[] evolvedOut = new SampleData[ numOutputs ];
for( int il = 0; il < numInputs; il++ )
{
evolvedIn[ il ] = new SampleData( inputSampleSeparation, evolvedTestLength );
for( int bl = 0; bl < evolvedTestLength; bl++ )
{
evolvedIn[ il ].setTo( bl, in[ il ].get( origTestLength + bl ) );
}
if( debug.DebugLib.trcLogger.isLogging )
{
debugText.append( "Extracted evolved input " + il + " with length " + evolvedIn[ il ].length() + " and ss " + evolvedIn[ il ].getSampleSeparation() );
}
}
for( int ol = 0; ol < numOutputs; ol++ )
{
evolvedOut[ ol ] = new SampleData( 1, evolvedTestLength * inputSampleSeparation);
for( int bl = 0; bl < evolvedTestLength * inputSampleSeparation; bl++ )
{
evolvedOut[ ol ].setTo( bl, out[ ol ].get( origTestLength * inputSampleSeparation + bl ) );
}
if( debug.DebugLib.trcLogger.isLogging )
{
debugText.append( "Extracted evolved output " + ol + " with length " + evolvedOut[ ol ].length() + " and ss " + evolvedOut[ ol ].getSampleSeparation() );
}
}
// 2.
//double completeFitness = experiment.getFitness( completeIn, completeOut );
double evolvedFitness = experiment.getFitness( evolvedIn, evolvedOut );
//debug.DebugLib.debug( this, "Complete fit: " + completeFitness + ", Evolved fit: " + evolvedFitness );
//3.
//double rv = Math.max( completeFitness - evolvedFitness, 0 );
double rv = Math.max( nextCircuitFitness - evolvedFitness, 0 );
if( debug.DebugLib.trcLogger.isLogging )
{
debugText.append( "Fitness: " + rv + "\n" );
debug.DebugLib.trcLogger.text( com.ibm.logging.IRecordType.TYPE_MISC_DATA, this.getClass().getName(), "getFitness", debugText.toString() );
}
return rv;
}
/** generates an array of inputs suitable for this experiment
* using default input sample separation.
*/
public SampleData[] generateInput()
{
return generateInput( 10 );
}
/** generates an array of inputs suitable for this experiment.
* @param inputSampleSeparation relative frequency of input to output samples. If this is n, then n outputs will be sampled for every change in inputs.
*/
public SampleData[] generateInput(int inputSampleSeparation)
{
return generateInput( null, inputSampleSeparation );
}
public int getNumOfInputs()
{
return numInputs;
}
public int getNumOfOutputs()
{
return experiment.getNumOfOutputs();
}
/** Generates an input sequence based on the standard input sequence of the
* experiment plus an input sequence defined by the test pattern individual.
* Here is where the genotype->phenotype mapping for test patterns is:
* <LI>Each chunk of [numInputs] bits represents one line of test input pattern.</LI>
* @param ind the test pattern individual.
*/
public SampleData[] generateInput( Genotype ind, int sampleSeparation )
{
//SampleData[] rv = ExperimentLib.generateCompleteTest( numInputs, sampleSeparation );
SampleData[] rv = experiment.generateInput( sampleSeparation );
origTestLength = rv[ 0 ].length();
evolvedTestLength = ind.length() / numInputs;
int testLength = origTestLength + evolvedTestLength;
for( int il = 0; il < numInputs; il++ )
{
rv[ il ].setLength( testLength );
for( int bl = 0; bl < evolvedTestLength; bl++ )
{
rv[ il ].setTo( bl + origTestLength, ind.get( bl * numInputs + il ) );
}
}
return rv;
}
public void setNextCircuitFitness( double ncf )
{
nextCircuitFitness = ncf;
}
public String toString()
{
String rv = "TestPattern4EvolvingExperiment";
return rv;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -