Skip to content

Commit 9e16a98

Browse files
author
linyiqun
committed
蚂蚁类添加当前路径包含子路径方法
蚂蚁类添加当前路径包含子路径方法
1 parent 5c08a1d commit 9e16a98

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

Others/DataMining_ACO/Ant.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* @author lyq
99
*
1010
*/
11-
public class Ant implements Comparable<Ant>{
12-
//蚂蚁当前所在城市
11+
public class Ant implements Comparable<Ant> {
12+
// 蚂蚁当前所在城市
1313
String currentPos;
1414
// 蚂蚁遍历完回到原点所用的总距离
1515
Double sumDistance;
@@ -51,41 +51,72 @@ public double calSumDistance() {
5151

5252
return sumDistance;
5353
}
54-
54+
5555
/**
5656
* 蚂蚁选择前往下一个城市
57+
*
5758
* @param city
58-
* 所选的城市
59+
* 所选的城市
5960
*/
60-
public void goToNextCity(String city){
61+
public void goToNextCity(String city) {
6162
this.currentPath.add(city);
6263
this.currentPos = city;
6364
this.nonVisitedCitys.remove(city);
6465
this.visitedCitys.add(city);
6566
}
66-
67+
6768
/**
6869
* 判断蚂蚁是否已经又重新回到起点
70+
*
6971
* @return
7072
*/
71-
public boolean isBack(){
73+
public boolean isBack() {
7274
boolean isBack = false;
7375
String startPos;
7476
String endPos;
75-
76-
if(currentPath.size() == 0){
77+
78+
if (currentPath.size() == 0) {
7779
return isBack;
7880
}
79-
81+
8082
startPos = currentPath.get(0);
81-
endPos = currentPath.get(currentPath.size()-1);
82-
if(currentPath.size() > 1 && startPos.equals(endPos)){
83+
endPos = currentPath.get(currentPath.size() - 1);
84+
if (currentPath.size() > 1 && startPos.equals(endPos)) {
8385
isBack = true;
8486
}
85-
87+
8688
return isBack;
8789
}
8890

91+
/**
92+
* 判断蚂蚁在本次的走过的路径中是否包含从城市i到城市j
93+
*
94+
* @param cityI
95+
* 城市I
96+
* @param cityJ
97+
* 城市J
98+
* @return
99+
*/
100+
public boolean pathContained(String cityI, String cityJ) {
101+
String lastCity;
102+
String currentCity;
103+
boolean isContained = false;
104+
105+
for (int i = 0; i < currentPath.size() - 1; i++) {
106+
lastCity = currentPath.get(i);
107+
currentCity = currentPath.get(i + 1);
108+
109+
// 如果某一段路径的始末位置一致,则认为有经过此城市
110+
if ((lastCity.equals(cityI) && currentCity.equals(cityJ))
111+
|| (lastCity.equals(cityJ) && currentCity.equals(cityI))) {
112+
isContained = true;
113+
break;
114+
}
115+
}
116+
117+
return isContained;
118+
}
119+
89120
@Override
90121
public int compareTo(Ant o) {
91122
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)