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

📄 bistim.java~

📁 Java遗传算法库
💻 JAVA~
📖 第 1 页 / 共 2 页
字号:
/*
 * 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 javax.swing.JComponent;

/** An Interaction Model for evolving circuits with Built-In-Self-Test (BIST)
 * functionality.  This functionality can be either evolved at the same time
 * as the circuit or post-evolved.  In both cases an incremental evolution
 * aproach is used.  This can be chosen to be either implicit or explicit by
 * changing the <code>explicit_incremental</code> boolean variable.
 * <p>Implicit
 * incremental evolution is implemented by having a weighten polynomial fitness
 * function in which the main task - the one to be evolved earlier though without
 * totally losing track of the others - is given a heavier weighting and
 * polynomial power.  For eg. for two tasks A,B:  F = f_a^2 * 0.9 + f_b * 0.1
 * clearly will favour a fitness increase in A far more than one in B so 
 * probably completing A faster but without loosing track of B.  Once A is
 * completed our fitness function becomes F = 0.9 + f_b * 0.1, which using
 * Rank Selection is equivalent to F = f_b, hence attention is focused 
 * solely on B.
 * <p>Explicit incremental evolution is implemented by the same fitness function
 * as the implicit one except: When task A has reached 100% fitness any individual
 * who performs below 100% fitness at A will be given a fitness of 0.  Hence 
 * evolution runs in explicit and is not allowed to go backwards.  The constants
 * <code>EVOEXP, EVOPERFAULT, EVOPERINST</code> are used to denote these stages.
 * <p><b>Note</b> that the constant MAX_FITNESS from the Genotype class is used to
 * measure if a circuit is to be
 * considered a solution to a problem.
 * <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 BISTIM extends StandardInteractionModel
{
    // Constants - Config
    static final boolean MATTFF = false;
    
    // Constants
    // Modes of operation for explicitly incremental evolution
    static public final int EVO_EXP = 0;
    static public final int EVO_PER_FAULT = 1;
    static public final int EVO_PER_INST = 2;
    static public final String[] modeNames = { "Evolving Only Experiment", "Evolving Experiment & Per Fault BIST", "Evolving Experiment, Per Fault BIST & Per Instance BIST" };
    
    // Config Vars
    protected boolean explicit_incremental = false;
    protected boolean overdetecting = false;
    protected double threshold = 0.1; // Amount by which fitness must drop to deem a circuit as failing.
    protected JComponent painter = new javax.swing.JPanel();
    // Variables for finding E
    protected double etSetup = 0; //0.1 // Proportion of output data to be ignored from the start of the set.
    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.    
    // Weights for fitness calculation
    protected double w_e = 0.9;    protected double w_b = 1 - w_e;
    protected double w_bpf = 0.9;  protected double w_bpi = 1- w_bpf;
    // Fitnesses at which to move on to next stage.
    protected double min_e = .4999;  protected double min_bpf = .9999;
    
    //Working Vars
    protected int mode = EVO_EXP;
    protected boolean flagModeIncrease = false;
    protected SingleFaultModel faultModel;
    protected SingleRandomFaultModel srfm = null; // If its randomness, its randomness will need to be controlled
    protected SimulatorFaultyCircuit circuit;
    
    /** Creates new BISTIM */
    public BISTIM(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;
        }        
    }

    public BISTIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, boolean overdetecting, boolean explicit_incremental)
    {
        this( evo, dep, cir, exp, fm );
        this.overdetecting = overdetecting;
        this.explicit_incremental = explicit_incremental;
    }
    
    public BISTIM(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 BISTIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, double th) {
        this( evo, dep, cir, exp, fm );
        threshold = th;
    }
    
    public BISTIM(Evolver evo, Deployment dep, SimulatorFaultyCircuit cir, Experiment exp, SingleFaultModel fm, double th, double ets, int es ) {
        this( evo, dep, cir, exp, fm, th );
        etSetup = ets;
        eSize = es;
    }
    
    public void evolve()
    {
        super.evolve();
        if( srfm != null )
        {
            srfm.nextRandomSeries(); // Same for all per generation, but != 'tween generations.
        }
        if( flagModeIncrease )
        {
            mode++;
            flagModeIncrease = false;
        }
    }
    
    /** Evaluates these individuals using the deployment and experiments and
     * procedure of this model.
     */
    synchronized public double[] evaluate(Genotype[] inds)
    {
        double fitness;
        Genotype ind = inds[ 0 ];
        
        // 1) Evaluate Ind with no faults.
        deployment.program( ind );
        SampleData[] input = experiment.generateInput( inputSampleSeparation );
        SampleData[] outputWithE = deployment.run( input );
        int nrOuts = outputWithE.length - 1;
        SampleData[] outputNoE = ESLib.getLines( outputWithE, 0, nrOuts );
        double f_e = experiment.getFitness( input, outputNoE );
        ind.setProperty( 0, new Double( f_e ) );
        fitness = w_e * f_e * f_e;
        
        if( explicit_incremental && mode == EVO_EXP )
        {
            if( f_e >= min_e )
            {
                flagModeIncrease = true; // Main experiment good enough to include other paramaters
            }
        }else
        {
            // 2) Compute f_b now.
            boolean E_f_i = getE( outputWithE );
            double f_b = 0;

            if( !E_f_i ) // Check that E low for no faults! If it isn't, then discard.
            {
                // 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 ];
                if( !explicit_incremental || mode != EVO_PER_FAULT )
                {
                    for( int ol = 0; ol < testLength; ol++ )
                    {
                        noFaultOuts[ ol ] = getOutAt( outputWithE, ol );
                    }
                }

                // 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 );
                        circuit.reset();

⌨️ 快捷键说明

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