Exemple #1
0
def main():
    args = parser.parse_args()
    print("\nWelcome to BioMonitor!\n\nUse Ctrl+C to quit\nStarting...")

    CameraConfig.simulation_source = args.source_file
    CameraConfig.index = args.cam_index
    IsolatorConfig.debug = args.debug
    if args.dontwait:
        args.fps = 99999999

    cam = camera.CameraInput(args.fps, MainConfig.buffer_size,
                             bool(args.source_file))
    ai = Classifier()
    ai.load()

    buffer = cam.get()

    while buffer:
        trajectories = ObjectIsolator.isolate(buffer)
        prepared = ai.prepare(trajectories)

        predictions = []
        for one in prepared:
            predictions.append(ai.predict(one, args.predictions))

        if args.verbose:
            print(
                f"\nDetected: \t{len(predictions)}\nContaminated: \t{sum([x.argmax() for x in predictions])}"
            )

        print("Rating:\t\t", Rating.evaluate(predictions))

        buffer = cam.get()

    cam.terminate()
    ai = Classifier()
    ai.load()

    buffer = cam.get()
    timeline = []
    ratings = []
    while buffer:
        op = ObjectIsolator.isolate(buffer)
        preped = ai.prepare(op)

        predictions = []
        for one in preped:
            predictions.append(ai.predict(one, False))
        timeline.append(predictions)

        r = Rating.evaluate(predictions)
        ratings.append(r)

        buffer = cam.get()

    n = [len(x) for x in timeline]
    cont = [sum([y.argmax() for y in x]) for x in timeline]
    ok = np.array(n) - np.array(cont)
    df = pd.DataFrame({
        'n': n,
        'clean': ok,
        'contaminated': cont,
        'rating': ratings
    })
    df.plot.line()
    plt.grid()
    def principalVariationSearch(self, alpha, beta, history, whiteKing,
                                 whiteQueen, whiteBishop, whiteKnight,
                                 whiteRook, whitePawn, blackKing, blackQueen,
                                 blackBishop, blackKnight, blackRook,
                                 blackPawn, whiteQueenCastle, whiteKingCastle,
                                 blackQueenCastle, blackKingCastle, whiteTurn,
                                 depth):
        Moves = self.Moves
        Rating = self.Rating

        #if we reach our max search depth, return the best score for the board at that level
        if (depth == self.maxDepth):
            bestScore = Rating.evaluate(
                history, whiteKing, whiteQueen, whiteBishop, whiteKnight,
                whiteRook, whitePawn, blackKing, blackQueen, blackBishop,
                blackKnight, blackRook, blackPawn, whiteQueenCastle,
                whiteKingCastle, blackQueenCastle, blackKingCastle, depth,
                self.playerIsWhite)
            return bestScore, "No Move"

        if (whiteTurn):
            moves = Moves.white_legalMoves(
                history, whiteKing, whiteQueen, whiteBishop, whiteKnight,
                whiteRook, whitePawn, blackKing, blackQueen, blackBishop,
                blackKnight, blackRook, blackPawn, whiteQueenCastle,
                whiteKingCastle, blackQueenCastle, blackKingCastle)
        else:
            moves = Moves.black_legalMoves(
                history, whiteKing, whiteQueen, whiteBishop, whiteKnight,
                whiteRook, whitePawn, blackKing, blackQueen, blackBishop,
                blackKnight, blackRook, blackPawn, whiteQueenCastle,
                whiteKingCastle, blackQueenCastle, blackKingCastle)

        #get the first legal move for the current player. After the moves have been sorted, we assume that the first move is always the best move.
        moves = self.sanitizeMove(moves, history, whiteKing, whiteQueen,
                                  whiteBishop, whiteKnight, whiteRook,
                                  whitePawn, blackKing, blackQueen,
                                  blackBishop, blackKnight, blackRook,
                                  blackPawn, whiteQueenCastle, whiteKingCastle,
                                  blackQueenCastle, blackKingCastle, whiteTurn)

        #if no legal move for current player, then it must be checkmate or stalemate
        #if this is player's turn, this is really bad. return -5000
        #if this is opponent's turn, this is good. we want this. return 5000
        #TODO: Check this AND FIX THIS!
        if (len(moves) == 0):
            if (self.playerIsWhite == whiteTurn):
                return -5000, "No Move"
            else:
                return 5000, "No Move"

        moves = self.sortMoves(moves, history, whiteKing, whiteQueen,
                               whiteBishop, whiteKnight, whiteRook, whitePawn,
                               blackKing, blackQueen, blackBishop, blackKnight,
                               blackRook, blackPawn, whiteQueenCastle,
                               whiteKingCastle, blackQueenCastle,
                               blackKingCastle, whiteTurn, depth)

        firstLegalMoveIndex = 0
        b = beta
        bestScore = -math.inf

        ####################################### throughroughly search firstLegalMove
        firstLegalMove = moves[firstLegalMoveIndex]

        try:
            originalX = int(firstLegalMove[0])
            originalY = int(firstLegalMove[1])
            newX = int(firstLegalMove[2])
            newY = int(firstLegalMove[3])
        except (ValueError):
            raise ValueError(
                "Error in principalVariationSearch: the first four character of firstLegalMove string must be integer."
            )

        start = (originalX * 8) + originalY
        end = (newX * 8) + newY

        #white pieces
        tempWhiteKing = Moves.makeMove(whiteKing, firstLegalMove, "K")
        tempWhiteQueen = Moves.makeMove(whiteQueen, firstLegalMove, "Q")
        tempWhiteBishop = Moves.makeMove(whiteBishop, firstLegalMove, "B")
        tempWhiteKnight = Moves.makeMove(whiteKnight, firstLegalMove, "H")
        tempWhiteRook = Moves.makeMove(whiteRook, firstLegalMove, "R")
        tempWhitePawn = Moves.makeMove(whitePawn, firstLegalMove, "P")
        #if castling, make castling move
        if ("WL" in firstLegalMove or "WR" in firstLegalMove):
            tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove(
                whiteKing, whiteRook, firstLegalMove)
        #black pieces
        tempBlackKing = Moves.makeMove(blackKing, firstLegalMove, "k")
        tempBlackQueen = Moves.makeMove(blackQueen, firstLegalMove, "q")
        tempBlackBishop = Moves.makeMove(blackBishop, firstLegalMove, "b")
        tempBlackKnight = Moves.makeMove(blackKnight, firstLegalMove, "h")
        tempBlackRook = Moves.makeMove(blackRook, firstLegalMove, "r")
        tempBlackPawn = Moves.makeMove(blackPawn, firstLegalMove, "p")
        if ("BL" in firstLegalMove or "BR" in firstLegalMove):
            tempBlackKing, tempBlackRook = Moves.makeCastlingMove(
                blackKing, blackRook, firstLegalMove)
        tempHistory = firstLegalMove

        ###update castling variables
        #copy castling variables from previous
        tempWhiteQueenCastle = whiteQueenCastle
        tempWhiteKingCastle = whiteKingCastle
        tempBlackQueenCastle = blackQueenCastle
        tempBlackKingCastle = blackKingCastle

        #update castling variable based on the firstLegalMove we made
        #if firstLegalMove is making white castling move, we can no longer castle again for white
        if ("WL" in firstLegalMove or "WR" in firstLegalMove):
            tempWhiteQueenCastle = False
            tempWhiteKingCastle = False
        #if firstLegalMove is making black castling move, we can no longer castle again for black
        elif ("BL" in firstLegalMove or "BR" in firstLegalMove):
            tempBlackQueenCastle = False
            tempBlackKingCastle = False
        else:
            #if firstLegalMove is moving whiteKing, white queen and king side castle become False
            if (((1 << start) & whiteKing) != 0):
                tempWhiteQueenCastle = False
                tempWhiteKingCastle = False
            #if firstLegalMove is moving blackKing, black queen and king side castle become False
            elif (((1 << start) & blackKing) != 0):
                tempBlackQueenCastle = False
                tempBlackKingCastle = False
            #if firstLegalMove is moving white left rook, white queenside castling become False
            elif (((1 << start) & whiteRook & (1 << 56)) != 0):
                tempWhiteQueenCastle = False
            #if firstLegalMove is moving white right rook, white kingside castling become False
            elif (((1 << start) & whiteRook & (1 << 63)) != 0):
                tempWhiteKingCastle = False
            elif (((1 << start) & blackRook & (1 << 0)) != 0):
                tempBlackQueenCastle = False
            elif (((1 << start) & blackRook & (1 << 7)) != 0):
                tempBlackKingCastle = False
        """
		# original algorithm
		score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
		"""
        #Alternate way of writing to original algorithm
        score, bestMove = self.principalVariationSearch(
            -b, -alpha, tempHistory, tempWhiteKing, tempWhiteQueen,
            tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn,
            tempBlackKing, tempBlackQueen, tempBlackBishop, tempBlackKnight,
            tempBlackRook, tempBlackPawn, tempWhiteQueenCastle,
            tempWhiteKingCastle, tempBlackQueenCastle, tempBlackKingCastle,
            not whiteTurn, depth + 1)
        score = -score
        #In principal variation search, we assume that our first move is the best move, and thus yield the best score.
        bestScore = self.max(bestScore, score)  #can also write bestScore=score
        alpha = self.max(alpha, score)
        bestMoveIndex = firstLegalMoveIndex
        if (alpha >= beta):
            return alpha, moves[bestMoveIndex]

        b = alpha + 1

        ####################################### simply and quickly search through remaining move to confirm our assumption that firstLegalMove is the best move
        for i in range(len(moves)):
            #if current iteration is firstLegalMoveIndex, skip, since we already thoroughly searched this move.
            if (i == firstLegalMoveIndex):
                continue
            #if current iteration is not firstLegalMoveIndex, simply and quickly search through current move.
            currentMove = moves[i]
            try:
                originalX = int(currentMove[0])
                originalY = int(currentMove[1])
                newX = int(currentMove[2])
                newY = int(currentMove[3])
            except (ValueError):
                raise ValueError(
                    "Error in principalVariationSearch: the first four character of currentMove string must be integer."
                )

            start = (originalX * 8) + originalY
            end = (newX * 8) + newY

            #white pieces
            tempWhiteKing = Moves.makeMove(whiteKing, currentMove, "K")
            tempWhiteQueen = Moves.makeMove(whiteQueen, currentMove, "Q")
            tempWhiteBishop = Moves.makeMove(whiteBishop, currentMove, "B")
            tempWhiteKnight = Moves.makeMove(whiteKnight, currentMove, "H")
            tempWhiteRook = Moves.makeMove(whiteRook, currentMove, "R")
            tempWhitePawn = Moves.makeMove(whitePawn, currentMove, "P")
            #if castling, make castling move
            if ("WL" in currentMove or "WR" in currentMove):
                tempWhiteKing, tempWhiteRook = Moves.makeCastlingMove(
                    whiteKing, whiteRook, currentMove)
            #black pieces
            tempBlackKing = Moves.makeMove(blackKing, currentMove, "k")
            tempBlackQueen = Moves.makeMove(blackQueen, currentMove, "q")
            tempBlackBishop = Moves.makeMove(blackBishop, currentMove, "b")
            tempBlackKnight = Moves.makeMove(blackKnight, currentMove, "h")
            tempBlackRook = Moves.makeMove(blackRook, currentMove, "r")
            tempBlackPawn = Moves.makeMove(blackPawn, currentMove, "p")
            if ("BL" in currentMove or "BR" in currentMove):
                tempBlackKing, tempBlackRook = Moves.makeCastlingMove(
                    blackKing, blackRook, currentMove)
            tempHistory = currentMove

            ###update castling variables
            #copy castling variables from previous
            tempWhiteQueenCastle = whiteQueenCastle
            tempWhiteKingCastle = whiteKingCastle
            tempBlackQueenCastle = blackQueenCastle
            tempBlackKingCastle = blackKingCastle

            #update castling variable based on the currentMove we made
            #if currentMove is making white castling move, we can no longer castle again for white
            if ("WL" in currentMove or "WR" in currentMove):
                tempWhiteQueenCastle = False
                tempWhiteKingCastle = False
            #if currentMove is making black castling move, we can no longer castle again for black
            elif ("BL" in currentMove or "BR" in currentMove):
                tempBlackQueenCastle = False
                tempBlackKingCastle = False
            else:
                #if currentMove is moving whiteKing, white queen and king side castle become False
                if (((1 << start) & whiteKing) != 0):
                    tempWhiteQueenCastle = False
                    tempWhiteKingCastle = False
                #if currentMove is moving blackKing, black queen and king side castle become False
                elif (((1 << start) & blackKing) != 0):
                    tempBlackQueenCastle = False
                    tempBlackKingCastle = False
                #if currentMove is moving white left rook, white queenside castling become False
                elif (((1 << start) & whiteRook & (1 << 56)) != 0):
                    tempWhiteQueenCastle = False
                #if currentMove is moving white right rook, white kingside castling become False
                elif (((1 << start) & whiteRook & (1 << 63)) != 0):
                    tempWhiteKingCastle = False
                elif (((1 << start) & blackRook & (1 << 0)) != 0):
                    tempBlackQueenCastle = False
                elif (((1 << start) & blackRook & (1 << 7)) != 0):
                    tempBlackKingCastle = False

            #TODO: check this!
            """
			# original algorithm
			score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
			"""
            #Alternate way of writing to original algorithm
            score, bestMove = self.principalVariationSearch(
                -b, -alpha, tempHistory, tempWhiteKing, tempWhiteQueen,
                tempWhiteBishop, tempWhiteKnight, tempWhiteRook, tempWhitePawn,
                tempBlackKing, tempBlackQueen, tempBlackBishop,
                tempBlackKnight, tempBlackRook, tempBlackPawn,
                tempWhiteQueenCastle, tempWhiteKingCastle,
                tempBlackQueenCastle, tempBlackKingCastle, not whiteTurn,
                depth + 1)
            score = -score
            if (score > alpha and score < beta):
                """
				# original algorithm
				score,bestMove=-self.principalVariationSearch(-beta,-score,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
				"""
                #alternate way of writing to original algorithm
                score, bestMove = self.principalVariationSearch(
                    -beta, -score, tempHistory, tempWhiteKing, tempWhiteQueen,
                    tempWhiteBishop, tempWhiteKnight, tempWhiteRook,
                    tempWhitePawn, tempBlackKing, tempBlackQueen,
                    tempBlackBishop, tempBlackKnight, tempBlackRook,
                    tempBlackPawn, tempWhiteQueenCastle, tempWhiteKingCastle,
                    tempBlackQueenCastle, tempBlackKingCastle, not whiteTurn,
                    depth + 1)
                score = -score
                ##For debugging
                # print("researched")
            if (score > bestScore):
                bestMoveIndex = i
                bestScore = score
            alpha = self.max(alpha, score)
            if (alpha >= beta):
                return alpha, moves[bestMoveIndex]
            b = alpha + 1

        return bestScore, moves[bestMoveIndex]