コード例 #1
0
        /*SampleNextStateWithEvaluation(...) does the following tasks:
         * 1. sample next MDPconfiguration if it is a pcase
         * 2. if not, it will directly return nex MDPconfiguration
         * 3. Evaluation on the next MDPconfiguration will also be done
         */
        //assume there is no no-deterministic choice
        //as we assume there is only deterministic choices, including probabilistic choices. Hence, if the list.length=1, corresponding to a normal step, if not, then, it should be a probability distribution, hence, a simple sampling process will be taken to evaluate the next step. Here, we assume there is no loop. and the whole precess is bounded.
        public bool sampleNextStateWithEvaluation(MDPConfiguration currentConfiguration)
        {
            numberOfTotalVisitedStates++;
            bool evaluationResult;

            currentStep++;
            if (currentConfiguration.IsDeadLock || currentStep > boundedStep)
            {
                return(false);
            }
            MDPConfiguration[] list = currentConfiguration.MakeOneMoveLocal().ToArray();
            if (list.Length == 0)
            {
                return(false);
            }

            if (list.Length == 1)
            {
                MDPConfiguration step = list[0];
                ExpressionValue  v    = EvaluatorDenotational.Evaluate(ReachableStateCondition, step.GlobalEnv);
                evaluationResult = (v as BoolConstant).Value;
                return(evaluationResult || sampleNextStateWithEvaluation(step));
            }


            double value = random.NextDouble();

            double sum = 0;

            foreach (MDPConfiguration configuration in list)
            {
                sum += configuration.Probability;
            }

            double accumulation = 0;

            foreach (MDPConfiguration configuration in list)
            {
                double preaccumulation = accumulation;
                accumulation += (configuration.Probability / sum);
                if (value > preaccumulation && value <= accumulation)
                {
                    ExpressionValue v = EvaluatorDenotational.Evaluate(ReachableStateCondition, configuration.GlobalEnv);
                    evaluationResult = (v as BoolConstant).Value;
                    return(evaluationResult || sampleNextStateWithEvaluation(configuration));
                }
            }
            // the folllowing codes will never happen. -----Should be.
            return(false);
        }
コード例 #2
0
        // public Expression ReachableStateCondition;
        //status=true means: single trial, otherwise, mutliple trails

        public SimulationNoMDP(MDPConfiguration initialConfiguration, Expression reachableStateCondition)
        {
            this.intialConfiguration     = initialConfiguration;
            this.ReachableStateCondition = reachableStateCondition;
            this.random = new Random();
        }