Example #1
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);
        }
Example #2
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);
        }