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

📄 repairfaultsequenceim.java

📁 Java遗传算法库
💻 JAVA
字号:
/* * RepairFaultSequenceIM.java * * Created on 21 October 2003, 12:32 */package jaga.pj.circuits.control;import jaga.Genotype;import jaga.evolve.Evolver;import jaga.evolve.Population;import jaga.deploy.Deployment;import jaga.experiment.Experiment;import jaga.control.InteractionModel;import jaga.control.ShellIM;import jaga.pj.circuits.fpgaft.FaultyCircuit;import java.awt.Point;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;/** * * @author  mmg20 */public class RepairFaultSequenceIM extends ShellIM {        // Constants    protected Genotype TERMINATION_GENOTYPE = new Genotype();    protected final int MAIN_POP = 0;    protected final static Object ELITE_FLAG = "E";    protected final static int ELITE_FLAG_PROPERTY_INDEX = 1;    protected final static int LOG_PROPERTY_INDEX = 3;        // Config    protected FaultyCircuit circuit;    protected Point[] faultSequence;    protected int maxGenerationsPerFault;    protected int generationsToTestSolution = 9;        // Working    protected Genotype SOLUTION_TEMPLATE;    protected int nextFaultIndex = 0;    protected int generationsForThisFault = 0;    protected int generationsWithSolution = 0;    protected Point[] faultsPresent = {};    protected boolean terminate = false;    protected ArrayList log = new ArrayList();        public RepairFaultSequenceIM(InteractionModel inIM, FaultyCircuit circuit, Point[] faultSequence, int maxGenerationsPerFault, int generationsToTestSolution )    {        this( inIM, circuit, faultSequence, maxGenerationsPerFault );        this.generationsToTestSolution = generationsToTestSolution;    }        /** Creates a new instance of RepairFaultSequenceIM */    public RepairFaultSequenceIM(InteractionModel inIM, FaultyCircuit circuit, Point[] faultSequence, int maxGenerationsPerFault )    {        super( inIM );        this.circuit = circuit;        this.faultSequence = faultSequence;        this.maxGenerationsPerFault = maxGenerationsPerFault;        SOLUTION_TEMPLATE = inIM.getMaxFitness();        TERMINATION_GENOTYPE.setFitness( SOLUTION_TEMPLATE.getFitness() * 2 );        addFaultToPresentSet( faultSequence[ nextFaultIndex++ ] );        circuit.setPersistentFaults( faultsPresent );    }        protected void addFaultToPresentSet( Point fault )    {        Point[] newFaultsPresent = new Point[ faultsPresent.length + 1 ];        System.arraycopy( faultsPresent, 0, newFaultsPresent, 0, faultsPresent.length );        newFaultsPresent[ faultsPresent.length ] = fault;        faultsPresent = newFaultsPresent;        log.add( fault );    }        public double[] evaluate( Genotype[] inds )    {        double[] rv = super.evaluate( inds );        inds[ MAIN_POP ].setProperty( LOG_PROPERTY_INDEX , log ); // Put here if want to show log always.        if( terminate )        {            double terminationFlagFitness = TERMINATION_GENOTYPE.getFitness() * 2;            inds[ MAIN_POP ].setFitness( terminationFlagFitness );            rv[ MAIN_POP ] = terminationFlagFitness;                        inds[ MAIN_POP ].setProperty( LOG_PROPERTY_INDEX , log );        }        return rv;    }        public Genotype getMaxFitness()    {        return TERMINATION_GENOTYPE;    }        public void evolve()    {        generationsForThisFault++;        terminate |= ( generationsForThisFault > maxGenerationsPerFault );                Population pop = yokeIM.getPopulations()[ MAIN_POP ];        Genotype nextElite = pop.getFittest( 1 )[ 0 ];                nextElite.setProperty( ELITE_FLAG_PROPERTY_INDEX, null ); // if elite then comparison will choose template        if( nextElite.compareTo( SOLUTION_TEMPLATE ) >= 0 )        {                        if( generationsWithSolution++ >= generationsToTestSolution )            {                int repairGenerations = generationsForThisFault - generationsToTestSolution - 1;                log.add( new Integer( repairGenerations ) );                                // 1. Next Fault                if( nextFaultIndex < faultSequence.length )                {                    addFaultToPresentSet( faultSequence[ nextFaultIndex++ ] );                    circuit.setPersistentFaults( faultsPresent );                                        // 2. House Keeping                    generationsForThisFault = 0;                    generationsWithSolution = 0;                }else                {                    terminate = true;                }            }        }else        {            generationsWithSolution = 0;        }        yokeIM.evolve();                pop = yokeIM.getPopulations()[ MAIN_POP ];        Genotype ind0 = pop.getGenotype( 0 );        Genotype ind1 = pop.getGenotype( 1 );                if( ind0.bitSetEquals( nextElite ) )        {            ind0.setProperty( this.ELITE_FLAG_PROPERTY_INDEX, this.ELITE_FLAG );            ind1.setProperty( this.ELITE_FLAG_PROPERTY_INDEX, null );                    }else        {            // Do not assume Evolver implementation            ind1.setProperty( this.ELITE_FLAG_PROPERTY_INDEX, this.ELITE_FLAG );            ind0.setProperty( this.ELITE_FLAG_PROPERTY_INDEX, null );        }                pop.touched();            }            public String toString()    {        String rv = "Repair Fault Sequence Interaction Model with: ";        rv += "\n  Maximum Generations = " + maxGenerationsPerFault;//        rv += "\n  Current Repair State = " + log;        rv += "\n  Internal interaction Model " + yokeIM;        return rv;    }}

⌨️ 快捷键说明

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