public Dictionary <string, double> GetWeights(List <Sample> samples) { int nAttributes = 13; int nSamples = samples.Count; double[,] tsData = new double[nSamples, nAttributes]; double[] resultData = new double[nSamples]; for (int i = 0; i < samples.Count; i++) { tsData[i, 0] = samples[i].Precipitation; tsData[i, 1] = samples[i].SoilWatFieldCapacity; tsData[i, 2] = samples[i].Swawp; tsData[i, 3] = samples[i].SoilWaterStorage; tsData[i, 4] = samples[i].IrrigationEfficiency; tsData[i, 5] = samples[i].MaxYield; tsData[i, 6] = samples[i].GrossIrrig; tsData[i, 7] = samples[i].InputCosts; tsData[i, 8] = samples[i].OperationCosts; tsData[i, 9] = samples[i].MiscCosts; tsData[i, 10] = samples[i].IrrigCosts; tsData[i, 11] = samples[i].MiscReturns; tsData[i, 6] = samples[i].TotalIrrig; tsData[i, 13] = samples[i].PriceUnit; resultData[i] = samples[i].Yield; } double[] weights = null; int fitResult = 0; alglib.lsfit.lsfitreport rep = new alglib.lsfit.lsfitreport(); alglib.lsfit.lsfitlinear(resultData, tsData, nSamples, nAttributes, ref fitResult, ref weights, rep); Dictionary <string, double> labelsAndWeights = new Dictionary <string, double>(); labelsAndWeights.Add("Precipitation", weights[0]); labelsAndWeights.Add("SoilWatFieldCapacity", weights[1]); labelsAndWeights.Add("Swawp", weights[2]); labelsAndWeights.Add("SoilWaterStorage", weights[2]); labelsAndWeights.Add("IrrigationEfficiency", weights[3]); labelsAndWeights.Add("MaxYield", weights[4]); labelsAndWeights.Add("GrossIrrig", weights[5]); labelsAndWeights.Add("InputCosts", weights[6]); labelsAndWeights.Add("OperationCosts", weights[7]); labelsAndWeights.Add("MiscCosts", weights[8]); labelsAndWeights.Add("IrrigCosts", weights[9]); labelsAndWeights.Add("MiscReturns", weights[10]); labelsAndWeights.Add("TotalIrrig", weights[11]); labelsAndWeights.Add("PriceUnit", weights[12]); return(labelsAndWeights); }
public static double PredictStockPrice(string SearchTerm, string StockCode, DateTime FromDate, DateTime ToDate) { Console.WriteLine("Retrieving historic data ({0} days available)", ToDate.Subtract(FromDate).Days); var samples = RetrieveEnties(SearchTerm, StockCode, FromDate, ToDate); Console.WriteLine("Calculating multiple regression"); int nAttributes = 3; int nSamples = samples.Count; double[,] tsData = new double[nSamples, nAttributes]; double[] resultData = new double[nSamples]; for (int i = 0; i < samples.Count; i++) { tsData[i, 0] = samples[i].FollowerCount; tsData[i, 1] = samples[i].MaxTweetDensity; tsData[i, 2] = samples[i].PositiveSentiment; resultData[i] = samples[i].ClosingPrice; } double[] weights = null; int fitResult = 0; alglib.lsfit.lsfitreport rep = new alglib.lsfit.lsfitreport(); alglib.lsfit.lsfitlinear(resultData, tsData, nSamples, nAttributes, ref fitResult, ref weights, rep); Console.WriteLine("Retrieving latest tweets"); var tweets = DataRetrieve.SearchTwitter(SearchTerm); var dayData = from t in tweets where t.CreatedAt.Date > DateTime.Today.AddDays(-_dayThreshold) && t.CreatedAt.Date <= DateTime.Today select t; var followerCount = (from row in dayData select row.FollowersCount + 1) .Sum(); var positive = (from row in dayData select row.Positive) .Sum() / dayData.Count(); var maxDensity = 0d; var time = DateTime.Now.AddHours(7); for (int i = 0; i <= 24 * _dayThreshold; i++) { var select = (from row in dayData where row.CreatedAt >= time.AddDays(-_dayThreshold).AddHours(i) && row.CreatedAt < time.AddDays(-_dayThreshold).AddHours(i + 1) select row.Positive); var density = select.Sum(); maxDensity = Math.Max(maxDensity, density); } var predictedPrice = weights[0] * followerCount + weights[1] * maxDensity + weights[2] * positive; return(predictedPrice); }
/// <summary> /// /// </summary> /// <param name="tables"></param>diningArea.tables /// <param name="customers"></param>diningArea.customers /// <param name="orders"></param>diningArea.orders /// <param name="finishedOrders"></param>training data /// <param name="allItem"></param> //make sure the itemId is continuous public void PredictWaitingTimeReg(List <Table> tables, List <Customer> customers, List <Order> orders, List <Order> finishedOrders, List <Item> allItem) { //HashSet<Item> hs = new HashSet<Item>(allItem);//去重 int nAttributes = allItem.Count + 1; int nSamples = finishedOrders.Count; double[,] tsData = new double[nSamples, nAttributes]; double[] resultData = new double[nSamples]; for (int i = 0; i < nSamples; i++) { for (int j = 0; j < nAttributes; j++) { tsData[i, j] = 0; } tsData[i, 0] = finishedOrders[i].NumberofPeople; foreach (var item in finishedOrders[i].Items) { tsData[i, item.ItemId] = item.ItemAmount; } TimeSpan ts = finishedOrders[i].FinishTime - finishedOrders[i].StartTime; resultData[i] = ts.TotalMinutes; } double[] weights = null; int fitResult = 0; alglib.lsfit.lsfitreport rep = new alglib.lsfit.lsfitreport(); alglib.lsfit.lsfitlinear(resultData, tsData, nSamples, nAttributes, ref fitResult, ref weights, rep); //about weight: weights[0] -> NumberofPeople,weights[1-allItem.Count]->ItemId,weights[allItem.Count+1 - nAttributes]->ItemAmount foreach (var t in tables) { if (t.TableStatus == TableStatus.Active) { t.CountDown = System.Data.SqlTypes.SqlDateTime.MinValue.Value; } else if (t.TableStatus == TableStatus.Cleaning) { //t.CountDown = DateTime.Now.AddMinutes(5); //this operation will be done when set tablestatus to cleaning. } else if (t.TableStatus == TableStatus.Reserved || t.TableStatus == TableStatus.Breakdown) { t.CountDown = System.Data.SqlTypes.SqlDateTime.MaxValue.Value; } else if (fitResult == 1 && t.OrderId > 0)//solved { Order currentOrder = orders.Find(x => x.OrderId == t.OrderId); double waitTime = weights[0] * currentOrder.NumberofPeople; foreach (var item in currentOrder.Items) { waitTime += weights[item.ItemId] * item.ItemAmount; } t.CountDown = currentOrder.StartTime.AddMinutes(waitTime); } } //Dictionary<int, double> labelsAndWeights = new Dictionary<int, double>(); //labelsAndWeights.Add(0, weights[0]); //labelsAndWeights.Add(1, weights[1]); //labelsAndWeights.Add(2, weights[2]); }