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

📄 circuitparsimonypim.java

📁 Java遗传算法库
💻 JAVA
字号:
/*
 * CircuitParsimonyIM.java
 *
 * Created on 01 May 2002, 20:35
 */

package jaga.pj.circuits.control;

import jaga.Genotype;
import jaga.SampleData;
import jaga.evolve.Evolver;
import jaga.evolve.Population;
import jaga.deploy.Deployment;
import jaga.experiment.Experiment;
import jaga.control.*;
import jaga.pj.circuits.*;
import jaga.pj.circuits.fpgaft.SimulatorLUT;

import java.util.Vector;

/**
 *
 * @author  Michael Garvie
 * @version 
 */
public class CircuitParsimonyPIM extends ShellIM
{
    // Constants
    protected final Genotype EMPTY_GENOTYPE = new Genotype();
    protected final int MAIN_POP = 0;
    protected final double threshold = 0.001;
    
    // Config Vars
    protected boolean parsForEver = false;
    protected double maxSize;
    protected int parsPriority = 2;
    
    // Working Data
    protected SimulatorLogicElement[][] inoutels;
    
        
    public CircuitParsimonyPIM (InteractionModel yokeIM,SimulatorCircuit circuit,double maxSize,boolean pars4ever)
    {
        this( yokeIM, circuit, maxSize );
        parsForEver = pars4ever;
    }    

    public CircuitParsimonyPIM (InteractionModel yokeIM,SimulatorCircuit circuit,double maxSize, int parsPriority)
    {
        this( yokeIM, circuit, maxSize );
        this.parsPriority = parsPriority;
    }
    
    
    public CircuitParsimonyPIM (InteractionModel yokeIM,SimulatorCircuit circuit,double maxSize)
    {
        super( yokeIM );
        this.maxSize = maxSize;
        inoutels = circuit.getInOutEls();
    }
    
    
    
    /** Do boxing tournament style, whoever beats champion, is champion
    */
    synchronized public double[] evaluate(Genotype[] inds)
    {
        // 1. Calculate Fitness
        double[] rv = yokeIM.evaluate( inds );
        
        // 2. Calculate circuit size
        double size = countUnits();
        inds[ MAIN_POP ].setProperty( parsPriority, new Double( 1 - size / maxSize ) );
        
        //System.out.println("setting pars " + parsPriority + " to " + inds[ MAIN_POP ] ); //D
        
        return rv;
    }
    
    
    /** This methods counts the number of units in the circuit but is optimized so that in the 
     * case of LUT elements it will recognize one input gates as half a two input gate.
     * Currently only works for 2 input LUTs.
    */
    public double countUnits()
    {
        Vector units = CircuitsLib.addConnectedGates( inoutels[ 1 ] );
        int size = units.size();
        double rv = size;
        SimulatorLUT slut;
        for( int ul = 0; ul < size; ul++ )
        {
            Object currUnit = units.get( ul );
            if( currUnit instanceof SimulatorLUT )
            {
                slut = ( SimulatorLUT ) units.get( ul );
                if( slut.table.length == 4 )
                {
                    if( ( slut.table[ 0 ] == slut.table[ 2 ] && slut.table[ 1 ] == slut.table[ 3 ] )
                      ||( slut.table[ 0 ] == slut.table[ 1 ] && slut.table[ 2 ] == slut.table[ 3 ] ) )
                    {
                        rv -= 0.5;
                    }
                }
            }
        }
        return rv;
    }
    
    public void evolve()
    {
        yokeIM.evolve();
    }
    
    public String toString()
    {
        String narrator = "Circuit Parsimony Property Interaction Model with:";
        narrator += "\nMaximum Size = " + maxSize;
        narrator += "\nInteraction Model: " + yokeIM;
        return narrator;
    }
        
    public Genotype getMaxFitness()
    {
        Genotype rv = super.getMaxFitness();
        if( parsForEver )
        {
            rv.setProperty( parsPriority, new Double( 2 ) ); // something unreachable
        }else
        {
            rv.setProperty( parsPriority, new Double( 1 ) ); // ** also unreachable!
        }
        return rv;
    }
}

⌨️ 快捷键说明

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