GridAdvVm.cs 8.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
using FlyADBase;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace Flyad7_WPF
{
    public class GridAdvVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public bool HasGridAdv { get; set; }

        public RelayCommand SaveCmd { get; private set; }
        public RelayCommand LoadCmd { get; private set; }

        public RelayCommand CalAdLagCmd { get; private set; }
        Chart chart3 = null;
        IFlyADClientAdv flyad;
        int timeGridAdvIndex = 0;
        List<TimeGridAdv2EventArgs> timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();

        public GridAdvVm(IFlyADClientAdv flyad) 
        {
            SaveCmd = new RelayCommand(Save);
            LoadCmd = new RelayCommand(Load);
            CalAdLagCmd = new RelayCommand(CalAdLag);

            this.flyad = flyad;
            flyad.TimeGridAdv2Event += flyad_TimeGridAdv2Event;
        }
        public void Init(Chart chart) 
        {
            this.chart3 = chart;
        }



        #region timeGridAdv
        private async void flyad_TimeGridAdv2Event(object sender, TimeGridAdv2EventArgs e)
        {
            if (chart3 == null)
                return;
            if (!HasGridAdv)
                return;

            if (timeGridAdv2Datas == null)
                timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
            timeGridAdv2Datas.Add(e);

            await Task.Factory.StartNew(() =>
            {
                //画图
                DrawGridAdv(timeGridAdv2Datas.Last());

                while (timeGridAdv2Datas.Count > chart3.Series.Count())
                    timeGridAdv2Datas.RemoveAt(0);

                if (timeGridAdv2Datas.Count() >= 2)
                {
                    CurrR = CalGridAdvR(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], 0);
                }
            });
        }

        void DrawGridAdv(TimeGridAdv2EventArgs units, int adLag = 0)
        {
            if (chart3 == null)
                return;
            var datas = TimeGridAdvHelperExt.ToGrid(units.AdList, units.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
            //画图
            timeGridAdvIndex++;
            if (timeGridAdvIndex >= chart3.Series.Count())
                timeGridAdvIndex = 0;

            App.Current.Dispatcher.Invoke(() =>
            {
                System.Windows.Forms.DataVisualization.Charting.Series series = chart3.Series[timeGridAdvIndex];
                series.Points.Clear();

                int gridLen = flyad.PosLen / flyad.PosOfGrid;

                for (int i = 0; i < datas.Length; i++)
                {
                    int pos = i * flyad.PosOfGrid;
                    int ad = datas[i];
                    series.Points.AddXY(pos, ad);
                    if (ad == Misc.MyBase.NULL_VALUE)
                        series.Points[i].IsEmpty = true;
                }

                chart3.ChartAreas[0].AxisX.Minimum = 0;
                chart3.ChartAreas[0].AxisX.Maximum = flyad.PosLen;
            });
        }

        //计算相关性
        public int BestAdLag { get; private set; }
        public double BestR { get; private set; }
        public double CurrR { get; private set; }

        public double ProgressOfAdLag { get; private set; }
        public double ProgressOfR { get; private set; }

        double CalGridAdvR(TimeGridAdv2EventArgs units1, TimeGridAdv2EventArgs units2, int adLag)
        {
            var datas1 = TimeGridAdvHelperExt.ToGrid(units1.AdList, units1.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
            var datas2 = TimeGridAdvHelperExt.ToGrid(units2.AdList, units2.PosList, flyad.PosOfGrid, flyad.GridLen, adLag);
            
            return Misc.MyMath.Correl(datas1, datas2);
        }

        void CalAdLag(TimeGridAdv2EventArgs units1, TimeGridAdv2EventArgs units2, int adLag, int range, double targetR, out int bestAdLag, out double bestR)
        {
            int step = range / 5;
            if (step < 0)
                step = 1;

            List<AdLagAndR> rList = new List<AdLagAndR>();
            for (int i = 0; i < range; i += step)
            {
                int _adLag = adLag - i;
                double _r = CalGridAdvR(units1, units2, _adLag);
                rList.Add(new AdLagAndR() { AdLag = _adLag, R = _r });
                ProgressOfAdLag = _adLag;
                ProgressOfR = _r;
            }
            for (int i = step; i < range; i += step)
            {
                int _adLag = adLag + i;
                double _r = CalGridAdvR(units1, units2, _adLag);
                rList.Add(new AdLagAndR() { AdLag = _adLag, R = _r });
                ProgressOfAdLag = _adLag;
                ProgressOfR = _r;
            }
            double maxR = rList.Max(lr => lr.R);
            adLag = rList.Find(_lr => _lr.R == maxR).AdLag;

            if (step == 1)
            {
                //已经是最小查找步进
                bestAdLag = adLag;
                bestR = maxR;
                return;
            }
            if (maxR >= targetR)
            {
                //找到了
                bestAdLag = adLag;
                bestR = maxR;
                return;
            }

            //缩小范围,继续找
            range = step * 2;
            CalAdLag(units1, units2, adLag, range, targetR, out bestAdLag, out bestR);

        }
        class AdLagAndR
        {
            public int AdLag;
            public double R;
        }

        private void Save()
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string path = System.IO.Path.Combine(strDesktopPath, $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.json");


            string json = Newtonsoft.Json.JsonConvert.SerializeObject(timeGridAdv2Datas);
            File.WriteAllText(path, json);

            MessageBox.Show($"成功保存到 {path}");
        }

        private async void Load()
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = ".json";
            openFileDialog.Filter = "json文件|*.json";
            openFileDialog.InitialDirectory = strDesktopPath;
            if (openFileDialog.ShowDialog() != true)
                return;

            string path = openFileDialog.FileName;
            string json = File.ReadAllText(path);
            try
            {
                timeGridAdv2Datas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TimeGridAdv2EventArgs>>(json);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"读取失败 {ex}");
                return;
            }
            if (timeGridAdv2Datas == null)
            {
                timeGridAdv2Datas = new List<TimeGridAdv2EventArgs>();
                MessageBox.Show($"读取失败 没有数据");
            }
            else
            {
                await Task.Factory.StartNew(() =>
                {
                    foreach (var units in timeGridAdv2Datas)
                    {

                        DrawGridAdv(units);
                    }
                    if (timeGridAdv2Datas.Count > chart3.Series.Count())
                        timeGridAdv2Datas.RemoveAt(0);

                    if (timeGridAdv2Datas.Count() >= 2)
                    {
                        CurrR = CalGridAdvR(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], 0);
                    }
                });

                MessageBox.Show($"读取成功");
            }

        }
        private async void CalAdLag()
        {
            if (timeGridAdv2Datas.Count() < 2)
            {
                MessageBox.Show("数量小于2次");
                return;
            }
            int bestAdLag = 0;
            double bestR = -1;
            await Task.Factory.StartNew(() => {
                CalAdLag(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2],
                    0, (int)(1000 / 1.28), 0.99, out bestAdLag, out bestR);
            });
            BestAdLag = bestAdLag;
            BestR = bestR;
            await Task.Factory.StartNew(() => {
                DrawGridAdv(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 1], BestAdLag);
                DrawGridAdv(timeGridAdv2Datas[timeGridAdv2Datas.Count() - 2], BestAdLag);
            });
            MessageBox.Show($"计算完成 最佳滞后={BestAdLag}");
        }
        #endregion
    }
}