public void GetRandomSituation_Returns_Some_Of_Every_Value() { // Arrange var allSituations = GetAllSituations().ToList(); IDictionary <Situation, int> results = new Dictionary <Situation, int>(); allSituations.ForEach(x => results.Add(x, 0)); // Act for (int i = 0; i < 2000; i++) { results[StrategyGenerator.GetRandomSituation()]++; } // Assert int total = 0; foreach (var robotAction in allSituations) { total += results[robotAction]; Debug.WriteLine("{0}: count = {1}", robotAction, results[robotAction]); } Debug.WriteLine("Total: {0}", total); foreach (var robotAction in allSituations) { Assert.AreNotEqual(0, results[robotAction], "Random generator never returned {0}", robotAction); } }
public void Can_Test_Multiple_Strategies_And_Order_Them_By_Score() { int strategiesToTest = 10; var strategies = new Dictionary <Situation, RobotAction> [strategiesToTest] .Select(x => StrategyGenerator.Random()).ToArray(); var testingRobots = new Robot[strategiesToTest] .Select((x, i) => new Robot(strategies[i], new Scorer())).ToArray(); foreach (var robot in testingRobots) { Board b = new Board(10, 10); var litterer = new BoardLitterer(0.5); litterer.Litter(b); b.AddElement(robot, new Point(0, 0)); const int numberOfTurns = 200; for (int i = 0; i < numberOfTurns; i++) { robot.Act(b); } } var orderedResults = testingRobots.OrderBy(x => x._scorer.Score); Debug.Print(String.Join(", ", orderedResults.Select(x => x._scorer.Score))); }
public void Can_Run_A_Robot_Through_A_Game_And_Get_A_Score() { IDictionary <Situation, RobotAction> moveStrategy = StrategyGenerator.Random(); Scorer scorer = new Scorer(); Robot robot = new Robot(moveStrategy, scorer); Board board = new Board(10, 10); var litterer = new BoardLitterer(0.5); litterer.Litter(board); board.AddElement(robot, new Point(0, 0)); const int numberOfTurns = 200; for (int i = 0; i < numberOfTurns; i++) { robot.Act(board); } int score = robot._scorer.Score; Debug.Print("Score is: {0}", score); }
protected override Robot ProduceChild(Tuple <Robot, Robot> parents) { var strategy1 = parents.Item1._moveStrategy; var strategy2 = parents.Item2._moveStrategy; var newStrategy = StrategyGenerator.Merge(strategy1, strategy2); return(new Robot(newStrategy, new Scorer())); }
public Robot GenerateChild(Robot[] orderedPopulation) { var populationSize = orderedPopulation.Count(); var parent = orderedPopulation[GetParentIndex(populationSize)]; var newStrategy = new Dictionary <Situation, RobotAction>(parent._moveStrategy); StrategyGenerator.RandomlyChange(0, 20, newStrategy); return(new Robot(newStrategy, new Scorer())); }
public void Can_Combine_Strategies() { IDictionary <Situation, RobotAction> s1 = StrategyGenerator.Random(); IDictionary <Situation, RobotAction> s2 = StrategyGenerator.Random(); var newStrategy = StrategyGenerator.Merge(s1, s2); Assert.AreEqual(s1.Count, newStrategy.Count); Assert.AreEqual(s2.Count, newStrategy.Count); }
private static void DoIncludeTest(string jsonFile, string codeFile) { string json = GetJson(jsonFile); string targetResult = GetTargetResult(codeFile); var model = JsonConvert.DeserializeObject <StrategyModel>(json); var generator = new StrategyGenerator(); var metaModel = MetaModelFactory.Create(model); var result = generator.Generate(metaModel); Assert.IsTrue(result.Contains(targetResult), string.Format("Target code not found: \n{0}\n in \n{1}", targetResult, result)); }
private static void DoTest(string jsonFile, string luaFile) { string json = GetJson(jsonFile); string targetResult = GetTargetResult(luaFile); var model = JsonConvert.DeserializeObject <StrategyModel>(json); var generator = new StrategyGenerator(new FileModuleProvider()); var metaModel = MetaModelFactory.Create(model); var result = generator.Generate(metaModel); Assert.AreEqual(targetResult, result, string.Format("Sources not equal: {0}", GetDiff(targetResult, result))); }
public void Strategy_Is_Created_With_Correct_Random_State() { var strategy = StrategyGenerator.Random(); var possibilities = Math.Pow(3, 5); // 3 = BoardContent possibilites, 5 = parts to Situation. Assert.AreEqual(243, strategy.Count()); int[] possibilityCounts = new int[7]; // 7 possibilities. foreach (var element in strategy) { ++possibilityCounts[(int)element.Value]; } Debug.Print("Possiblitiy counts: {0}", String.Join(": ", possibilityCounts)); Assert.IsTrue(possibilityCounts.Min() > 15, "Will PROBABLY pass. May randomly fail. Try running test again."); Assert.IsTrue(possibilityCounts.Max() < 50, "Will PROBABLY pass. May randomly fail. Try running test again."); }
public void GetRandomAction_Returns_A_Spread_Of_Actions_Across_All_Available_Actions() { // Arrange var allActions = Enum.GetValues(typeof(RobotAction)).Cast <RobotAction>().ToList(); IDictionary <RobotAction, int> results = new Dictionary <RobotAction, int>(); allActions.ForEach(x => results.Add(x, 0)); // Act for (int i = 0; i < 100; i++) { results[StrategyGenerator.GetRandomAction()]++; } // Assert foreach (var robotAction in allActions) { Assert.AreNotEqual(0, results[robotAction], "Random generator never returned {0}", robotAction); } }
public IEnumerable <Robot> GenerateInitialPopulation(int populationSize) { return(new Robot[populationSize] .Select((x, i) => new Robot(StrategyGenerator.Random(), new Scorer()))); }