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

📄 mealyfsmnode.java~

📁 Java遗传算法库
💻 JAVA~
字号:
/*
 * MealyFSMNode.java
 *
 * Created on May 30, 2003, 2:36 PM
 */

package es.pj.circuits.experiment;

import java.util.Vector;
import java.util.Random;

/** A Node in a Mealy Finite State Machine.  Provides functions for
 * walking along a graph of the FSM.
 * @author  Michael Garvie
 * @version 
 */
public class MealyFSMNode implements java.io.Serializable {

    public MealyFSMEdge[] nextStates;
    public Vector vNextStates; // pub for testing
    protected Random rnd;
    protected String name = "";
    
    public MealyFSMNode(int nrInputs, Random rnd, String name) {
        this( nrInputs, rnd );
        this.name = name;
    }
    
    public MealyFSMNode(int nrInputs, Random rnd) {
        this( nrInputs );
        this.rnd = rnd;
    }
    
    /** Creates new MealyFSMNode */
    public MealyFSMNode(int nrInputs) {
        rnd = new Random();
        nextStates = new MealyFSMEdge[ 1 << nrInputs ];
    }
    
    public int addEdge( MealyFSMNode dest, String ins, String outs )
    {
        return addEdge( dest, ins, outs, false );
    }
        
    public int addEdge( MealyFSMNode dest, String ins, String outs, boolean permanent )
    {
        int rv = 0;
        if( ins.indexOf( "-" ) >=0 )
        {
            int[] xPos = getDontCarePos( ins );
            int xs = xPos.length;
            int totalNewEdges = 1 << xs;
            for( int c = 0; c < totalNewEdges; c++ )
            {
                String xVals = es.ESLib.int2BinStr( c, xs );
                char[] newIns = ins.toCharArray();
                for( int dcl = 0; dcl < xs; dcl++ )
                {
                    newIns[ xPos[ dcl ] ] = xVals.charAt( dcl );
                }
                rv += addEdge( dest, new String( newIns ), outs, permanent );
                permanent = false;
            }
        }else
        {
            int inpCode = Integer.parseInt( ins, 2 );
            MealyFSMEdge newEdge = new MealyFSMEdge( dest, ins, outs );
            if( nextStates[ inpCode ] == null )
            {
                rv = 1;
            }else
            {
                rv = 0;
            }
            nextStates[ inpCode ] = newEdge;
            newEdge.permanent = permanent;
        }
        return rv;
    }
    
    
    public void resetWalk()
    {
        vNextStates = new Vector();
        MealyFSMEdge currEdge;
        for( int nl = 0; nl < nextStates.length; nl++ )
        {
            currEdge = nextStates[ nl ];
            if( currEdge != null )
            {
                currEdge.walked = false;
                vNextStates.add( currEdge );
            }
        }
    }
    
    public MealyFSMEdge pick()
    {
        return ( MealyFSMEdge ) vNextStates.get( rnd.nextInt( vNextStates.size() ) );
    }
    
    public boolean remove( MealyFSMEdge which )
    {
        if( !which.permanent )
        {
            vNextStates.remove( which );
            return true;
        }else
        {
            if( which.walked )
            {
                return false;
            }else
            {
                which.walked = true;
                return true;
            }
        }
    }
    
    protected static int[] getDontCarePos( String ins )
    {
        int howMany = 0;
        for( int ll = 0; ll < ins.length(); ll++ )
        {
            if( ins.charAt( ll ) == '-' )
            {
                howMany++;
            }
        }
        int[] rv = new int[ howMany ];
        for( int ll = 0; ll < ins.length(); ll++ )
        {
            if( ins.charAt( ll ) == '-' )
            {
                rv[ --howMany ] = ll;
            }
        }
        return rv;
    }
    
    public int getNumOfEdges()
    {
        int rv = 0;
        for( int sl = 0; sl < nextStates.length; sl++ )
        {
            if( nextStates[ sl ] != null )
            {
                rv++;
            }
        }
        return rv;
    }
}

⌨️ 快捷键说明

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