示例#1
0
        public static void Analyze(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename,
            Object printLock)
        {
            // Simple sorting of successful runs
            PerCountryAnalysis(modelResults, perCountrySummaryFilename, printLock);

            if (sweepMode == SweepMode.SweepAndCountry)
            {
                CrossCountryAnalysis(countries, models, portfolio,
                                     sweepMode, sweeps, modelResults,
                                     perCountrySummaryFilename, crossCountrySummaryFilename, printLock);
            }

        }
示例#2
0
        public static void CrossCountryAnalysis(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename,
            Object printLock)
        {
            Dictionary<SweepParameters, SweepStat> sorter = new Dictionary<SweepParameters, SweepStat>();

            foreach (ModelResult mr in modelResults)
            {
                SweepParameters swp = new SweepParameters();
                swp.Country = -1;
                swp.WithdrawalRate = mr.model.YearlyWithdrawal;
                swp.WorldShare = mr.model.WorldShare;
                swp.Equity = mr.model.StartEq;
                swp.Bonds = mr.model.StartBo;
                swp.Strategy = mr.model.Strategy;
                int countryIndex = 0;
                foreach(Country c in countries)
                {
                    if (c.Filename == mr.model.CountryName)
                        break;
                    else
                        countryIndex++;
                }

                if (!sorter.ContainsKey(swp))
                {
                    SweepStat sws = new SweepStat();
                    sws.sweepResults = new Dictionary<int, List<ModelResult>>();
                    sorter.Add(swp, sws);
                }

                if (!sorter[swp].sweepResults.ContainsKey(countryIndex))
                    sorter[swp].sweepResults.Add(countryIndex, new List<ModelResult>());

                sorter[swp].sweepResults[countryIndex].Add(mr);
            }

            List<SweepResult> sweepResults = new List<SweepResult>();

            foreach(SweepParameters swp in sorter.Keys)
            {
                SweepStat sws = sorter[swp];
                foreach (int country in sorter[swp].sweepResults.Keys)
                {
                    foreach (ModelResult mr in sorter[swp].sweepResults[country])
                    {
                        sws.totalPop += countries[country].Population;
                        sws.weightedProd += mr.productivity * countries[country].Population;
                        sws.weightedSuccessRate += mr.overallSuccessRate * countries[country].Population;
                    }
                }
                sws.weightedProd /= sws.totalPop;
                sws.weightedSuccessRate /= sws.totalPop;
                
                SweepResult swr = new SweepResult();
                swr.parameters = swp;
                swr.stat = sws;
                sweepResults.Add(swr);
            }
            
            IEnumerable<SweepResult> sortedResults = sweepResults.OrderBy(
                sr => (sr.stat.weightedSuccessRate * 10000.0 + sr.stat.weightedProd));

            using (StreamWriter sw = new StreamWriter(crossCountrySummaryFilename))
            {
                sw.WriteLine("WdRate,Strategy,World,Eq,Bo,Prod,Success,");
                foreach (SweepResult sr in sortedResults)
                {
                    sw.WriteLine("{0:F2},{1},{2:F2},{3},{4},{5:F2},{6:F2},",
                        sr.parameters.WithdrawalRate,
                        sr.parameters.Strategy,
                        sr.parameters.WorldShare,
                        sr.parameters.Equity,
                        sr.parameters.Bonds,
                        sr.stat.weightedProd,
                        sr.stat.weightedSuccessRate);

                }
            }

        }
 static bool NodeIsConstrained(int v, SweepMode sweepMode, LayerInfo layerInfo) {
     if (sweepMode == SweepMode.Starting)
         return false;
     return sweepMode == SweepMode.ComingFromAbove && NodeIsConstrainedAbove(v, layerInfo) ||
            sweepMode == SweepMode.ComingFromBelow && NodeIsConstrainedBelow(v, layerInfo);
 }
示例#4
0
        static void Execute(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            string resFile,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename)
        {
            ConcurrentBag<ModelResult> modelResults = new ConcurrentBag<ModelResult>();
            Object printLock = new Object();

            using (StreamWriter swr = new StreamWriter(resFile))
            {
                swr.WriteLine(Utils.ResultHeader);

                if (sweepMode == SweepMode.No)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSingle(countries, models, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteDouble(countries, models, modelResults, swr, printLock);
                    }
                }
                else if (sweepMode == SweepMode.SweepNoCountry)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSweepSingle(countries, models, sweeps, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteSweepDouble(countries, models, sweeps, modelResults, swr, printLock);
                    }
                }
                else if (sweepMode == SweepMode.SweepAndCountry)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSweepSingleByCountry(countries, models, sweeps, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteSweepDoubleByCountry(countries, models, sweeps, modelResults, swr, printLock);
                    }
                }
            }

            Analysis.Analyze(
                            countries,
                            models,
                            portfolio,
                            sweepMode,
                            sweeps,
                            modelResults, 
                            perCountrySummaryFilename, 
                            crossCountrySummaryFilename,
                            printLock);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="blockRoot"></param>
        /// <param name="layerInfo"></param>
        /// <param name="sweepMode"></param>
        /// <returns>-1 if no node is constrained</returns>
        static int GetFixedBlockNode(int blockRoot, LayerInfo layerInfo, SweepMode sweepMode) {
            if (sweepMode == SweepMode.Starting)
                return -1;

            if (sweepMode == SweepMode.ComingFromBelow)
                return GetFixedBlockNodeFromBelow(blockRoot, layerInfo);
            return GetFixedBlockNodeFromAbove(blockRoot, layerInfo);
        }
 double GetBaricenterOnPrevLayer(int v, SweepMode sweepMode) {
     Debug.Assert(sweepMode != SweepMode.Starting);
     return sweepMode == SweepMode.ComingFromAbove ? GetBaricenterAbove(v) : GetBaricenterBelow(v);
 }
 double FixedNodePosition(int v, SweepMode sweepMode) {
     Debug.Assert(sweepMode != SweepMode.Starting);
     LayerInfo layerInfo = layerInfos[LayerArrays.Y[v]];
     return sweepMode == SweepMode.ComingFromAbove
                ? XPosition(layerInfo.constrainedFromAbove[v])
                : XPosition(layerInfo.constrainedFromBelow[v]);
 }
 bool IsConnectedToPrevLayer(int v, SweepMode sweepMode) {
     return sweepMode == SweepMode.ComingFromAbove && ProperLayeredGraph.InEdgesCount(v) > 0 ||
            sweepMode == SweepMode.ComingFromBelow && ProperLayeredGraph.OutEdgesCount(v) > 0;
 }
        //at the moment we only are looking for the order of nodes in the layer
        void FillSolverWithoutKnowingLayerOrder(IEnumerable<int> layer, LayerInfo layerInfo, ISolverShell solver,
                                                       SweepMode sweepMode) {
            foreach (int v in layer)
                if (layerInfo.neigBlocks.ContainsKey(v)) {
                    //v is a block root
                    int blockNode = GetFixedBlockNode(v, layerInfo, sweepMode);
                    if (blockNode != -1)
                        solver.AddVariableWithIdealPosition(v, FixedNodePosition(blockNode, sweepMode),
                                                            ConstrainedVarWeight);
                    else {
                        IEnumerable<int> t = from u in layerInfo.neigBlocks[v].Concat(new[] { v })
                                             where IsConnectedToPrevLayer(u, sweepMode)
                                             select u;
                        if (t.Any()) {
                            blockNode = t.First();
                            solver.AddVariableWithIdealPosition(v, GetBaricenterOnPrevLayer(blockNode, sweepMode));
                        }
                    }
                } else if (!BelongsToNeighbBlock(v, layerInfo)) {
                    if (NodeIsConstrained(v, sweepMode, layerInfo))
                        solver.AddVariableWithIdealPosition(v, FixedNodePosition(v, sweepMode), ConstrainedVarWeight);
                    else if (IsConnectedToPrevLayer(v, sweepMode))
                        solver.AddVariableWithIdealPosition(v, GetBaricenterOnPrevLayer(v, sweepMode));
                }

            AddGoalToKeepFlatEdgesShortOnBlockLevel(layerInfo, solver);

            foreach (var p in layerInfo.leftRight)
                solver.AddLeftRightSeparationConstraint(p.Item1, p.Item2, GetGapBetweenBlockRoots(p.Item1, p.Item2));
        }