public double[] CreateInput(NeuralAgent agent, CardinalMovement action) { var input = InputCoder.Encode(agent); InputCoder.EncodeAction(input, action); return(input); }
public AgentNetworkPrediction Predict(double[] input) { double bestValue = double.MinValue; CardinalMovement bestAction = null; foreach (CardinalMovement action in CardinalMovement.All) { InputCoder.EncodeAction(input, action); double predictedValue = Network.Evaluate(input)[0]; if (predictedValue > bestValue) { bestValue = predictedValue; bestAction = action; } } InputCoder.EncodeAction(input, bestAction); return(new AgentNetworkPrediction(bestAction, bestValue, input)); }
private void CalculatePathToGoal() { var space = Anchor.Field.Space; int width = space.Width; int height = space.Height; Dictionary <UShortPoint, int> distances = new Dictionary <UShortPoint, int>(); Dictionary <UShortPoint, CardinalMovement> predecessors = new Dictionary <UShortPoint, CardinalMovement>(); List <UShortPoint> unvisited = new List <UShortPoint>(); List <UShortPoint> visited = new List <UShortPoint>(); UShortPoint goalPosition = new UShortPoint((ushort)Goal.Position.X, (ushort)Goal.Position.Y); for (ushort y = 0; y < height; y++) { for (ushort x = 0; x < width; x++) { var point = new UShortPoint(x, y); distances[point] = int.MaxValue; predecessors[point] = default; if (space[x, y].IsEmpty || space[x, y].HasAgent) { unvisited.Add(point); } } } void removeUnvisitedMin() { int minDistance = int.MaxValue; UShortPoint minVisited = default; UShortPoint minUnvisited = default; CardinalMovement minMove = default; foreach (UShortPoint visitedPoint in visited) { foreach (CardinalMovement move in CardinalMovement.AllExceptNothing) { UShortPoint neighbor = visitedPoint.Add(move); if (unvisited.Contains(neighbor)) { var neighborField = space[neighbor.X, neighbor.Y]; var distanceOverNeighbor = distances[visitedPoint] + 1 + neighbor.Distance(goalPosition); if (distanceOverNeighbor < minDistance) { minDistance = distanceOverNeighbor; minVisited = visitedPoint; minUnvisited = neighbor; minMove = move; } } } } distances[minUnvisited] = distances[minVisited] + 1; predecessors[minUnvisited] = minMove; unvisited.Remove(minUnvisited); visited.Add(minUnvisited); } UShortPoint startPosition = new UShortPoint((ushort)Anchor.Field.X, (ushort)Anchor.Field.Y); distances[startPosition] = 0; unvisited.Remove(startPosition); visited.Add(startPosition); while (visited.Contains(goalPosition) == false) { removeUnvisitedMin(); } while (goalPosition.Equals(startPosition) == false) { var action = predecessors[goalPosition]; UShortPoint predecessor = goalPosition.Add(action.Opposite); PathToGoal.AddFirst(action); goalPosition = predecessor; } }
public AgentNetworkPrediction(CardinalMovement action, double value, double[] input) { Action = action; Value = value; Input = input; }
public void EncodeAction(double[] input, CardinalMovement action) { Array.Clear(input, ActionsOffset, CardinalMovement.All.Length); input[ActionsOffset + action.Index] = 1; }