public Dictionary <HanoiState, double> GetStateTransitionDictionary(HanoiState state) { var nextStates = new List <HanoiState>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) { continue; } var nextState = this.MoveElementBetweenTowers(new List <List <int> > { state.Tower1, state.Tower2, state.Tower3 }, i, j); if (nextState != null) { nextStates.Add(nextState); } } } var numPossibleStateTransitions = nextStates.Count(); var stateTransitions = nextStates.Select(s => new HanoiStateTransition { State = s, Probability = 1.0 / (double)numPossibleStateTransitions }).ToList(); var stateTransitionsGroupings = stateTransitions.GroupBy(s => s.State.GetStringRepresentation()).Select(g => new HanoiStateTransition { State = g.First().State, Probability = g.Sum(x => x.Probability) }).ToList(); return(stateTransitionsGroupings.ToDictionary(d => d.State, d => d.Probability)); }
protected void SetDataForFS() { hanoiState = new HanoiState(this.towers); distance = new Dictionary <string, int>(); parents = new Dictionary <string, string>(); result = new List <string>(); distance[hanoiState.Id] = 0; parents[hanoiState.Id] = string.Empty; }
public double?GetExpectedNumMovesToSolveTowerOfHanoiRandomly(int numRings) { if (numRings <= 0) { throw new InvalidOperationException("There must be a non-zero number of rings"); } var initialState = new HanoiState { Tower1 = Enumerable.Range(1, numRings).ToList(), Tower2 = new List <int> { }, Tower3 = new List <int> { } }; return(this._markovChainSolver.GetExpectedValueOfNumTurnsToReachTerminalState(initialState, GetStateTransitionDictionary)); }
public override void Execute(string endstate) { startTime = DateTime.Now; var closetSet = new Dictionary <string, HanoiState>(); var openSet = new Dictionary <string, HanoiState>(); var fScore = new Dictionary <string, int>(); var gScore = new Dictionary <string, int>(); var cameFrom = new Dictionary <string, string>(); var idToState = new Dictionary <string, HanoiState>(); hanoiState = new HanoiState(this.towers); var id = hanoiState.Id; openSet[id] = new HanoiState(hanoiState); idToState[id] = openSet[id]; fScore[id] = 0; gScore[id] = 0; while (openSet.Count() > 0) { var current = GetLeastCost(openSet, fScore); if (current == endstate) { result = ReconstructPath(current, cameFrom); endTime = DateTime.Now; return; } openSet.Remove(current); closetSet[current] = idToState[current]; hanoiState = idToState[current]; var neighbors = hanoiState.GetNeighbor(); foreach (var neighbor in neighbors) { var neighborId = neighbor.Id; if (closetSet.ContainsKey(neighborId)) { continue; } var tentativeGScore = gScore[current] + 1; var tentativeIsBetter = false; if (!openSet.ContainsKey(neighborId)) { openSet[neighborId] = neighbor; idToState[neighborId] = neighbor; counter++; tentativeIsBetter = true; } else if (tentativeGScore < gScore[neighborId]) { tentativeIsBetter = true; } if (tentativeIsBetter) { cameFrom[neighborId] = current; gScore[neighborId] = tentativeGScore; fScore[neighborId] = tentativeGScore; } } } }