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();
        }
        void SetInputValues(string input)
        {
            string text = File.ReadAllText(input);

            string[] lines = text.Split('\n');
            this.NumOfNewspapers = Int32.Parse(lines[1]);
            this.NumOfRecords    = Int32.Parse(lines[4]);
            this.PurchasePrice   = Decimal.Parse(lines[7]);
            this.ScrapPrice      = Decimal.Parse(lines[10]);
            this.SellingPrice    = Decimal.Parse(lines[13]);
            string[] DTD = lines[16].Split(',');

            DayTypeDistribution tmp1 = new DayTypeDistribution();

            tmp1.DayType     = Enums.DayType.Good;
            tmp1.Probability = Decimal.Parse(DTD[0]);
            this.DayTypeDistributions.Add(tmp1);

            DayTypeDistribution tmp2 = new DayTypeDistribution();

            tmp2.DayType     = Enums.DayType.Fair;
            tmp2.Probability = Decimal.Parse(DTD[1]);
            this.DayTypeDistributions.Add(tmp2);

            DayTypeDistribution tmp3 = new DayTypeDistribution();

            tmp3.DayType     = Enums.DayType.Poor;
            tmp3.Probability = Decimal.Parse(DTD[2]);
            this.DayTypeDistributions.Add(tmp3);

            for (int i = 19; i < lines.Length; i++)
            {
                DemandDistribution D  = new DemandDistribution();
                string[]           DD = lines[i].Split(',');
                D.Demand = Int32.Parse(DD[0]);

                DayTypeDistribution D1 = new DayTypeDistribution();
                D1.DayType     = Enums.DayType.Good;
                D1.Probability = Decimal.Parse(DD[1]);
                D.DayTypeDistributions.Add(D1);

                DayTypeDistribution D2 = new DayTypeDistribution();
                D2.DayType     = Enums.DayType.Fair;
                D2.Probability = Decimal.Parse(DD[2]);
                D.DayTypeDistributions.Add(D2);

                DayTypeDistribution D3 = new DayTypeDistribution();
                D3.DayType     = Enums.DayType.Poor;
                D3.Probability = Decimal.Parse(DD[3]);
                D.DayTypeDistributions.Add(D3);

                this.DemandDistributions.Add(D);
            }
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
        public List <DemandDistribution> demandFill(List <int> D, List <double> P1, List <double> P2, List <double> P3, Enums.DayType DT1, Enums.DayType DT2, Enums.DayType DT3)
        {
            List <DemandDistribution> res = new List <DemandDistribution>();
            int    count = D.Count;
            double c1, c2, c3 = c2 = c1 = 0;
            int    min1, min2, min3 = min2 = min1 = 1;

            for (int i = 0; i < count; i++)
            {
                DemandDistribution item = new DemandDistribution();
                item.Demand = D[i];
                DayTypeDistribution d1 = new DayTypeDistribution(), d2 = new DayTypeDistribution(), d3 = new DayTypeDistribution();
                //fill d1
                d1.setter(DT1, P1[i], ref c1, ref min1);
                item.DayTypeDistributions.Add(d1);
                //fill d2
                d2.setter(DT2, P2[i], ref c2, ref min2);
                item.DayTypeDistributions.Add(d2);
                //fill d3
                d3.setter(DT3, P3[i], ref c3, ref min3);
                item.DayTypeDistributions.Add(d3);
                res.Add(item);
            }
            if (c1 != 1)
            {
                invalidprob[0] = true;
            }
            if (c2 != 1)
            {
                invalidprob[1] = true;
            }
            if (c3 != 1)
            {
                invalidprob[2] = true;
            }
            return((c1 != 1 || c2 != 1 || c3 != 1) ? null : res);
        }
        public void readData(string path)
        {
            string[] lines = File.ReadAllLines(path);
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Equals("NumOfNewspapers"))
                {
                    i++;
                    system.NumOfNewspapers = int.Parse(lines[i]);
                }
                else if (lines[i].Equals("NumOfRecords"))
                {
                    i++;
                    system.NumOfRecords = int.Parse(lines[i]);
                }
                else if (lines[i].Equals("PurchasePrice"))
                {
                    i++;
                    system.PurchasePrice = decimal.Parse(lines[i]);
                }
                else if (lines[i].Equals("ScrapPrice"))
                {
                    i++;
                    system.ScrapPrice = decimal.Parse(lines[i]);
                }
                else if (lines[i].Equals("SellingPrice"))
                {
                    i++;
                    system.SellingPrice = decimal.Parse(lines[i]);
                }
                else if (lines[i].Equals("DayTypeDistributions"))
                {
                    i++;
                    string[] l = lines[i].Split(',');

                    DayTypeDistribution dayDistribution = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Good;
                    dayDistribution.MinRange    = 1;
                    dayDistribution.Probability = decimal.Parse(l[0]);
                    //Console.WriteLine((dayDistribution.Probability * (decimal)100.0).ToString());
                    dayDistribution.MaxRange = (int)double.Parse((dayDistribution.Probability * (decimal)100.0).ToString());
                    system.DayTypeDistributions.Add(dayDistribution);

                    dayDistribution             = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Fair;
                    dayDistribution.MinRange    = system.DayTypeDistributions.Last().MaxRange + 1;
                    dayDistribution.Probability = decimal.Parse(l[1]);
                    dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0 + dayDistribution.MinRange - 1).ToString());
                    system.DayTypeDistributions.Add(dayDistribution);

                    dayDistribution             = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Poor;
                    dayDistribution.MinRange    = system.DayTypeDistributions.Last().MaxRange + 1;
                    dayDistribution.Probability = decimal.Parse(l[2]);
                    dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0 + dayDistribution.MinRange - 1).ToString());
                    system.DayTypeDistributions.Add(dayDistribution);
                }
                else if (lines[i].Equals("DemandDistributions"))
                {
                    i++;
                    DemandDistribution demdistribution = new DemandDistribution();
                    string[]           l = lines[i].Split(',');
                    demdistribution.Demand = int.Parse(l[0]);

                    DayTypeDistribution dayDistribution = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Good;
                    dayDistribution.MinRange    = 1;
                    dayDistribution.Probability = decimal.Parse(l[1]);
                    dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0).ToString());
                    demdistribution.DayTypeDistributions.Add(dayDistribution);

                    dayDistribution             = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Fair;
                    dayDistribution.MinRange    = 1;
                    dayDistribution.Probability = decimal.Parse(l[2]);
                    dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0).ToString());
                    demdistribution.DayTypeDistributions.Add(dayDistribution);

                    dayDistribution             = new DayTypeDistribution();
                    dayDistribution.DayType     = Enums.DayType.Poor;
                    dayDistribution.MinRange    = 1;
                    dayDistribution.Probability = decimal.Parse(l[3]);
                    dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0).ToString());
                    demdistribution.DayTypeDistributions.Add(dayDistribution);

                    system.DemandDistributions.Add(demdistribution);
                    i++;
                    while (i < lines.Length)
                    {
                        demdistribution = new DemandDistribution();
                        l = lines[i].Split(',');
                        demdistribution.Demand = int.Parse(l[0]);

                        dayDistribution             = new DayTypeDistribution();
                        dayDistribution.DayType     = Enums.DayType.Good;
                        dayDistribution.MinRange    = system.DemandDistributions.Last().DayTypeDistributions[0].MaxRange + 1;
                        dayDistribution.Probability = decimal.Parse(l[1]);
                        dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0 + dayDistribution.MinRange - 1).ToString());
                        demdistribution.DayTypeDistributions.Add(dayDistribution);

                        dayDistribution             = new DayTypeDistribution();
                        dayDistribution.DayType     = Enums.DayType.Fair;
                        dayDistribution.MinRange    = system.DemandDistributions.Last().DayTypeDistributions[1].MaxRange + 1;
                        dayDistribution.Probability = decimal.Parse(l[2]);
                        dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0 + dayDistribution.MinRange - 1).ToString());
                        demdistribution.DayTypeDistributions.Add(dayDistribution);

                        dayDistribution             = new DayTypeDistribution();
                        dayDistribution.DayType     = Enums.DayType.Poor;
                        dayDistribution.MinRange    = system.DemandDistributions.Last().DayTypeDistributions[2].MaxRange + 1;
                        dayDistribution.Probability = decimal.Parse(l[3]);
                        dayDistribution.MaxRange    = (int)double.Parse((dayDistribution.Probability * (decimal)100.0 + dayDistribution.MinRange - 1).ToString());
                        demdistribution.DayTypeDistributions.Add(dayDistribution);

                        system.DemandDistributions.Add(demdistribution); i++;
                    }
                }
            }
        }
        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
        }