📄 dlatchexperiment.java~
字号:
package jaga.pj.circuits.experiment;
import jaga.*;
import jaga.experiment.*;
import java.util.Random;
/**
*
* @author Michael Garvie
* @version
*/
public class DLatchExperiment implements Experiment {
private int inputDataLength;
private double tSetup;
/** Creates new DLatchExperiment */
public DLatchExperiment( int inputDataLength, double tSetup ) {
this.inputDataLength = inputDataLength;
this.tSetup = tSetup;
}
public DLatchExperiment( int inputDataLength )
{
this.inputDataLength = inputDataLength;
tSetup = 0.3;
}
/** Fitness computed by calculating what the output be and if output is this
* then adding a fitness point.
* We may have to be meaner for this to work though...
* @param out outputs assumed to be with sampleSeparation = 1
*/
public double getFitness(SampleData[] in,SampleData[] out)
{
// calculate what output should be, and then add points for correct ones
// allow for a tsetup time for the latch output. This will be measured
// as a fraction of the separation of input samples.
int inputSampleSeparation = in[ 0 ].getSampleSeparation();
int inputSamples = in[ 0 ].length();
int numInputs = getNumOfInputs();
BitSet C = ( BitSet ) in[ 0 ];
BitSet D = ( BitSet ) in[ 1 ];
BitSet Q = ( BitSet ) out[ 0 ];
BitSet desQ = new BitSet( Q.length() );
boolean[] currentInputs = new boolean[ numInputs ];
// calculate what Q should have been - standard D-Latch
for( int idl = 1; idl < inputSamples; idl++ )
{
for( int il = 0; il < numInputs; il++ )
{
currentInputs[ il ] = in[ il ].get( idl );
}
for( int odl = 0; odl < inputSampleSeparation; odl++ )
{
int currIdx = idl * inputSampleSeparation + odl;
boolean desiredQ = C.get( idl ) && D.get( idl);
desiredQ |= !C.get( idl ) && desQ.get( currIdx - inputSampleSeparation ); // or Q - inputSampleSeparation?
desQ.setTo( currIdx, desiredQ );
}
}
return Math.abs( ExperimentLib.getCorrelationFitness( tSetup, inputSamples, inputSampleSeparation, desQ, Q ) );
/*
// ignore the first cycle because there was no previous state.
for( int idl = 1; idl < D.length(); idl++ )
{
// boolean allgood = true; could be used at some point to give extra bonus for keeping output steady and correct
// or better count how many and have minimum required to give points..
int odlCycleStart = idl * inputSampleSeparation;
odlCycleStart += (int)( inputSampleSeparation * tsetup ); // compensate for tsetup
// calculate what Q should be at this point - standard D-Latch logic
boolean desiredQ = ( !C.get( idl ) && Q.get( odlCycleStart - inputSampleSeparation ) ) || ( C.get( idl ) && D.get( idl ) );
// check how many times during the cycle it was the same
for( int odl = odlCycleStart; odl < ( idl + 1 ) * inputSampleSeparation; odl++ )
{
debug( idl + " " + C.get( idl ) + " " + D.get( idl ) + " " + desiredQ + " " + Q.get( odl ) + " " + score );
if( Q.get( odl ) == desiredQ )
{
score++;
// reward change
if( Q.get( odl ) != Q.get( odl - inputSampleSeparation ) )
{
//score += 100;
}
}else
{
score -= 3;
}
perfectScore++;
}
}
score = Math.max( 0, score ); // never negative!
return score * 1.0 / perfectScore;
*/
}
/** 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( )
{
final int INPUT_SAMPLE_SEPARATION = 10;
return generateInput( INPUT_SAMPLE_SEPARATION );
}
public SampleData[] generateInput( int inputSampleSeparation )
{
final int NON_RANDOM_TEST_LENGTH = 6;
final double NON_RANDOM_TEST_PROPORTION = 1;
int[] inputBitsC = { 1, 4 };
int[] inputBitsD = { 3, 4, 5 };
int[][] bits = { inputBitsC, inputBitsD };
return ExperimentLib.generateInputFromTest( ExperimentLib.bitsToSampleDatas( bits, NON_RANDOM_TEST_LENGTH ) , NON_RANDOM_TEST_PROPORTION, inputDataLength, inputSampleSeparation );
}
public int getNumOfInputs()
{
return 2;
}
public int getNumOfOutputs()
{
return 1;
}
public String toString()
{
return "DLatchExperiment";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -