Пример #1
0
        public void Agent0x1Simulation_AgentTrajectories_MultiTrial()
        {
            Console.WriteLine("Agent0x1Simulation_AgentTrajectories_MultiTrial");
            LoggerInitialization.SetThreshold(typeof(sim_tests), LogLevel.Debug);
            LoggerInitialization.SetThreshold(typeof(Agent0x0), LogLevel.Info);

            int dim = 0;

            string []     names = new string [0];
            double []     mins  = new double [0];
            double []     maxs  = new double [0];
            IBlauSpace    s     = BlauSpace.create(dim, names, mins, maxs);
            IBlauPoint    mean  = new BlauPoint(s);
            IBlauPoint    std   = new BlauPoint(s);
            IDistribution d     = new Distribution_Gaussian(s, mean, std);

            IAgentFactory afact     = new Agent0x0_Factory(d);
            int           NUMAGENTS = 10;
            IPopulation   pop       = PopulationFactory.Instance().create(afact, NUMAGENTS);

            foreach (IAgent ag in pop)
            {
                SingletonLogger.Instance().DebugLog(typeof(metrics_tests), "agent: " + ag);
            }

            string PATH = "" + ApplicationConfig.EXECDIR + "orderbooks/orderbook.csv";
            AgentOrderbookLoader loader = MakeAgentOrderbookLoader(PATH);

            pop.addAgent(loader);

            IOrderbook_Observable ob = new Orderbook();

            // 1 hours
            ISimulationBundle simb = new SimulationBundle(pop, ob, 0.0, 3600.0);

            IAgent             agent = PopulationSelector.Select(pop);
            ITrajectoryFactory agTF  = new TrajectoryFactory_AgentOrders(agent, 10.0, 0.0);

            simb.add(agTF);

            int NUMTIALS = 2;

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Running Simulations");
            ISimulationResultsBundle resb = simb.run(NUMTIALS);

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Stopping Simulations");

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Results Bundle\n" + resb.ToStringLong());

            Assert.AreEqual(resb.Valid, true);
        }
Пример #2
0
        public void add(ISimulationResultsBundle resb)
        {
            foreach (ITrajectoryBundle tb in resb.getTrajectoryBundles())
            {
                foreach (ITrajectory t in tb.Trajectories)
                {
                    ITrajectoryBundle newtb = null;
                    if (!_tbundles.ContainsKey(t.Name))
                    {
                        newtb = new TrajectoryBundle(t.Name);
                        _tbundles.Add(t.Name, newtb);
                    }
                    else
                    {
                        newtb = _tbundles[t.Name];
                    }
                    newtb.addTrajectory(t);
                }
            }

            foreach (IAgentEvaluationBundle aeb in resb.getAgentEvaluationBundles())
            {
                foreach (IAgentEvaluation ae in aeb.Evaluations)
                {
                    IAgentEvaluationBundle newaeb = null;
                    if (!_aebundles.ContainsKey(ae.Name))
                    {
                        newaeb = new AgentEvaluationBundle(ae.Name);
                        _aebundles.Add(ae.Name, newaeb);
                    }
                    else
                    {
                        newaeb = _aebundles[ae.Name];
                    }
                    aeb.addAgentEvaluation(ae);
                }
            }
        }
Пример #3
0
        public override void run()
        {
            LoggerInitialization.SetThreshold(typeof(SingleDistributionExperiment), LogLevel.Debug);

            SingletonLogger.Instance().DebugLog(typeof(SingleDistributionExperiment), "run()");
            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "distribution => " + theActualDistribution);

            IAgentFactory af = CreateAgentFactory(theActualDistribution, theTableConfig.AgentFactoryClassName);

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "agent factory => " + theTableConfig.AgentFactoryClassName);

            af.Initialize(theTableConfig);

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "creating initial orderbook");
            IOrderbook_Observable ob = new Orderbook();

            /* No Burnin at this point
             *
             * SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Executing burnin...");
             * IPopulation popburn = CreatePopulation(af, theTableConfig.NumAgents, Path.Combine(ApplicationConfig.ROOTDIR, theTableConfig.InitialOrderbook));
             * ISimulation simburn = new Simulation(popburn, ob, 0.0, COMMON_BURNIN_TIME_SECONDS, false, "burnin") ;
             * ISimulationResults resultsburn = simburn.run();
             * ob = (IOrderbook_Observable)simburn.Orderbook;
             * SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Done with burnin.");
             */

            ISimulationBundle        accumulated_simb = null;
            ISimulationResultsBundle accumulated_resb = null;

            for (int popi = 0; popi < theTableConfig.Populations; popi++)
            {
                SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "creating population... " + theTableConfig.NumAgents);
                // Populations all contain an OrderBookLoader agent
                IPopulation pop = CreatePopulation(af, theTableConfig.NumAgents, Path.Combine(ApplicationConfig.ROOTDIR, theTableConfig.InitialOrderbook));
                // SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "done.");

                string popname = POP_FILE_PREFIX + "." + popi + ".Validation";
                string poppath = Path.Combine(_popdir, popname);
                SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "population " + popi + " => " + poppath);
                savePopulation(poppath, pop);

                _simb = new SimulationBundle(pop, ob, 0.0, theTableConfig.DurationHours * 3600.0, LOG_FRAMES, "pop-" + popi);

                if (accumulated_simb == null)
                {
                    accumulated_simb = _simb;
                }

                foreach (TrajectoryFactoryConfig tfc in theTrajConfig.getTrajectories())
                {
                    IPassiveTrajectoryFactory tf = CreatePassiveTrajectoryFactory(tfc);
                    tf.Initialize(theTableConfig);
                    _simb.add(tf);
                }

                /*
                 * foreach (IAgent ag in pop) {
                 *      if (ag is IAgent_NonParticipant) continue;
                 *
                 *      _simb.add (new TrajectoryFactory_AgentNamedMetric(ag, Agent1x0.NetWorth_METRICNAME, 1.0, 0.0));
                 * }
                 */

                double MAX_ALPHA = 0.10;
                double TEMPORAL_GRANULARITY_FOR_ALPHA_SLICES = 1.0;

                for (double alpha = 0.0; alpha <= MAX_ALPHA; alpha += MAX_ALPHA / (double)theTableConfig.NumCombs)
                {
                    TrajectoryFactory_AlphaSlice tf = new TrajectoryFactory_AlphaSlice(TEMPORAL_GRANULARITY_FOR_ALPHA_SLICES, 0.0, alpha, true);
                    tf.Initialize(theTableConfig);
                    _simb.add(tf);
                    TrajectoryFactory_AlphaSlice tf2 = new TrajectoryFactory_AlphaSlice(TEMPORAL_GRANULARITY_FOR_ALPHA_SLICES, 0.0, alpha, false);
                    tf2.Initialize(theTableConfig);
                    _simb.add(tf2);
                }

                foreach (AgentEvaluationConfig aefc in theAgentEvaluationFactorySetConfig.getAgentEvaluations())
                {
                    IAgentEvaluationFactory aef = CreateAgentEvaluationFactory(aefc);
                    _simb.add(aef, aefc);
                }

                _resb = _simb.run(theTableConfig.Trials);

                //popname = POP_FILE_PREFIX+"."+popi+".Validation-Post";
                //poppath = Path.Combine(_popdir, popname);
                //SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "population post "+popi+" => "+poppath);
                //savePopulation(poppath, pop);

                if (accumulated_resb == null)
                {
                    accumulated_resb = _resb;
                }
                else
                {
                    accumulated_resb.add(_resb);
                }

                //WriteTrajectories(popi, _simb, _resb);
            }

            //WriteTrajectories(-1, accumulated_simb, accumulated_resb);

            PresentationConfig.Directory = Path.Combine(OutputDirectory, "results");
            if (!Directory.Exists(PresentationConfig.Directory))
            {
                Directory.CreateDirectory(PresentationConfig.Directory);
            }

            Latex.ClearImages();

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Writing BlauSpaceEvaluations.");

            foreach (IAgentEvaluationBundle aeb in accumulated_resb.getAgentEvaluationBundles())
            {
                IBlauSpaceLattice    bsl      = accumulated_simb.getLattice(theActualDistribution, aeb);
                IBlauSpaceEvaluation meanEval = aeb.MeanEvaluation(bsl);
                IBlauSpaceEvaluation stdEval  = aeb.StdEvaluation(bsl);

                BlauSpaceEvaluationGnuplotPresenter pres = new BlauSpaceEvaluationGnuplotPresenter(_bsedir, PresentationConfig.Directory);
                pres.Present(this, meanEval, stdEval);
            }

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Writing TrajectoryBundles.");
            foreach (ITrajectoryBundle tb in accumulated_resb.getTrajectoryBundles())
            {
                TrajectoryBundleGnuplotPresenter pres = new TrajectoryBundleGnuplotPresenter(_trajdir, PresentationConfig.Directory);
                pres.Present(this, tb);
            }

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Writing Latex.");
            Latex.Instance().Present(this);

            CreateReport(this);

            SingletonLogger.Instance().InfoLog(typeof(SingleDistributionExperiment), "Done.");
        }
Пример #4
0
        public void Agent0x1Simulation_TrajectoryBundles()
        {
            Console.WriteLine("Agent0x1Simulation_TrajectoryBundles");
            LoggerInitialization.SetThreshold(typeof(sim_tests), LogLevel.Debug);
            //LoggerInitialization.SetThreshold(typeof(Agent0x1), LogLevel.Info);
            //LoggerInitialization.SetThreshold(typeof(AbstractAgent), LogLevel.Info);
            //LoggerInitialization.SetThreshold(typeof(SimulationBundle), LogLevel.Debug);
            //LoggerInitialization.SetThreshold(typeof(Scheduler), LogLevel.Debug);
            //LoggerInitialization.SetThreshold(typeof(Trajectory), LogLevel.Debug);

            int dim = 0;

            string []     names = new string [0];
            double []     mins  = new double [0];
            double []     maxs  = new double [0];
            IBlauSpace    s     = BlauSpace.create(dim, names, mins, maxs);
            IBlauPoint    mean  = new BlauPoint(s);
            IBlauPoint    std   = new BlauPoint(s);
            IDistribution d     = new Distribution_Gaussian(s, mean, std);

            IAgentFactory afact     = new Agent0x0_Factory(d);
            int           NUMAGENTS = 25;
            IPopulation   pop       = PopulationFactory.Instance().create(afact, NUMAGENTS);

            foreach (IAgent ag in pop)
            {
                SingletonLogger.Instance().DebugLog(typeof(metrics_tests), "agent: " + ag);
            }

            string PATH = "" + ApplicationConfig.EXECDIR + "orderbooks/orderbook.csv";
            AgentOrderbookLoader loader = MakeAgentOrderbookLoader(PATH);

            pop.addAgent(loader);

            IOrderbook_Observable ob = new Orderbook();

            string PROPERTY = "NetWorth";

            // 1 hours
            ISimulationBundle simb = new SimulationBundle(pop, ob, 0.0, 3600.0);

            IAgentEvaluationFactory metricEF = new NamedMetricAgentEvaluationFactory(PROPERTY);

            simb.add(metricEF);

            ITrajectoryFactory priceTF = new TrajectoryFactory_Price(10.0, 0.8);

            simb.add(priceTF);

            ITrajectoryFactory spreadTF = new TrajectoryFactory_Spread(10.0, 0.0);

            simb.add(spreadTF);

            int NUMTRIALS = 25;

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Running Simulation");
            ISimulationResultsBundle resb = simb.run(NUMTRIALS);

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Stopping Simulation");

            SingletonLogger.Instance().DebugLog(typeof(sim_tests), "resb: " + resb.ToString());

            int STEPS = 1;

            int [] STEPSarray = new int[s.Dimension]; for (int j = 0; j < s.Dimension; j++)
            {
                STEPSarray[j] = STEPS;
            }
            IBlauSpaceLattice bsl = BlauSpaceLattice.create(s, STEPSarray);

            foreach (IAgentEvaluationBundle aeb in resb.getAgentEvaluationBundles())
            {
                IBlauSpaceEvaluation meanEval = aeb.MeanEvaluation(bsl);
                IBlauSpaceEvaluation stdEval  = aeb.StdEvaluation(bsl);
                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "meanEval: " + meanEval.ToStringLong());
                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "stdEval: " + stdEval.ToStringLong());
            }

            foreach (ITrajectoryBundle tb in resb.getTrajectoryBundles())
            {
                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Computing meanTraj");
                ITrajectory meanTraj = tb.MeanTrajectory;
                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "Computing stdTraj");
                ITrajectory stdTraj = tb.StdTrajectory;

                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "meanTraj: " + meanTraj.ToStringLong());
                SingletonLogger.Instance().DebugLog(typeof(sim_tests), "stdTraj: " + stdTraj.ToStringLong());
            }

            Assert.AreEqual(resb.Valid, true);
        }
Пример #5
0
        private void WriteTrajectories(int popi, ISimulationBundle simb, ISimulationResultsBundle resb)
        {
            string     trajname   = TRAJ_FILE_PREFIX + "." + popi + ".Validation";
            string     trajpath   = Path.Combine(_trajdir, trajname);
            TextWriter trajWriter = File.CreateText(trajpath);

            foreach (IAgentEvaluationBundle aeb in resb.getAgentEvaluationBundles())
            {
                trajWriter.WriteLine("# AgentEvaluation: " + aeb.Name);
                trajWriter.Write("# BlauPoint-Coordinates \t ");
                trajWriter.Write("mean \t std \t count");
                trajWriter.WriteLine("");

                IBlauSpaceLattice bsl = simb.getLattice(theActualDistribution, aeb);

                IBlauSpaceEvaluation meanEval = aeb.MeanEvaluation(bsl);
                IBlauSpaceEvaluation stdEval  = aeb.StdEvaluation(bsl);
                IBlauSpaceEvaluation ctEval   = aeb.AssignmentCounts(bsl);

                foreach (IBlauPoint bp in meanEval.AssignedLatticePoints)
                {
                    for (int c = 0; c < bp.Space.Dimension; c++)
                    {
                        trajWriter.Write("" + bp.Space.getAxis(c).Name + "=" + bp.getCoordinate(c).ToString("#0.000") + "\t");
                    }
                    trajWriter.Write("" + meanEval.eval(bp).ToString("#0.000") + "\t");
                    trajWriter.Write("" + stdEval.eval(bp).ToString("#0.000") + "\t");
                    trajWriter.Write("" + ctEval.eval(bp).ToString("#0.000") + "\t");
                    trajWriter.WriteLine("");
                }
            }

            double NUMTICKS = 100.0;

            foreach (ITrajectoryBundle tb in resb.getTrajectoryBundles())
            {
                trajWriter.WriteLine("# TrajectoryBundle: " + tb.Name);
                trajWriter.WriteLine("# time \t mean \t std \t center \t dev");

                ITrajectory meanTraj   = tb.MeanTrajectory;
                ITrajectory stdTraj    = tb.StdTrajectory;
                ITrajectory centerTraj = tb.CentralTrajectory;
                ITrajectory devTraj    = tb.CentralDevTrajectory;

                double mint  = tb.MinimumTime;
                double maxt  = tb.MaximumTime;
                double stept = (maxt - mint) / NUMTICKS;

                for (double t = mint; t < maxt; t += stept)
                {
                    trajWriter.Write("" + t.ToString("#0.000") + "\t");
                    trajWriter.Write("" + meanTraj.eval(t).ToString("#0.000") + "\t");
                    trajWriter.Write("" + stdTraj.eval(t).ToString("#0.000") + "\t");
                    trajWriter.Write("" + centerTraj.eval(t).ToString("#0.000") + "\t");
                    trajWriter.Write("" + devTraj.eval(t).ToString("#0.000") + "\t");
                    trajWriter.WriteLine("");
                }
            }

            trajWriter.Flush();
            trajWriter.Close();
        }