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

📄 bistpim.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 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 BISTPIM extends StandardInteractionModel
{
    // Constants - Config
    static final boolean MATTFF = false;
    
    // Config Vars
    protected boolean overdetecting = false;
    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 E
    protected int nrEs = 1;
    protected int EFindStartAt = 0;
    protected int eSize = 8; // was 3 // Length of output data in time steps for minimum detectable raising of E.
    // 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 BISTPIM(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;
        }        
        Vector v = new Vector();
        v.add( cir );
    }
    
    public BISTPIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int es, boolean overdetecting) {
        this( evo, dep, cir, exp, fm );
        eSize = es;
        this.overdetecting = overdetecting;
    }
    
    public BISTPIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int es, boolean overdetecting, int iss)
    {
        this( evo, dep, cir, exp, fm, es, overdetecting );
        inputSampleSeparation = iss;
    }
    
    public BISTPIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, int es, int EFindStartAt, boolean overdetecting, int iss, SnapshotPainter painter)
    {
        this( evo, dep, cir, exp, fm, es, overdetecting, 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)
    {
        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[] outputWithE = deployment.run( input );
        int nrOuts = outputWithE.length - 1;
        SampleData[] outputNoE = ESLib.getLines( outputWithE, 0, nrOuts );
        double f_e = experiment.getFitness( input, outputNoE );
        
        //TI//if( printWhich >= PRINT_LONG )
        //TI//{
            //TI//longStory += ESLib.sampleDatasToString( input, outputWithE );
        //TI//}
        
        double f_bpf = 0;
        double f_bpi = 0;
        
        boolean E_f_i;
        boolean mainTaskOK = f_e > currMaxF_e - threshold;

        // 2) Compute f_b now.
        
        if( mainTaskOK && !( E_f_i = getE( outputWithE ) ) ) // Check that E low for no faults! If it isn't, then discard.
        {
            
            //TI//shortStory += "Fit=" + f_e + "E=" + E_f_i + " 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[] used = getUsed( nrOuts ); // Skipping faults in unused elements.

            // 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.getOutAt( outputWithE, ol, nrEs, inputSampleSeparation, validChunkProp );
            }

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

                if( used[ 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.
                    outputWithE = deployment.run( input );
                    outputNoE = ESLib.getLines( outputWithE, 0, nrOuts );

                    //E_f_i = getE( outputWithE ); // assume in one input block
                    boolean desE_f_i = E_f_i = false;
                    boolean E_firstInstance = false;

                    // 2.2.5)(b) If circuit failed in some way look at instances
                    
                    nrInstances += testLength; // we know how many tests
                    for( int ol = 0; ol < testLength; ol++ )
                    {
                        boolean currE = getE( outputWithE, ol );
                        E_f_i |= currE;                        
                        
                        if( noFaultOuts[ ol ] == BISTLib.getOutAt( outputWithE, ol, nrEs, inputSampleSeparation, validChunkProp ) )
                        {   // OK
                            if( overdetecting || !currE )
                            {
                                diagInstances++;
                            }
                        }else
                        {   // Failure
                            if( currE )
                            {
                                diagInstances++;
                            }
                            desE_f_i = true;
                        }
                        //TI//int inp = jaga.ESLib.getLine( input, ol );
                        //TI//boolean e = getE( outputWithE, ol );
                        //TI//if( printWhich > PRINT_SHORT_FULL )
                        //TI//{
                        //TI//shortFullStory += "\nIns= " + inp + " NFO= " + noFaultOuts[ ol ];
                        //TI//shortFullStory += " Outs= " + BISTLib.getOutAt( outputWithE, ol, nrEs, inputSampleSeparation, validChunkProp )+ " desE= " + ( noFaultOuts[ ol ] != BISTLib.getOutAt( outputWithE, ol, nrEs, inputSampleSeparation, validChunkProp ));
                        //TI//shortFullStory += " E= " + e;
                        //TI//}
                    }

                    if( ( E_f_i == desE_f_i ) || ( overdetecting && 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, outputWithE ) + "\n";
                    //TI//}
                    //TI//shortFullStory += "\n" + thisShortLine + "\n";
                }
            }

            //double f_bpf = ( double ) diagFaults / ( double ) nrFaults; // OLD PROP FF
            f_bpf = 1d / ( ( nrFaults - diagFaults ) / 25d + 1d );
            //System.out.println("bpf");

                //double f_bpi = ( double ) diagInstances / ( double ) nrInstances; // OLD PROP FF
            f_bpi = 1d / ( ( nrInstances - diagInstances ) / 180d + 1d );
            //**!! In postev this could be simply f_b ( * f_b ? )
            //totalf_b += f_b;
        }       
        ind.setProperty( pfPriority, new Double( f_bpf ) );
        ind.setProperty( piPriority, new Double( f_bpi ) );
        houseWork( ind, f_e );
        double[] rv = { f_e };
        return rv;        
    }
        
    protected boolean getE( SampleData[] output )
    {
        SampleData E = output[ output.length - 1 ];
        int outLen = E.length();
        int sampleSets = outLen / inputSampleSeparation;
        for( int ssl = 0; ssl < sampleSets; ssl++ )
        {
            if( getE( output, ssl ) )
            {
                return true;
            }
        }
        return false;
    }
    
    protected boolean getE( SampleData[] output, int ix )
    {
        SampleData E = output[ output.length - 1 ];
        int start = ix * inputSampleSeparation;
        int conc = 0;
        for( int bl = start + EFindStartAt; bl < start + inputSampleSeparation; bl++ )
        {
            if( E.get( bl ) )
            {
                conc++;
                if( conc == eSize )
                {
                    return true;
                }
            }else
            {
                conc = 0;
            }
        }
        return false;
    }
        
       
    public String toString()
    {
        String narrator = "BIST Interaction Model with:";
        narrator += "\n  Threshold = " + threshold;
        narrator += "\n  Error Line t_setup = " + EFindStartAt;
        narrator += "\n  Error High Minimum Size = " + eSize;
        narrator += "\n  Overdetecting is OK = " + overdetecting;
        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;
    }
    
    // Snapshot so far does normal thing
    
    protected boolean[] getUsed( int outs )
    {
        SimulatorLogicElement[] els = circuit.getElements();
        boolean[] rv = new boolean[ els.length ];
        for( int ol = 0; ol < outs; ol++ )
        {
            addConnectedGates( rv, els[ ol ], els );
        }
        return rv;
    }

    protected void addConnectedGates( boolean[] added, SimulatorLogicElement el, SimulatorLogicElement[] els )
    {
        int elinels = jaga.ESLib.indexOf( el, els );
        if( elinels >= 0 && !added[ elinels ] )
        {
            added[ elinels ] = true;
            SimulatorLogicElement[] ins = el.getInputs();
            if( ins != null )
            {
                for( int il = 0; il < ins.length; il++ )
                {
                    addConnectedGates( added, ins[ il ], els );
                }
            }
        }
    }
    
    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 + -