コード例 #1
0
        public async Task ExecutePrediction(PredictionParameters parms)
        {
            string accessJson = await _fileStorage.ReadFile("connectdefinition", "PPDMDataAccess.json");

            _accessDefs = JsonConvert.DeserializeObject <List <DataAccessDef> >(accessJson);
            ConnectParameters connector = await GetConnector(parms.DataConnector);

            databaseConnectionString = connector.ConnectionString;

            _dbConn.OpenConnection(connector);

            string sourceConnector = GetSource();

            if (parms.DataConnector == sourceConnector)
            {
                syncPredictions = true;
            }
            else
            {
                syncPredictions = false;
            }

            RuleManagement rules = new RuleManagement(_azureConnectionString);
            RuleModel      rule  = await rules.GetRuleAndFunction(parms.DataConnector, parms.PredictionId);

            manageIndexTable = new ManageIndexTable(_accessDefs, connector.ConnectionString, rule.DataType, rule.FailRule);
            manageIndexTable.InitQCFlags(false);
            await MakePredictions(rule, connector);

            _dbConn.CloseConnection();
            manageIndexTable.SaveQCFlags();
        }
コード例 #2
0
        public Options()
        {
            PredictionParameters PredictionParameters = new PredictionParameters()
            {
                NumberOfRegionProposals                    = Settings.Default.NumberOfRegionProposals,
                MiddlePointDistanceThreshold               = Settings.Default.MiddlePointDistanceThreshold,
                OuterSelectionThreshold                    = Settings.Default.OuterSelectionThreshold,
                DecimalPointProbabilityRounding            = Settings.Default.DecimalPointProbabilityRounding,
                RegionProposalsMultiplicity                = Settings.Default.RegionProposalsMultiplicity,
                SpatialDistanceOfCoordinatePointsThreshold = Settings.Default.SpatialDistanceOfCoordinatePointsThreshold,
                ImageResizeFactor = Settings.Default.ImageResizeFactor,
                NN_ModelPath      = Settings.Default.NN_ModelPath
            };
            Preferences Preferences = new Preferences()
            {
                UseGpuAcceleration        = Settings.Default.UseGpuAcceleration,
                NumberOfResultsPerElement = Settings.Default.NumberOfResultsPerElement,
                LeafProbabilityThreshold  = Settings.Default.LeafProbabilityThreshold,
                Simulink_OutputPath       = Settings.Default.Simulink_OutputPath
            };

            OptionsViewModel viewModel = new OptionsViewModel(PredictionParameters, Preferences, ApplicationService.Instance.EventAggregator);

            DataContext             = viewModel;
            viewModel.RequestClose += () => this.Close();

            InitializeComponent();
        }
コード例 #3
0
 public PredatorMCTSAgent(IScore scoring, MCTSParameters mctsParameters, PredictionParameters predictionParameters)
     : base(scoring, mctsParameters)
 {
     /*deck = ControlWarlock;
      * hero = CardClass.WARLOCK;
      */
     _predictionParameters = predictionParameters;
     _map = BigramMapReader.ParseFile(_predictionParameters.File);
 }
コード例 #4
0
        public static async Task <List <PredictionCorrection> > InitPredictions([ActivityTrigger] DataOpParameters pipe, ILogger log)
        {
            log.LogInformation($"InitPredictions: Starting");
            List <PredictionCorrection> predictionList = new List <PredictionCorrection>();
            Predictions          predictions           = new Predictions(pipe.StorageAccount);
            PredictionParameters parms = JObject.Parse(pipe.JsonParameters).ToObject <PredictionParameters>();

            predictionList = await predictions.GetPredictions(parms.DataConnector);

            log.LogInformation($"Number of predictions are {predictionList.Count}");
            log.LogInformation($"InitPredictions: Complete");
            return(predictionList);
        }
コード例 #5
0
        public async Task<ActionResult<string>> ExecutePrediction(PredictionParameters predictionParams)
        {
            try
            {
                if (predictionParams == null) return BadRequest();
                string tmpConnString = Request.Headers["AzureStorageConnection"];
                Predictions predict = new Predictions(tmpConnString);
                await predict.ExecutePrediction(predictionParams);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.ToString());
            }

            return Ok($"OK");
        }
コード例 #6
0
        public static async Task <string> DataPrediction([ActivityTrigger] DataOpParameters pipe, ILogger log)
        {
            try
            {
                log.LogInformation($"Prediction: Starting prediction");
                Predictions          predictions = new Predictions(pipe.StorageAccount);
                PredictionParameters parms       = JObject.Parse(pipe.JsonParameters).ToObject <PredictionParameters>();
                log.LogInformation($"Prediction: Processing prediction id {parms.PredictionId}");
                await predictions.ExecutePrediction(parms);

                log.LogInformation($"Prediction: Complete");
            }
            catch (Exception ex)
            {
                log.LogInformation($"Prediction:Serious exception {ex}");
            }

            return($"Prediction Rule {pipe.Id}");
        }
コード例 #7
0
        public PredictionResults Predict(PredictionParameters data)
        {
            var history = CsvUtils._Instance.Read <CallLogMap, CallLog>($"{Environment.CurrentDirectory}/history.csv").ToList();
            var group   = history
                          .Where(x => x.UserId == Guid.Parse(data.Id))
                          .Select(x => x.SessionSequence)
                          .Select(x => x.ToList().Select(x => (MobileTerminalState)(int.Parse(x.ToString()))))
                          .Select(x => x.Skip(1).Take(2))
                          .Where(x => x.StartsWith(new List <MobileTerminalState> {
                data.Service.GetState()
            }))
                          .SelectMany(x => x.Skip(1))
                          .GroupBy(x => x);

            if (!group.Any())
            {
                return(null);
            }

            //continue to compute frequency table
            var prediction = default(IGrouping <MobileTerminalState, MobileTerminalState>);
            var max        = 0;

            //Deal with ties
            foreach (var grp in group)
            {
                if (grp.Count() > max)
                {
                    prediction = grp;
                    max        = prediction.Count();
                }
                //Console.WriteLine( $"next state is {grp.Key}, Frequency: {grp.Count()}");
            }

            return(new PredictionResults
            {
                NextState = prediction.Key,
                Frequency = prediction.Count(),
                FrequencyDictionary = group.ToDictionary(g => (int)g.Key, g => g.Select(x => x).Count())
            });
        }
コード例 #8
0
        protected override void CalcBar()
        {
            // strategy logic
            if (!Bars.LastBarOnChart)
            {
                return;
            }
            if (Bars.CurrentBar <= trainingSize)
            {
                Output.WriteLine("Not enough bars on chart. Waiting for new data");
                return;
            }

            if (train)
            {
                int interval = Bars.CurrentBar - prevTrain;
                if (Bars.Status == EBarState.Close && (!isTrained || isTrained && interval == retrainInterval))
                {
                    Output.WriteLine(Bars.CurrentBarAbsolute().ToString());
                    // Establishing connection
                    socket = new TcpClient();
                    socket.Connect("localhost", 9090);                              // Connecting to python server on localhost
                    stream = socket.GetStream();                                    // Creating stream to read and write data

                    if (socket.Connected)
                    {
                        Output.WriteLine("connected!");

                        // Collecting close Price and Dates data
                        List <string> closePrice = new List <string>();
                        List <string> time       = new List <string>();
                        for (int index = 0; index < trainingSize; index++)
                        {
                            closePrice.Add(Bars.Close[index].ToString());
                            time.Add(Bars.Time[index].ToString());
                        }

                        closePrice.Reverse();
                        time.Reverse();

                        // Creating dynamic object to store model parameters
                        var jsonObject = new TrainParameters();

                        jsonObject.Data          = closePrice;
                        jsonObject.Time          = time;
                        jsonObject.FileName      = fileName;
                        jsonObject.GPU           = gpu;
                        jsonObject.Train         = train;
                        jsonObject.Architecture  = (int)architecture;
                        jsonObject.Optimizer     = (int)optimizer;
                        jsonObject.Loss          = (int)loss;
                        jsonObject.LearningRate  = learningRate;
                        jsonObject.Epochs        = epochs;
                        jsonObject.Scale         = scale;
                        jsonObject.Momentum      = momentum;
                        jsonObject.TestingPart   = testingPart;
                        jsonObject.TestingWeight = testingWeight;
                        jsonObject.Bars          = bars;

                        string jsonString = JsonConvert.SerializeObject(jsonObject);
                        Byte[] data       = Encoding.UTF8.GetBytes(jsonString);

                        stream.Write(data, 0, data.Length);

                        //Output.WriteLine("Sent : " + jsonString);

                        Output.WriteLine("Sent!");
                        isTrained = true;
                        prevTrain = Bars.CurrentBar;
                    }
                    else
                    {
                        Output.WriteLine("connection failed!");
                    }
                }

                if (isTrained && socket.Connected)
                {
                    if (stream.DataAvailable)
                    {
                        //socket.ReceiveTimeout = 20000;
                        byte[] data     = new Byte[2 * 256];
                        string response = string.Empty;
                        Int32  bytes    = stream.Read(data, 0, data.Length);
                        response = Encoding.UTF8.GetString(data, 0, bytes);

                        if (response != string.Empty)
                        {
                            Output.WriteLine("Received!");
                            var jsonObject = new PredictionParameters();
                            jsonObject = JsonConvert.DeserializeObject <PredictionParameters>(response);

                            // Plotting the predictions on  the chart
                            for (int i = 0; i < bars; i++)
                            {
                                double       ypred      = double.Parse(jsonObject.Pred[i].ToString());
                                IArrowObject arrowData1 = DrwArrow.Create(new ChartPoint(Bars.Time[0].AddMinutes(i), ypred), true);
                                arrowData1.Color = Color.Aqua;
                                arrowData1.Style = EArrowForms.ArrowForm6;
                                //Draw.Dot(this, "Prediction " + i.ToString(), true, i, ypred, Brushes.Aqua);
                            }

                            stream.Close();
                            socket.Close();
                        }
                        else
                        {
                            Output.WriteLine("No response");
                        }
                    }
                    else
                    {
                        Output.WriteLine("Prediction Data Not Available!");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (Bars.Status == EBarState.Close && !isForecasted)
                {
                    socket = new TcpClient();
                    socket.Connect("localhost", 9090);
                    stream = socket.GetStream();

                    if (socket.Connected)
                    {
                        Output.WriteLine("Connected!");

                        isForecasted = true;

                        var jsonObject = new SavedModelParameters();
                        jsonObject.FileName = fileName;
                        jsonObject.Train    = train;
                        jsonObject.Bars     = bars;

                        string jsonString = JsonConvert.SerializeObject(jsonObject);
                        Byte[] sentData   = Encoding.UTF8.GetBytes(jsonString);

                        Debug.Assert(!jsonObject.Train);
                        stream.Write(sentData, 0, sentData.Length);
                    }
                    else
                    {
                        Output.WriteLine("Connection Failed");
                    }
                }

                if (isForecasted && socket.Connected && !isPlotted)
                {
                    if (stream.DataAvailable)
                    {
                        //socket.ReceiveTimeout = 20000;
                        byte[] recievedData = new Byte[2 * 256];
                        string response     = string.Empty;
                        Int32  bytes        = stream.Read(recievedData, 0, recievedData.Length);
                        response = Encoding.UTF8.GetString(recievedData, 0, bytes);

                        if (response != string.Empty)
                        {
                            var resJsonObject = new SavedModelPredictionParameters();
                            resJsonObject = JsonConvert.DeserializeObject <SavedModelPredictionParameters>(response);

                            Output.WriteLine("Received Data");
                            // Plotting the predictions on  the chart
                            for (int i = 0; i < bars; i++)
                            {
                                Output.WriteLine(resJsonObject.Pred[i].ToString());
                                double       ypred      = double.Parse(resJsonObject.Pred[i].ToString());
                                IArrowObject arrowData1 = DrwArrow.Create(new ChartPoint(Bars.Time[0].AddMinutes(i), ypred), true);
                                arrowData1.Color = Color.Aqua;
                                arrowData1.Style = EArrowForms.ArrowForm6;
                                //Draw.Dot(this, "Prediction " + i.ToString(), true, i, ypred, Brushes.Aqua);
                            }
                            isPlotted = true;
                        }
                        else
                        {
                            Output.WriteLine("No response");
                        }
                    }
                    else
                    {
                        Output.WriteLine("Prediction Data Not Available!");
                    }
                }

                // Already forecasted based on saved model
                else
                {
                    return;
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            //For Training on Real-Time Data
            if (Historical)
            {
                return;
            }

            // If the model needs to be trained
            if (train)
            {
                // Collect Enough Data
                if (CurrentBar < trainingSize)
                {
                    return;
                }

                int interval = CurrentBar - prevTrain;                                // Interval elapsed since last training of the model

                if (!isTrained || (retrain && interval == retrainInterval))           // For sending data to the model
                {
                    // Establishing connection
                    try
                    {
                        socket = new TcpClient();
                        socket.Connect("localhost", 9090);                                  // Connecting to python server on localhost
                        stream = socket.GetStream();                                        // Creating stream to read and write data
                    }
                    catch (ArgumentNullException e)
                    {
                        Print(Time[0].ToString() + " Exception Occured! The hostname parameter is null. " + e.ToString());
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Print(Time[0].ToString() + " Exception Occured! The port parameter is not between MinPort and MaxPort." + e.ToString());
                    }
                    catch (SocketException e)
                    {
                        Print(Time[0].ToString() + " Exception Occured! " + e.ToString());
                    }
                    catch (ObjectDisposedException e)
                    {
                        Print(Time[0].ToString() + " Exception Occured! TcpClient is closed. " + e.ToString());
                    }

                    if (socket.Connected)
                    {
                        Print("Connected to localhost : 9090");
                        var message = DrawTextFixed("Chart Message", "Connected!", TextPosition.TopRight);
                        message.TextColor = Color.DarkBlue;

                        // Collecting close Price and Dates data
                        List <string> closePrice = new List <string>();
                        List <string> time       = new List <string>();
                        for (int index = 0; index < trainingSize; index++)
                        {
                            closePrice.Add(Close[index].ToString());
                            time.Add(Time[index].ToString());
                        }

                        closePrice.Reverse();
                        time.Reverse();

                        // Storing the parameters for training in a class object
                        var jsonObject = new trainParameters();

                        jsonObject.Data          = closePrice;
                        jsonObject.Time          = time;
                        jsonObject.FileName      = fileName;
                        jsonObject.Train         = train;
                        jsonObject.GPU           = gpu;
                        jsonObject.Architecture  = (int)architecture;
                        jsonObject.Optimizer     = (int)optimizer;
                        jsonObject.Loss          = (int)loss;
                        jsonObject.LearningRate  = learningRate;
                        jsonObject.Epochs        = epochs;
                        jsonObject.Scale         = scale;
                        jsonObject.Momentum      = momentum;
                        jsonObject.TestingPart   = testingPart;
                        jsonObject.TestingWeight = testingWeight;
                        jsonObject.Bars          = bars;

                        // Serializing to JSON
                        string jsonString = JsonConvert.SerializeObject(jsonObject);
                        Byte[] data       = Encoding.UTF8.GetBytes(jsonString);

                        if (stream.CanWrite)
                        {
                            stream.Write(data, 0, data.Length);                                              // sending data to the socket
                            //Print("Sent : " + jsonString);
                            Print("Data Sent Successfully!");
                            var msg = DrawTextFixed("Chart Message", "Data Sent...", TextPosition.TopRight);
                            msg.TextColor = Color.DarkBlue;

                            isTrained = true;
                            prevTrain = CurrentBar;
                        }
                        else
                        {
                            Print("Data cannot be sent to the stream!");
                            stream.Close();
                            socket.Close();
                            return;
                        }
                    }
                    else
                    {
                        Print("connection failed!");
                    }
                }
                // Receiving data after training from the server
                if (socket.Connected)
                {
                    if (stream.DataAvailable)
                    {
                        byte[] data     = new Byte[2 * 256];
                        string response = "";
                        Int32  bytes    = stream.Read(data, 0, data.Length);
                        response = Encoding.UTF8.GetString(data, 0, bytes);

                        if (response != "")
                        {
                            //Print("Received : " + response);
                            Print("Data Received Successfully!");
                            var message = DrawTextFixed("Chart Message", "Predictions Received!\nPlotting on Chart...", TextPosition.TopRight);
                            message.TextColor = Color.DarkBlue;

                            var jsonObject = new PredictionParameters();

                            // Deserializing JSON data
                            jsonObject = JsonConvert.DeserializeObject <PredictionParameters>(response);

                            // Plotting the predictions on the chart
                            for (int i = -1; i >= -1 * bars; i--)
                            {
                                double ypred = double.Parse(jsonObject.Pred[(-1 * i) - 1].ToString());
                                DrawDot("Prediction " + i.ToString(), true, i, ypred, Color.Cyan);
                                DrawTextFixed("Chart Message", "", TextPosition.TopRight);
                            }

                            // closing the socket
                            stream.Close();
                            socket.Close();
                        }
                        else
                        {
                            Print("Not Received!");
                        }
                    }
                    else
                    {
                        Print("Please Wait... Loading Predictions...");
                        var message = DrawTextFixed("Chart Message", "Loading Predictions\nPlease Wait...", TextPosition.TopRight);
                        message.TextColor = Color.DarkBlue;
                    }
                }
                else
                {
                    Print("Socket Disconnected! ");
                }
            }                    // end of train
            // receive predictions from previously trained model
            else if (!isReceived)
            {
                // Establishing connection
                try
                {
                    socket = new TcpClient();
                    socket.Connect("localhost", 9090);                              // Connecting to python server on localhost
                    stream = socket.GetStream();                                    // Creating stream to read and write data
                }
                catch (ArgumentNullException e)
                {
                    Print(Time[0].ToString() + " Exception Occured! The hostname parameter is null. " + e.ToString());
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Print(Time[0].ToString() + " Exception Occured! The port parameter is not between MinPort and MaxPort." + e.ToString());
                }
                catch (SocketException e)
                {
                    Print(Time[0].ToString() + " Exception Occured! " + e.ToString());
                }
                catch (ObjectDisposedException e)
                {
                    Print(Time[0].ToString() + " Exception Occured! TcpClient is closed. " + e.ToString());
                }

                if (socket.Connected)
                {
                    Print("Connected to localhost : 9090");
                    DrawTextFixed("Chart Message", "Connected!", TextPosition.TopRight);

                    // Storing parameters to be sent to model in a class object
                    var jsonObject = new SendParameters();

                    jsonObject.FileName = fileName;
                    jsonObject.Train    = train;
                    jsonObject.Bars     = bars;

                    // Serializing to JSON
                    string jsonString = JsonConvert.SerializeObject(jsonObject);
                    Byte[] data       = Encoding.UTF8.GetBytes(jsonString);

                    if (stream.CanWrite)
                    {
                        stream.Write(data, 0, data.Length);

                        Print("Data Sent Successfully!");
                        //Print("Sent : " + jsonString);
                        var message = DrawTextFixed("Chart Message", "Data Sent...", TextPosition.TopRight);
                        message.TextColor = Color.DarkBlue;
                    }
                    else
                    {
                        Print("Data cannot be sent to the stream!");
                        stream.Close();
                        socket.Close();
                        return;
                    }

                    if (stream.CanRead)
                    {
                        byte[] recData  = new Byte[256];
                        string response = string.Empty;
                        Int32  bytes    = stream.Read(recData, 0, recData.Length);
                        response = Encoding.UTF8.GetString(recData, 0, bytes);

                        if (response != string.Empty)
                        {
                            //Print("Received : " + response);
                            Print("Successfully Received Data!");
                            DrawTextFixed("Chart Message", "Predictions Received!\nPlotting on Chart...", TextPosition.TopRight);
                            var jsonObj = new ReceivePrediction();

                            // Deserializing JSON data
                            jsonObj = JsonConvert.DeserializeObject <ReceivePrediction>(response);

                            // Plotting the predictions on the chart
                            for (int i = -1; i >= -1 * bars; i--)
                            {
                                double ypred = double.Parse(jsonObj.Pred[(-1 * i) - 1].ToString());
                                DrawDot("Prediction " + i.ToString(), true, i, ypred, Color.Cyan);
                                DrawTextFixed("Chart Message", "", TextPosition.TopRight);
                            }

                            // closing the socket
                            stream.Close();
                            socket.Close();
                        }
                        else
                        {
                            Print("Prediction cannot be Received!");
                            var message = DrawTextFixed("Chart Message", "Predictions Not Available!\nPlease Train the Model...", TextPosition.TopRight);
                            message.TextColor = Color.DarkBlue;
                        }
                    }
                    else
                    {
                        Print("Prediction Data Not Available!\nPlease Train the Model...");
                        var message = DrawTextFixed("Chart Message", "Predictions Not Available!\nPlease Train the Model...", TextPosition.TopRight);
                        message.TextColor = Color.DarkBlue;
                    }

                    isReceived = true;
                }
                else
                {
                    Print("Connection could not be established!");
                    var message = DrawTextFixed("Chart Message", "Connection Failed!", TextPosition.TopRight);
                    message.TextColor = Color.DarkBlue;
                }
            }             // end of receive predictions from previously trained model
        }                 // end of OnBarUpdate
コード例 #10
0
 public Builder AddPredictionParameters(PredictionParameters predictionParams)
 {
     _predictionParams = predictionParams;
     return(this);
 }
コード例 #11
0
 public PredatorMctsAgent(IScore scoring, MctsParameters mctsParameters, PredictionParameters predictionParameters)
     : base(scoring, mctsParameters)
 {
     _predictionParameters = predictionParameters;
     _map = BigramMapReader.ParseFile(_predictionParameters.File);
 }