123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using Application.ATG_Classic_App.Model;
- namespace Application.ATG_Classic_App
- {
- public class HeightToVolumeCaculator
- {
- public enum Mode
- {
- /// <summary>
- /// when query height is not defined in profiles, the nearest(could be bigger or smaller) existed height value will be chosen for caculation.
- /// </summary>
- PreferNearest = 1,
- /// <summary>
- /// when query height is not defined in profiles, the next bigger value will be chosen for caculation.
- /// </summary>
- PreferCeiling = 2,
- /// <summary>
- /// when query height is not defined in profiles, the prior smaller value will be chosen for caculation.
- /// </summary>
- PreferFloor = 4,
- }
- public Mode EstimateMode { get; set; }
- private IOrderedEnumerable<TankProfileData> tankProfiles_by_Ascending;
- /// <summary>
- /// Default use EstimateMode = Mode.PreferNearest | Mode.PreferCeiling
- /// </summary>
- /// <param name="tankProfiles"></param>
- public HeightToVolumeCaculator(IEnumerable<TankProfileData> tankProfiles)
- {
- if (tankProfiles == null || !tankProfiles.Any()) throw new ArgumentNullException(nameof(tankProfiles));
- this.EstimateMode = Mode.PreferNearest | Mode.PreferCeiling;
- this.tankProfiles_by_Ascending = tankProfiles.OrderBy(p => p.Height);
- }
- public double GetVolume(double height)
- {
- var find = this.tankProfiles_by_Ascending.FirstOrDefault(p => p.Height == height);
- if (find != null) return find.Volume;
- TankProfileData estimation;
- if (this.EstimateMode.HasFlag(Mode.PreferNearest))
- {
- var next = this.tankProfiles_by_Ascending.FirstOrDefault(p => p.Height > height);
- if (next == null) return this.tankProfiles_by_Ascending.Last().Volume;
- var prior = this.tankProfiles_by_Ascending.LastOrDefault(p => p.Height < height);
- if (prior == null) return this.tankProfiles_by_Ascending.First().Volume;
- var offsetToNext = next.Height - height;
- var offsetToPrior = Math.Abs(prior.Height - height);
- if (offsetToNext < offsetToPrior)
- estimation = this.tankProfiles_by_Ascending.FirstOrDefault(p => p.Height == next.Height);
- else if (offsetToNext > offsetToPrior)
- estimation = this.tankProfiles_by_Ascending.FirstOrDefault(p => p.Height == prior.Height);
- else
- {
- if (this.EstimateMode.HasFlag(Mode.PreferCeiling))
- estimation = next;
- else
- estimation = prior;
- }
- }
- else if (this.EstimateMode == Mode.PreferCeiling)
- {
- // use the nearest up value for this query, so the queried volume would > real volume.
- estimation = this.tankProfiles_by_Ascending.FirstOrDefault(p => p.Height > height);
- if (estimation == null) return this.tankProfiles_by_Ascending.Last().Volume;
- }
- else if (this.EstimateMode == Mode.PreferFloor)
- {
- // use the nearest down value for this query, so the queried volume would < real volume.
- estimation = this.tankProfiles_by_Ascending.LastOrDefault(p => p.Height < height);
- if (estimation == null) return this.tankProfiles_by_Ascending.First().Volume;
- }
- else return 0;
- return estimation.Volume;
- }
- //public double GetTcVolume(double height)
- //{
- // return this.GetVolume(height);
- //}
- }
- public class TemperatureCompensationCaculator
- {
- private double coeff;
- /// <summary>
- ///
- /// </summary>
- /// <param name="expansionCoefficient">0.0007 is a typical value.</param>
- public TemperatureCompensationCaculator(double expansionCoefficient)
- {
- if (expansionCoefficient > 1 || expansionCoefficient < 0) throw new ArgumentException("invalid expansionCoefficient");
- this.coeff = expansionCoefficient;
- }
- /// <summary>
- /// Get the volume on standard temperature.
- /// </summary>
- /// <param name="standardTemp"></param>
- /// <param name="currentVol"></param>
- /// <param name="currentTemp"></param>
- /// <returns></returns>
- public double CaculateCompensatedVolume(double standardTemp, double currentVol, double currentTemp)
- {
- var volDiffPerLiter = 1 - this.coeff * (currentTemp - standardTemp);
- return volDiffPerLiter * currentVol;
- }
- }
- //public static class DeliveryDectector
- //{
- // /*according from Thomas.Gan: ATG will capture it as start delivery when it detect the product volume increase by >200 liter in the short period of time
- // * when the product has finish the delivery, the driver will wait for 15 minutes and go back to ATG console print out the latest tank slip.
- // * the driver will compare the volme before and after deliver and get the station staff to sign off
- // * on ATG, it will declar stop deivery when the product has not increase for the last 15 minutes
- // */
- // /* 1 meter squre = 1000L.
- // * in china, for one site, all tanks vol typically <=150000L, single tank <=50000L.
- // *
- // *
- // * by read a real site delivery reports with tank max vol 50000L, can get below facts:
- // * 需要记录,实发标准体积, 以V20温度计算,一般在15000L左右。
- // * each delivery vol amount is around 15000L.
- // *
- // */
- // private static MLContext mlContext;
- // public static DeviceProcessor.ATG.Delivery Get(IEnumerable<Model.Inventory> inventories)
- // {
- // IOrderedEnumerable<Model.Inventory> data = inventories.OrderBy(i => i.TimeStamp);
- // // Create MLContext to be shared across the model creation workflow objects
- // mlContext = new MLContext();
- // //assign the Number of records in dataset file to cosntant variable
- // int size = inventories.Count();
- // //Load the data into IDataView.
- // //This dataset is used while prediction/detecting spikes or changes.
- // IDataView dataView = mlContext.Data.LoadFromEnumerable<ProductInventoryData>(
- // data.Select(d =>
- // new ProductInventoryData() { TimeStamp = d.TimeStamp.ToString("yyyy-MM-dd HH:mm:ss fff") }));
- // //To detech temporay changes in the pattern
- // //DetectSpike(size, dataView);
- // //To detect persistent change in the pattern
- // var predictions = DetectChangepoint(size, dataView).ToList();
- // for (int i = 0; i < predictions.Count(); i++)
- // {
- // if (predictions[i].Prediction[0] == 1)
- // {
- // //return new De
- // }
- // }
- // return null;
- // }
- // public class ProductInventoryData
- // {
- // public string TimeStamp;
- // public float Volume;
- // }
- // public class ProductInventoryPrediction
- // {
- // //vector to hold alert,score,p-value values
- // [VectorType(3)]
- // public double[] Prediction { get; set; }
- // }
- // static IEnumerable<ProductInventoryPrediction> DetectChangepoint(int size, IDataView dataView)
- // {
- // //Console.WriteLine("===============Detect Persistent changes in pattern===============");
- // //STEP 1: Setup transformations using DetectIidChangePoint
- // var estimator = mlContext.Transforms.DetectIidChangePoint(
- // outputColumnName: "Prediction",
- // inputColumnName: "Volume", confidence: 95, changeHistoryLength: size / 4);
- // //STEP 2:The Transformed Model.
- // //In IID Change point detection, we don't need need to do training, we just need to do transformation.
- // //As you are not training the model, there is no need to load IDataView with real data, you just need schema of data.
- // //So create empty data view and pass to Fit() method.
- // ITransformer tansformedModel = estimator.Fit(CreateEmptyDataView());
- // //STEP 3: Use/test model
- // //Apply data transformation to create predictions.
- // IDataView transformedData = tansformedModel.Transform(dataView);
- // var predictions = mlContext.Data.CreateEnumerable<ProductInventoryPrediction>(transformedData, reuseRowObject: false);
- // //Console.WriteLine($"{nameof(ProductInventoryPrediction.Prediction)} column obtained post-transformation.");
- // //Console.WriteLine("Alert\tScore\tP-Value\tMartingale value");
- // foreach (var p in predictions)
- // {
- // if (p.Prediction[0] == 1)
- // {
- // //Console.WriteLine("{0}\t{1:0.00}\t{2:0.00}\t{3:0.00} <-- alert is on, predicted changepoint",
- // // p.Prediction[0], p.Prediction[1], p.Prediction[2], p.Prediction[3]);
- // }
- // else
- // {
- // //Console.WriteLine("{0}\t{1:0.00}\t{2:0.00}\t{3:0.00}",
- // // p.Prediction[0], p.Prediction[1], p.Prediction[2], p.Prediction[3]);
- // }
- // }
- // //Console.WriteLine("");
- // return predictions;//.Select(p => p.Prediction[0] == 1);
- // }
- // private static IDataView CreateEmptyDataView()
- // {
- // //Create empty DataView. We just need the schema to call fit()
- // IEnumerable<ProductInventoryData> enumerableData = new List<ProductInventoryData>();
- // var dv = mlContext.Data.LoadFromEnumerable(enumerableData);
- // return dv;
- // }
- //}
- }
|