private SortedDictionary<double, double> AnalyzeAvgHierarchic(double mu)
        {
            Int16 p = Int16.Parse(this.branchIndexCmb.Text);
            Int16 maxLevel = Int16.Parse(this.maxLevelCmb.Text);
            int realizationCount = (Int32)this.realizationCountNum.Value;

            SortedDictionary<double, double> result = new SortedDictionary<double, double>();
            for (Int16 i = 1; i <= maxLevel; ++i)
            {
                result.Add(i, 0);
            }

            Dictionary<GenerationParam, object> genParameters = new Dictionary<GenerationParam, object>();
            genParameters.Add(GenerationParam.BranchIndex, p);
            genParameters.Add(GenerationParam.Level, maxLevel);
            genParameters.Add(GenerationParam.Mu, mu);

            HierarchicGenerator hGenerator = new HierarchicGenerator();
            HierarchicAnalyzer hAnalyzer;

            for (int r = 0; r < realizationCount; ++r)
            {
                hGenerator.Generation(genParameters);
                hAnalyzer = new HierarchicAnalyzer((HierarchicContainer)hGenerator.Container);

                for (Int16 i = 1; i <= maxLevel; ++i)
                {
                    double maxOrder = hAnalyzer.GetAvgConnSubGraphPerLevel(i);
                    double normMaxOrder = maxOrder / (Math.Pow(p, i));
                    result[i] += normMaxOrder;
                }
            }

            for (Int16 i = 1; i <= maxLevel; ++i)
            {
                result[i] /= realizationCount;
            }

            return result;
        }
        private void startGlobal_Click(object sender, EventArgs e)
        {
            Int16 p = Int16.Parse(this.branchIndexCmb.Text);
            Int16 maxLevel = Int16.Parse(this.maxLevelCmb.Text);
            int realizationCount = (Int32)this.realizationCountNum.Value;
            double muLow = Double.Parse(this.muRangeLowExtendedTxt.Text);
            double muHigh = Double.Parse(this.muRangeHighExtendedTxt.Text);
            double muDelta = Double.Parse(this.deltaExtendedTxt.Text);

            ResultResearch result = new ResultResearch();
            result.Name = this.jobName;
            result.ModelType = typeof(HierarchicModel);
            result.Delta = muDelta;
            result.RealizationCount = realizationCount;
            result.Function = this.probabilityFunctionCmb.Text;
            result.GenerationParams[GenerationParam.BranchIndex] = p;
            result.GenerationParams[GenerationParam.Level] = maxLevel;
            result.Size = (int)Math.Pow(p, maxLevel);
            result.Result.Add(maxLevel, new SortedDictionary<double, SubGraphsInfo>());

            Dictionary<GenerationParam, object> genParameters = new Dictionary<GenerationParam, object>();
            genParameters.Add(GenerationParam.BranchIndex, p);
            genParameters.Add(GenerationParam.Level, maxLevel);

            HierarchicGenerator hGenerator = new HierarchicGenerator();
            HierarchicAnalyzer hAnalyzer;

            double muTemp = muLow;

            SortedDictionary<int, int> subGraphInfo = new SortedDictionary<int,int>();
            while (muTemp <= muHigh)
            {
                SubGraphsInfo tempInfo = new SubGraphsInfo();
                for (int r = 0; r < realizationCount; ++r)
                {
                    genParameters[GenerationParam.Mu] = muTemp;
                    hGenerator.Generation(genParameters);
                    hAnalyzer = new HierarchicAnalyzer((HierarchicContainer)hGenerator.Container);
                    subGraphInfo = hAnalyzer.GetConnSubGraph();

                    tempInfo.avgOrder += subGraphInfo.Last().Key;
                    tempInfo.avgOrderCount += subGraphInfo.Last().Value;

                    if (subGraphInfo.Count > 1)
                    {
                        subGraphInfo.Remove(subGraphInfo.Last().Key);
                        tempInfo.secondMax += subGraphInfo.Last().Key;
                        tempInfo.secondMaxCount += subGraphInfo.Last().Value;
                    }

                    if (subGraphInfo.Count > 2)
                    {
                        subGraphInfo.Remove(subGraphInfo.Last().Key);
                        tempInfo.avgOrderRest += subGraphInfo.Average(x => x.Key);
                    }
                }
                tempInfo.avgOrder /= (double)realizationCount;
                tempInfo.avgOrder /= result.Size;
                tempInfo.avgOrderCount /= (double)realizationCount;

                tempInfo.secondMax /= (double)realizationCount;
                tempInfo.secondMax /= result.Size;
                tempInfo.secondMaxCount /= (double)realizationCount;

                tempInfo.avgOrderRest /= (double)realizationCount;
                tempInfo.avgOrderRest /= result.Size;

                result.Result[maxLevel].Add(muTemp, tempInfo);

                muTemp += muDelta;
            }

            XMLResultStorage storage = new XMLResultStorage(Options.StorageDirectory);
            storage.SaveResearch(result);

            MessageBox.Show("Results are saved succesfully!");
        }
        private SortedDictionary<double, SubGraphsInfo> AnalyzeHierarchic(double mu)
        {
            Int16 p = Int16.Parse(this.branchIndexCmb.Text);
            Int16 maxLevel = Int16.Parse(this.maxLevelCmb.Text);
            int realizationCount = (Int32)this.realizationCountNum.Value;

            SortedDictionary<double, SubGraphsInfo> result = new SortedDictionary<double, SubGraphsInfo>();
            for (Int16 i = 1; i <= maxLevel; ++i)
            {
                result.Add(i, new SubGraphsInfo());
            }

            Dictionary<GenerationParam, object> genParameters = new Dictionary<GenerationParam, object>();
            genParameters.Add(GenerationParam.BranchIndex, p);
            genParameters.Add(GenerationParam.Level, maxLevel);
            genParameters.Add(GenerationParam.Mu, mu);

            HierarchicGenerator hGenerator = new HierarchicGenerator();
            HierarchicAnalyzer hAnalyzer;

            for (int r = 0; r < realizationCount; ++r)
            {
                hGenerator.Generation(genParameters);
                hAnalyzer = new HierarchicAnalyzer((HierarchicContainer)hGenerator.Container);

                SortedDictionary<int, int> subGraphInfo;
                for (Int16 i = 1; i <= maxLevel; ++i)
                {
                    subGraphInfo = hAnalyzer.GetConnSubGraphPerLevel(i);

                    result[i].avgOrder += (subGraphInfo.Count != 0) ?
                        subGraphInfo.Last().Key / Math.Pow(p, i) : 1 / Math.Pow(p, i);
                    result[i].avgOrderCount += (subGraphInfo.Count != 0) ?
                        subGraphInfo.Last().Value : 1;

                    if (subGraphInfo.Count > 1)
                    {
                        subGraphInfo.Remove(subGraphInfo.Last().Key);
                        result[i].secondMax += subGraphInfo.Last().Key / Math.Pow(p, i);
                        result[i].secondMaxCount += subGraphInfo.Last().Value;
                    }

                    if (subGraphInfo.Count > 2)
                    {
                        subGraphInfo.Remove(subGraphInfo.Last().Key);
                        result[i].avgOrderRest += subGraphInfo.Average(x => x.Key) / Math.Pow(p, i);
                    }
                }
            }

            for (Int16 i = 1; i <= maxLevel; ++i)
            {
                result[i].avgOrder /= (double)realizationCount;
                result[i].avgOrderCount /= (double)realizationCount;

                result[i].secondMax /= (double)realizationCount;
                result[i].secondMaxCount /= (double)realizationCount;

                result[i].avgOrderRest /= (double)realizationCount;
            }

            return result;
        }