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

📄 lutabsolutemappingvariablesized.java

📁 Java遗传算法库
💻 JAVA
字号:
/**
 * The first numOutputs elements defined will be used as outputs. If the 
 * genotype isn't long enough to define numOutputs then they'll be connected to
 * themselves with a LUT = 0.
*/

/*
 * LUTAbsoluteMapping.java
 *
 * Created on 01 April 2002, 19:00
 */

package jaga.pj.circuits.fpgaft;

import jaga.*;
import jaga.pj.circuits.*;


/** Genotype -> Phenotype mapping is: <p>
 * for each subblock of 2^LUTinputs + bitsPerVar * LUTInputs bits:
 * first 2^LUTInputs bits of block declare Look Up Table bits. <p>
 * other bitsPerVar * LUTInputs declare element from where inputs are taken thus: <ul>
 * <li> the first numInputs codes (0, 1 .. numInputs - 1) represent the circuit inputs. </li>
 * <li> the rest code for elements depending on their position in the genotype
 * such that the first element defined is numInputs, the second is numInputs + 1, etc.. </li>
 * <il> outputs are taken from first defined elements: numInputs, numInpts + 1, 
 * till numInputs + numOutputs - 1. </li>
 * <li> if there aren't enough block definitions to define all outputs then these will
 * be tied low </li>
 * </ul>
 * 
 * @author  Michael Garvie
 * @version 
 */
public class LUTAbsoluteMappingVariableSized implements CircuitMapping {

    protected int nrIns, nrOuts, LUTInputs, bitsPerVar;
    protected ElementDelayModel delayModel;
    protected boolean[] padLUT;
    protected int LUTSize, geneSize, numVars;
    
    public LUTAbsoluteMappingVariableSized(int ins, int outs, int bpv, int LUTins, ElementDelayModel delmod)
    {
        nrIns = ins;
        nrOuts = outs;
        LUTInputs = LUTins;
        bitsPerVar = bpv;
        delayModel = delmod;
        
        numVars = 1 << bitsPerVar;
        LUTSize = 1 << LUTInputs;
        geneSize = LUTSize + LUTInputs * bitsPerVar;
        
        padLUT = new boolean[ LUTSize ]; //(all false by def)
    }
        
    public SimulatorLogicElement[][] map( BitSet genotype )
    {
        int genotypeLength = genotype.length();
        int definedGenes = genotypeLength / geneSize;
        int nrEls = nrIns + Math.max( definedGenes, nrOuts );
        
        SimulatorLogicElement[] inputs = new SimulatorLogicElement[ nrIns ];
        SimulatorLogicElement[] outputs = new SimulatorLogicElement[ nrOuts ];
        SimulatorFaultyDelayLE[] elements = new SimulatorFaultyDelayLE[ nrEls ];
        
        int genPos = 0;
        // 1. Init LUTs
        for( int ll = 0; ll < definedGenes; ll++ )
        {
            elements[ nrIns + ll ] = new SimulatorLUT( genotype.getBooleanChunk( genPos, LUTSize ), delayModel.getDelay() );
            genPos += geneSize;
        }
        // 1.2 Fill in missing outputs
        for( int ll = definedGenes; ll < nrOuts; ll++ )
        {
            elements[ nrIns + ll ] = new SimulatorLUT( padLUT, delayModel.getDelay() );
        }
        // 2. Init inputs
        for( int il = 0; il < nrIns; il++ )
        {
            elements[ il ] = new SimulatorFaultyDelayLE( 0 );
        }
        
        // 3. Internal Connections
        genPos = 0;
        for( int ll = 0; ll < definedGenes; ll++ )
        {
            SimulatorLogicElement[] connections = new SimulatorLogicElement[ LUTInputs ];
            genPos += LUTSize;
            for( int il = 0; il < LUTInputs; il++ )
            {
                int inpIndex = genotype.bitsToInt( genPos, genPos + bitsPerVar );
                if( inpIndex >= nrEls )
                {
                    inpIndex %= nrEls;
                    // here we could modify the bitset, remember to keep fitness
                    Genotype ind = ( Genotype ) genotype;
                    double fitness = ind.getFitness();
                    for( int bl = 0; bl < bitsPerVar; bl++ )
                    {
                        ind.setTo( genPos + bitsPerVar - bl - 1, ( inpIndex & ( 1 << il ) ) > 0 );
                    }
                    ind.setFitness( fitness );
                }
                connections[ il ] = elements[ inpIndex ];
                genPos += bitsPerVar;
            }
            elements[ ll + nrIns ].connect( connections );
        }
        // 3.2 Connect dummy outputs
        for( int ll = definedGenes; ll < nrOuts; ll++ )
        {
            SimulatorLogicElement[] connections = new SimulatorLogicElement[ LUTInputs ];
            for( int il = 0; il < LUTInputs; il++ )
            {
                connections[ il ] = elements[ 0 ];
            }
            elements[ nrIns + ll ].connect( connections );
        }
        // 4. Connect Inputs
        for( int il = 0; il < nrIns; il++ )
        {
            inputs[ il ] = elements[ il ];
        }
        
        // 5. Connect Outputs
        for( int ol = 0; ol < nrOuts; ol++ )
        {
            outputs[ ol ] = elements[ ol + nrIns ];
        }
        
        SimulatorFaultyDelayLE[] onlyLUTs = new SimulatorFaultyDelayLE[ nrEls - nrIns ];
        for( int lel = 0; lel < onlyLUTs.length; lel++ )
        {
            onlyLUTs[ lel ] = elements[ lel + nrIns ];
        }
        
        SimulatorLogicElement[][] rv = { inputs, outputs, onlyLUTs };
        return rv;
    }
    
    public String toString()
    {
        String rv = "LUT based Circuit Mapping with Variable Size and Absolute Position with:";
        rv += "\n      inputs = " + nrIns;
        rv += "\n      outputs = " + nrOuts;
        rv += "\n      bits per variable = " + bitsPerVar;
        rv += "\n      LUT inputs = " + LUTInputs;
        rv += "\n      Delay Model: " + delayModel;
        return rv;
    }
    
    public void resetDelays( Object delayDef )
    {
        delayModel.set( delayDef );
    }
    
    public SimulatorLogicElement getElementFromAddress( SimulatorLogicElement[][] inoutels, int address )
    {
        int nrEls = inoutels[ ELEMENTS ].length;
        int totAdd = nrIns + nrEls;
        address %= totAdd;
        if( address < nrIns )
        {
            return inoutels[ INPUTS ][ address ];
        }else
        {
            return inoutels[ ELEMENTS ][ address - nrIns ];
        }
    }

    
}

⌨️ 快捷键说明

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