public static void ForecastAndExport_v3(string forecast_tag, string data_tag, int forecastPerspective = 2) { string tag_name = forecast_tag; double[] vMax, vMin; DateTime fdate = DateTime.Today.AddDays(forecastPerspective); // дата прогноза SOM model = CreateSomModel(data_tag, out vMax, out vMin); Dictionary <DateTime, double> solution = new Dictionary <DateTime, double>(); // прогноз TagDbContext context = new TagDbContext(); var data = context.TagValues .Where(t => t.Tag.TagName.Trim() == data_tag) .OrderByDescending(v => v.DateTime) .Select(v => new { v.DateTime.Hour, Day = SqlFunctions.DatePart("weekday", v.DateTime) + 1, Value = v.Value }) .Take(24 + 3 + 1) .AsEnumerable() .Reverse() .ToArray(); for (int index = 0; index < 24; index++) { var vector = new double[] { (double)(vMax[0] - (double)data[index].Hour) / (vMax[0] - vMin[0]), (double)(vMax[1] - (double)data[index].Day) / (vMax[1] - vMin[1]), (double)(vMax[2] - data[index + 1].Value) / (vMax[2] - vMin[2]), (double)(vMax[3] - data[index + 2].Value) / (vMax[3] - vMin[3]), (double)(vMax[4] - data[index + 3].Value) / (vMax[4] - vMin[4]) }; var value = model.Compute(vector)[0]; solution[fdate.AddHours(data[index].Hour)] = value; } ExportToDB(tag_name, solution, context); }
static SOM CreateSomModel_v2(string tagname) { int samples = 1500; var context = new TagDbContext(); double[] data = context.TagValues .Where(t => t.Tag.TagName == tagname) .OrderByDescending(v => v.DateTime) .Select(v => v.Value) .Take(samples + retrospective + 1) .AsEnumerable() .Reverse() .ToArray(); var inputs = new Vector[samples]; for (int i = 0; i < samples; i++) { inputs[i] = new Vector(); inputs[i].Input = data.Skip(i).Take(retrospective).ToArray(); // set output inputs[i].Output = new double[] { data[retrospective + i + 1] }; } SOM model = new SOM(5, 1, 15, 15); model.Train(inputs, 500); return(model); }
static SOM CreateSomModel(string tagname, out double[] vMax, out double[] vMin) { int retro = 3; int samples = 1500; var context = new TagDbContext(); var data = context.TagValues .Where(t => t.Tag.TagName.Trim() == tagname) .OrderByDescending(v => v.DateTime) .Select(v => new { v.DateTime.Hour, Day = SqlFunctions.DatePart("weekday", v.DateTime) + 1, Value = v.Value }) .Take(samples + retro + 1) .AsEnumerable() .Reverse() .ToArray(); vMax = new double[] { data.Max(d => d.Hour), data.Max(d => (double)d.Day), data.Max(d => d.Value), data.Max(d => d.Value), data.Max(d => d.Value) }; vMin = new double[] { data.Min(d => d.Hour), data.Min(d => (double)d.Day), data.Min(d => d.Value), data.Min(d => d.Value), data.Min(d => d.Value) }; var inputs = new Vector[samples]; for (int index = 0; index < samples; index++) { inputs[index] = new Vector(); inputs[index].Input = new double[] { (double)(vMax[0] - (double)data[index].Hour) / (vMax[0] - vMin[0]), (double)(vMax[1] - (double)data[index].Day) / (vMax[1] - vMin[1]), (double)(vMax[2] - data[index + 1].Value) / (vMax[2] - vMin[2]), (double)(vMax[3] - data[index + 2].Value) / (vMax[3] - vMin[3]), (double)(vMax[4] - data[index + 3].Value) / (vMax[4] - vMin[4]) }; inputs[index].Output = new double[] { data[index].Value }; } SOM model = new SOM(5, 1, 15, 15); model.Train(inputs, 500); return(model); }