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

📄 sisoutputreader.java

📁 Java遗传算法库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                fromER.connect( outERconns );
            }
        }
      
        // Debug - Print out everything in els and codes
        
        /*
        for( int el = 0; el < totalEls; el++ )
        {
            SimulatorLogicElement currEl = ( SimulatorLogicElement ) els.get( el );
            String story = "el " + el + "=" + currEl;
            SimulatorLogicElement[] currCons = currEl.getInputs();
            for( int il = 0; il < currCons.length; il++ )
            {
                story += " in" + il + "p=" + els.indexOf( currCons[ il ] ) + "=" + currCons[ il ];
            }
            System.out.println( story );
        }
        */
        
        if( varSized )
        {
            int inputsPos = 1 << bitsPerVar;
            for( int il = 0; il < totalInputs; il++ )
            {
                els.insertElementAt( els.remove( inputsPos - 1 ), il );
            }
            
        }            
        
        if( !FPGALUT )
        {
            if( varSized )
            {
                els.setSize( totalEls + totalInputs );
            }
            genotype = vector2Gene( els, globalIns, bitsPerVar, lutInputs, varSized );
        }else
        {
            genotype = vector2FPGAGene( els, globalIns, bitsPerVar, lutInputs, totalOutputs, varSized, reservedOutputs, totalEls );
            if( varSized )
            {
                // Cut off last bit
                int genLength = totalOutputs * ( bitsPerVar + 1 );
                int geneSize = ( 1 << lutInputs ) + ( lutInputs + 1 ) * ( bitsPerVar + 1 );
                genLength += geneSize * totalEls;
                genotype = genotype.substring( 0, genLength );
            }
        }
        
        //System.out.println("SQ:     # Ins = " + totalInputs ); //D
    }
    
    /** Calculates the number of Elements which will need to be addressed, which in turn will
     * be used to calculate the minimum number of bits per variable to use.
     */ // Tested 17/7/03
    public int calcAddEls( boolean FPGALUT, boolean voterSpace, int q, int rq, int tI, int l, int e )
    {
        int rv;
        
        int nrLUTs = q + e;
        int nrLatches = l;
        if( !FPGALUT )
        {
            nrLUTs += l * LUT2_LATCH_SIZE;
        }
        if( voterSpace )
        {
            nrLUTs = sizeWithVoterBIST( nrLUTs, q );
            nrLatches *= 2;
        }
        if( !FPGALUT )
        {
            nrLUTs += rq;
        }
        nrLUTs += tI;
        nrLatches += tI;
        
        rv = nrLUTs;
        //System.out.println("Using " + nrLUTs + " LUTS and " + nrLatches + " latches " );
        if( FPGALUT )
        {
            rv = Math.max( nrLUTs, nrLatches );
        }
        return rv;
    }
        
    /** Returns the size of a voter with two copies of the circuit with a given size and number of outputs
     */ // Tested 17/7/03
    public int sizeWithVoterBIST( int size, int outs )
    {
        int rv = size * 2;
        if( lutInputs == 4 )
        {
            rv += outs / 1.5 + 1;
        }else
        {
            rv += outs * 2 - 1;
        }
        return rv; // copies + comparator
    }
        
    /** Given the size of a circuit, returns how many bits per variable are the minimum necesary.
     */ // Tested 17/7/03
    public static int calcBitsPerVar( int size )
    {
        int rv = 1;
        while( ( 1 << rv ) <= size )
        {
            rv++;
        }
        return rv;
    }
        
    /** Scans through the output from Sis in the format: print_io, print, print_latch and puts the
     * names of the inputs, outputs, latches and latch inputs into vectors.
     */ // Tested 17/7/03
    public static void scan4IOLNames( String sisOut, Vector inputNames, Vector outputNames, Vector latchNames, Vector latchInputNames ) throws IOException
    {
        BufferedReader bur = new BufferedReader( new StringReader( sisOut ) );
        String line;
        
        // 2 - Scan for Latch names
        while( ( line = bur.readLine() ) != null)
        {
            if( line.startsWith( LATCH_DEF_START_TOKEN ) )
            {
                StringTokenizer stk = new StringTokenizer( line, " {}[]" );
                // Assume input mentioned before output
                while( !stk.nextToken().equals( LATCH_INPUT_DEF_TOKEN ) )
                {
                }
                latchInputNames.add( stk.nextToken() );
                while( !stk.nextToken().equals( LATCH_OUTPUT_DEF_TOKEN ) )
                {
                }
                latchNames.add( stk.nextToken() );
            }
        }

        // 1 - Scan for input names - Assume before outputs
        bur = new BufferedReader( new StringReader( sisOut ) );
        while( !( line = bur.readLine() ).startsWith( INPUT_DEF_START_TOKEN ) )
        {
        }
        StringTokenizer stk = new StringTokenizer( line.substring( INPUT_DEF_START_TOKEN.length(), line.length() ) , " {}[]" );
        String tk;
        while( stk.hasMoreTokens() )
        {
            tk = stk.nextToken();
            if( !latchNames.contains( tk ) )
            {
                inputNames.add( tk );
            }
        }
        // 1 - Scan outputs
        while( !( line = bur.readLine() ).startsWith( OUTPUT_DEF_START_TOKEN ) )
        {
        }
        stk = new StringTokenizer( line.substring( OUTPUT_DEF_START_TOKEN.length(), line.length() ) , " {}[]" );
        while( stk.hasMoreTokens() )
        {
            tk = stk.nextToken();
            if( !latchInputNames.contains( tk ) )
            {
                outputNames.add( tk );
            }
        }
    }
    
    /** Scans through the output from Sis in the format: print_io, print, print_latch and puts the
     * names of the elements into a vector.  It must be given the names of the outputs in a vector
     * to filter these out.
     */ // Tested 17-07-03
    public static Vector scan4ElementNames( String sisOut, Vector outs ) throws IOException
    {
        Vector rv = new Vector();
        BufferedReader bur = new BufferedReader( new StringReader( sisOut ) );
        String line;
        while( ( line = bur.readLine() ) != null )
        {
            StringTokenizer stk = new StringTokenizer( line, " {}[]" );
            if( stk.hasMoreTokens() )
            {
                String t0 = stk.nextToken();
                if( stk.hasMoreTokens() && stk.nextToken().equals( ELEMENT_DEF_TOKEN ) )
                {
                    if( !outs.contains( t0 ) )
                    {
                        rv.add( t0 );
                    }
                }
            }
        }
        return rv;
    }
            
    protected static void addLine( Vector els, Hashtable codes, String line, int lutInputs )
    {
        //System.out.println( line );//debug
        
        StringTokenizer stt = new StringTokenizer( line, SIS_DELIMETERS );
        String t0 = stt.nextToken();
        
        //System.out.println( t0 );//debug
        boolean[] table = extractTable( line, lutInputs );
        //System.out.println( jaga.ESLib.boolArr2String( table ) );// debug
        String[] unitInputNames = extractUnitInputNames( line, lutInputs );
        SimulatorLogicElement[] conns = getInputsFromNames( unitInputNames, els, codes );
        
        //SimulatorLogicElement[] conns = { getByName( els, codes, t1 ), getByName( els, codes, t2 ) };
        

        Integer posIx = ( Integer ) codes.get( t0 );
        SimulatorLUT e = ( SimulatorLUT )els.get( posIx.intValue() );
        //System.out.println(t0 + " ix " + posIx );//debug
        e.table = table;
        e.connect( conns );
        
    }
    
    /** Given a line representing a unit definition from the Sis output, having the format
     * X = [formula of Y0, Y1, Y2..] will return a String array with all the Yn
     */ // Tested 17-07-03
    public static String[] extractUnitInputNames( String line, int lutInputs )
    {
        String[] rv = new String[ lutInputs ];
        int lutIx = 0;
        StringTokenizer stt = new StringTokenizer( line, SIS_DELIMETERS );
        stt.nextToken(); // first one is name of output
        while( stt.hasMoreTokens() )
        {
            String t = stt.nextToken();
            //System.out.println(t);
            if( indexOf( t, rv ) < 0 )
            {
                rv[ lutIx++ ] = t;
            }
        }
        return rv;
    }
        
    /** Given a String array with input names, and a hashtable with the elements, 
      * returns an array of elements. */
    // Tested 17-07-03
    public static SimulatorLogicElement[] getInputsFromNames( String[] inputNames, Vector els, Hashtable codes )
    {
        SimulatorLogicElement[] rv = new SimulatorLogicElement[ inputNames.length ];
        for( int ll = 0; ll < inputNames.length; ll++ )
        {
            if( inputNames[ ll ] != null )
            {
                //System.out.println("Looking for " + inputNames[ ll ] + " at " + codes.get( inputNames[ ll ] ) );
            }
            rv[ ll ] = getByName( els, codes, inputNames[ ll ] );
        }
        return rv;
    }
    
    
    /** Extracts n-input LUT truth table for this line of sis output.
      */ // Tested 17-07-03
    public static boolean[] extractTable( String line, int lutInputs )
    {
        //System.out.println("Doing line = " + line);
        
        // Step 1 - Init
        int inputCombs = 1 << lutInputs;
        String[] unitInputNames = extractUnitInputNames( line, lutInputs );
        /*for( int ll = 0; ll < lutInputs; ll++ )
        {
            System.out.println( "Names " + ll + " = '" + unitInputNames[ ll ] + "'" );
        }*/
        
        boolean[] rv = new boolean[ inputCombs ]; // all false by default
        
        // Step 2
        StringTokenizer termTokenizer = new StringTokenizer ( line, "+=" );
        termTokenizer.nextToken(); // first is output
        
        while( termTokenizer.hasMoreTokens() )
        {
            String term = termTokenizer.nextToken();
            //System.out.println("Term = " + term ); //D
            boolean[] M = new boolean[ inputCombs ];
            for( int ml = 0; ml < inputCombs; ml++ )
            {
                M[ ml ] = true;
            }
            //System.out.println("M=" + jaga.ESLib.boolArr2String( M )); //D
            StringTokenizer symbolTokenizer = new StringTokenizer( term, " " );
            while( symbolTokenizer.hasMoreTokens() )
            {
                String symbol = symbolTokenizer.nextToken();
                //System.out.println("Symbol = " + symbol );
                boolean symbolNegated = symbol.endsWith( "'" );
                StringTokenizer symbolNoFrillsTok = new StringTokenizer( symbol, "[]{}'" );
                symbol = symbolNoFrillsTok.nextToken();
                //System.out.println("Symbol = '" + symbol + "'");
                int Xs = indexOf( symbol, unitInputNames );
                int sFilter = 1 << ( lutInputs - Xs - 1 );
                //System.out.println("Xs,sFilt " + Xs + " " + sFilter);
                for( int ml = 0; ml < inputCombs; ml++ )
                {
                    //System.out.println("ml, ml&sF " + ml + "," + ( ml & sFilter ) );
                    if( ( ( ml & sFilter ) == 0 ) == !symbolNegated )
                    {
                        M[ ml ] = false;

⌨️ 快捷键说明

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