public void SingleSolution() { CurrentState state = new CurrentState(); IDecision[] decisions = new IDecision[] { new MoveToFridge(state), new KillFridgeGuardian(state), new OpenFridgeDecision(state), new GetBananaFromFridge(state) }; Planner planner = new Planner(StateOffset.Heuristic); for (int i = 0; i < decisions.Length; i++) { planner.AddDecision(decisions[i]); } StateOffset goal = new StateOffset(); goal.Set("HasBanana", true); Queue<IDecision> plan = planner.CreatePlan(goal); Assert.Greater(plan.Count, 0); Assert.AreSame(decisions[0], plan.Dequeue()); // MoveToFridge Assert.AreSame(decisions[1], plan.Dequeue()); // KillFridgeGuardian Assert.AreSame(decisions[1], plan.Dequeue()); // KillFridgeGUardian Assert.AreSame(decisions[1], plan.Dequeue()); // KillFridgeGuardian Assert.AreSame(decisions[2], plan.Dequeue()); // OpenFridge Assert.AreSame(decisions[3], plan.Dequeue()); // GetBananaFromFridge }
public static int Heuristic(StateOffset s1, StateOffset s2) { IEnumerable<string> intersection = s1._stateValues.Keys.Intersect(s2._stateValues.Keys); int difference = 0; foreach (string key in intersection) { StateValue sv1 = s1._stateValues[key]; StateValue sv2 = s2._stateValues[key]; ValueUnion v1 = sv1.Value; ValueUnion v2 = sv2.Value; if (sv1.Type == sv2.Type) { switch (sv1.Type) { case StateValue.ValueType.Float: difference += (int)Math.Abs(v2.FloatValue - v1.FloatValue + 0.5f); break; case StateValue.ValueType.Int: difference += Math.Abs(v2.IntValue - v1.IntValue); break; case StateValue.ValueType.Bool: difference += (v2.BoolValue ? 1 : 0) - (v1.BoolValue ? 1 : 0); break; case StateValue.ValueType.Point: difference += Point.TaxicabDistance(v1.PointValue, v2.PointValue); break; } } } return difference; }
public SquadUnit(Combatant unit) { Unit = unit; Goal = new StateOffset(); IsManual = true; Planner = new Planner(StateOffset.Heuristic); }
public StateOffset(StateOffset offset = null) { if (offset == null) { _stateValues = new Dictionary<string, StateValue>(); } else { _stateValues = new Dictionary<string, StateValue>(offset._stateValues); } }
public void GetSetBool() { StateOffset state = new StateOffset(); state.Set("SetValue", true); Assert.IsTrue(state.Get("SetValue", false), "Assigned value for key does not match the retrieved value"); Assert.IsTrue(state.Get("NotSetValue", true), "Get did not return the default value for an unassigned key"); }
public void GetSetPoint() { StateOffset state = new StateOffset(); state.Set("SetValue", new Point(256, 512)); Assert.AreEqual(new Point(256, 512), state.Get("SetValue", Point.Zero), "Assigned value for key does not match the retrieved value"); Assert.AreEqual(new Point(123, 456), state.Get("NotSetValue", new Point(123, 456)), "Get did not return the default value for an unassigned key"); }
public void GetSetInt() { StateOffset state = new StateOffset(); state.Set("SetValue", 256); Assert.AreEqual(256, state.Get("SetValue", 999), "Assigned value for key does not match the retrieved value"); Assert.AreEqual(12345, state.Get("NotSetValue", 12345), "Get did not return the default value for an unassigned key"); }
public static StateOffset operator +(StateOffset s1, StateOffset s2) { StateOffset sum = new StateOffset(s1); foreach (string key in s2._stateValues.Keys) { sum._stateValues[key] = s2._stateValues[key]; } return sum; }
private void FindTarget() { foreach (Pawn pawn in _owner.PawnsInView) { Combatant target = pawn as Combatant; if (target != null && target.Team.IsHostile(_owner.Team)) { _target = target; _currentGoal = new StateOffset(); _currentGoal.Set(StateKey.Health(_target), 0); _attackDecision.Target = _target; _approachDecision.Target = _target; break; } } }
public void GoalNodeNeighborIterator() { CurrentState state = new CurrentState(); List<IDecision> decisions = new List<IDecision>() { new MoveToFridge(state), new KillFridgeGuardian(state), new OpenFridgeDecision(state), new GetBananaFromFridge(state) }; StateOffset goal = new StateOffset(); //goal.SetStateValue("HasBanana", true, false); GoalNode end = new GoalNode(goal, decisions); int i = decisions.Count - 1; foreach (GoalNode neighbor in end.GetNeighbors()) { Console.WriteLine(end.GetEdge(neighbor)); //Assert.AreSame(decisions[i--], end.GetEdge(neighbor)); } }
public IEnumerable<IGraphNode> GetNeighbors() { if (_cacheComplete) { foreach (GoalNode neighbor in _edges.Keys) { yield return neighbor; } } else { for (int i = 0; i < _decisions.Count; i++) { if (_decisions[i].ArePreconditionsMet(State)) { StateOffset post = new StateOffset(State); _decisions[i].ApplyPostconditions(post); GoalNode previousGoal = new GoalNode(post, _decisions); _edges.Add(previousGoal, _decisions[i]); yield return previousGoal; } } _cacheComplete = true; } }
public void Adding() { StateOffset first = new StateOffset(); first.Set("A", 1); first.Set("B", 2f); first.Set("C", true); first.Set("D", new Point(4, 4)); StateOffset second = new StateOffset(); second.Set("E", 123123123); Console.Write("First: " + first); Console.Write("Second: " + second); second += first; Console.Write("First + Second: " + second); Assert.AreEqual(1, second.Get("A", 0)); Assert.AreEqual(2f, second.Get("B", 0f)); Assert.AreEqual(true, second.Get("C", false)); Assert.AreEqual(new Point(4, 4), second.Get("D", Point.Zero)); Assert.AreEqual(123123123, second.Get("E", 0)); }
public bool ArePreconditionsMet(StateOffset state) { return state.Get("GuardianHealth", _state.GuardianHealth) > 0 && state.Get("FridgeLocked", _state.IsFridgeLocked) && state.Get("NearFridge", _state.IsNearFridge); }
public GoalNode(StateOffset state, List<IDecision> decisions) { State = state ?? new StateOffset(); _decisions = decisions ?? new List<IDecision>(); }
public abstract StateOffset ApplyPostconditions(StateOffset state);
public abstract bool ArePreconditionsMet(StateOffset state);
public bool IsSubsetOf(StateOffset other) { bool isSubset = true; foreach (string key in _stateValues.Keys) { StateValue value = _stateValues[key]; StateValue otherVal; other._stateValues.TryGetValue(key, out otherVal); if (!otherVal.Active || (otherVal.Active && !value.Value.Equals(otherVal.Value))) { isSubset = false; break; } } return isSubset; }
public StateOffset ApplyPostconditions(StateOffset state) { state.Set("HasBanana", true); return state; }
public bool ArePreconditionsMet(StateOffset state) { return !state.Get("FridgeLocked", _state.IsFridgeLocked) && !state.Get("FridgeOpen", _state.IsFridgeOpen); }
public StateOffset ApplyPostconditions(StateOffset state) { state.Set("FridgeOpen", true); return state; }
public Queue<IDecision> CreatePlan(StateOffset goal) { GoalNode start = new GoalNode(new StateOffset(), _decisions); GoalNode end = new GoalNode(goal, _decisions); Queue<GoalNode> goals = new Queue<GoalNode>(GraphMethods<GoalNode>.FindPath(start, end, Heuristic)); Queue<IDecision> plan = new Queue<IDecision>(); if (goals.Count > 0) { GoalNode previous = goals.Dequeue(); while (goals.Count > 0) { GoalNode top = goals.Dequeue(); IDecision edge = previous.GetEdge(top); plan.Enqueue(edge); previous = top; } } return plan; }
public bool ArePreconditionsMet(StateOffset state) { return state.Get("FridgeOpen", _state.IsFridgeOpen) && !state.Get("HasBanana", _state.HasBanana); }
public StateOffset ApplyPostconditions(StateOffset state) { int guardianHealth = state.Get("GuardianHealth", _state.GuardianHealth) - 1; if (guardianHealth <= 0) { state.Set("GuardianHealth", 0); state.Set("GuardianDead", true); state.Set("FridgeLocked", false); } else { state.Set("GuardianHealth", guardianHealth); state.Set("GuardianDead", false); state.Set("FridgeLocked", true); } return state; }
public void IsSubset() { StateOffset first = new StateOffset(); StateOffset second = new StateOffset(); first.Set("A", 1); second.Set("A", 1); first.Set("B", 2f); second.Set("B", 2f); first.Set("C", new Point(3, 3)); Console.Write("First: " + first); Console.Write("Second: " + second); Assert.IsTrue(second.IsSubsetOf(first), "Second was not determined to be subset to First"); Assert.IsFalse(first.IsSubsetOf(second), "First was evaulated as subset to Second. What the hell."); }
public bool ArePreconditionsMet(StateOffset state) { return !state.Get("NearFridge", _state.IsNearFridge); }