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

📄 queen.java

📁 Java source code for the Ant Colony Optimization Problem.
💻 JAVA
字号:
package jwo.jpss.ants;     // Part of the ant simulation package.
import java.awt.*;		   // For drawing the queen ant.
import jwo.jpss.spatial.*; // For spatial footprint.
import java.util.*;        // For dynamic collections.

//  *******************************************************
/** Class for defining Queen ants.
  * @author Jo Wood
  * @version 1.4, 10th October, 2001.
  */
//  *******************************************************

public class Queen extends Ant		
{      
    // ------------------ Class variables --------------------

    private static final int QUEEN_WIDTH    = 15;   // Width of queen ant.
    private static final int QUEEN_HEIGHT   = 9;    // Height of queen ant.
    private static final int METABOLIC_RATE = 2;    // Rate at which ant consumes food.
    private static final int FEED_RATE      = 200;  // Food per cycle that can be eaten.
    private static final int NESTING_LEVEL  = 20000;// Food level at which queen nests.

    // --------------------- Constructors --------------------
        
    /** Creates a queen with similar characteristics of the given
      * ant.
      * @param foodLevel Initial food level of the ant.
      * @param parent Ant supplying inheritable characteristics.
      * @param x Initial x location of the ant.
      * @param y Initial y location of the ant.
      * @param nest Nest in which this ant was born.
      */
    public Queen(int foodLevel, Ant parent, float x, float y, Nest nest)
    {
        super(foodLevel,parent,x,y,nest);
	
        // Make queen larger than worker ant.
        setBounds(new Footprint(x,y,QUEEN_WIDTH,QUEEN_HEIGHT));

    	// Queens are just like normal ants, but travel faster and are less
    	// likely to move in circles.
        straightSteps *=2;
        changeAntDirection();
    }
    
    // ------------------ Overridden  Methods ------------------
    
    /** Draws the ant using the given graphics context.
      * @param g Graphics context to draw to.
      */
    public void paint(Graphics g)
    {
        Footprint fp = getBounds();
        int x = Math.round(fp.getXOrigin());
        int y = Math.round(fp.getYOrigin());
        int width = Math.round(fp.getMERWidth());
        int height = Math.round(fp.getMERHeight());

        g.setColor(colour);	   
        g.fillOval(x,y,width,height);  
    }
        
    /** Let the ant go about its business for one time unit.
      */
    public void evolve()
    {    
        metabolise(METABOLIC_RATE);	

        if (isAlive())
    	{   
    	    // Find out if the ant is sitting on anything. 
            Enumeration enum = gListener.objectsAt(this).elements();    	
            while (enum.hasMoreElements())
            {
                SpatialObject spObject = (SpatialObject) enum.nextElement();
 
                // If queen is sitting on some food, eat it.
                if (spObject instanceof FoodSource)
                {
                    // Attempt to eat food at feeding rate
                    FoodSource foodSource = (FoodSource)spObject;
    	    	    int foodRemoved = foodSource.removeFood(FEED_RATE);

                    if (foodRemoved > 0)
                    {
                        eat(foodRemoved);  
                        return;  	
                    }
                }
    	    	
                // If queen's food level is high, set up a new nest.
                if ((spObject instanceof Garden) && 
                    (getFoodLevel() >NESTING_LEVEL))
                {
                    Garden garden = (Garden)spObject;

                    // Return straight steps back to normal ant levels.
                    straightSteps /= 2;

    	    	    // Add the new nest to the garden.  	    	    
                    garden.requestNewNest(new Nest(this));    	    	    
    	    	    
                    // Kill of this queen (it has now become part of the nest instead).
                    setAlive(false);   
                }
    	    }
    	    	    
            // Change direction if any has walked sufficient steps.
            if (numSteps%straightSteps == 0)
                changeAntDirection();    	    	

            move(xDir,yDir);

            // Only move if within bounds.
            if (gListener.canDraw(this))
                numSteps++;
            else
            {
                move(-xDir,-yDir);  
                changeAntDirection();
            }
        }
    }
    
    // ---------------------- Private Methods ----------------------
        
    /** Sets a new direction for queen to walk in.
      */
    private void changeAntDirection()
    {   
        // Queen chooses random direction and never returns home.
        xDir = (float)(Math.random()*10-5);
        yDir = (float)(Math.random()*10-5);
    }
}

⌨️ 快捷键说明

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