public override Task <MLBBaseballBatterPredictionResponse> MakeBaseBallBatterPrediction(MLBBaseballBatterPredictionRequest request, ServerCallContext context) { _logger.LogInformation("Making prediction for {0} : Predicting: {0}", request.MLBBaseballBatter.FullPlayerName, request.PredictionType); MLBHOFPrediction prediction; // If using ensemble, iterate through the different algorithms used in the models if (request.UseEnsembleOfAlgorithms) { List <MLBHOFPrediction> allModelAlgorithmPredictions = new List <MLBHOFPrediction>(); foreach (AlgorithmName algorithmName in Enum.GetValues(typeof(AlgorithmName))) { var predictionType = request.PredictionType.ToString() == "OnHoFballot" ? "OnHoFBallot" : "InductedToHoF"; var tempmodelNameForPrediction = string.Format("{0}-{1}", predictionType, algorithmName); var tempPrediction = _predictionPool.Predict(tempmodelNameForPrediction, request.MLBBaseballBatter); allModelAlgorithmPredictions.Add(tempPrediction); } prediction = new MLBHOFPrediction { Probability = (allModelAlgorithmPredictions.Sum(a => a.Probability) / allModelAlgorithmPredictions.Count()), Prediction = (allModelAlgorithmPredictions.Sum(a => a.Probability) / allModelAlgorithmPredictions.Count()) >= 0.5 ? true : false, Score = 0 // Score is meaningless, as each algorithm's processes use different scales }; } // Else, perform a simple prediction based on the source algorithm of the model else { var modelNameForPredictions = string.Format("{0}-{1}", request.PredictionType, request.AlgorithmName); prediction = _predictionPool.Predict(modelNameForPredictions, request.MLBBaseballBatter); } var response = new MLBBaseballBatterPredictionResponse { PredictionID = request.PredictionID, PredictionType = request.PredictionType, MLBBaseballBatter = request.MLBBaseballBatter, MLBHOFPrediction = prediction }; return(Task.FromResult(response)); }
static async Task Main(string[] args) { // Temporary workaround for Visual Studio 2022 Preview Environment.SetEnvironmentVariable("ASPNETCORE_PREVENTHOSTINGSTARTUP", "true"); Console.Title = "gRPC Baseball Predictions Client"; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Starting the gRPC Baseball Predictions Client."); Console.WriteLine(); // Retrieve Sample Baseball Data var mlbBaseballPlayerBatters = await BaseballData.GetSampleBaseballData(); // The port number(5001) must match the port of the gRPC server. using var channel = GrpcChannel.ForAddress("https://localhost:5001"); var baseBallPredictionClient = new BaseballBatterPrediction.BaseballBatterPredictionClient(channel); foreach (var mlbBaseballPlayerBatter in mlbBaseballPlayerBatters) { // Slow down predictions, to see a better representation on the Console program // Note: You would remove this in a real-world implementation await Task.Delay(600); // OnHallOfFameBallot Prediction var baseBallPredictionRequest = new MLBBaseballBatterPredictionRequest { PredictionID = Guid.NewGuid().ToString(), PredictionType = PredictionType.OnHoFballot, AlgorithmName = AlgorithmName.GeneralizedAdditiveModels, UseEnsembleOfAlgorithms = true, MLBBaseballBatter = mlbBaseballPlayerBatter }; var baseBallPredictionReply = await baseBallPredictionClient.MakeBaseBallBatterPredictionAsync(baseBallPredictionRequest); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(baseBallPredictionReply.MLBBaseballBatter.FullPlayerName); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("On Hall Of Fame Ballot ################################"); Console.ResetColor(); Console.WriteLine("PredictionID: {0}", baseBallPredictionReply.PredictionID); Console.WriteLine("Predicted Probability of {0}: {1}", baseBallPredictionRequest.PredictionType, Math.Round((Decimal)baseBallPredictionReply.MLBHOFPrediction.Probability, 5, MidpointRounding.AwayFromZero)); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("######################################################"); Console.WriteLine(); // InductedToHallOfFame Prediction baseBallPredictionRequest = new MLBBaseballBatterPredictionRequest { PredictionID = Guid.NewGuid().ToString(), PredictionType = PredictionType.InductedToHoF, AlgorithmName = AlgorithmName.GeneralizedAdditiveModels, UseEnsembleOfAlgorithms = false, MLBBaseballBatter = mlbBaseballPlayerBatter }; baseBallPredictionReply = await baseBallPredictionClient.MakeBaseBallBatterPredictionAsync(baseBallPredictionRequest); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(baseBallPredictionReply.MLBBaseballBatter.FullPlayerName); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Inducted to Hall Of Fame #############################"); Console.ResetColor(); Console.WriteLine("PredictionID: {0}", baseBallPredictionReply.PredictionID); Console.WriteLine("Predicted Probability of {0}: {1}", baseBallPredictionRequest.PredictionType, Math.Round((Decimal)baseBallPredictionReply.MLBHOFPrediction.Probability, 5, MidpointRounding.AwayFromZero)); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("######################################################"); Console.WriteLine(); } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(); Console.WriteLine("Finished the gRPC Baseball Predictions Client."); Console.ReadLine(); }