public NodeDecisionMaker(MT19937Generator rnd)
		{
			MaximumDepth = int.MaxValue;
			MinimumDuration = TimeSpan.MaxValue;
			MaximumDuration = TimeSpan.MaxValue;
			Rnd = rnd;
		}
		public KubismeBot(MT19937Generator rnd)
		{
			DecisionMaker = new NodeDecisionMaker(rnd)
			{
				Evaluator = new Evaluator(),
				Generator = new MoveGenerator(),
				MaximumDepth = 10,
				MaximumDuration = TimeSpan.FromMilliseconds(700),
				DefaultEvaluation = EvaluatorParameters.GetDefault(),
			};
		}
		public BattleSimulator(MT19937Generator rnd)
		{
			File = new FileInfo("parameters.xml");
			SearchDepth = AppConfig.Data.SearchDepth;

			Rnd = rnd;
			Randomizer = new ParameterRandomizer(rnd);

			Bots = new Bots();
			Results = new ConcurrentQueue<BattlePairing>();
		}
		public static Field GetInitial(MT19937Generator rnd)
		{
			var garbage = AppConfig.Data.Garbage;
			var field = new Field(0, 0, 0, (byte)AppConfig.Data.Rows, new ushort[AppConfig.Data.Rows]);
			if (garbage > 0)
			{
				var rows = Row.GetGarbage(garbage, 3, rnd);
				return field.Garbage(rows);
			}
			return field;
		}
예제 #5
0
		public static void Main(string[] args)
		{
			var file = new FileInfo("data.bin");

			int nodeSize = 0;

			var collection = new NodeCollection(LoadNodes(file));

			if (Log(args, collection)) { return; }

			if (args.Length == 0 || !Int32.TryParse(args[0], out nodeSize))
			{
				nodeSize = 50000;
			}
			Console.Clear();
			Console.WriteLine("Node size: {0:#,##0.0}k", nodeSize / 1000.0);

			var simulator = new Simulator();
			var rnd = new MT19937Generator();

			
			Merge(collection);

			var sw = Stopwatch.StartNew();
			long runs = 0;
			var shrinks = 0;
			while (true)
			{
				runs++;

				ClearNewStatus(collection);

				simulator.Simulate(collection, rnd);
				Write(collection, sw, runs, shrinks);

				if ((runs & 15) == 15)
				{
					collection.Save(file);
					Merge(collection);
				}
				if (collection.Count > nodeSize)
				{
					var nodes = collection.ToList();
					nodes.Shrink();
					collection.Clear();
					collection.AddRange(nodes);
					collection.Save(file);
					shrinks++;
					Write(collection, sw, runs, shrinks, true);
				}
			}
		}
예제 #6
0
		public static ushort[] GetGarbage(int count, int pointsOld, MT19937Generator rnd)
		{
			var garbage = new ushort[count];

			var single = ((pointsOld / 3) & 1) == 0;

			for (var i = 0; i < count; i++)
			{
				garbage[i] = single ? Garbage[rnd.Next(10)] : Garbage2[rnd.Next(45)];
				single = !single;
			}
			return garbage;
		}
		public static BattleBot Create(MT19937Generator rnd, EvaluatorParameters defPars, int maxDepth)
		{
			return new BattleBot()
			{
				DecisionMaker = new NodeDecisionMaker(rnd)
				{
					MaximumDepth = maxDepth,
					Evaluator = new Evaluator(),
					Generator = new MoveGenerator(),
					DefaultEvaluation = defPars,
				}
			};
		}
예제 #8
0
        /// <summary>
        ///   Resets the random number generator using the specified seed, so that it produces the
        ///   same random number sequence again. To understand whether this generator can be reset,
        ///   you can query the <see cref="CanReset"/> property.
        /// </summary>
        /// <param name="seed">The seed value used by the generator.</param>
        /// <returns>True if the random number generator was reset; otherwise, false.</returns>
        public override bool Reset(uint seed)
        {
            base.Reset(seed);

            var gen = new MT19937Generator(seed);

            _x = new uint[_longLag];
            for (uint j = 0; j < _longLag; ++j)
            {
                _x[j] = gen.NextUInt();
            }
            _i = _longLag;
            return(true);
        }
		public ApplyParameters(MT19937Generator rnd)
		{
			const int size = 16;
			sw = Stopwatch.StartNew();
			MaximumDepth = int.MaxValue;
			Rnd = rnd;
			Blocks = new Block[size][];

			Blocks[2] = Block.All;
			Blocks[3] = Block.SubsetsOf3[Rnd.Next(Block.SubsetsOf3.Length)];
			for (var i = 4; i < size; i++)
			{
				Blocks[i] = Block.SubsetsOf2[Rnd.Next(Block.SubsetsOf2.Length)];
			}
		}
		public void Randomize_NegativeDescading_RandomValues()
		{
			var random = new MT19937Generator(17);
			var rnd = new ParameterRandomizer(random);

			var parameters = new ParameterCollectionClass()
			{
				NegativeDescading = new int[] { 100, 150 },
			};

			var act = rnd.Randomize(parameters).NegativeDescading;

			var exp = new int[] { -1, -68 };

			CollectionAssert.AreEqual(exp, act);
		}
		private void RunSimulation(BotData bot0, BotData bot1, MT19937Generator rnd)
		{
			Console.Write("\r{0:d\\.hh\\:mm\\:ss} {1:#,##0} ({2:0.00} /sec) Last ID: {3}  ",
				sw.Elapsed,
				Simulations,
				Simulations / sw.Elapsed.TotalSeconds,
				Bots.LastId);

			var simulation = new BattleSimulation(bot0, bot1, SearchDepth);
			var result = simulation.Run(rnd, LogGames);

			Results.Enqueue(new BattlePairing(bot0, bot1) { Result = result });

			lock (lockElo)
			{
				Simulations++;
			}
		}
		private bool SimulateParallel(IEnumerable<BattlePairing> pairings)
		{
			var result = true;
			try
			{
				Parallel.ForEach(pairings, p =>
				{
					var rnd = new MT19937Generator();
					RunSimulation(p.Bot0, p.Bot1, rnd);
				});
				return result;
			}
			catch { return false; }
		}
예제 #13
0
		private static ACDCBot GetBot(NodeCollection nodes, PlayerName name, MT19937Generator rnd)
		{
			var bot = new ACDCBot(nodes, rnd)
			{
				Settings = new Settings()
				{
					YourBot = name,
				},
			};
			return bot;
		}
예제 #14
0
		public void Simulate(NodeCollection nodes, MT19937Generator rnd)
		{
			var bots = new Dictionary<PlayerName, ACDCBot>()
			{
				{ PlayerName.player1, GetBot(nodes, PlayerName.player1, rnd) },
				{ PlayerName.player2, GetBot(nodes, PlayerName.player2, rnd) },
			};

			var matches = new Matches();
			matches.Round = 1;
			var stack1 = 2000;
			var stack2 = 2000;

			var blind = 20;
			
			for (var round = 1; round < int.MaxValue; round++)
			{
				if (round % 10 == 0) { blind += 10; }
				var small = blind >> 1;

				matches.Round = round;
				matches.Current.Player1.Stack = stack1;
				matches.Current.Player2.Stack = stack2;

				var mirrored = (round & 1) == 0;

				var shuffled = Cards.GetShuffledDeck(rnd);
				var deck1 = Cards.Empty;
				var deck2 = Cards.Empty;
				var tabl0 = Cards.Empty;
				var tabl3 = Cards.Empty;
				var tabl4 = Cards.Empty;
				var tabl5 = Cards.Empty;

				deck1.AddRange(shuffled.Take(2));
				deck2.AddRange(shuffled.Skip(2).Take(2));
				tabl3.AddRange(shuffled.Skip(4).Take(3));
				tabl4.AddRange(shuffled.Skip(4).Take(4));
				tabl5.AddRange(shuffled.Skip(4).Take(5));

				var tables = new Cards[] { tabl0, tabl3, tabl4, tabl5 };

				matches.Current.BigBlind = blind;
				matches.Current.SmallBlind = small;
				matches.Current.Player1.Hand = deck1;
				matches.Current.Player2.Hand = deck2;

				SetPot(matches.Current, mirrored);

				var outcome = PlayerName.None;

				var showdown = false;

				foreach (var table in tables)
				{
					matches.Current.Table = table;
					outcome = SimulateRound(matches, bots);
					if (outcome != PlayerName.None) { break; }
				}
				if (outcome == PlayerName.None)
				{
					showdown = true;
					outcome = PokerHandEvaluator.GetOutcome(deck1, deck2, tabl5);
				}
				if (outcome == PlayerName.None)
				{
					matches.Current.Player1.Stack += matches.Current.Pot >> 1;
					matches.Current.Player2.Stack += matches.Current.Pot >> 1;
				}
				else
				{
					var realoutcome = outcome;
					if (!showdown)
					{
						realoutcome = PokerHandEvaluator.GetOutcome(deck1, deck2, tabl5);
					}
					matches.Current[outcome].Stack += matches.Current.Pot;

					if (realoutcome == outcome)
					{
						var wins = new WinsInstruction(outcome, matches.Current.Pot);
						bots[PlayerName.player1].Update(wins);
						bots[PlayerName.player2].Update(wins);
					}
					// A bot chickened later, but that should not matter for the bits it played.
					else
					{
						bots[PlayerName.player1].Update(new WinsInstruction(PlayerName.player1, matches.Current.Pot));
						bots[PlayerName.player2].Update(new WinsInstruction(PlayerName.player2, matches.Current.Pot));
					}
				}
				matches.Current.Player1.Pot = 0;
				matches.Current.Player2.Pot = 0;

				stack1 = matches.Current.Player1.Stack;
				stack2 = matches.Current.Player2.Stack;

				if (stack1 < blind || stack2 < blind)
				{
					break;
				}
			}
		}
예제 #15
0
		public Actor(NodeCollection nodes, MT19937Generator rnd)
		{
			Nodes = nodes;
			Buffer = new List<Node>();
			Rnd = rnd;
		}
예제 #16
0
		public void GetGarbage_5_Matches()
		{
			var rnd = new MT19937Generator(12);
			var act = Row.GetGarbage(5,0, rnd).Select(r => Row.ToString((ushort)r)).ToArray();
			var exp = new string[] 
			{ 
				"X.XXXXXXXX",
				"XX.XXX.XXX",
				"XXXXXXX.XX",
				"XXXXXX..XX",
				"XX.XXXXXXX",
			};
			CollectionAssert.AreEqual(exp, act);
		}
예제 #17
0
		public void GetGarbage_2WithSkip_Matches()
		{
			// With this randomizer the skip code should be triggered.
			var rnd = new MT19937Generator(0);
			var act = Row.GetGarbage(2, 3, rnd).Select(r => Row.ToString((ushort)r)).ToArray();

			var exp = new string[] 
			{ 
				"XXX..XXXXX",
				"XXXXX.XXXX",
			};
			CollectionAssert.AreEqual(exp, act);			
		}
		public Result Run(MT19937Generator rnd, bool logGames)
		{
			var field0 = GetInitial(rnd);
			var field1 = GetInitial(rnd);
			var out0 = Field.None;
			var out1 = Field.None;

			var current = Block.All[rnd.Next(Block.All.Length)];
			var next = Block.All[rnd.Next(Block.All.Length)];

			var b0 = BattleBot.Create(rnd, Bot0.DefPars, SearchDepth);
			var b1 = BattleBot.Create(rnd, Bot1.DefPars, SearchDepth);

			var s0 = true;
			var s1 = true;

			var g0 = 0;
			var g1 = 0;

			int turns = 1;

			var stats0 = new BotStats();
			var stats1 = new BotStats();

			while (s0 && s1)
			{
				out0 = b0.GetResponse(field0, field1, current, next, turns);
				out1 = b1.GetResponse(field1, field0, current, next, turns);
								
				stats0.Update(field0, out0, current);
				stats1.Update(field1, out1, current);

				field0 = out0;
				field1 = out1;

				var t0 = field0.Points / 3;
				var t1 = field1.Points / 3;

				if (t0 > g0)
				{
					field1 = field1.Garbage(Row.GetGarbage(t0 - g0, g0 * 3, rnd));
					g0 = t0;
				}
				if (t1 > g1)
				{
					field0 = field0.Garbage(Row.GetGarbage(t1 - g1, g1 * 3, rnd));
					g1 = t1;
				}

				if (turns++ % 15 == 0)
				{
					field0 = field0.LockRow();
					field1 = field1.LockRow();
				}

				s0 = !field0.IsNone;
				s1 = !field1.IsNone;

				Turns0.Add(field0);
				Turns1.Add(field1);

				current = next;
				next = Block.All[rnd.Next(Block.All.Length)];
			}

			if (logGames)
			{
				var visualiser = new FieldVisualizer(16);
				var dir = new DirectoryInfo(Path.Combine("games", DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss_fff")));
				dir.Create();
				visualiser.Draw(Turns0, Turns1, dir);
			}

			var result = new Result()
			{
				Turns = Turns0.Count,
				Points0 = Turns0[Turns0.Count - 1].IsNone ? Turns0[Turns0.Count - 2].Points : Turns0[Turns0.Count - 1].Points,
				Points1 = Turns1[Turns1.Count - 1].IsNone ? Turns1[Turns1.Count - 2].Points : Turns1[Turns1.Count - 1].Points,
				Stats0 = stats0,
				Stats1 = stats1,
				Outcome = Outcome.Draw,
			};

			if (s0) { result.Outcome = Outcome.Win; }
			else if (s1) { result.Outcome = Outcome.Loss; }

			return result;
		}
예제 #19
0
		public ACDCBot(NodeCollection nodes, MT19937Generator rnd)
		{
			Actor = new Actor(nodes, rnd);
		}
예제 #20
0
		public Actor(IList<Node> nodes, MT19937Generator rnd)
		{
			Nodes = new NodeCollection(nodes);
			Buffer = new List<Node>();
			Rnd = rnd;
		}