private State(State parent, int[] nodes) { Nodes = nodes; Parent = parent; Heuristic = parent.Heuristic; CalculateCost(); StateCode = GenerateStateCode(); }
public State(State parent, int[] nodes, HeuristicMethod heuristic) { Nodes = nodes; Parent = parent; Heuristic = heuristic; CalculateCost(); StateCode = GenerateStateCode(); }
public KMeans(int iterations, int centroids, double minBound, double maxBound, double[][] points, HeuristicMethod Heuristic) { this.iterations = iterations; this.centroids = centroids; this.dimensions = points[0].GetLength(0); this.minBound = minBound; this.maxBound = maxBound; this.points = points; this.Heuristic = Heuristic; DetermineClusters(); }
public KMeans(int iterations, int centroids, double minBound, double maxBound, double[][] points) { this.iterations = iterations; this.centroids = centroids; this.dimensions = points[0].GetLength(0); this.minBound = minBound; this.maxBound = maxBound; this.points = points; Heuristic = DistanceSquared; DetermineClusters(); }
private static List <T> SetHeuristics(HeuristicMethod method, IEnumerable <T> nodes, T targetNode) { var temp = nodes.ToList <T>(); for (int i = 0; i < temp.Count; i++) { switch (method) { case HeuristicMethod.Euclidean: temp[i].H = Euclidean(temp[i], targetNode); break; case HeuristicMethod.Manhatten: temp[i].H = Manhatten(temp[i], targetNode); break; } } return(temp); }
public Pathfinding(HeuristicMethod method, bool diagonal) { HeuristicMethod = method; DiagonalMovement = diagonal; }
public Stack <State> Run(int[] nodes, HeuristicMethod heuristic) { List <State> nextStates = new List <State>(); HashSet <string> openStates = new HashSet <string>(); MinPriorityQueue <State> openedQueue = new MinPriorityQueue <State>(nodes.Length); Dictionary <string, State> closedQueue = new Dictionary <string, State>(); State state = new State(parent: null, nodes: nodes, heuristic: heuristic); openedQueue.Enqueue(state); openStates.Add(state.GetStateCode()); while (!openedQueue.IsEmpty()) { State currentState = openedQueue.Dequeue(); openStates.Remove(currentState.GetStateCode()); // Is this final state if (currentState.IsFinalState()) { return(GetFinalPath(currentState)); } // Look into next state currentState.GetNextStates(ref nextStates); if (nextStates.Count > 0) { State closedState; State openState; State nextState; for (int i = 0; i < nextStates.Count; i++) { closedState = null; openState = null; nextState = nextStates[i]; if (openStates.Contains(nextState.GetStateCode())) { // We already have same state in the open queue. openState = openedQueue.Find(nextState, out int openStateIndex); if (openState.IsCostlierThan(nextState)) { // We have found a better way to reach at this state. Discard the costlier one openedQueue.Remove(openStateIndex); openedQueue.Enqueue(nextState); } } else { // Check if state is in closed queue string stateCode = nextState.GetStateCode(); if (closedQueue.TryGetValue(stateCode, out closedState)) { // We have found a better way to reach at this state. Discard the costlier one if (closedState.IsCostlierThan(nextState)) { closedQueue.Remove(stateCode); closedQueue[stateCode] = nextState; } } } // Either this is a new state, or better than previous one. if (openState == null && closedState == null) { openedQueue.Enqueue(nextState); openStates.Add(nextState.GetStateCode()); } } closedQueue[currentState.GetStateCode()] = currentState; } } // no result return(null); }
public AStar(HeuristicMethod method) { _heuristicMethod = method == HeuristicMethod.Manhatten ? (HeuristicDelegate)Heuristic.Manhatten : (HeuristicDelegate)Heuristic.Euclidean; }
/// <summary> /// Default constructor /// </summary> /// <param name="graph">The graph where the search will be performed</param> /// <param name="heuristic">The heuristic function that will guide A*</param> public AStarSearch(Graph <T, K> graph, HeuristicMethod <T, K> heuristic) : base(graph) { this.heuristic = heuristic; }
static public void Solve15PuzzleProblem(AStarAlgorithm algorithm, int[] initStates, HeuristicMethod heuristic) { var results = algorithm.Run(initStates, heuristic); Console.WriteLine($"Heuristic method selected: {heuristic}"); foreach (var item in results) { Console.WriteLine(item); } Console.WriteLine("End"); }