Esempio n. 1
0
 public static void populateResults(List <ProcessOutput> res,
                                    ProcessArgumentList gameParams,
                                    ParallelOptions parallelArgValues,
                                    ParallelOptions parallelSimRuns,
                                    AGameGraph graph)
 {
     Parallel.For(0, gameParams.ValuesCount, parallelArgValues, valIdx =>
     {
         Dictionary <string, string> processArgs = gameParams[valIdx];
         res[valIdx] =
             processParams(parallelSimRuns, processArgs, graph);
     });
 }
Esempio n. 2
0
        public void init(AGameGraph grGraph, AGameProcess p)
        {
            gameProc = (AdvRoutingGameProcess)p;
            graph    = (EmptyEnvironment)grGraph;

            //foreach (Pursuer pr in gameProc.Params.A_P)
            //{
            //    agentLocationAnswer[pr] = new Point(-1, -1);
            //
            //}
            //foreach (Evader ev in gameProc.Params.A_E)
            //{
            //    agentLocationAnswer[ev] = new Point(-1, -1);
            //
            //}
        }
Esempio n. 3
0
        public static List <List <ProcessOutput> > process(SimArguments args)
        {
            ParallelizationManager       trdMgr = new ParallelizationManager(args);
            List <List <ProcessOutput> > res    = new List <List <ProcessOutput> >();

            Parallel.For(0, args.argFiles.Count, trdMgr.parallelArgFiles, fileIdx =>
            {
                ArgFile af = args.argFiles[fileIdx];
                AppSettings.WriteLogLine("Processing Arg File:" + af.FileName);

                // load graph file
                AGameGraph graph = null;

                AppSettings.WriteLogLine("loading graph...");
                string graphVal = af.processParams[0][AppConstants.AppArgumentKeys.GRAPH_FILE.key];
                Utils.Exceptions.ConditionalTryCatch <Exception>(() =>
                {
                    if (graphVal.EndsWith(FileExtensions.GRAPH_FILE))
                    {
                        graph = AGameGraph.loadGraph(File.ReadAllLines(FileUtils.TryFindingFile(graphVal)));
                        //graph = new GridGameGraph(af.processParams[0][AppConstants.AppArgumentKeys.GRAPH_FILE.key]);
                    }
                    else
                    {
                        graph = AGameGraph.loadGraph(graphVal);
                    }
                },
                                                                 (Exception ex) =>
                {
                    AppSettings.WriteLogLine("couldn't load graph file:" + ex.Message);
                });

                AppSettings.WriteLogLine("graph loaded. processing...");
                // populate output table (ValuesCount is the amount of different values from param file, each should have separate theory/sim/optimizer process)
                res.Add(AlgorithmUtils.getRepeatingValueList <ProcessOutput>(af.processParams.ValuesCount));
                populateResults(res.Last(), af.processParams, trdMgr.parallelArgValues, trdMgr.parallelSimRuns, graph);
            });

            return(res);
        }
Esempio n. 4
0
        public static APolicyOptimizer getOptimizedPolicyInput(ParallelOptions parallelOptInner,
                                                               Dictionary <string, string> processArgs,
                                                               AGameGraph graph,
                                                               AGameProcess game,
                                                               IGameParams gameParams)
        {
            // use optimizer, if specified
            string optimizerName = AppConstants.AppArgumentKeys.POLICY_OPTIMIZER.tryRead(processArgs);

            if (optimizerName == "")
            {
                return(null);
            }

            Type policyOptimizerType = APolicyOptimizer.ChildrenByTypename[optimizerName];

            //AppSettings.WriteLogLine("invoking optimizer ...");

            APolicyOptimizer opt = (APolicyOptimizer)policyOptimizerType.GetConstructor(new Type[] { }).Invoke(new object[] { });

            opt.init(graph, gameParams, processArgs);
            opt.process(parallelOptInner);
            return(opt);
        }
Esempio n. 5
0
        /// <summary>
        /// processes the specific scenario, and runs simulation according to params in 'policyInput'
        /// NOTE: 1) 'policyInput' is assumed to include AppArgumentKeys.SIMULATION_REPETETION_COUNT
        /// NOTE: 2) results will contain policyInput
        /// </summary>
        /// <param name="parallelOptInner"></param>
        /// <param name="evadersPolicy"></param>
        /// <param name="pursuersPolicy"></param>
        /// <param name="p"></param>
        /// <param name="g"></param>
        /// <param name="policyInput"></param>
        /// <param name="game"></param>
        /// <returns></returns>
        public static Dictionary <string, string> getEstimatedResultsAverage(ParallelOptions parallelOptInner,
                                                                             string evadersPolicy, string pursuersPolicy, GameLogic.IGameParams p, AGameGraph g,
                                                                             Dictionary <string, string> policyInput,
                                                                             AGameProcess game,
                                                                             bool calcStdDev)
        {
            int repetetionCount = int.Parse(AppConstants.AppArgumentKeys.SIMULATION_REPETETION_COUNT.tryRead(policyInput));

            if (repetetionCount <= 0)
            {
                return(null);
            }

            PerformanceEstimation estimator = new PerformanceEstimation(evadersPolicy,
                                                                        pursuersPolicy,
                                                                        p,
                                                                        g);

            var res = new Dictionary <string, string> (policyInput);

            res.AddRange(
                Utils.Algorithms.AlgorithmUtils.Average(
                    estimator.estimatePerformance(
                        Utils.ReflectionUtils.constructEmptyCtorTypeFromObj <AGameProcess>(game),
                        repetetionCount, policyInput, int.MaxValue, false, parallelOptInner), calcStdDev));

            return(res);
        }
Esempio n. 6
0
        /// <summary>
        /// assumes 'processArgs' had a specific optimizer and/or policies, and uses them for the process
        /// </summary>
        /// <param name="parallelOptInner"></param>
        /// <param name="initialProcessArgs">
        /// empty values (i.e. keys with empty strings) will be ignored
        /// </param>
        /// <param name="graph"></param>
        /// <param name="optimizeInitialProcessArgs">
        /// if false, theoryOutput and optimizerOutput will not be populated
        /// and no optimizer will be used to override any of the values in optimizeInitialProcessArgs
        /// </param>
        /// <returns></returns>
        public static ProcessOutput processParams(ParallelOptions parallelOptInner,
                                                  Dictionary <string, string> initialProcessArgs,
                                                  AGameGraph graph,
                                                  bool optimizeInitialProcessArgs = true,
                                                  bool calcStdDev = true)
        {
            ProcessOutput res         = new ProcessOutput();
            var           processArgs = new Dictionary <string, string>(initialProcessArgs);

            foreach (var val in initialProcessArgs)
            {
                if (val.Value == "")
                {
                    processArgs.Remove(val.Key);
                }
            }

            // allocate game process
            string       gameType = AppConstants.AppArgumentKeys.GAME_MODEL.tryRead(processArgs);
            AGameProcess game     = Utils.ReflectionUtils.constructEmptyCtorType <AGameProcess>(gameType);

            // load game params file. processArgs may override some of the loaded values
            IGameParams gameParams = game.constructGameParams();

            gameParams.deserialize(AppConstants.AppArgumentKeys.PARAM_FILE_PATH.tryRead(processArgs), processArgs);

            APolicyOptimizer preferredOptimizer = null;// null if no optimizer specified

            if (optimizeInitialProcessArgs)
            {
                preferredOptimizer = getOptimizedPolicyInput(parallelOptInner, processArgs, graph, game, gameParams);
            }

            // if an optimizer was specified, it may have the freedom to choose from several policies, and also provide theoretical bounds *given this freedom*
            // note that the optimizer may attempt choosing policies, but can't override values in init file.
            string evadersPolicy, pursuersPolicy;

            if (preferredOptimizer != null)
            {
                res.theoryOutput = new Dictionary <string, string>(processArgs);

                // FIXME: for creating theoryOutput, if pursuers policy wasn't specificed in input params, we throw an exception.
                // this is not necessarily the correct way to do this, since theoretically the optimizer is the one that decides what pursuers policy
                // to use
                res.theoryOutput.AddRange(getPursuersPolicyTheoreticalOptimizerResult(AppConstants.AppArgumentKeys.PURSUER_POLICY.tryRead(processArgs, ""), graph, gameParams, processArgs));

                processArgs.AddRange(preferredOptimizer.optimizationOutput, false);

                res.optimizerOutput = new Dictionary <string, string>(processArgs);
                res.optimizerOutput.AddRange(preferredOptimizer.optimizationOutput);
            }

            // evadersPolicy and pursuersPolicy are extracted after the optimizer is used, since the optimizer may choose them
            evadersPolicy  = AppConstants.AppArgumentKeys.EVADER_POLICY.tryRead(processArgs);
            pursuersPolicy = AppConstants.AppArgumentKeys.PURSUER_POLICY.tryRead(processArgs);

            // if no optimizer specified, the theoretical results are calculated by the estimator (which assumes specific evaders policy and pursuers policy are given in processArgs)
            // similarly to the optimizer, the pursuer's policy can't override previous data in processArgs
            if (preferredOptimizer == null && optimizeInitialProcessArgs)
            {
                res.theoryOutput = new Dictionary <string, string>(processArgs);
                var theoreticalOptimizerRes = getPursuersPolicyTheoreticalOptimizerResult(pursuersPolicy, graph, gameParams, processArgs);
                res.theoryOutput.AddRange(theoreticalOptimizerRes);
                processArgs.AddRange(theoreticalOptimizerRes, false);
            }
            res.processOutput =
                getEstimatedResultsAverage(parallelOptInner, evadersPolicy, pursuersPolicy, gameParams, graph, processArgs, game, calcStdDev);

            return(res);
        }
Esempio n. 7
0
        /// <summary>
        /// constructs pursuers policy object, and returns its theoretical optimizer's output
        /// </summary>
        /// <param name="pursuersPolicy"></param>
        /// <param name="graph"></param>
        /// <param name="gameParams"></param>
        /// <param name="processArgs"></param>
        /// <returns></returns>
        public static Dictionary <string, string> getPursuersPolicyTheoreticalOptimizerResult(string pursuersPolicy,
                                                                                              AGameGraph graph,
                                                                                              IGameParams gameParams,
                                                                                              Dictionary <string, string> processArgs)
        {
            // construct a dummy policy, just to make the one-time calculation of preprocess
            APursuersPolicy initPP          = ReflectionUtils.constructEmptyCtorType <APursuersPolicy>(pursuersPolicy);
            var             theoryOptimizer = initPP.constructTheoreticalOptimizer();

            if (theoryOptimizer == null)
            {
                return(new Dictionary <string, string>());
            }

            theoryOptimizer.init(graph, gameParams, processArgs);
            theoryOptimizer.process();
            return(theoryOptimizer.optimizationOutput);
        }