📄 multiplierexperiment.java~
字号:
package jaga.pj.circuits.experiment;
import jaga.*;
import jaga.experiment.*;
import java.util.Random;
import java.util.Vector;
/** This is to be used to evolve Multipliers of two
* n bit wide inputs and a 2n bit wide output.
*
* @author Michael Garvie
* @version
*/
public class MultiplierExperiment implements ConfigurableRandomInputExperiment
{
protected int lastInputSampleSeparation;
protected int lastInputDataLength;
protected SampleData[] currTestData;
protected SampleData[] currInputs;
protected SampleData[] currDesOuts;
protected int width;
protected Object currRndSeed = new Long( System.currentTimeMillis() );
protected Random rnd = new Random();
protected FitnessFunction fitnessFunction = new CorrelationFitnessFunction();
protected TestPatternGenerator tpg = new CompleteShuffledTPG();
/** Creates new AndExperiment */
public MultiplierExperiment( int width, FitnessFunction fitnessFunction, TestPatternGenerator tpg ) {
this( width );
this.fitnessFunction = fitnessFunction;
this.tpg = tpg;
}
public MultiplierExperiment( int width ) {
this( width );
}
public MultiplierExperiment( int width ) {
this.width = width;
set( new Long( System.currentTimeMillis() ) );
}
public int getNumOfInputs()
{
return width * 2;
}
public int getNumOfOutputs()
{
return width * 2;
}
/** Generate input using default parameters
*/
public SampleData[] generateInput()
{
final int DEFAULT_INPUT_SAMPLE_SEPARATION = 15;
return generateInput( DEFAULT_INPUT_SAMPLE_SEPARATION );
}
/** 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)
{
int defaultInputDataLength;
defaultInputDataLength = currTestData[ 0 ].length();//* 1.3 );
return generateInput( inputSampleSeparation, defaultInputDataLength );
}
/** Generate input suitable for testing this experiment using
* custom parameters
* @param inputDataLength how long the input data should be
* @param inputSampleSeparation how often the input samples should change in time. Greater value -> less often
*/
public SampleData[] generateInput( int inputSampleSeparation, int inputDataLength )
{
final double NON_RANDOM_TEST_PROPORTION = 1;
SampleData[] rv;
if( ( inputDataLength == lastInputDataLength ) && ( inputSampleSeparation == lastInputSampleSeparation ) )
{
// must clone the array! It vill be destroyed if not..
rv = new SampleData[ currInputs.length ];
for( int il = 0; il < currInputs.length; il++ )
{
rv[ il ] = ( SampleData ) currInputs[ il ].clone();
}
}else
{
rv = ExperimentLib.generateInputFromTest( currTestData , NON_RANDOM_TEST_PROPORTION, currTestData[ 0 ].length(), inputSampleSeparation );
currInputs = rv;
lastInputSampleSeparation = inputSampleSeparation;
lastInputDataLength = inputDataLength;
}
return rv;
}
/*
public double getFitnessSumLine(SampleData[] in,SampleData[] out)
{
int inputSampleSeparation = in[ 0 ].getSampleSeparation();
int inputSamples = in[ 0 ].length();
int totalFitness = 0;
for( int idl = 0; idl < inputSamples; idl++ )
{
int a = ESLib.getLine( in, idl, 0, width - 1 );
int b = ESLib.getLine( in, idl, width, 2 * width - 1 );
int desiredQ = a * b;
int sampleRangeStart = ( int )( inputSampleSeparation * tSetup );
int sampleRangeEnd = inputSampleSeparation;
int sampleRangeSize = sampleRangeEnd - sampleRangeStart;
int samplePoint = sampleRangeStart + rnd.nextInt( sampleRangeSize );
int currIdx = idl * inputSampleSeparation + samplePoint;
if( ESLib.getLine( out, currIdx ) == desiredQ )
{
totalFitness++;
}
}
int maxFitness = inputSamples;
double rv = ( ( double ) totalFitness ) / ( ( double ) maxFitness );
return rv;
}
*/
public double getFitness(SampleData[] in,SampleData[] out)
{
// Calculate what output should be
int inputSampleSeparation = in[ 0 ].getSampleSeparation();
int inputSamples = in[ 0 ].length(), outputSamples = out[ 0 ].length();
SampleData[] desQ = new SampleData[ out.length ];
for( int ql = 0; ql < out.length; ql++ )
{
desQ[ ql ] = new SampleData( 1, outputSamples );
}
// calculate what Q should have been
for( int idl = 0; idl < inputSamples; idl++ )
{
int a = ESLib.getLine( in, idl, 0, width - 1 );
int b = ESLib.getLine( in, idl, width, 2 * width - 1 );
int desiredQ = a * b;
for( int odl = 0; odl < inputSampleSeparation; odl++ )
{
int currIdx = idl * inputSampleSeparation + odl;
for( int il = 0; il < width * 2; il++ )
{
boolean desQthis = ( desiredQ & ( 1 << il ) ) > 0;
desQ[ width * 2 - il - 1 ].setTo( currIdx, desQthis );
}
}
}
return fitnessFunction.getFitness( in, out, desQ );
}
public String toString()
{
String rv = "MultiplierExperiment with:";
rv += "\n width: " + width;
rv += "\n Fitness Function: " + fitnessFunction;
return rv;
}
/** Sets random seed for inputs generation to param where param instanceof Long */
public void set( Object param )
{
currRndSeed = param;
long longVal = ( ( Long ) param ).longValue();
ExperimentLib.setRandomSeed( longVal );
rnd.setSeed( longVal );
updateTestData();
lastInputDataLength = -1; // uncomment this for changing inputs every generation!
}
protected void updateTestData()
{
currTestData = tpg.getPattern( width * 2 );
}
public Object get( Object param )
{
return currRndSeed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -