Example #1
0
def AStarSearch(initBoard, boardSize):
    states = []
    attack = queens.attacking(initBoard)
    PreviousBoard = [initBoard, attack, 0]
    closedStates = []
    node = 0
    while 1:
        for i in range(boardSize):
            y = PreviousBoard[0][i].row
            for j in range(boardSize):
                if y == j:
                    continue
                states.append(copy.deepcopy(PreviousBoard))
                states[-1][0][i].up(j - y)
                attack = queens.attacking(states[-1][0])
                cost = abs(j - y) * states[-1][0][i].weight**2
                states[-1][1] = attack
                states[-1][2] = cost + PreviousBoard[2]
                if attack == 0:
                    return states[-1]
        allCost = [_[1] * 100 + _[2] for _ in states]
        while 1:
            minCost = min(allCost)
            node = allCost.index(minCost)
            x = list(_.row for _ in states[node][0])
            if x in closedStates:
                states.pop(node)
                allCost.pop(node)
            else:
                closedStates.append(x)
                break
        print(minCost)
        #queens.chessBoard(states[node][0],boardSize)
        PreviousBoard = states[node]
        states.pop(node)
Example #2
0
def ids(queenList, depth):
    orignQueens = queenList.copy()
    for depth in range(depth + 1):
        state = [0]
        idsPrpcess(orignQueens, depth, state)
        if (queens.attacking(queenList) == 0):
            return
Example #3
0
def movesForAllQueens(AllQueens):
    boards = []
    moveCost = []
    stateCost = []

    boardSize = AllQueens[0].boardSize

    for index in range(0, len(AllQueens)):
        row = AllQueens[index].row
        # queens.chessBoard(AllQueens, 10)
        # loop the column
        for i in range(1, row + 1):
            # down
            AllQueensCopy = copy.deepcopy(AllQueens)

            AllQueensCopy[index].down(i)

            if noSamePosition(AllQueensCopy[index], AllQueensCopy):
                move_cost = AllQueensCopy[index].weight * i
                state_cost = AllQueensCopy[
                    index].weight * i + queens.attacking(AllQueensCopy) * 100

                boards.append(AllQueensCopy)
                moveCost.append(move_cost)
                stateCost.append(state_cost)
                # queens.chessBoard(AllQueensCopy, 10)

        for j in range(1, boardSize - row):  # up

            AllQueensCopy = copy.deepcopy(AllQueens)
            AllQueensCopy[index].up(j)

            if noSamePosition(AllQueensCopy[index], AllQueensCopy):
                move_cost = AllQueensCopy[index].weight * j
                state_cost = AllQueensCopy[
                    index].weight * j + queens.attacking(AllQueensCopy) * 100

                boards.append(AllQueensCopy)
                moveCost.append(move_cost)
                stateCost.append(state_cost)
                # queens.chessBoard(AllQueensCopy, 10)

    return boards, np.array(moveCost), np.array(stateCost)
Example #4
0
def hillCliming(Queens, temperature=10):
    # Queens = queens.generateQueens(boardSize, True)
    initialStateCost = queens.attacking(Queens) * 100

    # for round in range(0,10):

    init_T = temperature
    Time = 0
    initialState = copy.deepcopy(Queens)

    previousTotalCost = initialStateCost
    loss_function.append(previousTotalCost) if len(
        loss_function
    ) == 0 or loss_function[-1] > previousTotalCost else loss_function.append(
        loss_function[-1])
    time_function.append(time.time() - start_time)
    previousTotalMoveCost = 0
    previousState = copy.deepcopy(initialState)
    cost_record = []

    while True:
        cost_record.append(previousTotalCost)
        T = np.exp(-Time / 2) * temperature

        boards, moveCost, stateCost = movesForAllQueens(previousState)
        totalCost = stateCost + previousTotalMoveCost
        # decision based on the difference between total cost on this state and the total cost on previous state
        delta = totalCost - previousTotalCost
        # print(np.min(delta))
        if np.min(delta) >= 0:
            return cost_record, previousState

        delta_T = delta / T
        delta_T[delta_T >= 100] = 100
        # print(delta_T)
        acceptRate = 1 / (1 + np.exp(delta_T))

        decision_index = random.choices(list(range(len(boards))),
                                        acceptRate)[0]

        thisBoard = boards[decision_index]
        thisMoveCost = moveCost[decision_index]
        thisStateCost = stateCost[decision_index]

        previousState = thisBoard
        previousTotalMoveCost += thisMoveCost
        previousTotalCost = previousTotalMoveCost + thisStateCost

        Time = Time + 1
        loss_function.append(
            previousTotalCost) if len(loss_function) == 0 or loss_function[
                -1] > previousTotalCost else loss_function.append(
                    loss_function[-1])
        time_function.append(time.time() - start_time)
Example #5
0
def idsPrpcess(queenList, depth, state):
    if depth == 0 and queens.attacking(queenList) == 0:
        return
    elif depth > 0:
        for index in range(len(queenList)):
            if state[0] == 1:
                return
            r = queenList[index].row
            for y in range(len(queenList)):
                if r == y:
                    continue
                queenList[index].up(y - r)

                if queens.attacking(queenList) == 0:
                    state[0] = 1
                    return
                else:
                    idsPrpcess(queenList, depth - 1, state)
                    if (state[0] == 1):
                        return
                    queenList[index].up(r - y)

    return
Example #6
0
    return


def totalCost(originQueens, targetQueens):
    total = 0
    for index in range(len(targetQueens)):
        total += abs(originQueens[index].row - targetQueens[index].row) * pow(
            targetQueens[index].weight, 2)
    return total


if __name__ == "__main__":
    boardSize = 7
    nPlusOne = False
    initBoard = queens.generateQueens(boardSize, nPlusOne)
    attack = queens.attacking(initBoard)
    print("Original attack : ", attack)
    original_board = copy.deepcopy(initBoard)
    # queens.chessBoard(initBoard, boardSize)
    # print(original_board[0].row)
    #  run  ids to get result
    start = time.time()
    ids(initBoard, 7)
    end = time.time()
    new_attack = queens.attacking(initBoard)
    # print(initBoard[0].row)
    print("New attack : ", new_attack)
    cost = totalCost(original_board, initBoard)
    print("Total cost : ", cost)
    print("Run time : ", end - start)
    queens.chessBoard(initBoard, boardSize)