Beispiel #1
0
        /// <summary>
        /// Computes the forward cost heuristics for the given state in the relaxed planning graph.
        /// </summary>
        /// <param name="state">Starting state.</param>
        /// <param name="goalConditions">Goal conditions.</param>
        /// <param name="evaluationStrategy">Evaluation strategy.</param>
        /// <returns>Forward cost heuristic value from the specified state.</returns>
        private double ComputeForwardCost(IState state, IConditions goalConditions, ForwardCostEvaluationStrategy evaluationStrategy)
        {
            IStateLayer previousStateLayer = null;
            IStateLayer stateLayer         = CreateLabeledStateLayer(state.GetRelaxedState());
            ActionLayer actionLayer        = new ActionLayer();

            while (!stateLayer.Equals(previousStateLayer))
            {
                // check goal conditions

                if (goalConditions.Evaluate(stateLayer.GetState()))
                {
                    return(goalConditions.EvaluateOperatorPlanningGraphLabel(stateLayer.GetStateLabels(), evaluationStrategy));
                }

                // build new action layer

                actionLayer.Clear();
                foreach (var successor in RelaxedProblem.GetSuccessors(stateLayer.GetState()))
                {
                    IOperator appliedOperator = successor.GetAppliedOperator();
                    double    label           = appliedOperator.ComputePlanningGraphLabel(stateLayer.GetStateLabels(), evaluationStrategy);
                    actionLayer.Add(new ActionNode(appliedOperator, label + appliedOperator.GetCost()));
                }

                // build new state layer

                previousStateLayer = stateLayer;
                stateLayer         = CreateLabeledStateLayer(stateLayer, actionLayer);
            }

            // failure, solution cannot be found from the specified state
            return(int.MaxValue);
        }
Beispiel #2
0
    // Use this for initialization
    void Start()
    {
        FSM_action = GameManager.Instance.fsm_layer.a_layer;

        _pickHolder = new GameObject("test");
        _pickHolder.transform.parent = gameObject.transform;
        picks = _pickHolder.AddComponent<Pickingclass>();
        picks.name = "pick";

        speed = 10.0f;
        pickPOS.x = 0.0f;
        pickPOS.y = 1.0f;
        pickPOS.z = 0.0f;
    }
Beispiel #3
0
        /// <summary>
        /// Constructs the state layer from the preceding state layer and action layer.
        /// </summary>
        /// <param name="sLayer">State layer.</param>
        /// <param name="aLayer">Action layer.</param>
        public StateLayer(StateLayer sLayer, ActionLayer aLayer) : this(sLayer)
        {
            foreach (var actionNode in aLayer)
            {
                var appliedOperator = (Operator)actionNode.Operator;
                var operatorLabel   = actionNode.Label;

                foreach (var effect in appliedOperator.Effects)
                {
                    if (effect.IsApplicable(sLayer.State))
                    {
                        var assignment = effect.GetAssignment();
                        StoreLabel(assignment, operatorLabel);
                    }
                }

                State = (RelaxedState)appliedOperator.Apply(State);
            }
        }
Beispiel #4
0
        public void ActionDataTest()
        {
            var input = new ActionLayer
            {
                Hotkey       = 'h',
                Name         = "Name",
                Parent       = new ResourceRef("Parent", 42),
                Prerequisite = "Prerequisite",
                Verbs        = new[] { "verb1", "verb2" }
            };

            var serializer = new ActionLayerHandler();
            var output     = (ActionLayer)serializer.Reserialize(input);

            Assert.That(output.Hotkey, Is.EqualTo(input.Hotkey));
            Assert.That(output.Name, Is.EqualTo(input.Name));
            Assert.That(output.Parent.Name, Is.EqualTo(input.Parent.Name));
            Assert.That(output.Parent.Version, Is.EqualTo(input.Parent.Version));
            Assert.That(output.Prerequisite, Is.EqualTo(input.Prerequisite));
            Assert.That(output.Verbs, Is.EquivalentTo(input.Verbs));
        }
Beispiel #5
0
        /// <summary>
        /// Constructs the state layer from the preceding state layer and action layer.
        /// </summary>
        /// <param name="sLayer">State layer.</param>
        /// <param name="aLayer">Action layer.</param>
        public StateLayer(StateLayer sLayer, ActionLayer aLayer) : this(sLayer)
        {
            foreach (var actionNode in aLayer)
            {
                var appliedOperator = actionNode.Operator;
                var operatorLabel   = actionNode.Label;

                var newState = (IState)appliedOperator.Apply(State);

                foreach (var atom in newState.GetPredicates())
                {
                    if (sLayer.State.HasPredicate(atom))
                    {
                        // this atom is not a result of the operator application -> ignore
                        continue;
                    }

                    StoreLabel(atom, operatorLabel);
                    State.AddPredicate(atom);
                }
            }
        }
Beispiel #6
0
 void Awake()
 {
     m_layer = new MovementLayer();
     a_layer = new ActionLayer();
 }
Beispiel #7
0
 /// <summary>
 /// Creates the labeled state layer for the forward cost evaluation, from the previous state layer and the action layer.
 /// </summary>
 /// <param name="sLayer">Previous state layer.</param>
 /// <param name="aLayer">Action layer.</param>
 /// <returns>New state layer.</returns>
 protected override IStateLayer CreateLabeledStateLayer(IStateLayer sLayer, ActionLayer aLayer)
 {
     return(new StateLayer((StateLayer)sLayer, aLayer));
 }
            public virtual void OnGUI()
            {
                EditorGUILayout.Space();
                using (new EditorHelper.Box(true, 0))
                {
                    using (new EditorHelper.Indent(-4))
                        using (new EditorHelper.Horizontal())
                        {
                            enabled = EditorGUILayout.Toggle(enabled, GUILayout.ExpandWidth(false), GUILayout.Width(15));

                            bool existed = Config.actionFold.ContainsKey(this);
                            if (!existed)
                            {
                                Config.actionFold[this] = true;
                            }

                            bool          foldout = Config.actionFold[this];
                            GUIStyle      gs      = new GUIStyle(EditorStyles.foldout);
                            GUIStyleState normal  = new GUIStyleState();
                            normal.textColor = new Color(226 / 255f, 100 / 255f, 226 / 255f, 1);
                            gs.fontStyle     = FontStyle.Bold;
                            gs.normal        = normal;
                            gs.focused       = normal;
                            gs.hover         = normal;
                            gs.active        = normal;
                            gs.onActive      = normal;
                            gs.onFocused     = normal;
                            gs.onHover       = normal;
                            gs.onNormal      = normal;

                            foldout = GUILayout.Toggle(
                                foldout, $"{GetTitleText()}", gs,
                                GUILayout.ExpandWidth(false), GUILayout.Width(150)
                                );

                            if (Config.actionFold[this] != foldout)
                            {
                                Config.ResetCacheRectData();
                            }
                            Config.actionFold[this] = foldout;
                            if (!foldout)
                            {
                                return;
                            }

                            IsRemoved = GUILayout.Button("Remove", GUILayout.Width(80));

                            if (GUILayout.Button("Copy", GUILayout.Width(80)))
                            {
                                copiedAction = this;
                            }

                            IsPasted = GUILayout.Button("Paste", GUILayout.Width(80));
                        }

                    using (new EditorHelper.Indent(-3))
                    {
                        ActionLayer layer = ShowActionLayer();
                        layer       = (ActionLayer)EditorGUILayout.EnumPopup("Active Layer", layer);
                        actionLayer = layer.ToString();

                        if (!Config.showSpawnerMode)
                        {
                            ActionName oldActionName = ShowActionName();
                            ActionName newActionName = (ActionName)EditorGUILayout.EnumPopup(
                                "Action Name", oldActionName);
                            actionName      = newActionName.ToString();
                            IsActionChanged = !oldActionName.Equals(newActionName);
                        }

                        ActionTriggerCondition oldCondition = ShowTriggerCondition();
                        ActionTriggerCondition newCondition = (ActionTriggerCondition)EditorGUILayout.EnumPopup("Trigger Condition", oldCondition);
                        triggerCondition = newCondition.ToString();

                        if (newCondition == ActionTriggerCondition.Time)
                        {
                            waitTime = EditorGUILayout.FloatField("Activate at(s)", waitTime);
                            waitTime = Mathf.Max(0, waitTime);
                        }
                        else if (newCondition == ActionTriggerCondition.ByEvent)
                        {
                            eventId = EditorGUILayout.IntField("Event ID", eventId);
                        }
                        else if (newCondition == ActionTriggerCondition.OnWaveFinish)
                        {
                            waveOrder = EditorGUILayout.IntField("Wave Order", waveOrder);
                            waveOrder = Mathf.Max(1, waveOrder);
                        }

                        DrawGUI();
                    }
                }
            }
Beispiel #9
0
        /// <summary>
        /// Gets the best (lowest cost) relevant action node satisfying the given proposition during the FF evaluation.
        /// </summary>
        /// <param name="proposition">Proposition to satisfy.</param>
        /// <param name="actionLayer">Action layer.</param>
        /// <returns>Best relevant action node.</returns>
        private static ActionNode GetBestRelevantActionNodeFF(IProposition proposition, ActionLayer actionLayer)
        {
            ActionNode bestActionNode = null;

            foreach (var actionNode in actionLayer)
            {
                if (actionNode.Successors.Contains(proposition))
                {
                    if (bestActionNode == null || actionNode.Operator.GetCost() < bestActionNode.Operator.GetCost())
                    {
                        bestActionNode = actionNode;
                    }
                }
            }

            return(bestActionNode);
        }
Beispiel #10
0
 /// <summary>
 /// Creates the labeled state layer for the forward cost evaluation, from the previous state layer and the action layer.
 /// </summary>
 /// <param name="sLayer">Previous state layer.</param>
 /// <param name="aLayer">Action layer.</param>
 /// <returns>New state layer.</returns>
 protected abstract IStateLayer CreateLabeledStateLayer(IStateLayer sLayer, ActionLayer aLayer);
Beispiel #11
0
        /// <summary>
        /// Builds the relaxed planning graph and computes the FF heuristic value.
        /// </summary>
        /// <param name="state">Starting state.</param>
        /// <param name="goalConditions">Goal conditions.</param>
        /// <returns>FF cost heuristic value from the specified state.</returns>
        private double ComputeFFCost(IState state, IConditions goalConditions)
        {
            // build an explicit relaxed planning graph

            StateLayers.Clear();
            ActionLayers.Clear();
            StateLayers.Add(CreateFFStateLayer(state.GetRelaxedState()));

            while (true)
            {
                // check goal conditions

                IStateLayer stateLayer = StateLayers[StateLayers.Count - 1];

                if (goalConditions.Evaluate(stateLayer.GetState()))
                {
                    ActionLayers.Add(new ActionLayer {
                        CreateFFGoalActionNode(goalConditions, stateLayer.GetState())
                    });
                    break;
                }

                // build new action layer and the next state layer

                ActionLayer actionLayer = new ActionLayer();
                IState      newState    = stateLayer.GetState().Clone();

                foreach (var successor in RelaxedProblem.GetSuccessors(stateLayer.GetState()))
                {
                    IOperator appliedOperator = successor.GetAppliedOperator();

                    actionLayer.Add(CreateFFActionNode(appliedOperator, stateLayer.GetState()));

                    newState = appliedOperator.Apply(newState, true);
                }

                ActionLayers.Add(actionLayer);
                StateLayers.Add(CreateFFStateLayer(newState));
            }

            // compute FF value

            UnsatisfiedOnCurrentLayer.Clear();
            UnsatisfiedOnNextLayer.Clear();
            MarkedActionNodes.Clear();

            var goalNode = ActionLayers[ActionLayers.Count - 1][0];

            foreach (var proposition in goalNode.Predecessors)
            {
                UnsatisfiedOnCurrentLayer.Push(proposition);
            }

            for (int i = StateLayers.Count - 1; i > 0; --i)
            {
                IStateLayer nextStateLayer     = StateLayers[i - 1];
                ActionLayer currentActionLayer = ActionLayers[i - 1];

                while (UnsatisfiedOnCurrentLayer.Count != 0)
                {
                    IProposition proposition = UnsatisfiedOnCurrentLayer.Pop();

                    // 1.) try to satisfy the proposition by an idle arc to the next state layer

                    if (nextStateLayer.HasProposition(proposition))
                    {
                        UnsatisfiedOnNextLayer.Push(proposition);
                        continue;
                    }

                    // 2.) try to satisfy the proposition by a support action node

                    ActionNode relevantActionNode = GetBestRelevantActionNodeFF(proposition, currentActionLayer);
                    if (relevantActionNode != null)
                    {
                        MarkedActionNodes.Add(relevantActionNode.Operator);
                        foreach (var prevProposition in relevantActionNode.Predecessors)
                        {
                            UnsatisfiedOnNextLayer.Push(prevProposition);
                        }
                    }
                }

                UnsatisfiedSwapper        = UnsatisfiedOnNextLayer;
                UnsatisfiedOnNextLayer    = UnsatisfiedOnCurrentLayer;
                UnsatisfiedOnCurrentLayer = UnsatisfiedSwapper;
                UnsatisfiedOnNextLayer.Clear();
            }

            // the result value is a sum of costs of marked action nodes

            double result = 0;

            foreach (var markedActionNode in MarkedActionNodes)
            {
                result += markedActionNode.GetCost();
            }
            return(result);
        }