JavaACOFramework的各个类介绍(part3 : Ant4ACS类)

package aco.ant;

import java.util.ArrayList;

import sys.Settings;
import util.PseudoRandom;
import aco.ACO;


public class Ant4ACS extends Ant4AS {
    
    /** 进行Exploitation的概率 */
    protected double Q0 = Settings.Q0;
    
    /** 信息素挥发的概率*/
    protected double P = Settings.P;
    
    public Ant4ACS(ACO aco) {
        super(aco);        
    }
    /*实现了父类的explore操作的接口*/
    @Override
    public void explore() {
        while (!nodesToVisit.isEmpty()) {
            int nextNode = -1;

            if (PseudoRandom.randDouble(0, 1) <= Q0) {
                nextNode = doExploitation(currentNode);//Exploitation操作
            } else {
                nextNode = doExploration(currentNode);//Exploration操作
            }

            localUpdateRule(currentNode, nextNode);//挥发连接当前节点和下一个节点边的信息素

            // Save next node
            tour.add(new Integer(nextNode));//将下一个节点存入蚂蚁的路径列表
            path[currentNode][nextNode] = 1;//将当前节点与下一节点间的边标记为已访问过
            path[nextNode][currentNode] = 1;//将下一节点与当前节点间的边标记为已访问过

            aco.p.updateTheMandatoryNeighborhood(this);//更新邻域

            currentNode = nextNode;//将下一节点作为当前节点
        }
    }
/* 信息素挥发 */
    protected void localUpdateRule(int i, int j) {
        double evaporation = (1.0 - P) * aco.getTau(i, j);
        double deposition = P * aco.p.getT0();
        
        aco.setTau(i, j, evaporation + deposition);
        aco.setTau(j, i, evaporation + deposition);    
    }
/*该操作选择和当前节点i相邻的所有未被访问过的节点中tij * nij值最高的节点作为下一个节点*/
    protected int doExploitation(int i) {
        int nextNode = -1;
        double maxValue = Double.MIN_VALUE;
        
        // Update the sum
        for (Integer j : nodesToVisit) {
            if (aco.getTau(i, j) == 0.0) {
                throw new RuntimeException("tau == 0.0");
            }

            double tij = aco.getTau(i, j);
            double nij = Math.pow(aco.p.getNij(i, j), BETA);
            double value = tij * nij;
            
            if(value > maxValue){
                maxValue = value;
                nextNode = j;
            }
        }
        
        if (nextNode == -1) {
            throw new RuntimeException("nextNode == -1");
        }

        nodesToVisit.remove(new Integer(nextNode));

        return nextNode;
    }

    @Override
/*实现父类的clone接口,复制蚂蚁*/
public Ant clone() { Ant ant = new Ant4ACS(aco); ant.id = id; ant.currentNode = currentNode; ant.tourLength = tourLength; ant.tour = new ArrayList<Integer>(tour); ant.path = path.clone(); return ant; } }

 

posted @ 2016-07-26 18:39  victorchew  阅读(285)  评论(0)    收藏  举报