void Execute() { var planner = new ActionPlanner(); planner.Clear(); planner.SetPrecondition(Actions.Scout, Atoms.Alive, true); planner.SetPostcondition(Actions.Scout, Atoms.EnemyVisible, true); planner.SetPrecondition(Actions.Approach, Atoms.EnemyVisible, true); planner.SetPostcondition(Actions.Approach, Atoms.NearEnemy, true); planner.SetPrecondition(Actions.Aim, Atoms.EnemyVisible, true); planner.SetPrecondition(Actions.Aim, Atoms.WeaponLoaded, true); planner.SetPostcondition(Actions.Aim, Atoms.EnemyLinedUp, true); planner.SetPrecondition(Actions.Shoot, Atoms.EnemyLinedUp, true); planner.SetPostcondition(Actions.Shoot, Atoms.EnemyAlive, false); planner.SetPrecondition(Actions.Load, Atoms.ArmedWithGun, true); planner.SetPostcondition(Actions.Load, Atoms.WeaponLoaded, true); planner.SetPrecondition(Actions.DetonateBomb, Atoms.ArmedWithBomb, true); planner.SetPrecondition(Actions.DetonateBomb, Atoms.NearEnemy, true); planner.SetPostcondition(Actions.DetonateBomb, Atoms.Alive, false); planner.SetPostcondition(Actions.DetonateBomb, Atoms.EnemyAlive, false); planner.SetCost(Actions.DetonateBomb, 5); planner.SetPrecondition(Actions.Flee, Atoms.EnemyVisible, true); planner.SetPostcondition(Actions.Flee, Atoms.EnemyVisible, false); Debug.Log(planner.ToString<Actions, Atoms>()); var state = new WorldState(); state.Set(Atoms.EnemyVisible, false); state.Set(Atoms.ArmedWithGun, true); state.Set(Atoms.WeaponLoaded, false); state.Set(Atoms.EnemyLinedUp, false); state.Set(Atoms.EnemyAlive, true); state.Set(Atoms.ArmedWithBomb, true); state.Set(Atoms.NearEnemy, false); state.Set(Atoms.Alive, true); Debug.LogFormat("State: {0}", state.ToString<Atoms>()); var goal = new WorldState(); goal.Set(Atoms.EnemyAlive, false); Debug.LogFormat("Goal: {0}", goal.ToString<Atoms>()); var search = new Fringe(Graph.PlannerHeuristic); var stateNode = new WorldStateNode(planner, state); var goalNode = new WorldStateNode(planner, goal); var path = search.FindPath(stateNode, goalNode); foreach (WorldStateNode node in path) { Debug.Log(node.State); } }
public IEnumerable<INode> GetFringePath(GameObject Start, GameObject End) { IEnumerable<INode> path; PathFinder.Fringe fringe = new PathFinder.Fringe(Heuristic); Node startNode = getNodeAtPos(Start.transform.position); Node endNode = getNodeAtPos(End.transform.position); startNode.isStartEnd = true; endNode.isStartEnd = true; path = fringe.FindPath((INode)startNode, (INode)endNode); if (fringe.PathCost > 200) { return new List<INode>(); } startNode.isStartEnd = false; endNode.isStartEnd = false; return path; }
protected void Awake() { gridSpace = transform.Find("GridSpace") as RectTransform; var size = 100; var grid = new Grid2D(size, size); grid[5, 5].Weight = float.MaxValue; grid[5, 6].Weight = float.MaxValue; grid[5, 7].Weight = float.MaxValue; grid[5, 8].Weight = float.MaxValue; grid[5, 9].Weight = float.MaxValue; grid[5, 10].Weight = float.MaxValue; grid[5, 11].Weight = float.MaxValue; grid[5, 12].Weight = float.MaxValue; grid[5, 13].Weight = float.MaxValue; grid[5, 14].Weight = float.MaxValue; grid[15, 5].Weight = float.MaxValue; grid[15, 6].Weight = float.MaxValue; grid[15, 7].Weight = float.MaxValue; grid[15, 8].Weight = float.MaxValue; grid[15, 9].Weight = float.MaxValue; grid[15, 10].Weight = float.MaxValue; grid[15, 11].Weight = float.MaxValue; grid[15, 12].Weight = float.MaxValue; grid[15, 13].Weight = float.MaxValue; grid[15, 14].Weight = float.MaxValue; grid[5, 15].Weight = float.MaxValue; grid[6, 15].Weight = float.MaxValue; grid[7, 15].Weight = float.MaxValue; grid[8, 15].Weight = float.MaxValue; grid[9, 15].Weight = float.MaxValue; grid[10, 15].Weight = float.MaxValue; grid[11, 15].Weight = float.MaxValue; grid[12, 15].Weight = float.MaxValue; grid[13, 15].Weight = float.MaxValue; grid[14, 15].Weight = float.MaxValue; grid[15, 15].Weight = float.MaxValue; //grid[5, 4].Weight = float.MaxValue; //grid[6, 4].Weight = float.MaxValue; //grid[7, 4].Weight = float.MaxValue; //grid[8, 4].Weight = float.MaxValue; //grid[9, 4].Weight = float.MaxValue; //grid[10, 4].Weight = float.MaxValue; //grid[11, 4].Weight = float.MaxValue; //grid[12, 4].Weight = float.MaxValue; //grid[13, 4].Weight = float.MaxValue; //grid[14, 4].Weight = float.MaxValue; //grid[15, 4].Weight = float.MaxValue; tiles = new Image[size, size]; gridSpace.gameObject.AddComponent<Selectable>(); Vector2 anchorMin, anchorMax; for (int i = 0; i < size; i++) { anchorMin.x = ((float)i / size); anchorMax.x = ((float)(i + 1) / size); for (int j = 0; j < size; j++) { anchorMin.y = ((float)j / size); anchorMax.y = ((float)(j + 1) / size); var obj = new GameObject(string.Format("Node [{0},{1}]", i, j)); obj.transform.SetParent(gridSpace); var trans = obj.AddComponent<RectTransform>(); trans.anchorMin = anchorMin; trans.anchorMax = anchorMax; trans.offsetMax = Vector2.zero; trans.offsetMin = Vector2.zero; trans.localScale = Vector3.one; var image = obj.AddComponent<Image>(); image.color = (Color.blue * anchorMax.x + Color.red * (1 - anchorMax.x)) * anchorMax.y + (Color.yellow * anchorMax.x + Color.green * (1 - anchorMax.x)) * (1 - anchorMax.y); var c = 1f / (grid[i, j].Weight); image.color = new Color(c, c, c, 1); tiles[i, j] = image; } } var search = new Fringe(Grid2D.HeuristicManhattan2); var searchA = new AStar(Grid2D.HeuristicManhattan2); var start = grid[14, 5]; var end = grid[90, 90]; tiles[start.X, start.Y].color = Color.blue + Color.white * .5f; tiles[end.X, end.Y].color = Color.yellow + Color.white * .5f; //StartCoroutine(search.AdvanceFrontier(start, end, () => { // foreach (Node node in search.mappedNodes.Keys) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.red + Color.white * .5f; // foreach (Node node in search.frontier) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.green + Color.white * .5f; // return StartCoroutine(WaitKeyDown(KeyCode.None)); //})); // astar var beforea = System.DateTime.Now; var pathA = searchA.FindPath(start, end); var aftera = System.DateTime.Now; Debug.Log((aftera - beforea).Milliseconds); //foreach (Node2D node in searchA.mappedNodes.Keys) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.red + Color.white * .5f; //foreach (Node2D node in searchA.frontier) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.green + Color.white * .5f; //foreach (Node2D node in pathA) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.magenta + Color.white * .5f; // fringe var before = System.DateTime.Now; var path = search.FindPath(start, end); var after = System.DateTime.Now; Debug.Log((after - before).Milliseconds); foreach (Node2D node in search.cache.Keys) if (node != start && node != end) tiles[node.X, node.Y].color = Color.red + Color.white * .5f; foreach (Node2D node in search.fringe) if (node != start && node != end) tiles[node.X, node.Y].color = Color.green + Color.white * .5f; foreach (Node2D node in path) if (node != start && node != end) tiles[node.X, node.Y].color = Color.magenta + Color.white * .5f; //StartCoroutine(search.FindInteractive(start, end, () => //{ // foreach (Node2D node in search.cache.Keys) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.red + Color.white * .5f; // foreach (Node2D node in search.fringe) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.green + Color.white * .5f; // foreach (Node2D node in search.pathInteractive) // if (node != start && node != end) // tiles[node.X, node.Y].color = Color.magenta + Color.white * .5f; // return StartCoroutine(WaitKeyDown(KeyCode.None)); //})); }
void Execute() { var planner = new ActionPlanner(); planner.Clear(); var scout = planner.CreateAction("Scout"); scout.SetPrecondition ("Alive", true); scout.SetPostcondition("EnemyVisible", true); var approach =planner.CreateAction("Approach"); approach.SetPrecondition ("EnemyVisible", true); approach.SetPostcondition("NearEnemy", true); var aim =planner.CreateAction("Aim"); aim.SetPrecondition ("EnemyVisible", true); aim.SetPrecondition ("WeaponLoaded", true); aim.SetPostcondition("EnemyLinedUp", true); var shoot =planner.CreateAction("Shoot"); shoot.SetPrecondition ("EnemyLinedUp", true); shoot.SetPostcondition("EnemyAlive", false); var load =planner.CreateAction("Load"); load.SetPrecondition ("ArmedWithGun", true); load.SetPostcondition("WeaponLoaded", true); var detonateBomb =planner.CreateAction("DetonateBomb", 5); detonateBomb.SetPrecondition ("ArmedWithBomb", true); detonateBomb.SetPrecondition ("NearEnemy", true); detonateBomb.SetPostcondition("Alive", false); detonateBomb.SetPostcondition("EnemyAlive", false); var flee =planner.CreateAction("Flee"); flee.SetPrecondition ("EnemyVisible", true); flee.SetPostcondition("EnemyVisible", false); Debug.Log(planner.ToString()); var state = new WorldState(); state[planner.StringToAtom("EnemyVisible")] = false; state[planner.StringToAtom("ArmedWithGun")] = true; state[planner.StringToAtom("WeaponLoaded")] = false; state[planner.StringToAtom("EnemyLinedUp")] = false; state[planner.StringToAtom("EnemyAlive")] = true; state[planner.StringToAtom("ArmedWithBomb")] = true; state[planner.StringToAtom("NearEnemy")] = false; state[planner.StringToAtom("Alive")] = true; Debug.LogFormat("State: {0}", state.ToString(planner.Atoms)); var goal = new WorldState(); goal[planner.StringToAtom("EnemyAlive")] = false; Debug.LogFormat("Goal: {0}", goal.ToString(planner.Atoms)); var search = new Fringe(GoapGraph.PlannerHeuristic); var graph = new GoapGraph(planner); var stateNode = new WorldStateNode(graph, state, null); var goalNode = new WorldStateNode(graph, goal, null); var path = search.FindPath(stateNode, goalNode); foreach (WorldStateNode node in path) { Debug.Log(node.State.ToString(planner.Atoms)); } }
public void Execute() { var sb = new StringBuilder(); var planner = new ActionPlanner(); planner.Clear(); foreach (Transform actionViewTransform in actionsContainer.container) { var actionView = actionViewTransform.GetComponent<ActionView>(); if (!actionView) continue; var action = planner.CreateAction(actionView.GetName()); action.Cost = actionView.GetCost(); foreach (var atom in actionView.GetPreconditions()) { action.SetPrecondition(atom.GetName(), atom.GetState()); } foreach (var atom in actionView.GetPostconditions()) { action.SetPostcondition(atom.GetName(), atom.GetState()); } } sb.AppendLine(".:: Action PLanner ::."); sb.AppendLine(planner.ToString()); var state = new WorldState(); foreach (Transform atom in initialStateContainer.container) { var atomView = atom.GetComponent<AtomView>(); if (atomView) state[planner.StringToAtom(atomView.GetName())] = atomView.GetState(); } sb.AppendLine(".:: Initial State ::."); sb.AppendFormat("{0}\n\n", state.ToString(planner.Atoms)); var goal = new WorldState(); foreach (Transform atom in goalContainer.container) { var atomView = atom.GetComponent<AtomView>(); if (atomView) goal[planner.StringToAtom(atomView.GetName())] = atomView.GetState(); } sb.AppendLine(".:: Goal ::."); sb.AppendFormat("{0}\n\n", goal.ToString(planner.Atoms)); var search = new Fringe(GoapGraph.PlannerHeuristic); var graph = new GoapGraph(planner); var stateNode = new WorldStateNode(graph, state, null); var goalNode = new WorldStateNode(graph, goal, null); var path = search.FindPath(stateNode, goalNode); var padding = 0; foreach (WorldStateNode node in path) { if (node.Action == null) continue; padding = System.Math.Max(node.Action.Name.Length, padding); } var format = "{0," + padding + "}: {1}\n"; sb.AppendLine(".:: Action Plan ::."); foreach (WorldStateNode node in path) { sb.AppendFormat(format, node.Action == null ? "" : node.Action.Name, node.State.ToString(planner.Atoms)); } outputText.text = sb.ToString(); }
public IEnumerable<INode> GetFringePath(Vector3 startPosition, Vector3 endPosition, int maxSteps = int.MaxValue) { var fringe = new Fringe(Heuristic); var startNode = getNodeAtPos(startPosition); var endNode = getNodeAtPos(endPosition); if (startNode == null || endNode == null) return new List<INode>(); startNode.isStartEnd = true; endNode.isStartEnd = true; var path = fringe.FindPath(startNode, endNode, maxSteps); if (fringe.PathCost > 2000) { return new List<INode>(); } startNode.isStartEnd = false; endNode.isStartEnd = false; return path; }