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

📄 circuitparsimonyim.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 CircuitParsimonyIM extends ShellIM
{
    // Constants
    protected final Genotype EMPTY_GENOTYPE = new Genotype();
    protected final int MAIN_POP = 0;
    protected final int SIZE_PROP = 3;
    protected final double threshold = 0.001;
    
    // Config Vars
    protected boolean stopAtSolution = true;
    protected double maxSize;
    
    // Working Data
    protected SimulatorLogicElement[][] inoutels;
    
    protected Genotype bestInd = EMPTY_GENOTYPE;
    protected double bestFit = -1;
    protected double bestSize = Double.MAX_VALUE;  // min if maximizing
    
    protected Genotype prevInd = EMPTY_GENOTYPE;
    protected double prevFit = -1;
    protected double prevSize = Double.MAX_VALUE;  // min if maximizing
    
    public CircuitParsimonyIM( InteractionModel yokeIM, SimulatorCircuit circuit, double maxSize, boolean stop )
    {
        this( yokeIM, circuit, maxSize );
        stopAtSolution = stop;
    }
    
    public CircuitParsimonyIM( InteractionModel yokeIM, SimulatorCircuit circuit, boolean stop )
    {
        this( yokeIM, circuit );
        stopAtSolution = stop;
    }

    public CircuitParsimonyIM( InteractionModel yokeIM, SimulatorCircuit circuit, double maxSize )
    {
        this( yokeIM, circuit );
        this.maxSize = maxSize;
    }
    
    public CircuitParsimonyIM( InteractionModel yokeIM, SimulatorCircuit circuit )
    {
        super( yokeIM );
        //pop = yokeIM.getPopulations()[ MAIN_POP ];
        inoutels = circuit.getInOutEls();
        circuit.reconfigure( EMPTY_GENOTYPE );
        maxSize = inoutels[ 2 ].length; // doesn't work for varsize!!
    }    
    
    /** Do boxing tournament style, whoever beats champion, is champion
    */
    synchronized public double[] evaluate(Genotype[] inds)
    {
        Genotype ind = inds[ MAIN_POP ];
        
        boolean wasElite = ind.getFitness() > 0;
        
        // 1. Calculate Fitness
        double fitness = yokeIM.evaluate( inds )[ MAIN_POP ];
        if( !stopAtSolution )
        {
            fitness /= 2;
        }
        
        //if( fitness >= bestInd.getFitness() )
        if( fitness >= ( bestFit - threshold ) )
        {
            // 2. Calculate circuit size
            double size = countUnits();
            if( ( size <= bestSize ) || ( fitness > bestFit ) )  // min
            {
                // 3. Replace best!
                bestInd = ind;
                bestSize = size;
                bestFit = fitness;
                bestInd.setProperty( SIZE_PROP, new Double( size / maxSize ) );
            }
        }
        if( wasElite )
        {
            prevInd = ind;
        }//else setFit 0.4!
        ind.setFitness( 0 );
        double[] rv = { fitness };
        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.
    */
    protected double countUnits()
    {
        Vector units = CircuitsLib.addConnectedGates( inoutels[ 1 ] );
        int size = units.size();
        double rv = size;
        SimulatorLUT slut;
        for( int ul = 0; ul < size; ul++ )
        {
            slut = ( SimulatorLUT ) units.get( ul );
            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()
    {
        if( ( bestFit >= prevFit - threshold ) && ( ( bestSize <= prevSize ) || bestFit > prevFit ) ) // min
        {
            bestInd.setFitness( bestFit );
            prevInd = bestInd;
            prevFit = bestFit;
            prevSize = bestSize;
        }else
        {
            prevInd.setFitness( prevFit );
        }
        yokeIM.evolve();
        bestInd = EMPTY_GENOTYPE;
        bestFit = -1;
        bestSize = Integer.MAX_VALUE; // min
    }
    
    public String toString()
    {
        String narrator = "Circuit Parsimony Interaction Model with:";
        //narrator += "\nExperiment Weight = " + EXP_WEIGHT;
        //narrator += "\nParsimony Weight = " + SIZE_WEIGHT;
        narrator += "\nInteraction Model: " + yokeIM;
        return narrator;
    }
    
    public Genotype[] getFittest()
    {
        Genotype rv0 = ( Genotype ) bestInd.clone();
        rv0.setFitness( bestFit );
        Genotype[] rv = { rv0 };
        return rv;
    }
}

⌨️ 快捷键说明

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