package aco.ant; import java.util.ArrayList; import util.RouletteWheel;//引入轮盘类
import aco.ACO;//引入蚁群类 /**
* The AS Ant Class
*
* @author Thiago Nascimento
* @since 2014-07-27
* @version 1.0
*/
public class Ant4AS extends Ant { /*父类的构造函数*/
public Ant4AS(ACO aco) {
super(aco);
}
/*实现父类的explore结口,完成构造路径的工作*/
@Override
public void explore() {
while (!nodesToVisit.isEmpty()) {
int nextNode = doExploration(currentNode);//找到下一个访问的节点 //Save next node
tour.add(new Integer(nextNode));//将下一个节点的编号加入路径
path[currentNode][nextNode] = 1;//将标志位矩阵中当前节点与下一节点间的边的访问标志置为1
path[nextNode][currentNode] = 1;//将标志位矩阵中下一节点与当前节点间的边的访问标志置为1 aco.p.updateTheMandatoryNeighborhood(this);//更新领域 currentNode = nextNode;//把下一节点作为当前节点
}
} /**
* 返回下一个访问节点的编号
*
* @param i :当前节点的编号
* @return 下一节点的编号,该函数是利用轮盘赌的方法选择下一个要访问的节点
*/
protected int doExploration(int i) {
int nextNode = -1;
double sum = 0.0; // Update the sum
for (Integer j : nodesToVisit) {
if (aco.getTau(i, j) == 0.0) {
throw new RuntimeException("tau == 0.0");
} double tij = Math.pow(aco.getTau(i, j), ALPHA);
double nij = Math.pow(aco.p.getNij(i, j), BETA);
sum += tij * nij;
} if (sum == 0.0) {
throw new RuntimeException("sum == 0.0");
} double[] probability = new double[aco.p.getNodes()];
double sumProbability = 0.0; for (Integer j : nodesToVisit) {
double tij = Math.pow(aco.getTau(i, j), ALPHA);
double nij = Math.pow(aco.p.getNij(i, j), BETA);
probability[j] = (tij * nij) / sum;
sumProbability += probability[j];
} // Select the next node by probability
nextNode = RouletteWheel.select(probability, sumProbability); if (nextNode == -1) {
throw new RuntimeException("nextNode == -1");
} nodesToVisit.remove(new Integer(nextNode)); return nextNode;
} @Override 实现父类的clone接口,复制ant对象
public Ant clone() {
Ant ant = new Ant4AS(aco);
ant.id = id;
ant.currentNode = currentNode;
ant.tourLength = tourLength;
ant.tour = new ArrayList<Integer>(tour);
ant.path = path.clone();
return ant;
}
}