Esempio n. 1
0
 public void ReadINPUT(string textfilename)
 {
     string[] Separator = new string[1] {
         "," + " "
     };
     string[] lines = System.IO.File.ReadAllLines("../../TestCases/" + textfilename + ".txt");
     NumOfNewspapers = Convert.ToInt32(lines.ElementAt(1));
     NumOfRecords    = Convert.ToInt32(lines.ElementAt(4));
     PurchasePrice   = Convert.ToDecimal(lines.ElementAt(7));
     SellingPrice    = Convert.ToDecimal(lines.ElementAt(13));
     ScrapPrice      = Convert.ToDecimal(lines.ElementAt(10));
     UnitProfit      = SellingPrice - PurchasePrice;
     string[] DayTypes = lines.ElementAt(16).Split(Separator, StringSplitOptions.None);
     for (int i = 0; i < 3; i++)
     {
         DayTypeDistribution DTD = new DayTypeDistribution((Enums.DayType)(i), Convert.ToDecimal(DayTypes[i]));
         DayTypeDistributions.Add(DTD);
     }
     for (int i = 19; i < lines.Count(); i++)
     {
         DayTypes = lines.ElementAt(i).Split(Separator, StringSplitOptions.None);
         DemandDistribution DD = new DemandDistribution();
         DD.Demand = Convert.ToInt32(DayTypes[0]);
         for (int j = 1; j <= 3; j++)
         {
             DayTypeDistribution DTD = new DayTypeDistribution((Enums.DayType)(j - 1), Convert.ToDecimal(DayTypes[j]));
             DD.DayTypeDistributions.Add(DTD);
         }
         DemandDistributions.Add(DD);
     }
     DistributionsOfNewsDays();
     DistributionsOfDemand();
 }
        public void getSystemInformation(string filePath)
        {
            string line;

            System.IO.StreamReader file =
                new System.IO.StreamReader(filePath);
            List <string> data = new List <string>();

            while ((line = file.ReadLine()) != null)
            {
                if (line.Length > 0 && line[0] >= '0' && line[0] <= '9')
                {
                    data.Add(line);
                }
            }
            NumOfNewspapers = int.Parse(data[0]);
            NumOfRecords    = int.Parse(data[1]);
            PurchasePrice   = decimal.Parse(data[2]);
            ScrapPrice      = decimal.Parse(data[3]);
            SellingPrice    = decimal.Parse(data[4]);
            UnitProfit      = SellingPrice - PurchasePrice;
            DayTypeDistribution typeTemp = new DayTypeDistribution();

            typeTemp.setData(data[5], DayTypeDistributions);
            DemandDistribution demandTemp = new DemandDistribution();

            for (int i = 6; i < data.Count(); i++)
            {
                demandTemp = new DemandDistribution();
                demandTemp.setData(data[i]);
                DemandDistributions.Add(demandTemp);
            }
            demandTemp.postProcess(DemandDistributions);
            file.Close();
        }
Esempio n. 3
0
        public void FillDemandDistrubution(string Entry)
        {
            List <int>         DemandValues = new List <int>();
            List <List <int> > TableValues  = new List <List <int> >();

            string[] Entries = Entry.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string s in Entries)
            {
                string curDemand = s.Split(',')[0];
                DemandValues.Add(Convert.ToInt32(curDemand));
                string Values = s.Replace(curDemand + ',', "");
                TableValues.Add(new List <int>()
                {
                    int.Parse(Values.Split(',')[0].Replace("0.", "")),
                    int.Parse(Values.Split(',')[1].Replace("0.", "")),
                    int.Parse(Values.Split(',')[2].Replace("0.", "")),
                });
            }

            int[] Accumaltions = { 0, 0, 0 };

            for (int i = 0; i < DemandValues.Count; i++)
            {
                DemandDistribution DDi = new DemandDistribution();
                DDi.Demand = DemandValues[i];

                for (int j = 0; j < 3; j++)
                {
                    DDi.DayTypeDistributions.Add(new DayTypeDistribution());
                    DDi.DayTypeDistributions.Last().MinRange = Accumaltions[j] + 1;
                    if (DDi.DayTypeDistributions.Last().MinRange > 100)
                    {
                        DDi.DayTypeDistributions.Last().MinRange = 100;
                    }
                    DDi.DayTypeDistributions.Last().DayType = (Enums.DayType)j;
                    Accumaltions[j] += TableValues[i][j];
                    DDi.DayTypeDistributions.Last().Probability = Convert.ToDecimal("0." + TableValues[i][j].ToString());
                    DDi.DayTypeDistributions.Last().MaxRange    = Accumaltions[j];
                }
                DemandDistributions.Add(DDi);
            }
        }
        public void readfile(string fna)
        {
            FileStream   fs = new FileStream(fna, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            while (sr.Peek() != -1)
            {
                string fineline = sr.ReadLine();
                if (fineline == "")
                {
                    continue;
                }
                else if (fineline == "NumOfNewspapers")
                {
                    NumOfNewspapers = int.Parse(sr.ReadLine());
                }
                else if (fineline == "NumOfRecords")
                {
                    NumOfRecords = int.Parse(sr.ReadLine());
                }
                else if (fineline == "PurchasePrice")
                {
                    PurchasePrice = decimal.Parse(sr.ReadLine());
                }
                else if (fineline == "ScrapPrice")
                {
                    ScrapPrice = decimal.Parse(sr.ReadLine());
                }
                else if (fineline == "SellingPrice")
                {
                    SellingPrice = decimal.Parse(sr.ReadLine());
                    UnitProfit   = SellingPrice - PurchasePrice;
                }
                else if (fineline == "DayTypeDistributions")
                {
                    string[] g  = sr.ReadLine().Split(',');
                    int      cp = 0;
                    for (int f = 0; f < g.Length; f++)
                    {
                        DayTypeDistribution xy = new DayTypeDistribution();
                        int prob = Convert.ToInt32(decimal.Parse(g[f]) * 100);
                        xy.Probability     = prob;
                        xy.MinRange        = cp + 1;
                        cp                += prob;
                        xy.CummProbability = cp;
                        xy.MaxRange        = cp;
                        if (f == 0)
                        {
                            xy.DayType = Enums.DayType.Good;
                        }
                        else if (f == 1)
                        {
                            xy.DayType = Enums.DayType.Fair;
                        }
                        else if (f == 2)
                        {
                            xy.DayType = Enums.DayType.Poor;
                        }
                        DayTypeDistributions.Add(xy);
                    }
                }
                else if (fineline == "DemandDistributions")
                {
                    int [] cp = new int[3];
                    while (sr.Peek() != -1)
                    {
                        string td = sr.ReadLine();
                        if (td == "" || td == null)
                        {
                            break;
                        }
                        string[]           l = td.Split(',');
                        DemandDistribution u = new DemandDistribution();
                        u.Demand = int.Parse(l[0]);
                        for (int f = 0; f < 3; f++)
                        {
                            DayTypeDistribution xy = new DayTypeDistribution();
                            int prob = Convert.ToInt32(decimal.Parse(l[f + 1]) * 100);
                            xy.Probability     = prob;
                            xy.MinRange        = cp[f] + 1;
                            cp[f]             += prob;
                            xy.CummProbability = cp[f];
                            xy.MaxRange        = cp[f];
                            if (f == 0)
                            {
                                xy.DayType = Enums.DayType.Good;
                            }
                            else if (f == 1)
                            {
                                xy.DayType = Enums.DayType.Fair;
                            }
                            else if (f == 2)
                            {
                                xy.DayType = Enums.DayType.Poor;
                            }
                            u.DayTypeDistributions.Add(xy);
                        }
                        DemandDistributions.Add(u);
                    }
                }
            }
            fs.Close();
        }
        public void InputData(string[] lines)
        {
            int stringType = 0;

            char[] delimiters = { ',', ' ' };
            foreach (string line in lines)
            {
                if (line == "\n" || line.Length == 0)
                {
                    continue;
                }
                switch (line)
                {
                case "NumOfNewspapers":
                    stringType = 1;
                    continue;

                case "NumOfRecords":
                    stringType = 2;
                    continue;

                case "PurchasePrice":
                    stringType = 3;
                    continue;

                case "ScrapPrice":
                    stringType = 4;
                    continue;

                case "SellingPrice":
                    stringType = 5;
                    continue;

                case "DayTypeDistributions":
                    stringType = 6;
                    continue;

                case "DemandDistributions":
                    stringType = 7;
                    continue;
                }

                switch (stringType)
                {
                case 1:
                    NumOfNewspapers = int.Parse(line);
                    break;

                case 2:
                    NumOfRecords = int.Parse(line);
                    break;

                case 3:
                    PurchasePrice = decimal.Parse(line);
                    break;

                case 4:
                    ScrapPrice = decimal.Parse(line);
                    break;

                case 5:
                    SellingPrice = decimal.Parse(line);
                    break;

                case 6:
                    string[] distributions = line.Split(delimiters);
                    int      type          = 0;
                    decimal  cummProb      = 0;
                    foreach (string distribution in distributions)
                    {
                        if (distribution.Length == 0)
                        {
                            continue;
                        }
                        DayTypeDistribution DTD = new DayTypeDistribution();
                        DTD.DayType         = (Enums.DayType)type;
                        DTD.Probability     = decimal.Parse(distribution);
                        DTD.MinRange        = decimal.ToInt32(cummProb * 100 + 1);
                        cummProb           += DTD.Probability;
                        DTD.MaxRange        = decimal.ToInt32(cummProb * 100);
                        DTD.CummProbability = cummProb;
                        type += 1;

                        DayTypeDistributions.Add(DTD);
                    }
                    break;

                case 7:
                    string[]           numbers   = line.Split(delimiters);
                    int                typeCase7 = 0;
                    bool               flag      = false;
                    DemandDistribution DD        = new DemandDistribution();
                    foreach (string number in numbers)
                    {
                        if (number.Length == 0)
                        {
                            continue;
                        }
                        if (flag)
                        {
                            DayTypeDistribution DTD = new DayTypeDistribution();
                            DTD.DayType     = (Enums.DayType)typeCase7;
                            DTD.Probability = decimal.Parse(number);
                            typeCase7      += 1;

                            DD.DayTypeDistributions.Add(DTD);
                        }
                        else
                        {
                            flag      = true;
                            DD.Demand = int.Parse(number);
                        }
                    }
                    DemandDistributions.Add(DD);
                    break;
                }
            }

            decimal[] cummSum = { 0, 0, 0 };
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < DemandDistributions.Count; j++)
                {
                    DemandDistributions[j].DayTypeDistributions[i].MinRange = decimal.ToInt32(cummSum[i] * 100 + 1);
                    cummSum[i] += DemandDistributions[j].DayTypeDistributions[i].Probability;
                    DemandDistributions[j].DayTypeDistributions[i].MaxRange        = decimal.ToInt32(cummSum[i] * 100);
                    DemandDistributions[j].DayTypeDistributions[i].CummProbability = cummSum[i];
                }
            }

            UnitProfit = SellingPrice - PurchasePrice;
        }
        public void LoadCase(string fileName)
        {
            var lines = File.ReadAllLines(path + fileName);

            NumOfNewspapers = int.Parse(lines[1]);
            NumOfRecords    = int.Parse(lines[4]);
            PurchasePrice   = decimal.Parse(lines[7]);
            ScrapPrice      = decimal.Parse(lines[10]);
            SellingPrice    = decimal.Parse(lines[13]);
            UnitProfit      = SellingPrice - PurchasePrice;

            #region
            // day type distribution
            var DayTypes = lines[16].Split(',');
            var cummProb = 0.0m;
            foreach (var day in DayTypes)
            {
                var dayTypeDistribution = new DayTypeDistribution
                {
                    Probability = decimal.Parse(day),
                    MinRange    = (int)(cummProb * 100) + 1
                };
                cummProb += dayTypeDistribution.Probability;
                dayTypeDistribution.CummProbability = cummProb;
                dayTypeDistribution.MaxRange        = (int)(cummProb * 100);

                DayTypeDistributions.Add(dayTypeDistribution);
            }
            DayTypeDistributions[0].DayType = Enums.DayType.Good;
            DayTypeDistributions[1].DayType = Enums.DayType.Fair;
            DayTypeDistributions[2].DayType = Enums.DayType.Poor;
            #endregion

            #region
            // Demand distribution
            decimal goodCumm = 0.0m, fairCumm = 0.0m, poorCum = 0.0m;
            for (int i = 19; i < lines.Length; i++)
            {
                var values = lines[i].Split(',');                       // demand, good, fair, poor

                var goodDay = new DayTypeDistribution
                {
                    DayType     = Enums.DayType.Good,
                    Probability = decimal.Parse(values[1]),
                    MinRange    = (int)(goodCumm * 100) + 1,
                    MaxRange    = (int)((goodCumm + decimal.Parse(values[1])) * 100)
                };
                goodCumm += goodDay.Probability;
                goodDay.CummProbability = goodCumm;

                var fairDay = new DayTypeDistribution
                {
                    DayType     = Enums.DayType.Fair,
                    Probability = decimal.Parse(values[2]),
                    MinRange    = (int)(fairCumm * 100) + 1,
                    MaxRange    = (int)((fairCumm + decimal.Parse(values[2])) * 100)
                };
                fairCumm += fairDay.Probability;
                fairDay.CummProbability = fairCumm;

                var poorDay = new DayTypeDistribution
                {
                    DayType     = Enums.DayType.Poor,
                    Probability = decimal.Parse(values[3]),
                    MinRange    = (int)(poorCum * 100) + 1,
                    MaxRange    = (int)((poorCum + decimal.Parse(values[3])) * 100)
                };
                poorCum += poorDay.Probability;
                fairDay.CummProbability = poorCum;

                var demandDistribution = new DemandDistribution
                {
                    Demand = int.Parse(values[0])
                };
                if (goodDay.Probability > 0)
                {
                    demandDistribution.DayTypeDistributions.Add(goodDay);
                }
                if (fairDay.Probability > 0)
                {
                    demandDistribution.DayTypeDistributions.Add(fairDay);
                }
                if (poorDay.Probability > 0)
                {
                    demandDistribution.DayTypeDistributions.Add(poorDay);
                }

                DemandDistributions.Add(demandDistribution);
            }
            #endregion
        }