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

📄 evorobotech.java~

📁 Java遗传算法库
💻 JAVA~
字号:
package jaga.pj.circuits.control;

import jaga.control.*;import jaga.deploy.*;import jaga.evolve.*;import jaga.experiment.*;import jaga.*;
import jaga.pj.circuits.*;import jaga.pj.circuits.experiment.*;import jaga.pj.circuits.fpgaft.*;
import jaga.pj.gral.*;import islandev.IslandsEvolutionServer;import debug.DebugLib;
import java.util.Vector;import java.util.Random;import java.rmi.*;import java.io.*;import java.awt.Point;

public abstract class EvoRobotech
{
    // Tasks to be sent to server
    static Vector taskQ = new Vector();
    static Vector taskQNames = new Vector();
    static final double migrationRate = 0.5;
    
    // Log Stuff
    static String logDir;
    static final String LOG_FILE_NAME = "robotech.txt";
    static final String fs = File.separator;
    static final String LOG_BASE_DIR = fs + "windows" + fs + "d" + fs + "log" + fs;
    
    // Constants
    static final int EVALS_PER_SEC = 50;
    
    public static void main( String[] args )
    {
        logDir = args[ 0 ];
        //DebugLib.trcLogger.isLogging = true;
        DebugLib.logFileName = LOG_FILE_NAME;
        try
        {
            // I - Bench Single
            //addRepairComBenchAllSingle( "C17", 2 );
            
            // II - Bench Multiple                                                                                                                                                                                                                                                              
            int extraBPV = 1;   int hoursToRepair = 24;
            addRepairComBenchMultipleNoLatch( "C17", 5, extraBPV, hoursToRepair );
            
            // I - Two bit mult Single Fault repair
            /*
            int usedEls = 4;
            int N = 2;
            FaultModel faultModel = new SingleFullFaultModel( usedEls );
            for( int nl = 0; nl < N; nl++ )
            {
                while( faultModel.hasMoreElements() )
                {
                    Point fault = ( Point ) faultModel.nextElement();
                    addRepair2MultSingle( nl, fault );
                }
                faultModel.reset();
            }
            */
            // II - Multiple faults
            //addRepair2MultMultiple( 5 );
            
            IslandsEvolutionServer ms = new IslandsEvolutionServer( "Robotech", taskQ, taskQNames, logDir, migrationRate );
            ms.bindServer();
        }catch( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    
    private static int countBelow( Vector ints, int belowWhat )
    {
        int rv = 0;
        for( int el = 0; el < ints.size(); el++ )
        {
            int intVal = ( ( Integer ) ints.get( el ) ).intValue();
            if( intVal < belowWhat )
            {
                rv++;
            }
        }
        return rv;
    }    
        
    private static void addRepairComBenchMultipleNoLatch( String fileName, int M, int extraBitsPerVar, int hoursToRepair ) throws IOException
    {
        String sisOutputFileName = "/home/mmg20/eh/benchmarks/" + fileName + "L4.sout";
        int resQ = 0;    boolean fpga = false;    boolean voter = false;    int LUT_INPUTS = 4;  boolean varSized = false;
        SisOutputReader sor = new SisOutputReader( new File( sisOutputFileName ), resQ, LUT_INPUTS, fpga, voter, extraBitsPerVar, varSized );
        int usedEls = sor.getTotalEls();
        int bitsPerVar = sor.getBitsPerVar();
        String blifFileName = "/home/mmg20/eh/benchmarks/" + fileName + ".blif";
        ConfigurableRandomInputExperiment combexp = new CombinationalBLIFExperiment( blifFileName, 0 );
        int nrIns = combexp.getNumOfInputs();// no latch, no clock
        int totalCLBs = ( 1 << bitsPerVar ) - nrIns;
        int spareLUTs = totalCLBs - usedEls;
        int gensPerSec = EVALS_PER_SEC;
        int gensPerHour = gensPerSec * 60 * 60;
        int maxGens = gensPerHour * hoursToRepair;
        
        String narrator = "Repairing combinational benchmark: " + fileName + " under multiple faults.";
        narrator += "\nBPV(CLB) = " + bitsPerVar + ".  Total CLBs = " + totalCLBs + ".  Used LUTs = " + usedEls;
        narrator += "\nSpares: " + spareLUTs + " LUTs. ";
        narrator += "\nSequences totally random ending when only " + usedEls + " LUTs left (assume minimal design)";
        narrator += "\nMax Gens per repair = (" + hoursToRepair + "h) = " + maxGens;
        narrator += "\nOrderings:\n";
        
        int[] faultPos = new int[ totalCLBs ]; // in FPGALUTAbsMapping it skips inputs
        for( int flp = 0; flp < totalCLBs; flp++ )
        {
            faultPos[ flp ] = flp;
        }
        Vector[] orders = new Vector[ M ];
        Random rnd = new Random();
        for( int ml = 0; ml < M; ml++ )
        {
            orders[ ml ] = new Vector();
            Vector allFaults = new Vector();
            for( int flp = 0; flp < faultPos.length; flp++ )
            {
                allFaults.add( new Integer( faultPos[ flp ] ) ); // Extra level of inderection maintained just in case mapping changes
            }
            while( orders[ ml ].size() < spareLUTs )
            {
                int ix = rnd.nextInt( allFaults.size() );
                Integer currFPos = ( Integer ) allFaults.remove( ix );
                int currFVal = rnd.nextInt( 2 ); // SSA0 or 1
                orders[ ml ].add( new Point( currFPos.intValue(), currFVal ) );
            }
            narrator += ml+": " + orders[ ml ] + "\n";
        }
        String dirName = LOG_BASE_DIR + "Repair" + fs + fileName + fs + "Multiple" + fs;
        File orderFile = new File( dirName + "orders.txt" );
        BufferedWriter multipleBW = new BufferedWriter( new FileWriter( orderFile ) );
        multipleBW.write( narrator );
        multipleBW.flush();        
        multipleBW.close();
        
        for( int ml = 0; ml < M; ml++ )
        {
            addRepairComBlifSequenceNoLatch( fileName, ml, orders[ ml ], extraBitsPerVar, maxGens );
        }
    }    
    
    private static void addRepairComBlifSequenceNoLatch( String fileName, int ix, Vector seq, int extraBitsPerVar, int maxGensPerFault  ) throws IOException
    {
        Point[] faultSequence = new Point[ seq.size() ];
        for( int flp = 0; flp < faultSequence.length; flp++ )
        {
            faultSequence[ flp ] = ( Point ) seq.get( flp );
        }
        
        int ISS = 30; double T_SETUP = 0.84;
        //int[] samLen = { 5 }; int[] samPos = { 45 };
        double MIN_MU = 2;      double SIGMA = 0.1;     double MAX_MU = 6;
        boolean sameRndPerGen = false; boolean alwaysResetBeforeRun = false; // Noise 2, 3
        //FitnessFunction inFF = new SimpleCorrelationFitnessFunction();
        FitnessFunction inFF = new SumFitnessFunction();
        FitnessFunction pFF = new PessimisticFitnessFunction( inFF );
        FitnessFunction rFF = new PerRowFitnessFunction( pFF );
        FitnessFunction fitnessFunction = new SingleSampleAvgFitnessFunction( rFF );
        //FitnessFunction fitnessFunction = new SingleNoisySizeSampleAvgFitnessFunction( pFF, 0.7 );
        //FitnessFunction fitnessFunction = new MultipleSampleAvgFitnessFunction( pff, samLen, samPos );
        int repeats = 1;    TestPatternGenerator tpg = new NRepeatsTPG( repeats );
        
        //FitnessFunction fitnessFunction = inFF;
        //TestPatternGenerator tpg = new CompleteShuffledTPG(); // NORMAL
        
        int nrTrials = 16;
        String blifFileName = "/home/mmg20/eh/benchmarks/" + fileName + ".blif";
        ConfigurableRandomInputExperiment experiment = new CombinationalBLIFExperiment( blifFileName, T_SETUP, fitnessFunction, tpg );
        String sisOutputFileName = "/home/mmg20/eh/benchmarks/" + fileName + "L4.sout";
        int resQ = 0;        boolean fpga = false;        boolean voter = false;     boolean varSized = false;
        int LUT_INPUTS = 4;    int DUMP_POP_EVERY = 10000;
        SisOutputReader sor = new SisOutputReader( new File( sisOutputFileName ), resQ, LUT_INPUTS, fpga, voter, extraBitsPerVar, varSized );
        Genotype seed = new Genotype( sor.getVassilevGenotype() );
        int bitsPerVar = sor.getBitsPerVar();        int usedEls = sor.getTotalEls();
        int addEls = ( 1 << bitsPerVar ) - experiment.getNumOfInputs();
        int HOUR = EVALS_PER_SEC * 60 * 60;  int MU_D = 3 * HOUR;   int MU_S = 1 * HOUR;
        MU_D *= 2 * addEls; MU_S *= 2 * addEls;// must multiply these by number of elements in circuit and popsize
        ElementDelayModel delayModel = new DriftingGaussianDelayModel( MIN_MU,MAX_MU,SIGMA,MU_D,MU_S ); // Noise 1
        //ElementDelayModel delayModel = new GaussianDelayModel( MIN_MU, SIGMA ); // mu,s        
        CircuitMapping inMap = new LUTAbsoluteMapping( experiment.getNumOfInputs(), experiment.getNumOfOutputs(), bitsPerVar, LUT_INPUTS, delayModel );
        CircuitMapping circuitMapping = new VassilevMapping( inMap, experiment.getNumOfOutputs(), bitsPerVar );
        SimulatorFaultyCircuit circuit = new SimulatorFaultyCircuit( circuitMapping );
        SimulatorDeployment deployment = new SimulatorDeployment( circuit, alwaysResetBeforeRun );
        int GENOTYPE_MUT = 1; // *** Set to 3 for DECOD, normal is 1.
        int POP_SIZE = 2;  int NUM_OF_ELITES = 1;  double[] RANK_PROBS = { 1,0 };
        int genotypeLength = seed.length(); final Genotype[] SEEDS = { seed };
        GeneticOperator m = new ExactGenotypeMutator( GENOTYPE_MUT );   GeneticOperator[] geneticOps = { m };  double[] opsProbs = { 1 };        
        Selector selector = new RankSelector( RANK_PROBS );
        Evolver evolver = new StandardEvolver( POP_SIZE, genotypeLength, geneticOps, opsProbs, selector, NUM_OF_ELITES, SEEDS );
        InteractionModel inIM = new StandardInteractionModel( evolver, deployment, experiment,ISS );
        InteractionModel nIM = new NoisyIM( inIM, deployment, experiment, 1, NoisyIM.MINIMUM, sameRndPerGen );
        InteractionModel hIM = new HistoryWindowIM( nrTrials, nIM );        
        String dirName = "Repair" + fs + fileName + fs + "Multiple" + fs + ix;
        String repairTimesLog = "times.txt";
        InteractionModel interactionModel = new RepairFaultSequenceIM( hIM, circuit, faultSequence, maxGensPerFault, LOG_BASE_DIR + dirName + fs + repairTimesLog, nrTrials + 1 ); // to fill up history window with solutions
        Monica monica = new Monica( interactionModel, DUMP_POP_EVERY, java.lang.Integer.MAX_VALUE );
        monica.setName( dirName );
        taskQ.add( monica );
        taskQNames.add( dirName );
        ControlLib.writeGNUPlotScript( dirName, logDir, LOG_FILE_NAME );
    }   
    
    
}

⌨️ 快捷键说明

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