我正在学校开设一个数学课程,我选择在旅行推销员问题上做我的,我一直想调查一下。 但是,我的蛮力求解算法存在问题。
* 请转到底部的更新以查看最新版本的代码
如果您知道旅行销售员问题是什么,请跳过此段: 为了尽可能地总结,TSP是这样的:你是一个想要访问某个地区的每个城市的推销员(一个城市基本上是地图上的一个点)。在有界x和y区域中有'n'个城市,每个城市与每个城市相连(通过直线道路)。您需要在城市中找到允许您访问每个城市的最短路径。我想要使用的其中一种算法(我需要测试其他算法)是Brute Force,它检查每条可能的路线并返回最短的路线路线。这并不总是使用的原因是因为它需要我们检查(n-1)!可能的路线,这个数字随着'n'的增加而变大 - 实际上,只有50个城市,那将是608281864034267560872252163321295376887552831379210240000000000路线要检查。
所有关于我们将要使用4个城市的任意区域的帖子的所有示例(即使该算法可以处理n个城市。我们也不关心距离 - 我们想用蛮力击中每一条可能的路线。
这是一个简单的图片演示我正在谈论的内容(我正在开始检查4个城市是否正常运行)
这是Brute Force算法(假设所有其他被调用的方法都正常工作,因为它们可以):
(请在下方查看更多解释)
[代码]
public void BruteForceFindBestRoute(Route r) //Must start r having 1 unflagged city to begin with
{
if(!r.allFlagged() && r.route.size() != m.cities.size())
{
/*STEP 1 Begin with last unflagged city*/
City pivot = r.lastCityAdded();
/*STEP 2: Flag city*/
pivot.visited = true;
/*STEP 3: Find cities "NOT IN ROUTE"*/
ArrayList<City> citiesNotInRoute = new ArrayList<City>();
for(int i = 0; i<m.cities.size(); i++)
{
if(!r.isCityInRoute(m.cities.get(i).name))
{
citiesNotInRoute.add(m.cities.get(i));
}
}
/*STEP 4: Recursively call BruteForceFindBestRoute() using these cities added to the end of our original route*/
for(int i = 0; i<citiesNotInRoute.size(); i++)
{
Route newRoute = r;
newRoute.addToRoute(citiesNotInRoute.get(i));
BruteForceFindBestRoute(newRoute);
}
}
/*STEP 5: If the route is full but the last city isn't flagged, then flag it call BruteForceFindBestRoute() again, with the last city flagged*/
else if(!r.allFlagged() && r.route.size() == m.cities.size())
{
if(r.allFlaggedButLast())
{
Route x = r;
x.flagLastCity();
BruteForceFindBestRoute(x);
}
}
/*STEP 6: If all cities are flagged, the route is full. Check to see if it's the best route.*/
else if(r.allFlagged())
{
if(IsBestRoute(r))
bestRoute = r;
}
else
System.err.println("Error: somehow all cities got flagged, but the route isn't full");
}
这是我的逻辑: (注意:city对象有一个名为“visited”的“flag”布尔变量)
(如果没有标记所有路线,并且路线不包含每个可能的城市)
(如果所有路线都没有标记,但路线包含每个城市)
(否则......这意味着路线会标记每个城市并包含每个可能的城市)
此图片将帮助我解释问题...... 因此程序正确地向左侧移动。然而,在它到达底部之后,人们会期望递归跳回到步骤4,它会这样做。然而,不是R标记城市A而城市B未标记,然后在包含Aflag和B的“新路线”上递归调用自身,R现在包括所有4个城市,并且所有4个都被标记。它失败了,因为它再次将城市D添加到“newRoute”,递归地再次调用自身,并且在另一种方法中我们得到一个数组越界错误,因为我的区域中没有5个城市,但错误地是路线r中的5个城市(A,B,C,d,d)。
Helpful picture of recursive tree structure
问题与在循环中调用递归有关,或者在递归调用中引用'r'。
如果你知道我需要做什么,我会非常感谢你的帮助。
感谢任何能帮助我的人。我会将整个项目发送给愿意提供帮助的任何人。
更新
好吧,所以我试图缩短和简化我的原始方法,这就是我所拥有的:
public void BruteForceFindBestRoute(Route r, ArrayList<City> citiesNotInRoute)
{
if(!citiesNotInRoute.isEmpty())
{
for(int i = 0; i<citiesNotInRoute.size(); i++)
{
City justRemoved = (City) citiesNotInRoute.remove(0).clone();
Route newRoute = (Route) r.clone();
newRoute.addToRoute(justRemoved);
BruteForceFindBestRoute(newRoute, citiesNotInRoute);
citiesNotInRoute.add(justRemoved);
}
}
else //if(citiesNotInRoute.isEmpty())
{
if(IsBestRoute(r))
bestRoute = r;
}
}
问题在于,当我们突破递归时,for循环中的变量i似乎失去了它的含义,并且循环不会继续。想法?
答案 0 :(得分:5)
您应该在递归调用返回后从路由中删除城市。你这样做:
Route newRoute = r;
newRoute.addToRoute(citiesNotInRoute.get(i));
BruteForceFindBestRoute(newRoute);
但绝不是newRoute.removeFromRoute
或类似的。
请注意,您正在编写Java,而在Java中,对象的分配是通过引用完成的。这意味着r
和newRoute
是同一个对象。 newRoute
不是您所期望的r
的独立副本。因此,对newRoute
的任何修改也会更改r
。您可能希望明确地复制您的路线。 Java术语是clone。确保您的克隆足够深,即克隆所有相关数据结构,而不是在原始克隆和克隆之间共享它们。
注意:在很多地方你可以让你的代码更有效率,但是在任何情况下蛮力都远没有效率,你只谈论几个城市,也许你不要我得关心。但是,如果您想调查替代方案,请考虑维护所有未访问城市的单个链接列表。每个调用都会遍历该列表,删除一个元素,递归并将元素放回去。无需在每次调用时从头开始构建此列表。 dancing links的概念可以巧妙地应用于此任务,作为预制链表实现的替代方案。
修改强>
以下代码变体适用于我:
import java.util.*;
class SO11703827 {
private static ArrayList<Integer> bestRoute;
public static void bruteForceFindBestRoute
(ArrayList<Integer> r,
ArrayList<Integer> citiesNotInRoute)
{
if(!citiesNotInRoute.isEmpty())
{
for(int i = 0; i<citiesNotInRoute.size(); i++)
{
Integer justRemoved =
(Integer) citiesNotInRoute.remove(0);
ArrayList<Integer> newRoute =
(ArrayList<Integer>) r.clone();
newRoute.add(justRemoved);
bruteForceFindBestRoute(newRoute, citiesNotInRoute);
citiesNotInRoute.add(justRemoved);
}
}
else //if(citiesNotInRoute.isEmpty())
{
if(isBestRoute(r))
bestRoute = r;
}
}
private static boolean isBestRoute(ArrayList<Integer> r) {
System.out.println(r.toString());
return false;
}
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<Integer>();
for (int i = 0; i < 6; ++i)
lst.add(i);
ArrayList<Integer> route = new ArrayList<Integer>();
bruteForceFindBestRoute(route, lst);
}
}