protected static GraphPlan <State> CreatePlanner() { var planningActions = new List <IPlanningAction <State> > { new PlanningAction <State>( name: "swap 1 with 2", conditions: x => x["1"] > 1, effects: x => { x["1"] -= 1; x["2"] += 1; }), new PlanningAction <State>( name: "swap 2 with 1", conditions: x => x["2"] > 1, effects: x => { x["1"] += 1; x["2"] -= 1; }), }; var stateComparer = new StateComparer(); return(new GraphPlan <State>(Enums.PlanningMethod.DepthFirst, planningActions.ToArray(), stateComparer)); }
public GoalService(WorldStateService stateService) { _stateService = stateService; _registeredGoals = new List<Goal>(); _stateComparer = new StateComparer(); _stateService.WorldStateUpdated += ValidateGoals; }
static void TestComparer() { Puzzle puzzle = Puzzles.Raise(); State state1 = new State(puzzle.start, new List<Lift>() { puzzle.locations[0].edgesFrom[0].lift }); State state2 = new State(puzzle.start, new List<Lift>() { puzzle.locations[0].edgesFrom[0].lift }); StateComparer comparer = new StateComparer(); Console.WriteLine(comparer.Equals(state1, state2)); Console.WriteLine(comparer.GetHashCode(state1)); Console.WriteLine(comparer.GetHashCode(state2)); Console.ReadLine(); }
public Node(Node parent, ISuccessor successor) { State = successor.State; Parent = parent; Successor = successor; Cost = parent.Cost + successor.Cost; Depth = parent.Depth + 1; Path = false; Children = new List<Node>(); _stateComparer = new StateComparer(); }
public Node(IState state) { State = state; Parent = null; Successor = null; Cost = 0; Depth = 0; Path = false; Children = new List<Node>(); _stateComparer = new StateComparer(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired // 1.To sort the list by CITY Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST ALPHABETICALLY SORTED BY CITY **"); IComparer <City> comparer = new NameComparer(); cities.Sort(comparer); PrintCities(cities); // 2.To sort the list by STATE Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST ALPHABETICALLY SORTED BY STATE **"); IComparer <City> scomparer = new StateComparer(); cities.Sort(scomparer); PrintCities(cities); // 3.To sort the list by POPULATION largest to smallest Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST SORTED BY POPULATION-LARGEST TO SMALLEST **"); IComparer <City> pcomparer = new PopulationComparer(); cities.Sort(pcomparer); cities.Reverse(); PrintCities(cities); //BONUS MISSION- TO SORT FIRST BY STATE AND THEN BY POPULATION Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST SORTED BY STATE AND THEN BY POPULATION **"); CompoundComparer ccomparer = new CompoundComparer(); ccomparer.Comparers.Add(new StateComparer()); ccomparer.Comparers.Add(new PopulationComparer()); cities.Sort(ccomparer); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired IComparer <City> comparer = new NameComparer(); cities.Sort(comparer); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerState = new StateComparer(); cities.Sort(comparerState); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerArea = new AreaComparer(); cities.Sort(comparerArea); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerPopulation = new PopulationComparer(); cities.Sort(comparerPopulation); PrintCities(cities); Console.ReadLine(); CompoundComparer comparerCompound = new CompoundComparer(); comparerCompound.Comparers.Add(new StateComparer()); //cities.Sort(comparerCompound); comparerCompound.Comparers.Add(new PopulationComparer()); //cities.Sort(comparerCompound); cities.Sort(comparerCompound); PrintCities(cities); Console.ReadLine(); }
static void TestComparer() { Puzzle puzzle = Puzzles.Raise(); State state1 = new State(puzzle.start, new List <Lift>() { puzzle.locations[0].edgesFrom[0].lift }); State state2 = new State(puzzle.start, new List <Lift>() { puzzle.locations[0].edgesFrom[0].lift }); StateComparer comparer = new StateComparer(); Console.WriteLine(comparer.Equals(state1, state2)); Console.WriteLine(comparer.GetHashCode(state1)); Console.WriteLine(comparer.GetHashCode(state2)); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired IComparer <City> comparer = new NameComparer(); IComparer <City> statecomp = new StateComparer(); IComparer <City> popcomp = new PopulationComparer(); IComparer <City> areacomp = new AreaComparer(); //cities.Sort(comparer); //cities.Sort(statecomp); //cities.Sort(popcomp); //cities.Sort(areacomp); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired IComparer <City> comparerN = new NameComparer(); IComparer <City> comparerS = new StateComparer(); IComparer <City> comparerA = new AreaComparer(); IComparer <City> comparerP = new PopulationComparer(); //cities.Sort(comparerN); //cities.Sort(comparerS); cities.Sort(comparerA); //cities.Sort(comparerP); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); IComparer <City> comparer; Console.WriteLine("Choose to sort by: \n" + "1) City name \n" + "2) State name \n" + "3) Population \n" + "4) Area"); string choice = Console.ReadLine(); if (choice == "1") { comparer = new NameComparer(); } else if (choice == "2") { comparer = new StateComparer(); } else if (choice == "3") { comparer = new PopulationComparer(); } else { comparer = new AreaComparer(); } // TODO Swap out comparers as desired //IComparer<City> comparer = new NameComparer(); //IComparer<City> comparer = new StateComparer(); //IComparer<City> comparer = new PopulationComparer(); //IComparer<City> comparer = new AreaComparer(); cities.Sort(comparer); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); IComparer <City> comparerName = new NameComparer(); IComparer <City> comparerState = new StateComparer(); IComparer <City> comparerArea = new AreaComparer(); IComparer <City> comparerPopulation = new PopulationComparer(); // TODO Swap out comparers as desired string choice; do { Console.WriteLine("Sort by (A)rea, (N)ame, (P)opulation, or (S)tate"); choice = Console.ReadLine().ToLower(); if (choice == "n" || choice == "name") { cities.Sort(comparerName); } else if (choice == "a" || choice == "area") { cities.Sort(comparerArea); } else if (choice == "s" || choice == "state") { cities.Sort(comparerState); } else { cities.Sort(comparerPopulation); } PrintCities(cities); Console.ReadLine(); } while (choice != ""); }
public Queue <GOAPAction> GetPlan(List <GOAPState> startState, List <GOAPState> goalState, List <GOAPAction> actionList) { Queue <GOAPAction> actionQueue = new Queue <GOAPAction>(); //Create a state comparer to compare the states StateComparer stateComparer = new StateComparer(); //Opened List contains list of nodes that are still to be assessed //Closed List contains list of nodes that have been processed //previousNodes stores the links between nodes SimplePriorityQueue <List <GOAPState> > openedList = new SimplePriorityQueue <List <GOAPState> >(); Dictionary <List <GOAPState>, bool> closedList = new Dictionary <List <GOAPState>, bool>(stateComparer); Dictionary <List <GOAPState>, List <GOAPState> > previousNodesState = new Dictionary <List <GOAPState>, List <GOAPState> >(stateComparer); Dictionary <List <GOAPState>, GOAPAction> previousNodesAction = new Dictionary <List <GOAPState>, GOAPAction>(stateComparer); Dictionary <List <GOAPState>, float> costs = new Dictionary <List <GOAPState>, float>(stateComparer); var node = goalState; node.OrderBy(n => n.name).ToList(); openedList.Enqueue(node, 1); costs.Add(node, 0); while (openedList.Count > 0) { node = openedList.First; //Check if we've found the solution if (GoalNodeReached(startState, node)) { break; } openedList.Dequeue(); //Add to the closedList closedList.Add(node, true); //Get list of neighbours for (var i = 0; i < actionList.Count; i++) { var action = actionList[i]; if (action.CanResolve(node)) { //Create a new state based on the effects and preconditions of the current action List <GOAPState> tempState = action.UnsetStateEffects(node); tempState = action.SetStatePrecons(tempState); tempState.OrderBy(n => n.name).ToList(); if (closedList.ContainsKey(tempState)) { continue; } float F, G, H; if (!costs.ContainsKey(tempState)) { G = costs[node] + StateGetDist(goalState, tempState); costs.Add(tempState, G); } else { G = costs[tempState]; } H = action.cost; //Calculate the current cost of the path to the current action plus it's own cost F = G + H; var lastF = -1f; if (openedList.TryGetPriority(tempState, out lastF)) { if (F < lastF) { openedList.UpdatePriority(tempState, F); //Keep track of the states and the edges (actions) that lead up to each state if (!previousNodesState.ContainsKey(tempState)) { previousNodesState.Add(tempState, node); } else { previousNodesState[tempState] = node; } if (!previousNodesAction.ContainsKey(tempState)) { previousNodesAction.Add(tempState, action); } else { previousNodesAction[tempState] = action; } } } else { openedList.Enqueue(tempState, F); //Keep track of the states and the edges (actions) that lead up to each state if (!previousNodesState.ContainsKey(tempState)) { previousNodesState.Add(tempState, node); } else { previousNodesState[tempState] = node; } if (!previousNodesAction.ContainsKey(tempState)) { previousNodesAction.Add(tempState, action); } else { previousNodesAction[tempState] = action; } } } } } //If we've found the startState, our regressive search has finished if (GoalNodeReached(startState, node)) { //Ensure the actionQueue is clear actionQueue.Clear(); while (previousNodesAction.ContainsKey(node)) { //Build the new plan var action = previousNodesAction[node]; actionQueue.Enqueue(action); previousNodesAction.Remove(node); node = previousNodesState[node]; } } //Return the plan return(actionQueue); }
public WorldStateService() { _stateSources = new List<IStateSource>(); _stateComparer = new StateComparer(); }
public int CompareTo(object obj) { return(StateComparer.Compare(this, obj as IState)); }
/// <summary> /// Find all folders which match <see cref="Pattern"/>. /// </summary> /// <param name="pathComparer">Search order</param> /// <param name="progress">Reports all searched directory paths</param> /// <returns>Folders which match <see cref="Pattern"/></returns> private IEnumerable <string> FindAll( IComparer <string> pathComparer, IProgress <string> progress) { var comparer = new StateComparer(pathComparer); var parts = Pattern.GetParts().ToList(); if (parts.Count == 0) { yield break; } var firstPath = parts[0]; if (!_fileSystem.DirectoryExists(firstPath)) { yield break; } if (parts.Count == 1) { yield return(firstPath); yield break; } var visited = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase); var states = new BinaryHeap <State>(comparer); states.Enqueue(new State(firstPath, 1)); while (states.Count > 0) { var state = states.Dequeue(); progress.Report(state.Path); if (state.MatchedPartCount >= parts.Count) { if (!visited.Contains(state.Path)) { yield return(state.Path); visited.Add(state.Path); } } else { var part = parts[state.MatchedPartCount]; if (!PathPattern.ContainsSpecialCharacters(part)) { // path part is a relative path without any special characters var path = Path.Combine(state.Path, part); if (!_fileSystem.DirectoryExists(path)) { continue; } states.Add(new State(path, state.MatchedPartCount + 1)); } else if (part == "**") { // assume the pattern has been matched states.Add(new State(state.Path, state.MatchedPartCount + 1)); // assume it has not been matched yet foreach (var dir in EnumerateDirectories(state.Path, null)) { states.Add(new State(dir, state.MatchedPartCount)); } } else { foreach (var dir in EnumerateDirectories(state.Path, part)) { states.Add(new State(dir, state.MatchedPartCount + 1)); } } } } }
/// <summary> /// Find all folders which match <see cref="Pattern"/>. /// </summary> /// <param name="pathComparer">Search order</param> /// <param name="progress">Reports all searched directory paths</param> /// <returns>All folders which match <see cref="Pattern"/></returns> private IEnumerable <string> FindAll( IComparer <string> pathComparer, IProgress <string> progress) { var comparer = new StateComparer(pathComparer); var parts = Pattern.GetParts().ToList(); if (parts.Count == 0) { yield break; } // the root directory does not exist var firstPath = parts[0]; if (!_fileSystem.DirectoryExists(firstPath)) { yield break; } // pattern contains only the root directory if (parts.Count == 1) { yield return(firstPath); yield break; } var states = new BinaryHeap <State>(comparer); states.Enqueue(new State(firstPath, 1)); // find all directories which match given pattern while (states.Count > 0) { var state = states.Dequeue(); progress.Report(state.Path); if (state.MatchedPartCount >= parts.Count) { // this path has matched the whole pattern => return it yield return(state.Path); } else { var part = parts[state.MatchedPartCount]; if (part == PathPattern.RecursivePattern) { // This part is after the first ** pattern. // Use a different algorithm: search all directories once and use pattern // matching to determine whether to include given directory in the result. if (Pattern.Match(state.Path)) { yield return(state.Path); } // search all subdirectories exactly once foreach (var child in EnumerateDirectories(state.Path, null)) { // MatchedPartCount can be any value >= firstGeneralPatternIndex states.Enqueue(new State(child, state.MatchedPartCount)); } } else if (!PathPattern.ContainsSpecialCharacters(part)) { // path part is a relative path without any special characters var path = Path.Combine(state.Path, part); if (!_fileSystem.DirectoryExists(path)) { continue; } states.Add(new State(path, state.MatchedPartCount + 1)); } else // part has to be a simple pattern which does not contain ** { Trace.Assert(part != PathPattern.RecursivePattern); foreach (var dir in EnumerateDirectories(state.Path, part)) { states.Add(new State(dir, state.MatchedPartCount + 1)); } } } } }
public OpenedStateCollection (int size, int buffer, StateComparer comparer) { m_Items = new State[size + buffer]; m_Size = size; m_HalfSize = size / 2; m_Comparer = comparer; }