//Check available movements from a cell and returns one of them randomly. private Movement GetRandomMovement(int fromCell) { List <Movement> possibleMoves = LabryntRules.GetPossibleMovements(fromCell); int randomIndex = Random.Range(0, possibleMoves.Count); return(possibleMoves[randomIndex]); }
//Checks the Q-Matrix and returns the best move from the available ones. //Best movement is decided by choosing the one with the highest q-value. If the highest value appears in more //than one move, chooses randomly between those moves private Movement GetBestMovement(int fromCell) { List <Movement> bestMovements = new List <Movement>(); List <Movement> possibleMovements = LabryntRules.GetPossibleMovements(fromCell); //There is at least one move from all cells (terminal states stop the algorithm before getting here) //so it is saved to compare with the rest. bestMovements.Add(possibleMovements[0]); double highestQValue = qMat.GetQValue(fromCell, possibleMovements[0]); //Compare to other values, keep a list with the movements with highest q-value. for (int x = 1; x < possibleMovements.Count; x++) { double qValue = qMat.GetQValue(fromCell, possibleMovements[x]); if (highestQValue == qValue) { bestMovements.Add(possibleMovements[x]); } else if (highestQValue < qValue) { bestMovements.Clear(); bestMovements.Add(possibleMovements[x]); highestQValue = qValue; } } //If more than one has the highest value, choos randomly between them. int index = 0; if (bestMovements.Count > 1) { index = Random.Range(0, bestMovements.Count); } return(bestMovements[index]); }