목표 C에서 15 가지 퍼즐 문제를 해결하기 위해 IDA * 알고리즘을 구현하려고합니다. 알고리즘을 잘 구현했지만 정확한 결과를 얻지 못했습니다. 다음은 내 코드입니다.맨해튼의 휴리스틱과 함께 IDA * 알고리즘의 올바른 구현은 무엇입니까?
- (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;
}
depthLimitedSearch는 항상 null을 반환하고 동일한 노드를 반복하여 확장합니다. 내가 잘못 한 것은 제안하시기 바랍니다. 내가 최적의 비용보다 더 많은 제한을 설정 한 경우 Iterative Deepening A Star (IDA*) to solve n-puzzle (sliding puzzle) in Java
것은 내가 지금 bestSolution를 반환하고 따라 다음 그것이 최적하지 않은, 처음으로 발견 된 솔루션을 반환
구현에 주어진 자바 코드와 유사하다. 지금 무엇을해야할까요?
으로 바꿀 수 있습니다. depthLimitSearch의 최종 반환 값은 nil이므로 재귀를 끝내면 항상 nil이됩니다. 당신의 다른 반환은 논평된다 – Paulw11
그것은 저것을 이용하고 있지 않기 때문에 나는 bestSolution를 돌려 보내기 위하여 반환 nil를, 대체했다 논평된다. 이것은 IDA * 알고리즘에 따라 괜찮습니까? –
도움이 될 것입니다. 귀하의 솔루션이 즉시 발견되지 않는 한 그 방법은 무익합니다. – Paulw11