private void loadState(GameState state) { physP1.Fixture.Body.Position = state.P1.Coords; physP2.Fixture.Body.Position = state.P2.Coords; physP1.Fixture.Body.LinearVelocity = state.P1.Velocity; physP2.Fixture.Body.LinearVelocity = state.P2.Velocity; }
public static float EstimateScore(PlayerId pId, GameState state, Input move, Vector2 target) { // prof.Start (); float similarity; if (move == Input.Noop) { similarity = 0f; } else { var player = state.Player (pId); Vector2 gravOffset = state.IsGrounded (player) ? Vector2.Zero : gravVector; Vector2 targetVector = Vector2.Subtract (target, player.Target + gravOffset); Vector2.Normalize (targetVector); // Debug.WriteLine("targetVector: {0}", gravOffset); similarity = (float)Util.CosineSimilarity(targetVector, moveVectors [move]); } // prof.End (); return -similarity; }
public float CombinedDistance(CombinedPlatformAStar cpas, GameState state, GameObject next1, GameObject next2) { var combinedPath = cpas.CombinedPlatformPath (state.P1, state.P2, state.Goal, state.Goal); return Vector2.Distance (state.P1.SurfaceCenter, next1.Target) + Vector2.Distance (state.P2.SurfaceCenter, next2.Target) + WaypointHeuristic.PlatformPathDistance(combinedPath.Select(x => x.Item1), next1) + WaypointHeuristic.PlatformPathDistance(combinedPath.Select(x => x.Item2), next2); }
public GameState nextState(GameState state, Input input1, Input input2, int nSteps = 1) { loadState (state); Debug.WriteLineIf ((Vector2.Distance (physP1.Fixture.Body.Position, state.P1.Coords) > 0.001f || Vector2.Distance (physP2.Fixture.Body.Position, state.P2.Coords) > 0.001f), "The gamestate and forwardmodel are out of sync" ); return nextState (state.Health, input1, input2, nSteps); }
public static GameState nextState(ForwardModel forwardModel, GameState game, Input p1move, Input p2move, int nSteps = 1) { GameState lastState = game; int intermediateSteps = 1; // set to match a reasonable Gameloop#humanInputDelayFrames() value... for (int i = 0; i < intermediateSteps; i++) { lastState = forwardModel.nextState(lastState, p1move, p2move, nSteps); } return lastState; }
public ForwardModel(GameState state) { initialState = state; world = new World (new Vector2 (0f, -50f)); physP1 = new StatelessPlayerPhysics (world, playerFix (state.P1.X, state.P1.Y, state.P1.W, state.P1.H)); physP2 = new StatelessPlayerPhysics (world, playerFix (state.P2.X, state.P2.Y, state.P2.W, state.P2.H)); foreach (var plat in state.Platforms) { platformFix (plat.X, plat.Y, plat.W, plat.H); } goalFix (state.Goal.X, state.Goal.Y, state.Goal.Radius); loadState (state); }
public float statusWrap(GameState state, float s) { float healthScore = System.Math.Abs(state.Health); var healthWeight = 150f; var score = s + (healthWeight * healthScore); if (state.PlayStatus.isDied () || state.Player(pId).BottomBoundary < state.Platforms.Min(x => x.TopBoundary)) { score = 5*GameState.Width; } else if (state.PlayStatus.isWon ()) { score = 0; } return score; }
public static float statusWrap(GameState state, float s) { float healthScore = System.Math.Abs(state.Health); var healthWeight = 1; var score = s + (healthWeight * healthScore); float minPlatHeight = state.Platforms.Min (x => x.TopBoundary); if (state.PlayStatus.isDied () || state.P1.BottomBoundary < minPlatHeight || state.P2.BottomBoundary < minPlatHeight) { score = DeathScore; } else if (state.PlayStatus.isWon ()) { score = 0; } return score; }
public abstract List<Input> nextInputList(GameState state, PlayerId pId, Heuristic heuristic);
public override float Score(GameState state) { float goalDistance = Vector2.Distance(state.Player(pId).Coords, state.Goal.Coords); float healthScore = System.Math.Abs(state.Health); var healthWeight = 0f; return goalDistance + (healthWeight * healthScore); }
public Input nextInput(GameState origState) { return nextInputList (origState).First (); }
public GameObject NextPlatform(GameState state) { return pas.NextPlatform (state.Player(pId), state.Goal); }
public AI(GameState state, PlayerId pId, Heuristic heuristic) { this.pId = pId; this.forwardModel = new ForwardModel(state); this.heuristic = heuristic; }
protected abstract Input predictPartnerInput(GameState state);
void addChildrenToOpenSet(TreeDictionary<double, StateNode> dict, StateNode parent, GameState state, CombinedHeuristic heuristic) { var parentScore = stateNodeScorer (heuristic, parent); foreach (var input in Input.All.CartesianProduct(Input.All)) { var stateNode = new StateNode (parent, input, state); // var target1 = state.Goal; // var target2 = state.Goal; var targets = heuristic.cpas.NextPlatform (state.P1, state.P2, state.Goal, state.Goal); var target1 = targets.Item1; var target2 = targets.Item2; var score = parentScore + heuristic.EstimateScore (state, input.Item1, input.Item2, target1.Target, target2.Target); var noiseyScore = AStar.addNoise (score); dict.Add (noiseyScore, stateNode); } }
public WaypointHeuristic(GameState initialState, PlayerId pId) : base(pId) { // this.initialState = initialState; pas = new PlatformAStar (initialState.Platforms); }
protected float EstimateScore(GameState state, Input move, Vector2 target) { return EstimateScore (this.pId, state, move, target); }
public abstract float EstimateScore(GameState state, Input move);
public abstract float Score(GameState state);
public static float PlayerDistance(PlatformAStar pas, GameState state, PlayerId pId, GameObject target) { return Vector2.Distance (state.Player(pId).SurfaceCenter, target.Target) + PlatformPathDistance (pas.PlatformPath (state.Player (pId), state.Goal), target); }
private float PlatformPathDistance(GameState state, PlayerId pId, GameObject platform) { return PlatformPathDistance (pas.PlatformPath (state.Player (pId), state.Goal), platform); }
public override float Score(GameState state) { GameObject next = NextPlatform (state); var dist = PlayerDistance(pas, state, pId, next); return statusWrap(state, dist); // return dist; }
public List<Input> nextInputList(GameState origState) { return nextInputList (origState, pId, heuristic); }
public NullAI(GameState state, PlayerId pId) : base(state, pId, new LinearHeuristic(pId)) { }
protected GameState nextState(GameState state, Input move, int nSteps = 1) { return pId == PlayerId.P1 ? nextState (forwardModel, state, move, predictPartnerInput (state), nSteps) : nextState (forwardModel, state, predictPartnerInput (state), move, nSteps); }
public override List<Input> nextInputList(GameState state, PlayerId pId, Heuristic heuristic) { return new List<Input>() { new Input () }; }
// public TreeDictionary<double, IEnumerable<B>> AStar<A, B>(Func<B, AStarNode<A, B>> nextState, A state) { // var openSet = new TreeDictionary<double, B>(); // Known, but unexplored // var closedSet = new TreeDictionary<double, B>(); // Fully explored // // AStarNode<A, B> parentStub = new AStarNode<A, B> (null, null, state); // addChildrenToOpenSet(openSet, parentStub, state, heuristic); // // int maxIters = 100; // int nRepetitions = 5; // // AStarNode<A, B> best; // // int i = 0; // do { // var bestKV = openSet.First (); // openSet.Remove(bestKV.Key); // // best = bestKV.Value; // // var bestNextMove = best.Input; // // // repeat the same input a few times // B resultState = best.Value; // for (int j = 0; j < nRepetitions; j++) { // resultState = nextState(new AStarNode<A, B>(best, bestNextMove, resultState)); // } // // addChildrenToOpenSet(openSet, best, resultState, heuristic); // // var stateNode = new AStarNode<A, B> (best, bestNextMove, resultState); // var score = AStar.addNoise(stateNodeScorer (heuristic, stateNode)); // // closedSet.Add(score, stateNode); // // } while(i++ < maxIters && closedSet.First().Key > 0 && openSet.Count > 0); // // return closedSet; // } // // public List<Tuple<Input, Input>> nextInputsList2(GameState state) { // // } public List<Tuple<Input, Input>> nextInputsList(GameState state) { var openSet = new TreeDictionary<double, StateNode>(); // Known, but unexplored var closedSet = new TreeDictionary<double, StateNode>(); // Fully explored StateNode parentStub = new StateNode (null, Tuple.Create(Input.Noop, Input.Noop), state); addChildrenToOpenSet(openSet, parentStub, state, heuristic); int maxIters = 100; int nRepetitions = 5; StateNode bestOpen; int i = 0; do { var bestOpenKV = openSet.First (); openSet.Remove(bestOpenKV.Key); bestOpen = bestOpenKV.Value; var bestNextMove = bestOpen.Input; // repeat the same input a few times GameState resultState = bestOpen.Value; for (int j = 0; j < nRepetitions; j++) { resultState = AStar.nextState(forwardModel, resultState, bestNextMove.Item1, bestNextMove.Item2); } addChildrenToOpenSet(openSet, bestOpen, resultState, heuristic); var stateNode = new StateNode (bestOpen, bestNextMove, resultState); var score = AStar.addNoise(stateNodeScorer (heuristic, stateNode)); closedSet.Add(score, stateNode); } while(i++ < maxIters && !closedSet.First().Value.Value.PlayStatus.isWon() && openSet.Count > 0); // Debug.WriteLine ("closedSet size: {0}", closedSet.Count); // int k = 0; // foreach (var pth in closedSet) { // var pathStr = string.Join(", ", pth.Value.ToPath().Select(tup1 => tup1.Item1.Item1 + "|" + tup1.Item1.Item2)); // Debug.WriteLine("closedSet[{0}]: {1} - {2}", k++, pth.Key, pathStr); // } lock (allPaths) { allPaths = closedSet; } var path = closedSet.First().Value.ToPath (); var deRooted = path.Count > 1 ? path.Skip (1) : path; // Ignore the root node // Debug.Print("bestPath1: {0}", moveListStr(deRooted.Select(x => x.Item1.Item1))); // Debug.Print("bestPath2: {0}", moveListStr(deRooted.Select(x => x.Item1.Item2))); var res = deRooted .SelectMany (t => Enumerable.Repeat(t.Item1,nRepetitions)).ToList(); return res; }
protected override Input predictPartnerInput(GameState state) { return new Input (); }
public CombinedAi(GameState state) { this.heuristic = new CombinedHeuristic (state); this.forwardModel = new ForwardModel(state); this.allPaths = new TreeDictionary<double, StateNode> (); }
public override float EstimateScore(GameState state, Input input) { GameObject target = NextPlatform (state); return EstimateScore (state, input, target.Target); }