示例#1
0
 public PlanningAction(string name, IEnumerable <IPrecondition> preconditions, IEnumerable <IEffect> effects, double cost)
 {
     this.Name          = PreconditionUtils.EnsureNotBlank(name, "name");
     this.preconditions = new List <IPrecondition>(PreconditionUtils.EnsureNotNull(preconditions, "preconditions"));
     this.effects       = new List <IEffect>(PreconditionUtils.EnsureNotNull(effects, "effects"));
     this.Cost          = cost;
 }
示例#2
0
        public GoalSelector(IDictionary <Goal, GoalEvaluator> evaluators, float reevaluationPeriod = DEFAULT_REEVALUATION_PERIOD)
        {
            this.evaluators         = PreconditionUtils.EnsureNotNull(evaluators, "evaluators");
            this.reevaluationPeriod = reevaluationPeriod;
            this.reevaluationTimer  = new ResettableStopwatchExecutionTimer(false);

            this.ForceReevaluation();
        }
示例#3
0
        //private readonly Func<int, int> effectDeapplication;

        public SingleSymbolEffect(SymbolId symbolId, Func <int, int> effectApplication /*, Func<int, int> effectDeapplication = null*/)
        {
            this.symbolId          = symbolId;
            this.effectApplication = PreconditionUtils.EnsureNotNull(effectApplication, "effectApplication");
//			this.effectDeapplication = effectDeapplication == null
//				? ((value) => value)	// Use identity as default deapplication
//				: effectDeapplication;
        }
示例#4
0
 public static ForwardNode MakeRegularNode(WorldState worldState, INodeExpander <ForwardNode> nodeExpander)
 {
     return(new ForwardNode(
                PreconditionUtils.EnsureNotNull(worldState, "worldState"),
                PreconditionUtils.EnsureNotNull(nodeExpander, "nodeExpander"),
                default(Goal),
                false
                ));
 }
示例#5
0
        public SimpleActionFactory(
            IDictionary <PlanningAction, Func <IAction> > actionFactories
            )
        {
            PreconditionUtils.EnsureNotNull(actionFactories, "actionFactories");

            this.actionFactories     = actionFactories.ToDictionary(kvp => kvp.Key.Name, kvp => kvp.Value);
            SupportedPlanningActions = new HashSet <PlanningAction>(actionFactories.Keys);
        }
示例#6
0
 public PlanningAction(string name, IEnumerable <IPrecondition> preconditions, IEnumerable <IEffect> effects,
                       double cost)
 {
     IsExperience  = false;
     Name          = PreconditionUtils.EnsureNotBlank(name, "name");
     Preconditions = new List <IPrecondition>(PreconditionUtils.EnsureNotNull(preconditions, "preconditions"));
     Effects       = new List <IEffect>(PreconditionUtils.EnsureNotNull(effects, "effects"));
     Cost          = cost;
 }
示例#7
0
        private GoalSelector(IDictionary <Goal, GoalEvaluator> evaluators,
                             float reevaluationPeriod = DefaultReevaluationPeriod)
        {
            this.evaluators         = PreconditionUtils.EnsureNotNull(evaluators, "evaluators");
            this.reevaluationPeriod = reevaluationPeriod;
            reevaluationTimer       = new ResettableStopwatchExecutionTimer(false);

            ForceReevaluation();
        }
 public RelevantSymbolsPopulator(IEnumerable <PlanningAction> availableActions, Goal goal)
 {
     this.relevantSymbols = PreconditionUtils.EnsureNotNull(availableActions, "availableActions").Aggregate(
         new List <SymbolId>(),
         (soFar, action) => {
         soFar.AddRange(action.GetRelevantSymbols());
         return(soFar);
     }
         ).Concat(PreconditionUtils.EnsureNotNull(goal, "goal").PreconditionSymbols);
 }
示例#9
0
        public PlanExecutor(IActionFactory actionFactory)
        {
            this.actionFactory = PreconditionUtils.EnsureNotNull(actionFactory, "actionFactory");

            Status             = ExecutionStatus.None;
            Plan               = null;
            CurrentActionIndex = -1;

            currentAction = null;
        }
示例#10
0
        //public bool AssumeNonNegativeCosts { get; }

        private AstarPathfinderConfiguration(
            PathCostHeuristic <GraphNode> heuristic,
            int maxSearchDepth,
            float maxSecondsPerSearch            //,
            //bool assumeNonNegativeCosts
            )
        {
            this.Heuristic           = PreconditionUtils.EnsureNotNull(heuristic, "heuristic must not be null");
            this.MaxSearchDepth      = maxSearchDepth;
            this.MaxSecondsPerSearch = maxSecondsPerSearch;
            //this.AssumeNonNegativeCosts = assumeNonNegativeCosts;
        }
 private AStarPathfinderConfiguration(
     PathCostHeuristic <TGraphNode> heuristic,
     int maxSearchDepth          = UnlimitedSearchDepth,
     float maxSecondsPerSearch   = UnlimitedSecondsPerSearch,
     bool assumeNonNegativeCosts = DefaultAssumeNonNegativeCosts
     )
 {
     Heuristic              = PreconditionUtils.EnsureNotNull(heuristic, "heuristic must not be null");
     MaxSearchDepth         = maxSearchDepth;
     MaxSecondsPerSearch    = maxSecondsPerSearch;
     AssumeNonNegativeCosts = assumeNonNegativeCosts;
 }
示例#12
0
 public static RegressiveNode MakeRegular(
     RegressiveState currentConstraints,
     WorldState initialState,
     INodeExpander <RegressiveNode> nodeExpander
     )
 {
     return(new RegressiveNode(
                currentConstraints,
                initialState,
                PreconditionUtils.EnsureNotNull(nodeExpander, "nodeExpander"),
                false
                ));
 }
示例#13
0
 private AgentConfiguration(
     IGoalSelector goalSelector,
     IPlanner planner,
     IKnowledgeProvider knowledgeProvider,
     IPlanExecutor planExecutor,
     IReevaluationSensor reevaluationSensor = null
     )
 {
     GoalSelector       = PreconditionUtils.EnsureNotNull(goalSelector, "goalSelector");
     Planner            = PreconditionUtils.EnsureNotNull(planner, "planner");
     KnowledgeProvider  = PreconditionUtils.EnsureNotNull(knowledgeProvider, "knowledgeProvider");
     PlanExecutor       = PreconditionUtils.EnsureNotNull(planExecutor, "planExecutor");
     ReevaluationSensor = reevaluationSensor ?? new NullReevaluationSensor();
 }
示例#14
0
        // etc.

        public AgentEnvironment(
            IGoalSelector goalSelector,
            IPlanner planner,
            IKnowledgeProvider knowledgeProvider,
            IPlanExecutor planExecutor,
            IReevaluationSensor reevaluationSensor = null
            )
        {
            this.GoalSelector       = PreconditionUtils.EnsureNotNull(goalSelector, "goalSelector");
            this.Planner            = PreconditionUtils.EnsureNotNull(planner, "planner");
            this.KnowledgeProvider  = PreconditionUtils.EnsureNotNull(knowledgeProvider, "knowledgeProvider");
            this.PlanExecutor       = PreconditionUtils.EnsureNotNull(planExecutor, "planExecutor");
            this.ReevaluationSensor = reevaluationSensor != null ? reevaluationSensor : new NullReevaluationSensor();
        }
        public WorldState PopulateWorldState(IKnowledgeProvider knowledgeProvider, WorldState initialWorldState = default(WorldState))
        {
            PreconditionUtils.EnsureNotNull(knowledgeProvider, "knowledgeProvider");

            var builder = initialWorldState.BuildUpon();

            foreach (var symbolId in relevantSymbols)
            {
                if (!initialWorldState.Contains(symbolId))
                {
                    builder.SetSymbol(symbolId, knowledgeProvider.GetSymbolValue(symbolId));
                }
            }
            return(builder.Build());
        }
示例#16
0
文件: Plan.cs 项目: mikedemele/E-GOAP
        public Plan(IEnumerable<PlanningAction> actions, Goal goal)
        {
            var actionList = actions.ToList();
            PrintRawPlan(actionList);
            for (var i = 0; i < actionList.Count; i++)
            {
                var action = actionList[i];
                if (action.IsExperience)
                {
                    actionList.Remove(action);
                    actionList.InsertRange(i, ((ExperienceAction) action).Actions);
                }
            }

            Actions = new List<PlanningAction>(PreconditionUtils.EnsureNotNull(actionList, "actions")).AsReadOnly();
            Goal = PreconditionUtils.EnsureNotNull(goal, "goal");
        }
示例#17
0
 public SymbolId(string name)
 {
     Name = PreconditionUtils.EnsureNotBlank(name, "name");
 }
示例#18
0
 public ForwardEdge(PlanningAction action, ForwardNode sourceNode, ForwardNode targetNode)
 {
     this.Action     = PreconditionUtils.EnsureNotNull(action, "action");
     this.SourceNode = sourceNode;
     this.TargetNode = targetNode;
 }
示例#19
0
文件: Plan.cs 项目: terrapass/game-ai
 public Plan(IEnumerable <PlanningAction> actions, Goal goal)
 {
     this.Actions = new List <PlanningAction>(PreconditionUtils.EnsureNotNull(actions, "actions")).AsReadOnly();
     this.Goal    = PreconditionUtils.EnsureNotNull(goal, "goal");
 }
示例#20
0
 public void AddSensor(IReevaluationSensor sensor)
 {
     sensors.Add(PreconditionUtils.EnsureNotNull(sensor, nameof(sensor)));
 }
示例#21
0
        public IEnumerable <IGraphEdge <RegressiveNode> > ExpandNode(RegressiveNode node)
        {
            if (PreconditionUtils.EnsureNotNull(node, "node").IsTarget)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "{0} cannot expand a target {1}",
                              this.GetType(),
                              node.GetType()
                              )
                          );
            }

            var result = new List <IGraphEdge <RegressiveNode> >();

            foreach (var action in availableActions)
            {
                bool incompatible = false;                      // Whether any effect contradicts existing constraints
                // TODO: Ensure this is calculated properly.
                bool useful  = false;                           // Whether any effect affects existing constraints
                bool deadend = false;                           // Whether any resulting constraint is unsatisfiable

                var resultingConstraintsBuilder = node.CurrentConstraints.BuildUpon();
                foreach (var effect in action.Effects)
                {
                    if (effect.ValueAssigned != null)
                    {
                        if (!node.CurrentConstraints[effect.SymbolId].Contains(effect.ValueAssigned.Value))
                        {
                            // (At least) one of this action's effects contradicts the current constraints,
                            // so this action cannot immediately precede the current node.
                            incompatible = true;
                            break;
                        }
                        else
                        {
                            // If updating the symbol value matters
                            if (!node.CurrentConstraints[effect.SymbolId].Equals(ValueRange.AnyValue))
                            {
                                // Since the effect resets symbol to a new value,
                                // its value before action is not important
                                // (unless there is a corresponding precondition on the action itself,
                                // but this is checked later)
                                resultingConstraintsBuilder.SetRange(effect.SymbolId, ValueRange.AnyValue);
                                useful = true;
                            }
                        }
                    }
                    else if (effect.ValueDelta != null)
                    {
                        // Shift constraints to account for this effect's contribution into symbol value.
                        resultingConstraintsBuilder.SetRange(
                            effect.SymbolId, node.CurrentConstraints[effect.SymbolId].ShiftBy(-effect.ValueDelta.Value)
                            );
                        //useful = true;
                        //useful = (useful || resultingConstraintsBuilder[effect.SymbolId].Size > node.CurrentConstraints[effect.SymbolId].Size);
                        // The action is useful if at least one of its effects
                        // shifts at least one of existing constraints
                        // closer to the initial state value for corresponding symbol.
                        useful = (
                            useful
                            ||
                            resultingConstraintsBuilder[effect.SymbolId].AbsDistanceFrom(node.InitialState[effect.SymbolId])
                            <
                            node.CurrentConstraints[effect.SymbolId].AbsDistanceFrom(node.InitialState[effect.SymbolId])
                            );
                    }
                    else
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      "Effect {0} cannot be used with RegressivePlanner: either ValueAssigned or ValueDelta must return non-null",
                                      effect.GetType()
                                      )
                                  );
                    }
                }

                if (incompatible)
                {
                    continue;
                }

                foreach (var precond in action.Preconditions)
                {
                    try
                    {
                        resultingConstraintsBuilder.IntersectRange(precond.SymbolId, precond.AsValueRange);
                        // If we've arrived at an unsatisfiable constraint
                        if (resultingConstraintsBuilder[precond.SymbolId].IsEmpty)
                        {
                            deadend = true;
                            break;
                        }
                    }
                    catch (NotImplementedException)
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      "Precondition {0} cannot be used with RegressivePlanner: AsValueRange must be implemented",
                                      precond.GetType()
                                      )
                                  );
                    }
                }

                if (!incompatible && useful && !deadend)
                {
                    result.Add(
                        new RegressiveEdge(
                            action,
                            node,
                            RegressiveNode.MakeRegular(
                                resultingConstraintsBuilder.Build(),
                                node.InitialState,
                                this
                                )
                            )
                        );
                }
            }

            return(result);
        }
示例#22
0
 protected SingleSymbolEffect(SymbolId symbolId,
                              Func <int, int> effectApplication)
 {
     SymbolId = symbolId;
     this.effectApplication = PreconditionUtils.EnsureNotNull(effectApplication, "effectApplication");
 }
示例#23
0
 public ForwardNodeExpander(IEnumerable <PlanningAction> availableActions)
 {
     this.availableActions = PreconditionUtils.EnsureNotNull(availableActions, "availableActions");
 }
示例#24
0
 public ActionGetResource(Townsman subject, ResourceType resource, Storage source)
     : base(subject)
 {
     resourceType = resource;
     this.source  = PreconditionUtils.EnsureNotNull(source, "source");
 }
示例#25
0
 public RegressiveEdge(PlanningAction action, RegressiveNode sourceNode, RegressiveNode targetNode)
 {
     Action     = PreconditionUtils.EnsureNotNull(action, "action");
     SourceNode = sourceNode;
     TargetNode = targetNode;
 }
示例#26
0
 public ActionPutResource(Townsman subject, ResourceType resource, Storage target)
     : base(subject)
 {
     resourceType = resource;
     this.target  = PreconditionUtils.EnsureNotNull(target, "target");
 }
示例#27
0
 public void RemoveSensor(IReevaluationSensor sensor)
 {
     sensors.Remove(PreconditionUtils.EnsureNotNull(sensor, nameof(sensor)));
 }
示例#28
0
 public ActionGetTool(Townsman subject, ToolType toolType, ToolBench source)
     : base(subject)
 {
     this.toolType = toolType;
     this.source   = PreconditionUtils.EnsureNotNull(source, "source");
 }
示例#29
0
 public Goal(string name, IEnumerable <IPrecondition> preconditions)
 {
     Name          = PreconditionUtils.EnsureNotBlank(name, "name");
     Preconditions = new List <IPrecondition>(PreconditionUtils.EnsureNotNull(preconditions, "preconditions"));
 }
示例#30
0
 public ActionPutTool(Townsman subject, ToolType toolType, ToolBench target)
     : base(subject)
 {
     this.toolType = toolType;
     this.target   = PreconditionUtils.EnsureNotNull(target, "target");
 }