Beispiel #1
0
        private IList <State> FindBranches(State s)
        {
            var branches = new List <State>();
            var exp      = _actionsToPerform.Observations[s.CurrentTime + 1].ToList();
            var effect   =
                _currentAction?.Effects.FirstOrDefault(e => e.Time == s.CurrentTime + 1 - _currentAction.StartTime); //check

            if (effect != null)
            {
                exp.Add(effect.Expression);
            }
            if (exp.Any())
            {
                //get conjunction
                var fullExp = exp[0];
                for (var i = 1; i < exp.Count; i++)
                {
                    fullExp = new Expression.And(fullExp, exp[i]);
                }
                //convert expression to branches
                //get all true evaluation -> foreach generate new state
                var list = ExpressionMethods.GetAllFluentEvaluationsSatisfyingExpression(fullExp);
                branches.AddRange(list.Select(fluentValues => new State
                {
                    CurrentTime   = s.CurrentTime + 1,
                    Fluents       = fluentValues,
                    CurrentAction = _currentAction?.Action
                }));
            }
            else
            {
                //fluents are derieved from parent
                branches.Add(new State {
                    CurrentTime = s.CurrentTime + 1, Fluents = s.Fluents, CurrentAction = _currentAction?.Action
                });
            }
            return(branches);
        }
Beispiel #2
0
        private bool FillState(State s)
        {
            if (s.CurrentTime < 0)
            {
                return(true); //accept the root artificial state at -1 time.
            }
            if (_currentAction != null)
            {
                if (_currentAction.StartTime + (_currentAction.Effects.Any() ? _currentAction.Effects.Max(e => e.Time) : 0) == s.CurrentTime)
                {
                    _currentAction = null; //finish action
                }
                else
                {
                    //set dependent fluents to occlusion
                    var expressions = _currentAction.Effects
                                      .Where(e => e.Time + _currentAction.StartTime > s.CurrentTime).Select(e => e.Expression);
                    //foreach exp get all fluents to single list
                    //set each fluent from the list to occlusion
                    var fluents = new List <int>();
                    foreach (var expression in expressions)
                    {
                        fluents.AddRange(ExpressionMethods.FindAllFluentsInExpression(expression));
                    }
                    foreach (var i in fluents.Distinct())
                    {
                        s.Fluents[i] = FluentValue.UnderOcclusion;
                    }
                }
            }
            //add triggers for state check all triggers sentence condition
            foreach (var triggers in _sentences.Where(sen => sen is SenI.Triggers).Cast <SenI.Triggers>())
            {
                if (triggers.Condition == null ||
                    ExpressionMethods.CheckExpressionSatisfaction(triggers.Condition, s.Fluents) == FluentValue.True)
                {
                    _actionsToPerform.Actions[s.CurrentTime].Add(triggers.Action);
                }
            }
            //add releases observation
            foreach (var releases in _sentences.Where(sen => sen is SenI.Releases).Cast <SenI.Releases>())
            {
                if (releases.Condition != null &&
                    ExpressionMethods.CheckExpressionSatisfaction(releases.Condition, s.Fluents) !=
                    FluentValue.True)
                {
                    continue;
                }
                if (releases.Duration + s.CurrentTime <= _scenario.Time)
                {
                    _actionsToPerform.Observations[releases.Duration + s.CurrentTime].Add(releases.Result);
                }
            }
            if (GetActionsForTime(s.CurrentTime, out var action) && action > 0)
            {
                //start action
                _currentAction = new ActionExecution
                {
                    Action    = action,
                    StartTime = s.CurrentTime
                };
                foreach (var causes in _sentences.OfType <SenI.Causes>().Where(se => se.Action == action))
                {
                    if (ExpressionMethods.CheckExpressionSatisfaction(causes.Condition, s.Fluents) != FluentValue.True)
                    {
                        continue;
                    }
                    _currentAction.Effects.Add(new ActionEffect
                    {
                        Expression = causes.Result,
                        Time       = causes.Duration
                    });
                }
                //invokes
                foreach (var invokes in _sentences.OfType <SenI.Invokes>().Where(i => i.Invoker == action &&
                                                                                 (i.Condition == null || ExpressionMethods.CheckExpressionSatisfaction(i.Condition, s.Fluents) == FluentValue.True)))
                {
                    var time = _currentAction.StartTime + _currentAction.Effects.Max(e => e.Time) + invokes.Delay;
                    if (time <= _scenario.Time)
                    {
                        _actionsToPerform.Actions[time].Add(invokes.Invokee);
                    }
                }
            }
            //check state!
            //check observations
            var obs = _actionsToPerform.Observations[s.CurrentTime];

            if (obs?.Any(o => ExpressionMethods.CheckExpressionSatisfaction(o, s.Fluents) != FluentValue.True) ?? false)
            {
                return(false); //model contradictory!
            }
            //check actions
            var acs = _actionsToPerform.Actions[s.CurrentTime] ?? new List <int>();

            return(acs.Count <= 1 || acs[0] == _currentAction.Action);
        }