public async Task Get_Cooperator_Players_Strategies() { var strategyRepository = new StrategyRepository(connection); var strategyService = new StrategyService(strategyRepository); var simulationRepository = new SimulationRepository(connection); var gameService = new GameService(strategyService, gameSettingsProvider); var populationService = new PopulationService(gameService); //TODO: simplify /\ var simulationServce = new SimulationService(simulationRepository, populationService, strategyService, new SimulationSettingsProvider()); Strategy cooperator = await strategyRepository .GetByNameAsync(NoMemoryStrategies.GetSimpleCooperator().Name); var players = new List <Player>(); for (int i = 0; i < 10; i++) { players.Add(new Player() { StrategyId = cooperator.Id }); } players = simulationServce.GetPlayersStrategies(players); bool badPlayers = players.Where(p => p.StrategyId != cooperator.Id).Any(); Assert.IsFalse(badPlayers); }
public void GetGenerationStrategyTypeByName_InvalidName_Exception() { Assert.Catch <ArgumentException>(() => { PopulationService.GetGenerationStrategyTypeByName("Test"); }, "There is no IGenerationStrategy implementation with name 'Test'."); }
public async Task Run_Once_When_Consistent() { var strategyRepository = new StrategyRepository(connection); var strategyService = new StrategyService(strategyRepository); var simulationRepository = new SimulationRepository(connection); var gameService = new GameService(strategyService, gameSettingsProvider); var populationService = new PopulationService(gameService); //TODO: simplify /\ var simulationServce = new SimulationService(simulationRepository, populationService, strategyService, new SimulationSettingsProvider()); Strategy cooperator = await strategyRepository .GetByNameAsync(NoMemoryStrategies.GetSimpleCooperator().Name); var players = new List <Player>(); for (int i = 0; i < 10; i++) { players.Add(new Player() { StrategyId = cooperator.Id }); } Simulation simulation = await simulationServce.Run(players); Assert.IsNotNull(simulation.Winner); Assert.AreEqual(1, simulation.PopulationsCompleated); }
public async Task Winner_Score_Is_Total_Score() { var strategyRepository = new StrategyRepository(connection); var strategyService = new StrategyService(strategyRepository); var simulationRepository = new SimulationRepository(connection); var gameService = new GameService(strategyService, gameSettingsProvider); var populationService = new PopulationService(gameService); //TODO: simplify /\ var simulationServce = new SimulationService(simulationRepository, populationService, strategyService, new SimulationSettingsProvider()); Strategy cooperator = await strategyRepository .GetByNameAsync(NoMemoryStrategies.GetSimpleCooperator().Name); var players = new List <Player>(); for (int i = 0; i < 10; i++) { players.Add(new Player() { StrategyId = cooperator.Id }); } Simulation simulation = await simulationServce.Run(players); GameSettings gameSettings = gameSettingsProvider.GetGameSettings(); int totalScore = gameSettings.TotalRounds * (gameSettings.CooperateModifier + gameSettings.MoveModifier) * (players.Count - 1); Assert.AreEqual(totalScore, simulation.Winner.Score); }
public void CreateGenerationStrategyByName_ValidNameButInvalidConstructorArgs_Exception() { Assert.Catch <ArgumentException>(() => { PopulationService.CreateGenerationStrategyByName("Tracking", 1); }, "A IGenerationStrategy's implementation with name 'Tracking' was found, but seems the constructor args were invalid."); }
public void GetGenerationStrategyTypeByName_InvalidName_Exception() { ExceptionAssert.IsThrowing(new ArgumentException("There is no IGenerationStrategy implementation with name 'Test'.", "name"), () => { PopulationService.GetGenerationStrategyTypeByName("Test"); }); }
public void CreateGenerationStrategyByName_ValidNameButInvalidConstructorArgs_Exception() { ExceptionAssert.IsThrowing(new ArgumentException("A IGenerationStrategy's implementation with name 'Tracking' was found, but seems the constructor args were invalid.", "constructorArgs"), () => { PopulationService.CreateGenerationStrategyByName("Tracking", 1); }); }
public async Task Population_Is_Not_Consistent() { var gameServiceMock = new Mock <IGameService>(); var populationService = new PopulationService(gameServiceMock.Object); var players = new List <Player>(); for (int i = 0; i < 10; i++) { players.Add(new Player() { StrategyId = "1" }); } players.Add(new Player() { StrategyId = "2" }); bool isConsistent = await populationService.IsPopulationConsistent(new Population() { Players = players }); Assert.IsFalse(isConsistent); }
public async Task New_Population_Players_Count_Did_Not_Change() { var gameServiceMock = new Mock <IGameService>(); var populationService = new PopulationService(gameServiceMock.Object); var players = new List <Player>(); for (int i = 0; i < 9; i++) { players.Add(new Player() { StrategyId = "1", Score = 21 }); } players.Add(new Player() { StrategyId = "2", Score = 20 }); Population population = await populationService.GetNewPopulation(new Population() { Players = players }); bool isConsistent = await populationService.IsPopulationConsistent(population); Assert.IsTrue(isConsistent); Assert.AreEqual(players.Count, population.Players.Count); }
public void GetGenerationStrategyNames_NoArgs_AllAvailableGenerationStrategiesNames() { var actual = PopulationService.GetGenerationStrategyNames(); Assert.AreEqual(2, actual.Count); Assert.AreEqual("Performance", actual [0]); Assert.AreEqual("Tracking", actual [1]); }
public void GetGenerationStrategyTypes_NoArgs_AllAvailableIGenerationStrategy() { var actual = PopulationService.GetGenerationStrategyTypes(); Assert.AreEqual(2, actual.Count); Assert.AreEqual(typeof(PerformanceGenerationStrategy), actual [0]); Assert.AreEqual(typeof(TrackingGenerationStrategy), actual [1]); }
public void GetGenerationStrategyTypeByName_ValidName_GenerationStrategyTpe() { var actual = PopulationService.GetGenerationStrategyTypeByName("Performance"); Assert.AreEqual(typeof(PerformanceGenerationStrategy), actual); actual = PopulationService.GetGenerationStrategyTypeByName("Tracking"); Assert.AreEqual(typeof(TrackingGenerationStrategy), actual); }
public void CreateGenerationStrategyByName_ValidName_GenerationStrategyCreated() { IGenerationStrategy actual = PopulationService.CreateGenerationStrategyByName("Performance", 1) as PerformanceGenerationStrategy; Assert.IsNotNull(actual); actual = PopulationService.CreateGenerationStrategyByName("Tracking") as TrackingGenerationStrategy; Assert.IsNotNull(actual); }
public MapController(EcoContext context, ContryService contryService, PopulationService populationService, ResourceService resourceService) { Context = context; ContryService = contryService; PopulationService = populationService; ResourceService = resourceService; string CultureName = Thread.CurrentThread.CurrentCulture.Name; CultureInfo ci = new CultureInfo(CultureName); if (ci.NumberFormat.NumberDecimalSeparator != ".") { // Forcing use of decimal separator for numerical values ci.NumberFormat.NumberDecimalSeparator = "."; Thread.CurrentThread.CurrentCulture = ci; } }
public async Task Scores_Are_Correct_After_Evaluation() { var gameServiceMock = new Mock <IGameService>(); var rounds = new List <Round>(); for (int i = 0; i < 10; i++) { rounds.Add(new Round() { FirstPlayerScore = 2, SecondPlayerScore = -1 }); } gameServiceMock.Setup(g => g.PlayAsync(It.IsAny <Player>(), It.IsAny <Player>())) .Returns(Task.FromResult(new Game() { Rounds = rounds })); var populationService = new PopulationService(gameServiceMock.Object); Player firstPlayer = new Player() { Id = Guid.NewGuid().ToString() }; Player secondPlayer = new Player() { Id = Guid.NewGuid().ToString() }; Population population = await populationService.Evaluate(new List <Player>() { firstPlayer, secondPlayer }); Player newFirstPlayer = population.Players.Where(p => p.Id == firstPlayer.Id).FirstOrDefault(); Player newSecondPlayer = population.Players.Where(p => p.Id == secondPlayer.Id).FirstOrDefault(); Assert.IsTrue(firstPlayer.Score == 20); Assert.AreEqual(-10, newSecondPlayer.Score); }
public PopulationService CreatePopulationService() { var q1 = new PopulationService(new RatioCalculator(), new CollectionGenerator(new NumberGenerator(1, 2))); return(q1); }
public FunctionOptimizerService() { PopulationService = new PopulationService(); Function = new BoothFunction(); Inversion = new Inversion(); }
public async Task StartAsync() { _paretoChartData = new List <ICanvasJSDataPoint>(); _bestChromosomeChartData = new List <ICanvasJSDataPoint>(); _evolutionChartData = new Dictionary <ChromosomeFactor, List <ICanvasJSDataPoint> >(); _evolutionConfig = CanvasJsChartService.GetBasicOptionsForEvolutionChart(); _paretoConfig = CanvasJsChartService.GetBasicOptionsForParetoChart(); _matrix = MatrixService.GenerateMatrix(nodeCount: _nodeCount, probability: _edgeProbability); var startPopulation = PopulationService.Initialize(_matrix, populationSize: _populationSize, maxDiffBetweenNode: _maxDiffBetweenNode); _populationHistory = new Dictionary <int, IPopulationResult> { [0] = startPopulation }; _graphData = GraphChartService.GraphDataFromMatrix(_matrix); _graphOptions = GraphChartService.GetDefaultGraphOptions(); var sampleDictionary = new Dictionary <ChromosomeFactor, List <ICanvasJSDataPoint> > { [ChromosomeFactor.ConnectedEdgeWeigthSum] = new List <ICanvasJSDataPoint> { new CanvasJSDataPoint { X = 0, Y = 0 }, }, [ChromosomeFactor.EdgeCount] = new List <ICanvasJSDataPoint> { new CanvasJSDataPoint { X = 0, Y = 0 }, }, [ChromosomeFactor.ConnectedEdgeWeigthSum | ChromosomeFactor.EdgeCount] = new List <ICanvasJSDataPoint> { new CanvasJSDataPoint { X = 0, Y = 0 }, } }; var sampleBest = new List <ICanvasJSDataPoint> { new CanvasJSDataPoint { X = 0, Y = 0 } }; _evolutionConfig.Data = CanvasJsChartService.GetEvolutionChartData(sampleDictionary, sampleBest); _paretoConfig.Data = CanvasJsChartService.GetParetoChartData(new List <ICanvasJSDataPoint> { new CanvasJSDataPoint { X = 0, Y = 0 }, }); await Task.Run(() => { _graphChart.RenderAsync(_graphData, _graphOptions); }); await Task.Run(() => { _evolutionChart.RenderAsync(_evolutionConfig); }); await Task.Run(() => { _paretoChart.RenderAsync(_paretoConfig); }); }