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

📄 addclockexperiment.java~

📁 Java遗传算法库
💻 JAVA~
字号:
/* * AddClockExperiment.java * * Created on 29 July 2003, 14:46 */package es.pj.circuits.experiment;import es.SampleData;import es.experiment.Experiment;/** Adds an extra input to the nested experiments generated input which behaves thus:  Oscillates * cyclesPerSample times starting high and ending low.  The input sample separation used must * be divisible by 2 * cyclesPerSample. * * @author  mmg20 */public class AddClockExperiment implements Experiment {        Experiment nestedExp;    int cyclesPerSample = 1;        public AddClockExperiment(Experiment exp, int cyclesPerSample) {        this( exp );        this.cyclesPerSample = cyclesPerSample;    }        /** Creates a new instance of AddClockExperiment */    public AddClockExperiment(Experiment exp) {        nestedExp = exp;    }        /** generates an array of inputs suitable for this experiment     * using default input sample separation.     */    public SampleData[] generateInput() {        return addClock( nestedExp.generateInput() );    }        protected SampleData[] addClock( SampleData[] nckIns )    {        int oldiss = nckIns[ 0 ].getSampleSeparation();        int oldidlen = nckIns[ 0 ].length();        int factor = 2 * cyclesPerSample;        int newiss = oldiss / factor; // must be divisible by factor        int newidlen = oldidlen * factor;        SampleData[] rv = new SampleData[ nckIns.length + 1 ];                // 1. Copy old inputs to new        int il;        for( il = 0; il < nckIns.length; il++ )        {            rv[ il ] = new SampleData( newiss, newidlen );            for( int idl = 0; idl < oldidlen; idl++ )            {                boolean currBit = nckIns[ il ].get( idl );                for( int flp = 0; flp < factor; flp++ )                {                    rv[ il ].setTo( factor * idl + flp, currBit );                }            }        }        rv[ il ] = new SampleData( newiss, newidlen );        for( int idl = 0; idl < oldidlen; idl++ )        {            for( int flp = 0; flp < cyclesPerSample; flp++ )            {                int ix = factor * idl + 2 * flp;                rv[ il ].setTo( ix, true ); // rising edge on input change                rv[ il ].setTo( ix + 1, false );            }        }        return rv;    }    protected SampleData[] removeClock( SampleData[] ckIns )    {        int newiss = ckIns[ 0 ].getSampleSeparation();        int newidlen = ckIns[ 0 ].length();        int factor = 2 * cyclesPerSample;        int oldiss = newiss * factor; // must have been divisible        int oldidlen = newidlen / factor;        SampleData[] rv = new SampleData[ ckIns.length - 1 ];                // 1. Copy new inputs to old        int il;        for( il = 0; il < rv.length; il++ )        {            rv[ il ] = new SampleData( oldiss, oldidlen );            for( int idl = 0; idl < oldidlen; idl++ )            {                boolean currBit = ckIns[ il ].get( idl * factor );                rv[ il ].setTo( idl, currBit );            }        }        return rv;    }            /** generates an array of inputs suitable for this experiment.     * @param inputSampleSeparation relative frequency of input to output samples.  If this is n, then n outputs will be sampled for every change in inputs.     */    public SampleData[] generateInput(int inputSampleSeparation) {        return addClock( nestedExp.generateInput( inputSampleSeparation ) );    }        /** returns a fitness associated to a given input/output pair for     * this experiment.  The fitness is a double and is in adjusted     * fitness format.  From 0 to 1, 1 being perfectly fit.     * @param in array of inputs     * @param out array of outputs     */    public double getFitness(SampleData[] in, SampleData[] out) {        return nestedExp.getFitness( removeClock( in ), out );    }        public int getNumOfInputs() {        return nestedExp.getNumOfInputs() + 1;    }        public int getNumOfOutputs() {        return nestedExp.getNumOfOutputs();    }        public String toString()    {        String narrator = "Adding Clock with: ";        narrator+= "\n  Cycles per Sample = " + this.cyclesPerSample;        narrator += "\n  Experiment: " + nestedExp;        return narrator;    }    }

⌨️ 快捷键说明

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