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

📄 ant.java

📁 MASON代表多主体邻里或网络仿真(Multi-Agent Simulator of Neighborhoods or Networks)。它是乔治梅森大学用Java开发的离散事件多主体仿真核心库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                if( x < grid.field.length-1 && y < grid.field[x].length-1 )                    amount = /*Strict*/Math.max( /*Strict*/Math.max( grid.field[x+1][y+1]-subtractingRatio, minPheromone ), amount );                grid.field[x][y] = amount;                pheromoneRatio = amount;                break;            }        }    public DecisionInfo decideGreedyAction( final SimState state, final int myx, final int myy, final int orientation )        {        final AntsForage af = (AntsForage)state;        final DecisionMaker decisionMaker = af.decisionMaker;        decisionMaker.reset();        // add all neighboring cells and move to one of them        addInformation( state, myx,   myy+1, 0 );        addInformation( state, myx+1, myy+1, 1 );        addInformation( state, myx+1, myy,   2 );        addInformation( state, myx+1, myy-1, 3 );        addInformation( state, myx,   myy-1, 4 );        addInformation( state, myx-1, myy-1, 5 );        addInformation( state, myx-1, myy,   6 );        addInformation( state, myx-1, myy+1, 7 );        if( hasFoodItem )            return decisionMaker.getHomeGreedyDecision( state );        else            return decisionMaker.getFoodGreedyDecision( state );        }    public void step( final SimState state )        {        final AntsForage af = (AntsForage)state;        final DecisionMaker decisionMaker = af.decisionMaker;                Int2D location = af.buggrid.getObjectLocation(this);        int myx = location.x;        int myy = location.y;        if( justCreated )            {            DecisionInfo temp = decideGreedyAction( state, myx, myy, orientation );            if( temp == null )                return;            orientation = temp.orientation;            justCreated = false;            }        final int START=-1;        int bestx, besty, besto;        DecisionInfo movingDecision = null;        if( hasFoodItem )            movingDecision = decideGreedyAction( state, myx, myy, orientation );        else            {            decisionMaker.reset();            addInformation( state, myx,   myy+1, 0 );            addInformation( state, myx+1, myy+1, 1 );            addInformation( state, myx+1, myy,   2 );            addInformation( state, myx+1, myy-1, 3 );            addInformation( state, myx,   myy-1, 4 );            addInformation( state, myx-1, myy-1, 5 );            addInformation( state, myx-1, myy,   6 );            addInformation( state, myx-1, myy+1, 7 );            int max = 0;            int howMany = 1;            for( int i = 1 ; i < decisionMaker.numInfos ; i++ )                if( decisionMaker.info[max].foodPheromoneAmount ==                    decisionMaker.info[i].foodPheromoneAmount )                    {                    howMany++;                    }                else if( decisionMaker.info[max].foodPheromoneAmount <                         decisionMaker.info[i].foodPheromoneAmount )                    {                    max = i;                    howMany = 1;                    }            if( howMany == 1 )                movingDecision = decideGreedyAction( state, myx, myy, orientation );            else                movingDecision = decideAction( state, myx, myy, orientation );            }        if( movingDecision == null )            {            movingDecision = decideGreedyAction( state, myx, myy, orientation );            if( movingDecision == null )                {                bestx = myx;                besty = myy;                besto = orientation;                }            else                {                bestx = movingDecision.position.x;                besty = movingDecision.position.y;                besto = movingDecision.orientation;                }            }        else            {            bestx = movingDecision.position.x;            besty = movingDecision.position.y;            besto = movingDecision.orientation;            }        if( ( bestx != myx || besty != myy ))            {            // add some pheromones            if( hasFoodItem )                {                addPheromone(af.toFoodGrid,myx,myy,(/*pheromoneToLeaveBehind*/pheromoneRatio));                }            else                {                addPheromone(af.toHomeGrid,myx,myy,(/*pheromoneToLeaveBehind*/pheromoneRatio));                }            if( bestx != myx && besty != myy )                pheromoneRatio -= subtractingRatio*1.4142;            else                pheromoneRatio -= subtractingRatio;            if( pheromoneRatio < 0 )                {                die( state );                return;                }            // adjust the position of the agent, and then deposit the "to food" and "to home" pheromones            af.buggrid.setObjectLocation(this,bestx,besty);            orientation = besto;            if( ( besty >= AntsForage.HOME_YMIN ) && ( besty <= AntsForage.HOME_YMAX ) &&                ( bestx >= AntsForage.HOME_XMIN ) && ( bestx <= AntsForage.HOME_XMAX ) )                {                if( hasFoodItem )                    {                    af.foodCollected++;                    hasFoodItem = false;                    pheromoneRatio = 1.0 * maxPheromone;                    if( GREEDY_REPOSITIONING )                        {                        // pick greediest orientation!                        DecisionInfo temp = decideGreedyAction( state, myx, myy, orientation );                        if( temp != null )                            orientation = temp.orientation;                        else                            orientation = (orientation+4)%8;                        }                    else                        orientation = (orientation+4)%8; // rotate 180                    }                }            else if(  ( besty >= AntsForage.FOOD_YMIN ) && ( besty <= AntsForage.FOOD_YMAX ) &&                      ( bestx >= AntsForage.FOOD_XMIN ) && ( bestx <= AntsForage.FOOD_XMAX ) )                {                if( !hasFoodItem )                    {                    hasFoodItem = true;                    pheromoneRatio = 1.0 * maxPheromone;                    if( GREEDY_REPOSITIONING )                        {                        // pick greediest orientation!                        DecisionInfo temp = decideGreedyAction( state, myx, myy, orientation );                        if( temp != null )                            orientation = temp.orientation;                        else                            orientation = (orientation+4)%8;                        }                    else                        orientation = (orientation+4)%8; // rotate 180                    }                }            timeToLive--;            if( timeToLive <= 0 )                {                die( state );                return;                }            }        }    // a few tweaks by Sean    private Color noFoodColor = Color.black;    private Color foodColor = Color.red;    public final void draw(Object object, Graphics2D graphics, DrawInfo2D info)        {        if( hasFoodItem )            graphics.setColor( foodColor );        else            graphics.setColor( noFoodColor );        // this code was stolen from OvalPortrayal2D        int x = (int)(info.draw.x - info.draw.width / 2.0);        int y = (int)(info.draw.y - info.draw.height / 2.0);        int width = (int)(info.draw.width);        int height = (int)(info.draw.height);        graphics.fillOval(x,y,width, height);        }        public Stoppable toDiePointer = null;    public void die( final SimState state )        {        AntsForage antsforage = (AntsForage)state;        antsforage.numberOfAnts--;        antsforage.buggrid.remove( this );        if(toDiePointer!=null) toDiePointer.stop();        }    }

⌨️ 快捷键说明

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