Ios IDA*算法的曼哈顿启发式正确实现是什么?
我正在尝试实现IDA*算法来解决目标C中的15个难题,我已经很好地实现了该算法,但没有得到准确的结果。这是我的密码-Ios IDA*算法的曼哈顿启发式正确实现是什么?,ios,objective-c,puzzle,heuristics,Ios,Objective C,Puzzle,Heuristics,我正在尝试实现IDA*算法来解决目标C中的15个难题,我已经很好地实现了该算法,但没有得到准确的结果。这是我的密码- - (TreeNode *)IDAStarSolution:(TreeNode *)startState { int limit = 5;//2*[startState fx]; appDelegate.CN=[NSNumber numberWithInt:limit]; TreeNode *result=nil; while (result==nil) { [ap
- (TreeNode *)IDAStarSolution:(TreeNode *)startState {
int limit = 5;//2*[startState fx];
appDelegate.CN=[NSNumber numberWithInt:limit];
TreeNode *result=nil;
while (result==nil) {
[appDelegate.closedList addObject:startState];
newLimit = 99999;
result = [self depthLimitSearch:startState costLimit:limit];
limit = newLimit;
appDelegate.CN=[NSNumber numberWithInt:newLimit];
[appDelegate.closedList removeAllObjects];
}
return result;
}
- (TreeNode*)depthLimitSearch:(TreeNode *)current costLimit:(int)currentCostBound {
NSArray *neighbors =[current expandNodeToChilds];
for (TreeNode *s in neighbors) {
if ([s.puzzleBox isFinalStateBox]) {
appDelegate.result=s.moveString;
return s;
}
if (![self closedSetContains:s]) {
int currentCost = [s.cost intValue] + [s.puzzleBox.manhattanDistance intValue];
if (currentCost <= currentCostBound) {
[appDelegate.closedList addObject:s];
[s.puzzleBox displayPuzzleBox];
TreeNode *solution = [self depthLimitSearch:s costLimit:currentCostBound];
if (solution!=nil&& (bestSolution ==nil|| [solution.cost intValue] < [bestSolution.cost intValue])) {
bestSolution = solution;
//return solution;
}
}else {
if (currentCost < newLimit) {
NSLog(@"new limit %d", currentCost);
newLimit = currentCost;
}
}
}
}
return bestSolution;
}
-(BOOL)closedSetContains:(TreeNode *)node{
for (TreeNode *tNode in appDelegate.closedList) {
if (tNode==node) {
return YES;
}
}
return NO;
}
-(TreeNode*)IDAStarSolution:(TreeNode*)startState{
int limit=5;//2*[startState fx];
appDelegate.CN=[NSNumber numberwhithint:limit];
TreeNode*结果=零;
while(结果=nil){
[appDelegate.closedList addObject:startState];
newLimit=99999;
结果=[self-depthLimitSearch:startState costLimit:limit];
限制=新限制;
appDelegate.CN=[NSNumber numberwhithint:newLimit];
[appDelegate.closedList removeAllObjects];
}
返回结果;
}
-(TreeNode*)深度限制搜索:(TreeNode*)当前成本限制:(int)当前成本限制{
NSArray*邻居=[current expandNodeToChilds];
用于(邻域中的TreeNode*s){
如果([s.Puzzbox是FinalStateBox]){
appDelegate.result=s.moveString;
返回s;
}
如果(![self-closedSetContains:s]){
int currentCost=[s.cost intValue]+[s.puzzbox.manhattanDistance intValue];
如果(currentCost我认为您的变异/枚举问题是由closedSetContains
方法的结构造成的。您正在枚举appDelegate.closedList
,但从枚举的中间返回。我怀疑当您修改appDelegate.closedList
时,会导致错误,因为它看起来是ike枚举仍处于活动状态。您应该能够通过使用调试器中的数组appDelegate.closedList
检查异常中的数组引用来确认这一点
将您的closedSetContains
更改为此-
-(BOOL)closedSetContains:(TreeNode *)node{
BOOL ret=NO;
for (TreeNode *tNode in appDelegate.closedList) {
if (tNode==node) {
ret=YES;
break;
}
}
return ret;
}
可能会有帮助。但是如果appDelegate.closedList
是一个NSMutableArray,您可以用[appDelegate.closedList containsObject:node]替换整个方法
depthLimitSearch的最终返回值为零,因此当您退出递归时,您总是会得到零。您的另一个返回值是注释的,因为我没有使用该返回值,所以我将返回零替换为返回最佳解。根据IDA*算法,这可以吗?这应该会有帮助。除非您r解决方案立即被发现,您将得到nilNow,因为在枚举时发生了变异。我认为这是针对邻居的,但为什么?因为我现在返回的是最佳解决方案,如果我设置的限制超过最佳成本,那么它将返回找到的第一个解决方案,这不是最佳的。现在该怎么办?谢谢,这是最简单的方法。