⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsccombinationalim.java

📁 Java遗传算法库
💻 JAVA
字号:
/*
 * BISTIM.java
 *
 * Created on 19 May 2002, 18:56
 */

package jaga.pj.circuits.control;

import jaga.evolve.Evolver;
import jaga.deploy.Deployment;
import jaga.experiment.*;
import jaga.control.StandardInteractionModel;
import jaga.*;
import jaga.pj.circuits.fpgaft.*;
import jaga.pj.circuits.SimulatorLogicElement;
import jaga.pj.circuits.CircuitsLib;

import java.util.Random;
import java.util.Vector;

import islandev.SnapshotPainter;
import islandev.EmptySnapshotPainter;

/** An Interaction Model for evolving circuits with Built-In-Self-Test (BIST)
 * functionality. <p>Implicit
 * incremental evolution is implemented by establishing a priority order on three
 * different fitness evaluations: f_t fitness at main task, f_bpf BIST performance
 * per fault (how many faults are detectable) and f_bpi BIST performance per instance
 * (how many fault - input vector instances are detected correctly).  f_t is set as 
 * the main genotype fitness, the other two are set as properties with the type
 * Double.  If these are set as FullOrderGenotypes then their compareTo method will
 * establish a full order between them suitable for use with rank selection which
 * sorts the population.
 *
 * <p> <b>WARNING:</b> For now a single fault model is assumed.
 * <p> <b>Note:</b> Error output should go high when the circuit deviates from 
 * its normal functioning behaviour, which may not be optimal because it hasn't evolved
 * completely yet.
 *
 * @author  Michael Garvie
 * @version 
 */
public class TSCCombinationalIM extends StandardInteractionModel
{
    // Constant Vars
    protected final int nrZs = 2; // Dual rail assumed
    
    // Config Vars
    protected double threshold = 0.01; // Amount by which fitness must drop to deem a circuit as failing.
    public int pfPriority = 0;
    public int piPriority = 1;
    
    // Variables for finding Z
    protected int EFindStartAt = 0;
    protected int eSize = 8; // was 3 // Length of output data in time steps for minimum detectable raising of Z
    
    // Variable for extracting value of output (kind of inverse of t_setup)
    protected double validChunkProp = 0.2; // Proportion at end of output data used to measure its value.    
    
    //Working Vars
    protected double currMaxF_e = -1;
    protected SingleFaultModel faultModel;
    protected SingleRandomFaultModel srfm = null; // If its randomness, its randomness will need to be controlled
    protected SimulatorFaultyCircuit circuit;
    protected Random rnd = new Random();
        
    /** Creates new BISTIM */
    public TSCCombinationalIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm) {
        super( evo, dep, exp );
        circuit = cir;
        faultModel = fm;
        if( faultModel instanceof SingleRandomFaultModel )
        {
            srfm = ( SingleRandomFaultModel ) fm;
        }        
        BISTLib.setGetEMode( BISTLib.E_MODE_DUAL );
    }
    
    public TSCCombinationalIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int eSize) {
        this( evo, dep, cir, exp, fm );
        this.eSize = eSize;
    }
    
    public TSCCombinationalIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int eSize, int iss)
    {
        this( evo, dep, cir, exp, fm, eSize );
        inputSampleSeparation = iss;
    }
    
    public TSCCombinationalIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int eSize, int EFindStartAt, int iss, SnapshotPainter painter)
    {
        this( evo, dep, cir, exp, fm, eSize, iss );
        this.painter = painter;
        this.EFindStartAt = EFindStartAt;
    }
    
    public void evolve()
    {
        super.evolve();
        if( srfm != null )
        {
            srfm.nextRandomSeries(); // Same for all per generation, but != 'tween generations.
        }
        currMaxF_e = -1;
    }
    
    /** Evaluates these individuals using the deployment and experiments and
     * procedure of this model.
     */
    synchronized public double[] evaluate(Genotype[] inds)
    {
        BISTLib.setGetEMode( BISTLib.E_MODE_DUAL ); // Must be also done here because we're not in client's VM until here.
        
        Genotype ind = inds[ 0 ];
        
        //TI//String longStory = "";
        //TI//String shortStory = "";
        //TI//String shortFullStory = "";
        
        // 1) Evaluate Ind with no faults.
        deployment.program( ind );
        SampleData[] input = experiment.generateInput( inputSampleSeparation );
        
        //TI//if( !stateNoise )
        //TI//{
            //TI//circuit.reset();
        //TI//}else
        //TI//{
        circuit.randomReset();
        //TI//}
        
        SampleData[] outputYZ = deployment.run( input );
        int nrYs = outputYZ.length - nrZs;
        SampleData[] outputY = ESLib.getLines( outputYZ, 0, nrYs );
        double f_e = experiment.getFitness( input, outputY );
        
        //TI//if( printWhich >= PRINT_LONG )
        //TI//{
            //TI//longStory += ESLib.sampleDatasToString( input, outputYZ );
        //TI//}
        
        double f_bpf = 0;
        double f_bpi = 0;
        
        boolean mainTaskOK = f_e > currMaxF_e - threshold;
        boolean errDuringNormal = BISTLib.getE( outputYZ, eSize, nrZs, inputSampleSeparation, EFindStartAt );

        // 2) Compute f_b now.
        
        if( mainTaskOK && !errDuringNormal )
        {
            
            //TI//shortStory += "Fit=" + f_e + "E=" + errDuringNormal + " desE=false\n";
            //TI//shortFullStory += shortStory + "\n";
            
            currMaxF_e = Math.max( f_e, currMaxF_e );
            
            // 2.1) Init variables.

            //2a) Check if faults are detected as a whole.  Ie: there exists
            // some input condition for which the fault is detected.  This
            // is per fault detection.

            //boolean[] usedForY = CircuitsLib.getUsed( circuit, nrYs ); // Skipping faults in unused elements.
            boolean[] usedForYZ = CircuitsLib.getUsed( circuit );

            // Per Fault stats:
            int nrFaults = 1; // how many faults tested for 
            int diagFaults = 1; // how many correctly diagnosed, including no faults.

            // 2b) Check if faults are detected at the right moment.  Ie: if
            // at the first moment the circuit gives out a wrong output the
            // error line is high.  This is per instance detection.

            // Per Instance stats:
            int nrInstances = 1; // how many instances tested
            int diagInstances = 1; // how many correctly diagnosed, including no faults.
            
            // Compress No faults output into int array.
            int testLength = input[ 0 ].length();
            int[] noFaultOuts = new int[ testLength ];
            for( int ol = 0; ol < testLength; ol++ )
            {
                noFaultOuts[ ol ] = BISTLib.getSingleOutAt( outputY, ol, 0, inputSampleSeparation );
            }

            // 2.2) Iterate through faults
            faultModel.reset();
            while( faultModel.hasMoreElements() )
            {
                java.awt.Point fPosVal = ( java.awt.Point ) faultModel.nextElement();

                if( usedForYZ[ fPosVal.x ] ) // Skipping faults in unused elements.
                {
                    // 2.2.1) Set Fault
                    circuit.setFault( fPosVal.x, fPosVal.y );

                    //TI//if( !stateNoise )
                    //TI//{
                        //TI//circuit.reset();
                    //TI//}else
                    //TI//{
                    circuit.randomReset();
                    //TI//}

                    // 2.2.2) Run Circuit with Fault.
                    outputYZ = deployment.run( input );
                    outputY = ESLib.getLines( outputYZ, 0, nrYs );

                    boolean desE_f_i = true; // we want all faults to be detectable.
                    boolean E_f_i = false;

                    nrInstances += testLength; // we know how many tests
                    for( int ol = 0; ol < testLength; ol++ )
                    {
                        boolean currE = BISTLib.getE( outputYZ, ol, eSize, nrZs, inputSampleSeparation, EFindStartAt );
                        E_f_i |= currE;
                        
                        if( noFaultOuts[ ol ] == BISTLib.getSingleOutAt( outputY, ol, 0, inputSampleSeparation ) )
                        {   // OK
                            diagInstances++; // doesn't matter if detected or not.
                        }else
                        {   // Failure
                            if( currE )
                            {
                                diagInstances++;
                            }
                        }
                        //TI//int inp = jaga.ESLib.getLine( input, ol );
                        //TI//boolean e = BISTLib.getE( outputYZ, ol, eSize, nrZs, inputSampleSeparation, EFindStartAt );
                        //TI//if( printWhich > PRINT_SHORT_FULL )
                        //TI//{
                        //TI//shortFullStory += "\nIns= " + inp + " NFO= " + noFaultOuts[ ol ];
                        //TI//shortFullStory += " Outs= " + BISTLib.getSingleOutAt( outputY, ol, 0, inputSampleSeparation ) + " desE= " + ( noFaultOuts[ ol ] != BISTLib.getSingleOutAt( outputY, ol, 0, inputSampleSeparation ) );
                        //TI//shortFullStory += " E= " + e;
                        //TI//}
                    }

                    if( E_f_i )
                    {
                        diagFaults++;
                    }
                    nrFaults++;

                    // clear fault
                    circuit.setFault( fPosVal.x, FTLib.NOFAULT );
                    //TI//String thisShortLine = "Fault: Pos=" + fPosVal.x + " Val=" + fPosVal.y + " E=" + E_f_i + " desE=" + desE_f_i;
                    //TI//shortStory += thisShortLine + "\n";
                    //TI//if( printWhich >= PRINT_LONG )
                    //TI//{
                        //TI//longStory += thisShortLine + ESLib.sampleDatasToString( input, outputYZ ) + "\n";
                    //TI//}
                    //TI//shortFullStory += "\n" + thisShortLine + "\n";
                }
            }

            f_bpf = 1d / ( ( nrFaults - diagFaults ) / 25d + 1d );
            f_bpi = 1d / ( ( nrInstances - diagInstances ) / 180d + 1d );
        }       
        ind.setProperty( pfPriority, new Double( f_bpf ) );
        ind.setProperty( piPriority, new Double( f_bpi ) );
        houseWork( ind, f_e );
        double[] rv = { f_e };
        return rv;        
    }
    
    public String toString()
    {
        String narrator = "TSC Combinational Interaction Model with:";
        narrator += "\n  Threshold = " + threshold;
        narrator += "\n  Error Signal = " + BISTLib.getEModeStrings[ BISTLib.getEMode ];
        narrator += "\n  Error Line t_setup = " + EFindStartAt;
        narrator += "\n  Error High Minimum Size = " + eSize;
        narrator += "\n  Fault Model: " + faultModel;
        narrator += "\n  Input Sample Separation: " + inputSampleSeparation;
        narrator += "\n\nExperiment: " + experiment;
        narrator += "\n\nDeployment: " + deployment;
        narrator += "\n\nEvolver: " + evolver;
        narrator += "\n";
        return narrator;
    }
        
    public Genotype getMaxFitness()
    {
        Genotype rv = super.getMaxFitness();
        rv.setProperty( pfPriority, new Double( 1 ) );
        rv.setProperty( piPriority, new Double( 1 ) );
        return rv;
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -