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

📄 evenoscillatorexperiment.java

📁 Java遗传算法库
💻 JAVA
字号:
/*
 * EvenOscillatorExperiment.java
 *
 * Created on 24 April 2001, 23:09
 */

package jaga.pj.circuits.experiment;

import jaga.*;
import jaga.experiment.*;

/**
 *
 * @author  Michael Garvie
 * @version 
 */
public class EvenOscillatorExperiment implements Experiment {

    private int period;
    
    // sample enough so that we try and achieve what number of rising edges:
    // ie. if we are looking for 1 rising edge every 10 samples then we have to
    // generate a test of 200 samples to expect to find 20 rising edges.
    private int periodsToTest = 20;
        
    /** Creates new SimpleOscillatorExperiment
     * @param period how often we want a rising edge to appear in the output measured in sampleData elements (the smallest simulator time slice)
    */
    public EvenOscillatorExperiment( int period ) {
        this.period = period;
    }
            
    /** 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)
    {
        SampleData output = out[ 0 ]; // our output data, hopefully oscilates!
        
        int edges = 1;
        int totalSeparation = 0;
        
        int lastRiseEdgePos = 0;
        
        for( int ol = 1; ol < output.length(); ol++ )
        {
            if( output.get( ol ) && !output.get( ol - 1 ) )
            {
                totalSeparation += ( ol - lastRiseEdgePos );
                edges++;
                lastRiseEdgePos = ol;
            }
        }
        
        double avgSeparation = ( ( double ) totalSeparation ) / ( edges );
        
        double fitness = 1d / ( Math.abs( avgSeparation - period ) + 1 );
        
        return fitness;
        
    }
    
    /** generates an array of inputs suitable for this experiment
     * using default input sample separation.
     */
    public SampleData[] generateInput()
    {
        SampleData[] rv = new SampleData[ 1 ];
        rv[ 0 ] = new SampleData( period * periodsToTest, 1 );
        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 generateInput();
    }
    
    public int getNumOfInputs()
    {
        return 1;
    }
    
    public int getNumOfOutputs()
    {
        return 1;
    }
    
    public String toString()
    {
        String rv = "EvenOscillatorExperiment with period = " + period;
        return rv;
    }
}

⌨️ 快捷键说明

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