public void SetAgentInfo(GameStarted gameInfo) { var sh = new StrategyHandler(gameInfo); var strategy = sh.GetStrategy(Configuration.Strategy); this.AgentInfo = new AgentInfo(strategy, gameInfo); }
public static Vector2 GetBestMove(bool player1, GameState gs, StrategyHandler sh, ScoreFunction sf) { // First let the strategy handler determine all possible move options for this round List <Vector2> options = sh.ComputeOptions(gs, player1); // Convert the options to scored moves List <ScoredMove> scoredMoves = options.ConvertAll(o => new ScoredMove(o)); foreach (ScoredMove scoredMove in scoredMoves) { // The depth specified is the number of moves after the first next move float value = DoMiniMax(scoredMove.Move, scoredMove, 1, !player1, gs, sh, sf); // Debug.Log("==============================> score: " + value); // Debug.Log("-----> Move: " + scoredMove.Move); scoredMove.SetScore(value); } float bestScore; if (player1) { bestScore = float.MinValue; } else { bestScore = float.MaxValue; } ScoredMove bestMove = scoredMoves[0]; foreach (ScoredMove scoredMove in scoredMoves) { if (player1) { // Maximize the score if minimax is used for player 1 if (scoredMove.Score > bestScore) { bestScore = scoredMove.Score; bestMove = scoredMove; } } else { // Minimize the score if minimax is used for player 2 if (scoredMove.Score < bestScore) { bestScore = scoredMove.Score; bestMove = scoredMove; } } } Vector2 move = bestMove.Move; storedOptions = options; return(move); }
public void SetUp() { _mockLogger = new Mock <ILogger <StrategyHandler> >(); _mockAlpacaClient = new Mock <IAlpacaClient>(); _mockTrackingRepository = new Mock <ITrackingRepository>(); _mockStrategy = new Mock <IStrategy>(); _strategyHandler = new StrategyHandler( _mockLogger.Object, _mockAlpacaClient.Object, _mockTrackingRepository.Object, _mockStrategy.Object, _tradingFrequency, _percentageOfEquityToAllocate, _stockSymbol ); }
// Use this for initialization public void Start() { // create initial delaunay triangulation (three far-away points) m_delaunay = Delaunay.Create(); // add auxiliary vertices as unowned foreach (var vertex in m_delaunay.Vertices) { m_ownership.Add(vertex, EOwnership.UNOWNED); } m_fishManager = new FishManager(); // create polygon of rectangle window for intersection with voronoi float z = Vector2.Distance(m_meshFilter.transform.position, Camera.main.transform.position); var bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, z)); var topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, z)); m_meshRect = new Polygon2D( new List <Vector2>() { new Vector2(bottomLeft.x, bottomLeft.z), new Vector2(bottomLeft.x, topRight.z), new Vector2(topRight.x, topRight.z), new Vector2(topRight.x, bottomLeft.z) }); if (useFirstPlayerAi) { GameObject goVoronoiAI1 = new GameObject("Ai player 1"); m_voronoiAI1 = goVoronoiAI1.AddComponent <VoronoiAI>(); m_voronoiAI1.SetPlayer1(true); m_voronoiAI1.SetCorners(bottomLeft, topRight); m_voronoiAI1.DrawBorders = true; m_voronoiAI1.DrawDelauneyTriangulation = true; m_voronoiAI1.DrawVoronoiDiagram = true; StrategyHandler sh1 = new StrategyHandler() .Add(new OutsideCHStrategy(1f, new RandomStrategy(1))); //.Add(new GridStrategy(6, 4)) //.Add(new RandomStrategy(25)); //.Add(new LargestCellStrategy()); m_voronoiAI1.SetStrategyHandler(sh1); // Select a score function used by the AI // new AreaScore(); // new DistanceScore(); // new StandardDeviationScore(); // new CircumferenceScore(); m_voronoiAI1.SetScoreFunction(new AreaScore()); } if (useSecondPlayerAi) { GameObject goVoronoiAI2 = new GameObject("Ai player 2"); m_voronoiAI2 = goVoronoiAI2.AddComponent <VoronoiAI>(); m_voronoiAI2.SetPlayer1(false); m_voronoiAI2.SetCorners(bottomLeft, topRight); if (!useFirstPlayerAi) { m_voronoiAI2.DrawBorders = true; m_voronoiAI2.DrawDelauneyTriangulation = true; m_voronoiAI2.DrawVoronoiDiagram = true; } StrategyHandler sh2 = new StrategyHandler() //.Add(new OutsideCHStrategy(1f, new RandomStrategy(4))) //.Add(new GridStrategy(6, 4)) .Add(new RandomStrategy(25)); //.Add(new LargestCellStrategy()); m_voronoiAI2.SetStrategyHandler(sh2); // Select a score function used by the AI // new AreaScore(); // new DistanceScore(); // new StandardDeviationScore(); // new CircumferenceScore(); m_voronoiAI2.SetScoreFunction(new AreaScore()); } VoronoiDrawer.CreateLineMaterial(); }
private static float DoMiniMax(Vector2 nextMove, ScoredMove scoredMove, int depth, bool maximizingPlayer, GameState gs, StrategyHandler sh, ScoreFunction sf) { // Create a copy of the gamestate and apply the move option GameState gsTemp = gs.Copy(); gsTemp.AddPoint(nextMove, !maximizingPlayer); if (depth <= 0) { return(sf.ComputeScore(nextMove, gsTemp)); } List <Vector2> options = sh.ComputeOptions(gsTemp, maximizingPlayer); if (maximizingPlayer) { float max = float.MinValue; foreach (Vector2 option in options) { // Execute minimax on the new game state float value = DoMiniMax(option, scoredMove, depth - 1, false, gsTemp, sh, sf); max = Mathf.Max(max, value); } return(max); } else { float min = float.MaxValue; foreach (Vector2 option in options) { // Execute minimax on the new game state float value = DoMiniMax(option, scoredMove, depth - 1, true, gsTemp, sh, sf); min = Mathf.Min(min, value); } return(min); } }
public void SetStrategyHandler(StrategyHandler sh) { this.sh = sh; }