コード例 #1
0
        public AnomalyDetection(IConfigurationRoot config)
        {
            var kMeansClusters = Convert.ToInt32(config["kMeansClusters"]);

            GetProxy <IOrchestrator>().Detection.Subscribe(this, async signal =>
            {
                try
                {
                    var cluster = 0;
                    if (_kMeansScoring != null)
                    {
                        lock (_syncClustering)
                        {
                            if (_kMeansScoring != null)
                            {
                                cluster = _kMeansScoring.Score(new[] { signal.Value });
                            }
                        }
                    }

                    if (cluster < 0)
                    {
                        await Anomaly.PublishAsync(new Anomaly {
                            Temperature = signal
                        });
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, $"Error processing {signal.ToString()}");
                }

                return(MessageResult.Ok);
            });

            GetProxy <IOrchestrator>().Model.Subscribe(this, model =>
            {
                //if the messages have been stored and forwarded, but the file has been deleted (e.g. a restart)
                //then the message can be empty (null)
                if (model == null)
                {
                    return(Task.FromResult(MessageResult.Ok));
                }

                try
                {
                    lock (_syncClustering)

                    {
                        switch (model.Algorithm)
                        {
                        case Algorithm.kMeans:
                            _kMeansScoring = new KMeansScoring(kMeansClusters);
                            _kMeansScoring.DeserializeModel(model.DataJson);
                            break;

                        case Algorithm.LSTM:
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, $"Error processing {model.ToString()}");
                }

                return(Task.FromResult(MessageResult.Ok));
            });
        }
コード例 #2
0
        public AnomalyDetection(IOrchestrator orcherstratorProxy, IModelTraining modelTrainingProxy)
        {
            orcherstratorProxy.Detection.Subscribe(this, async signal =>
            {
                try
                {
                    int cluster = 0;
                    if (_kMeansScoring != null)
                    {
                        lock (_syncClustering)
                            if (_kMeansScoring != null)
                            {
                                cluster = _kMeansScoring.Score(new double[] { signal.Value });
                            }
                    }

                    if (cluster < 0)
                    {
                        await Anomaly.PublishAsync(new Anomaly()
                        {
                            Temperature = signal
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                return(MessageResult.Ok);
            });

            modelTrainingProxy.Model.Subscribe(this, async(model) =>
            {
                //if the messages has been stored and forwarded, but the file has been deleted (e.g. a restart)
                //then the message can be empty (null)
                if (model == null)
                {
                    return(MessageResult.Ok);
                }

                try
                {
                    lock (_syncClustering)

                        switch (model.Algorithm)
                        {
                        case ThermostatApplication.Algorithm.kMeans:
                            _kMeansScoring = new KMeansScoring(_numClusters);
                            _kMeansScoring.DeserializeModel(model.DataJson);
                            break;

                        case ThermostatApplication.Algorithm.LSTM:
                            break;

                        default:
                            break;
                        }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                return(MessageResult.Ok);
            });
        }