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

📄 truerandompessimisticmultiplierexperiment.java~

📁 Java遗传算法库
💻 JAVA~
字号:
/* * TrueRandomPessimisticMultiplierExperiment.java * * Created on 26 August 2003, 16:56 */package es.pj.circuits.experiment;import es.SampleData;import es.ESLib;import java.util.Vector;/** * * @author  mmg20 */public class TrueRandomPessimisticMultiplierExperiment extends MultiplierExperiment {        protected int N = 4;    protected int errTh = 3;    protected static final int cacheSize = 0;        protected SampleData[][] cachedInputs = null;        public TrueRandomPessimisticMultiplierExperiment( int width, double tSetup, boolean alwaysRandom, int N, int errTh )     {        this( width, tSetup, alwaysRandom );        this.N = N;        this.errTh = errTh;    }        /** Creates a new instance of TrueRandomPessimisticMultiplierExperiment */    public TrueRandomPessimisticMultiplierExperiment( int width, double tSetup, boolean alwaysRandom )     {        super( width, tSetup, alwaysRandom );    }        public double getFitness(SampleData[] in,SampleData[] out)    {        // calculate what output should be,        // allow for a tsetup time for the latch output.  This will be measured        // as a fraction of the separation of input samples.                // Unseen inputs treated as correct.        int numInputs = getNumOfInputs();        int numOutputs = getNumOfOutputs();        boolean[][] correct = new boolean[ numOutputs ][ 1 << numInputs ];        for( int ql = 0; ql < correct.length; ql++ )        {            for( int clp = 0; clp < correct[ ql ].length; clp++ )            {                correct[ ql ][ clp ] = true;            }        }                int inputSampleSeparation = in[ 0 ].getSampleSeparation();        int startCheckAt = ( int )( inputSampleSeparation * tSetup );        int inputSamples = in[ 0 ].length();                int totalFitness = 0;        for( int idl = 0; idl < inputSamples; idl++ )        {            // calculate what Q should have been            int a = ESLib.getLine( in, idl, 0, width - 1 );            int b = ESLib.getLine( in, idl, width, 2 * width - 1 );            int inputCode = ESLib.getLine( in, idl );            int desiredQ = a * b;            boolean desQBA[] = new boolean[ 2 * width ];            for( int bl = 0; bl < width * 2; bl++ )            {                desQBA[ width * 2 - bl - 1 ] = ( desiredQ & ( 1 << bl ) ) > 0;            }            int[] currErr = new int[ 2 * width ];            for( int odl = startCheckAt; odl < inputSampleSeparation; odl++ )            {                   int currIdx = idl * inputSampleSeparation + odl;                for( int bl = 0; bl < width * 2; bl++ )                {                    if( out[ bl ].get( currIdx ) != desQBA[ bl ] )                    {                        currErr[ bl ]++;                    }                }            }            for( int bl = 0; bl < width * 2; bl++ )            {                if( currErr[ bl ] > errTh )                {                    correct[ bl ][ inputCode ] = false;                }            }                                    /* // This code gets average value for each input for fitness eval            int[] highCount = new int[ width * 2 ];                        for( int odl = startCheckAt; odl < inputSampleSeparation; odl++ )            {                   int currIdx = idl * inputSampleSeparation + odl;                // check correctness of output, bit by bit                for( int bl = 0; bl < width * 2; bl++ )                {                    if( out[ bl ].get( currIdx ) )                    {                        highCount[ bl ]++;                    }else                    {                        highCount[ bl ]--;                    }                }            }*//*            for( int bl = 0; bl < width * 2; bl++ )            {                // check if this bit correct                boolean desQthis = ( desiredQ & ( 1 << bl ) ) > 0;                if( desQthis != ( highCount[ width * 2 - bl - 1 ] > 0 ) ) // if equal nr then low                {                    correct[ bl ][ inputCode ] =  false;                }            }   */                             }        int totalCount = correct.length * correct[ 0 ].length;        int correctCount = 0;                for( int ql = 0; ql < correct.length; ql++ )        {            for( int clp = 0; clp < correct[ ql ].length; clp++ )            {                if( correct[ ql ][ clp ] )                {                    correctCount++;                }            }        }                double rv = Math.max( 0 , ( ( double ) correctCount ) / ( ( double ) totalCount ) );                return rv;    }        protected void updateTestData()    {        if( cacheSize == 0 )        {            currTestData = generateTestData();        }        else        {            if( cachedInputs == null )            {                cachedInputs = new SampleData[ cacheSize ][];                for( int tl = 0; tl < cacheSize; tl++ )                {                    cachedInputs[ tl ] = generateTestData();                }            }            int cachePick = rnd.nextInt( cacheSize );            currTestData = cachedInputs[ cachePick ];        }    }            protected SampleData[] generateTestData()    {        SampleData[] rv;        // Create TP by randomizing each line but stopping when we have N copies of each vector.        int numInputs = getNumOfInputs();        int numVectors = 1 << numInputs;        int[] visitCount = new int[ numVectors ];        Vector TP = new Vector();        while( someLessThan( visitCount, N ) )        {            int toAdd = rnd.nextInt( numVectors );            //System.out.println(toAdd); //D            visitCount[ toAdd ]++;            TP.add( new Integer( toAdd ) );        }        rv = new SampleData[ numInputs ];        for( int il = 0; il < numInputs; il++ )        {            rv[ il ] = new SampleData( 1, TP.size() );        }        for( int idl = 0; idl < TP.size(); idl++ )        {            int currVector = ( ( Integer ) TP.get( idl ) ).intValue();              es.ESLib.setLine( rv, idl, currVector );        }        return rv;    }                protected boolean someLessThan( int[] arr, int n )    {        for( int l = 0; l < arr.length; l++ )        {            if( arr[ l ] < n )            {                return true;            }        }        return false;    }    }

⌨️ 快捷键说明

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