IDA星型算法实现无限循环

时间:2013-03-12 19:29:13

标签: objective-c algorithm a-star

我正在尝试在目标c中实现IDA星型算法。一些如何制作无限循环。这是代码

- (BOOL)IDAStarSolution:(NPuzzleState *)startState goal:(NPuzzleState *)goalState {
    int limit = [startState heuristicCostEstimateWith:goalState];
    bestSolution = nil;
    NPuzzleState *result = nil;

    while (!result) {
        [self visitedSetAddState:startState];
        newLimit = 99999;
        result = [self depthLimitSearch:startState goal:goalState costLimit:limit];
        limit = newLimit;
        [self.visitedSet removeAllObjects];
    }

    return TRUE;
}

- (NPuzzleState *)depthLimitSearch:(NPuzzleState *)current goal:(NPuzzleState *)goal costLimit:(int)currentCostBound {
    NSArray *neighbors = [current neightborStates];
    for (NPuzzleState *s in neighbors) {
        if ([s isEqualToState:goal]) {
            s.parentState = current;
            return s;
        }

        if (![self visitedSetContains:s]) {
            s.gScore = current.gScore + 1;
            s.parentState = current;

            int currentCost = [s generalCostEstimateWith:current] + [s heuristicCostEstimateWith:goal];
            if (currentCost <= currentCostBound) {
                [self visitedSetAddState:s];

                NPuzzleState *solution = [self depthLimitSearch:s goal:goal costLimit:currentCostBound];
                if (!solution && (!bestSolution || [solution generalCostEstimateWith:current] < [bestSolution generalCostEstimateWith:bestSolution])) {
                    bestSolution = solution;
                } else {
                    if (currentCost < newLimit) {
                        NSLog(@"new limit %d", newLimit);
                        newLimit = currentCost;
                    }
                }
            }
        }
    }

    return nil;
}

0 个答案:

没有答案