Esempio n. 1
0
        /// <summary>
        /// Averages specified results by realization count.
        /// </summary>
        /// <param name="results">List of results for realizations.</param>
        /// <returns>Ensemble result, which represents avereged values of realizations results.</returns>
        public static EnsembleResult AverageResults(List <RealizationResult> results)
        {
            Debug.Assert(results.Count != 0);

            EnsembleResult r = new EnsembleResult(results[0].NetworkSize);

            double rCount = results.Count;

            foreach (RealizationResult res in results)
            {
                r.EdgesCount += res.EdgesCount;
            }
            r.EdgesCount /= rCount;
            Math.Round(r.EdgesCount, 4);

            foreach (AnalyzeOption option in results[0].Result.Keys)
            {
                AnalyzeOptionInfo[] info = (AnalyzeOptionInfo[])option.GetType().GetField(option.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false);
                OptionType          ot   = info[0].OptionType;

                #region EIGENVALUES
                if (option == AnalyzeOption.EigenValues)
                {
                    FillEigenValuesResults(option, results, r);
                    continue;
                }
                else if (option == AnalyzeOption.EigenDistanceDistribution)
                {
                    FillEigenDistanceDistributionResults(option, results, r);
                    continue;
                }
                #endregion

                Debug.Assert(option != AnalyzeOption.EigenValues && option != AnalyzeOption.EigenDistanceDistribution);
                switch (ot)
                {
                case OptionType.Global:
                    FillDoubleResults(option, results, r);
                    break;

                case OptionType.ValueList:
                case OptionType.Centrality:
                    FillListResults(option, results, r);
                    break;

                case OptionType.Distribution:
                    FillDictionaryResults(option, results, r);
                    break;

                case OptionType.Trajectory:
                    FillTrajectoryResults(option, results, r);
                    break;

                default: { break; }
                }
            }

            return(r);
        }
        /// <summary>
        /// Averages specified results by realization count.
        /// </summary>
        /// <param name="results">List of results for realizations.</param>
        /// <returns>Ensemble result, which represents avereged values of realizations results.</returns>
        public static EnsembleResult AverageResults(List<RealizationResult> results)
        {
            Debug.Assert(results.Count != 0);

            EnsembleResult r = new EnsembleResult();
            r.NetworkSize = results[0].NetworkSize;

            double rCount = results.Count;
            foreach (RealizationResult res in results)
            {
                r.EdgesCount += res.EdgesCount;
            }
            r.EdgesCount /= rCount;
            Math.Round(r.EdgesCount, 4);

            foreach (AnalyzeOption option in results[0].Result.Keys)
            {
                AnalyzeOptionInfo[] info = (AnalyzeOptionInfo[])option.GetType().GetField(option.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false);
                OptionType ot = info[0].OptionType;

                #region EIGENVALUES
                if (option == AnalyzeOption.EigenValues)
                {
                    FillEigenValuesResults(option, results, r);
                    continue;
                }
                else if (option == AnalyzeOption.EigenDistanceDistribution)
                {
                    FillEigenDistanceDistributionResults(option, results, r);
                    continue;
                }
                #endregion

                Debug.Assert(option != AnalyzeOption.EigenValues && option != AnalyzeOption.EigenDistanceDistribution);
                switch (ot)
                {
                    case OptionType.Global:
                        FillDoubleResults(option, results, r);
                        break;
                    case OptionType.ValueList:
                    case OptionType.Centrality:
                        FillListResults(option, results, r);
                        break;
                    case OptionType.Distribution:
                    case OptionType.Trajectory:
                        FillDictionaryResults(option, results, r);
                        break;
                    default: { break; }
                }
            }

            return r;
        }
 private static void FillEigenValuesResults(AnalyzeOption option, List<RealizationResult> results, EnsembleResult r)
 {
     Debug.Assert(option == AnalyzeOption.EigenValues);
     List<Double> temp = new List<Double>();
     foreach (RealizationResult res in results)
     {
         Debug.Assert(res.Result[option] is List<Double>);
         List<Double> e = res.Result[option] as List<Double>;
         foreach (Double k in e)
             temp.Add(k);
     }
     r.Result.Add(option, temp);
 }
 private static void FillEigenDistanceDistributionResults(AnalyzeOption option, List<RealizationResult> results, EnsembleResult r)
 {
     Debug.Assert(option == AnalyzeOption.EigenDistanceDistribution);
     SortedDictionary<Double, Double> temp = new SortedDictionary<Double, Double>();
     foreach (RealizationResult res in results)
     {
         Debug.Assert(res.Result[option] is SortedDictionary<Double, Double>);
         SortedDictionary<Double, Double> d = res.Result[option] as SortedDictionary<Double, Double>;
         foreach (double k in d.Keys)
         {
             if (temp.ContainsKey(k))
                 temp[k] += d[k];
             else
                 temp.Add(k, d[k]);
         }
     }
     r.Result.Add(option, temp);
 }
 private static void FillDoubleResults(AnalyzeOption option, List<RealizationResult> results, EnsembleResult r)
 {
     double temp = 0;
     foreach (RealizationResult res in results)
     {
         temp += (double)(res.Result[option]) / results.Count;
     }
     r.Result.Add(option, double.IsNaN(temp) ? 0 : Math.Round(temp, 4));
 }
 private static void FillDictionaryResults(AnalyzeOption option, List<RealizationResult> results, EnsembleResult r)
 {
     SortedDictionary<Double, Double> temp = new SortedDictionary<Double, Double>();
     foreach (RealizationResult res in results)
     {
         Debug.Assert(res.Result[option] is SortedDictionary<Double, Double>);
         SortedDictionary<Double, Double> d = res.Result[option] as SortedDictionary<Double, Double>;
         foreach (double k in d.Keys)
         {
             if (temp.ContainsKey(k))
                 temp[k] += Math.Round(d[k] / results.Count, 4);
             else
                 temp.Add(k, Math.Round(d[k] / results.Count, 4));
         }
     }
     r.Result.Add(option, temp);
 }
        /// <summary>
        /// Averages specified results by realization count.
        /// </summary>
        /// <param name="results">List of results for realizations.</param>
        /// <returns>Ensemble result, which represents avereged values of realizations results.</returns>
        public static EnsembleResult AverageResults(List<RealizationResult> results)
        {
            EnsembleResult r = new EnsembleResult();
            r.NetworkSize = results[0].NetworkSize;
            r.Result = new Dictionary<AnalyzeOption, object>();

            double rCount = results.Count;
            foreach (RealizationResult res in results)
            {
                r.EdgesCount += res.EdgesCount;
            }
            r.EdgesCount /= rCount;
            Math.Round(r.EdgesCount, 4);

            foreach (AnalyzeOption option in results[0].Result.Keys)
            {
                AnalyzeOptionInfo[] info = (AnalyzeOptionInfo[])option.GetType().GetField(option.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false);
                Type t = info[0].RealizationResultType;

                if(t.Equals(typeof(Double)))
                {
                    double temp = 0;
                    foreach (RealizationResult res in results)
                    {
                        temp += (double)(res.Result[option]) / rCount;
                    }
                    r.Result.Add(option, Math.Round(temp, 4));
                }
                else if(t.Equals(typeof(UInt32)))
                {
                    double temp = 0;
                    uint uTemp = 0;
                    foreach (RealizationResult res in results)
                    {
                        uTemp = (UInt32)res.Result[option];
                        temp += uTemp / rCount;
                    }
                    r.Result.Add(option, Math.Round(temp, 4));
                }
                else if(t.Equals(typeof(List<Double>)))
                {
                    List<Double> temp = new List<double>(results[0].Result[option] as List<Double>);
                    for (int i = 0; i < temp.Count; ++i)
                        temp[i] /= rCount;

                    // TODO check the theory logic of averaging eigen values and round to 3 digits
                    /*for (int i = 1; i < results.Count; ++i)
                    {
                        List<Double> l = results[i].Result[option] as List<Double>;
                        for (int j = 0; j < l.Count; ++j)
                            temp[j] += l[j] / rCount;
                    }*/
                    r.Result.Add(option, temp);
                }
                else if (t.Equals(typeof(SortedDictionary<Double, UInt32>)))
                {
                    SortedDictionary<Double, Double> temp = new SortedDictionary<double, double>();
                    foreach (RealizationResult res in results)
                    {
                        SortedDictionary<Double, UInt32> d = res.Result[option] as SortedDictionary<Double, UInt32>;
                        foreach (double k in d.Keys)
                        {
                            if (temp.ContainsKey(k))
                                temp[k] += Math.Round(d[k] / rCount, 3);
                            else
                                temp.Add(k, Math.Round(d[k] / rCount, 3));
                        }
                    }
                    r.Result.Add(option, temp);
                }
                else if (t.Equals(typeof(SortedDictionary<UInt32, UInt32>)))
                {
                    SortedDictionary<UInt32, Double> temp = new SortedDictionary<uint, double>();
                    foreach (RealizationResult res in results)
                    {
                        SortedDictionary<UInt32, UInt32> d = res.Result[option] as SortedDictionary<UInt32, UInt32>;
                        foreach (uint k in d.Keys)
                        {
                            if (temp.ContainsKey(k))
                                temp[k] += Math.Round(d[k] / rCount, 3);
                            else
                                temp.Add(k, Math.Round(d[k] / rCount, 3));
                        }
                    }
                    r.Result.Add(option, temp);
                }
                else if (t.Equals(typeof(SortedDictionary<UInt32, Double>)))
                {
                    SortedDictionary<UInt32, Double> temp = new SortedDictionary<uint, double>();
                    foreach (RealizationResult res in results)
                    {
                        SortedDictionary<UInt32, Double> d = res.Result[option] as SortedDictionary<UInt32, Double>;
                        foreach (uint k in d.Keys)
                        {
                            if (temp.ContainsKey(k))
                                temp[k] += Math.Round(d[k] / rCount, 3);
                            else
                                temp.Add(k, Math.Round(d[k] / rCount, 3));
                        }
                    }
                    r.Result.Add(option, temp);
                }
                else if (t.Equals(typeof(SortedDictionary<UInt16, Double>)))
                {
                    SortedDictionary<UInt16, Double> temp = new SortedDictionary<UInt16, Double>();
                    foreach (RealizationResult res in results)
                    {
                        SortedDictionary<UInt16, Double> d = res.Result[option] as SortedDictionary<UInt16, Double>;
                        foreach (UInt16 k in d.Keys)
                        {
                            if (temp.ContainsKey(k))
                                temp[k] += Math.Round(d[k] / rCount, 3);
                            else
                                temp.Add(k, Math.Round(d[k] / rCount, 3));
                        }
                    }
                    r.Result.Add(option, temp);
                }
            }

            return r;
        }
        private void SaveEnsembleResult(EnsembleResult e, int id)
        {
            foreach (AnalyzeOption opt in e.Result.Keys)
            {
                AnalyzeOptionInfo info = ((AnalyzeOptionInfo[])opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false))[0];
                OptionType optionType = info.OptionType;

                switch (optionType)
                {
                    case OptionType.Global:
                        SaveToGlobalSheet(id, info, e.Result[opt]);
                        break;
                    case OptionType.ValueList:
                        SaveValueListSheet(id, info, e.Result[opt]);
                        break;
                    case OptionType.Centrality:
                        SaveCentralitySheet(id, info, e.Result[opt]);
                        break;
                    case OptionType.Distribution:
                    case OptionType.Trajectory:
                        SaveDistributionSheet(id, info, e.Result[opt]);
                        break;
                    default:
                        break;
                }
            }
        }
        /// <summary>
        /// Calculates the average value of specified global analyze option.
        /// </summary>
        /// <param name="opt">Analyze option.</param>
        public void CalculateGlobalOption(AnalyzeOption opt)
        {
            if (EnsembleResultsAvg.Count != 0 && EnsembleResultsAvg[0].Result.ContainsKey(opt))
                return;

            ResearchResult tr = null;
            AnalyzeOptionInfo[] info = (AnalyzeOptionInfo[])opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false);
            Debug.Assert(info[0].OptionType == OptionType.Global);

            double avg = 0;
            foreach (Guid id in researches)
            {
                tr = StatSessionManager.GetResearchResult(id);
                Debug.Assert(tr.EnsembleResults.Count == 1);
                Debug.Assert(tr.EnsembleResults[0].Result[opt] != null);
                avg += Convert.ToDouble(tr.EnsembleResults[0].Result[opt]) * tr.RealizationCount;
            }
            avg /= RealizationCountSum;
            EnsembleResult er = null;
            if (EnsembleResultsAvg.Count == 0)
            {
                er = new EnsembleResult();
                EnsembleResultsAvg.Add(er);
            }
            else
                er = EnsembleResultsAvg[0];
            er.Result.Add(opt, Math.Round(avg, 4));
        }
        private void SaveEnsembleResult(EnsembleResult e, int id)
        {
            writer.WriteStartElement("Ensemble");
            writer.WriteAttributeString("id", id.ToString());

            foreach (AnalyzeOption opt in e.Result.Keys)
            {
                AnalyzeOptionInfo info = ((AnalyzeOptionInfo[])opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false))[0];
                OptionType optionType = info.OptionType;

                switch (optionType)
                {
                    case OptionType.Global:
                        writer.WriteElementString(opt.ToString(), e.Result[opt].ToString());
                        break;
                    case OptionType.ValueList:
                        writer.WriteStartElement(opt.ToString());
                        SaveValueList(e.Result[opt]);
                        writer.WriteEndElement();
                        break;
                    case OptionType.Centrality:
                        // TODO add code
                        break;
                    case OptionType.Distribution:
                    case OptionType.Trajectory:
                        writer.WriteStartElement(opt.ToString());
                        SaveDistribution(info, e.Result[opt]);
                        writer.WriteEndElement();
                        break;
                    default:
                        break;
                }
            }

            writer.WriteEndElement();
        }
        private void LoadEnsembleResults(ResearchResult r)
        {
            while (reader.Read())
            {
                if (reader.Name == "Ensemble" && !reader.IsEmptyElement)
                {
                    EnsembleResult e = new EnsembleResult();
                    e.NetworkSize = r.Size;
                    e.EdgesCount = r.Edges;
                    e.Result = new Dictionary<AnalyzeOption, Object>();

                    reader.Read();
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        AnalyzeOption opt = (AnalyzeOption)Enum.Parse(typeof(AnalyzeOption), reader.Name);
                        AnalyzeOptionInfo optInfo = (AnalyzeOptionInfo)(opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false)[0]);
                        switch (optInfo.OptionType)
                        {
                            case OptionType.Global:
                                e.Result.Add(opt, reader.ReadElementContentAsDouble());
                                break;
                            case OptionType.ValueList:
                                e.Result.Add(opt, LoadValueList());
                                reader.Read();
                                break;
                            case OptionType.Distribution:
                            case OptionType.Trajectory:
                                e.Result.Add(opt, LoadDistribution());
                                reader.Read();
                                break;
                            default:
                                break;
                        }
                    }

                    r.EnsembleResults.Add(e);
                }
            }
        }
Esempio n. 12
0
        private static void FillTrajectoryResults(AnalyzeOption option, List <RealizationResult> results, EnsembleResult r)
        {
            SortedDictionary <Double, Double> temp = new SortedDictionary <Double, Double>();

            foreach (RealizationResult res in results)
            {
                Debug.Assert(res.Result[option] is SortedDictionary <Double, Double>);
                SortedDictionary <Double, Double> d = res.Result[option] as SortedDictionary <Double, Double>;
                foreach (KeyValuePair <double, double> k in d)
                {
                    if (temp.ContainsKey(k.Key))
                    {
                        temp[k.Key] += k.Value / results.Count;
                    }
                    else
                    {
                        temp.Add(k.Key, k.Value / results.Count);
                    }
                }
            }
            r.Result.Add(option, temp);
        }
Esempio n. 13
0
        private static void FillListResults(AnalyzeOption option, List <RealizationResult> results, EnsembleResult r)
        {
            List <Double> temp = new List <double>();

            foreach (RealizationResult res in results)
            {
                Debug.Assert(res.Result[option] is List <Double>);
                List <Double> l = res.Result[option] as List <Double>;
                for (int j = 0; j < l.Count; ++j)
                {
                    if (j < temp.Count())
                    {
                        temp[j] += l[j] / results.Count;
                    }
                    else
                    {
                        temp.Add(l[j] / results.Count);
                    }
                }
            }
            r.Result.Add(option, temp);
        }
Esempio n. 14
0
        private static void FillDoubleResults(AnalyzeOption option, List <RealizationResult> results, EnsembleResult r)
        {
            double temp = 0;

            foreach (RealizationResult res in results)
            {
                temp += (double)(res.Result[option]) / results.Count;
            }
            r.Result.Add(option, double.IsNaN(temp) ? 0 : Math.Round(temp, 4));
        }
Esempio n. 15
0
        private static void FillEigenValuesResults(AnalyzeOption option, List <RealizationResult> results, EnsembleResult r)
        {
            Debug.Assert(option == AnalyzeOption.EigenValues);
            List <Double> temp = new List <Double>();

            foreach (RealizationResult res in results)
            {
                Debug.Assert(res.Result[option] is List <Double>);
                List <Double> e = res.Result[option] as List <Double>;
                foreach (Double k in e)
                {
                    temp.Add(k);
                }
            }
            r.Result.Add(option, temp);
        }
 private static void FillListResults(AnalyzeOption option, List<RealizationResult> results, EnsembleResult r)
 {
     List<Double> temp = new List<double>();
     foreach (RealizationResult res in results)
     {
         Debug.Assert(res.Result[option] is List<Double>);
         List<Double> l = res.Result[option] as List<Double>;
         for (int j = 0; j < l.Count; ++j)
         {
             if (j < temp.Count())
                 temp[j] += l[j] / results.Count;
             else
                 temp.Add(l[j] / results.Count);
         }
     }
     r.Result.Add(option, temp);
 }
        private void LoadEnsembleResults(Workbook book, ResearchResult r)
        {
            EnsembleResult e = new EnsembleResult();
            e.NetworkSize = r.Size;
            e.EdgesCount = r.Edges;
            e.Result = new Dictionary<AnalyzeOption, Object>();

            Array existingOptions = Enum.GetValues(typeof(AnalyzeOption));
            foreach (AnalyzeOption opt in existingOptions)
            {
                AnalyzeOptionInfo optInfo = (AnalyzeOptionInfo)(opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false)[0]);
                switch (optInfo.OptionType)
                {
                    case OptionType.Global:
                        Double v = FindValueInGlobals(book, optInfo.FullName);
                        if ( v != -1)
                            e.Result.Add(opt, v);
                        break;
                    case OptionType.ValueList:
                        Object vl = LoadValueList(book, optInfo);
                        if (vl != null)
                            e.Result.Add(opt, vl);
                        break;
                    case OptionType.Distribution:
                    case OptionType.Trajectory:
                        Object vd = LoadDistribution(book, optInfo);
                        if (vd != null)
                            e.Result.Add(opt, vd);
                        break;
                    default:
                        break;
                }
            }

            r.EnsembleResults.Add(e);
        }
        /// <summary>
        /// Calculates the average distribution of specified distributed analyze option.
        /// </summary>
        /// <param name="opt">Analyze option.</param>
        public void CalculateDistributedOption(AnalyzeOption opt)
        {
            if (EnsembleResultsAvg.Count != 0 && EnsembleResultsAvg[0].Result.ContainsKey(opt))
                return;

            AnalyzeOptionInfo[] info = (AnalyzeOptionInfo[])opt.GetType().GetField(opt.ToString()).GetCustomAttributes(typeof(AnalyzeOptionInfo), false);
            Debug.Assert(info[0].OptionType == OptionType.Distribution);
            EnsembleResult er = null;
            if (EnsembleResultsAvg.Count == 0)
            {
                er = new EnsembleResult();
                EnsembleResultsAvg.Add(er);
            }
            else
                er = EnsembleResultsAvg[0];
            er.Result.Add(opt, CalculateDoubleAverage(opt));
        }