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 void startHierarchic_Click(object sender, EventArgs e)
        {
            ResultResearch result = new ResultResearch();
            result.Name = this.jobName;
            result.ModelType = typeof(HierarchicModel);
            result.Delta = Double.Parse(this.deltaExtendedTxt.Text);
            result.RealizationCount = (Int32)this.realizationCountNum.Value;
            result.Function = this.probabilityFunctionCmb.Text;
            result.GenerationParams[GenerationParam.BranchIndex] = Int16.Parse(this.branchIndexCmb.Text);
            result.GenerationParams[GenerationParam.Level] = Int16.Parse(this.maxLevelCmb.Text);
            result.Size = (int)Math.Pow(Int16.Parse(this.branchIndexCmb.Text), Int16.Parse(this.maxLevelCmb.Text));

            Int16 maxLevel = Int16.Parse(this.maxLevelCmb.Text);
            for (Int16 g = 1; g <= maxLevel; ++g)
            {
                result.Result.Add(g, new SortedDictionary<double, SubGraphsInfo>());
            }

            double muLow = Double.Parse(this.muRangeLowExtendedTxt.Text);
            double muHigh = Double.Parse(this.muRangeHighExtendedTxt.Text);
            double muDelta = Double.Parse(this.deltaExtendedTxt.Text);

            double muTemp = muLow;
            SubGraphsInfo tempInfo = new SubGraphsInfo();
            while (muTemp < muHigh)
            {
                SortedDictionary<double, SubGraphsInfo> r = AnalyzeHierarchic(muTemp);
                foreach (double gamma in r.Keys)
                {
                    result.Result[gamma].Add(muTemp, r[gamma]);
                }

                muTemp += muDelta;
            }

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

            MessageBox.Show("Results are saved succesfully!");
        }
        /*private void startExtended_Click(object sender, EventArgs e)
        {
            ResultResearch result = new ResultResearch();
            result.Name = this.jobName;
            result.ModelType = typeof(HierarchicModel);
            result.Delta = Double.Parse(this.deltaExtendedTxt.Text);
            result.RealizationCount = (Int32)this.realizationCountNum.Value;
            result.Function = this.probabilityFunctionCmb.Text;
            result.GenerationParams[GenerationParam.BranchIndex] = Int16.Parse(this.branchIndexCmb.Text);
            result.GenerationParams[GenerationParam.Level] = Int16.Parse(this.maxLevelCmb.Text);
            result.Size = (int)Math.Pow(Int16.Parse(this.branchIndexCmb.Text), Int16.Parse(this.maxLevelCmb.Text));

            Int16 maxLevel = Int16.Parse(this.maxLevelCmb.Text);
            for (Int16 g = 1; g <= maxLevel; ++g)
            {
                result.Result.Add(g, AnalyzeExtendedHierarchic(g));
            }

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

            MessageBox.Show("Results are saved succesfully!");
        }*/
        private void startER_Click(object sender, EventArgs e)
        {
            ResultResearch result = new ResultResearch();
            result.Name = this.jobName;
            result.ModelType = typeof(ERModel);

            // TODO remove hardcoded data
            result.Delta = 0.00001;
            result.RealizationCount = 100;
            result.GenerationParams[GenerationParam.Vertices] = 65536;
            SortedDictionary<double, SubGraphsInfo> pInfo =
                new SortedDictionary<double, SubGraphsInfo>();

            this.folderBrowserDialog1.ShowDialog();
            string dirName = this.folderBrowserDialog1.SelectedPath;

            DirectoryInfo parentDir = new DirectoryInfo(dirName);
            foreach (FileInfo f in parentDir.GetFiles())
            {
                double p = Double.Parse(f.Name.Remove(f.Name.Length - 4));
                SubGraphsInfo value = new SubGraphsInfo();

                SortedDictionary<int, double> r = new SortedDictionary<int, double>();
                using (StreamReader streamReader = new StreamReader(f.FullName, System.Text.Encoding.Default))
                {
                    string contents;

                    while ((contents = streamReader.ReadLine()) != null)
                    {
                        string first = "", second = "";
                        int j = 0;
                        while (contents[j] != ' ')
                        {
                            first += contents[j];
                            ++j;
                        }

                        second = contents.Substring(j);

                        r.Add(int.Parse(first), double.Parse(second));
                    }
                }

                value.avgOrder = r.First().Value;
                value.avgOrderCount = r.First().Key;

                if (r.Count > 1)
                {
                    r.Remove(r.First().Key);
                    value.secondMax = r.First().Value;
                    value.secondMaxCount = r.First().Key;
                }

                if (r.Count > 2)
                {
                    r.Remove(r.First().Key);
                    value.avgOrderRest = r.Average(x => x.Value);
                }

                pInfo.Add(p, value);
            }

            result.Result.Add(1, pInfo);

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

            MessageBox.Show("Results are saved succesfully!");
        }