public PossessionGraph(TeamFormationAttributes teamFormationAttributes, bool isHome, bool isDefendingZero) { TeamFormationAttributes = teamFormationAttributes; EdgeIndices = new Dictionary <T, IList <Edge <T> > >(); Random = new Random((int)DateTime.Now.Ticks); IsHome = isHome; IsDefendingZero = isDefendingZero; Team = IsHome ? TeamFormationAttributes.HomeTeamPlayers.Cast <T>().ToList() : TeamFormationAttributes.AwayTeamPlayers.Cast <T>().ToList(); Opposition = IsHome ? TeamFormationAttributes.AwayTeamPlayers.Cast <T>().ToList() : TeamFormationAttributes.HomeTeamPlayers.Cast <T>().ToList(); var orderedPlayers = IsDefendingZero ? Team.OrderBy(p => p.Location.Y).ToList() : Team.OrderByDescending(p => p.Location.Y).ToList(); orderedPlayers. Select((p, i) => new { Index = i, Player = p }). Execute(a => EdgeIndices[a.Player] = orderedPlayers. Skip(a.Index + 1). Where(op => IsDefendingZero ? op.Location.Y > a.Player.Location.Y : op.Location.Y < a.Player.Location.Y). Select(p => new Edge <T>(p, Cost(a.Player, p, TeamFormationAttributes.TeamStrength))). ToList() ); if (TeamFormationAttributes.Log != null) { LogPossessionGraph(TeamFormationAttributes.Log); } }
public int PhaseOfPlay(ref T possessor, out bool isShooting) { var option = Int32.MinValue; var receiver = default(T); if (EdgeIndices[possessor].Any()) { var phaseEdge = EdgeIndices[possessor]. Select(e => new Edge <T>(e.Vertex, e.Cost + Random.Next(-500, 500))). Aggregate((a, b) => a.Cost > b.Cost ? a : b); receiver = phaseEdge.Vertex; option = receiver == null ? 0 : (int)( ( Math.Exp(-Math.Sqrt(IsDefendingZero ? receiver.Location.Y : 1 - receiver.Location.Y)) * phaseEdge.Cost * (TeamFormationAttributes.TeamPositionalBalance(IsHome) / TeamFormationAttributes.TeamPositionalBalance(!IsHome)) * TeamFormationAttributes.TeamAttackingShape(IsHome) ) / TeamFormationAttributes.TeamDefendingShape(!IsHome) ); Passes.WriteLine("\"{0}\",{1},{2},{3},{4},{5},{6},{7}", phaseEdge.Vertex.TeamName, phaseEdge.Cost, TeamFormationAttributes.TeamPositionalBalance(IsHome), TeamFormationAttributes.TeamPositionalBalance(!IsHome), (TeamFormationAttributes.TeamPositionalBalance(IsHome) / TeamFormationAttributes.TeamPositionalBalance(!IsHome)), TeamFormationAttributes.TeamAttackingShape(IsHome), TeamFormationAttributes.TeamDefendingShape(!IsHome), option); Passes.Flush(); if (TeamFormationAttributes.Log != null) { TeamFormationAttributes.Log(string.Format("{0} has option to pass to {1} with success {2}", possessor.LastName, receiver.LastName, option)); } } var shootOption = (int)((1 + Random.NextDouble()) * possessor.Rating / ( Math.Pow(possessor.Location.Distance(new Coordinate { X = 0.5, Y = IsDefendingZero ? 1 : 0 }), 2) * TeamFormationAttributes.TeamStrength(!IsHome, possessor.Location) * TeamFormationAttributes.TeamDefendingShape(!IsHome) ) ); shootOption = (int)((shootOption * 5d + 1500d) * Random.NextDouble()); shootOption -= CheckOffside(possessor.Location.Y, TeamFormationAttributes.TeamOffsideLine(!IsHome)); if (TeamFormationAttributes.Log != null) { TeamFormationAttributes.Log(string.Format("{0} has option to shoot with success {1}", possessor.LastName, shootOption)); } if (shootOption > option) { Shots.WriteLine("\"{0}\",{1},{2},{3},{4},{5}", possessor.TeamName, possessor.Rating, Math.Pow(possessor.Location.Distance(new Coordinate { X = 0.5, Y = IsDefendingZero ? 1 : 0 }), 2), TeamFormationAttributes.TeamStrength(!IsHome, possessor.Location), TeamFormationAttributes.TeamDefendingShape(!IsHome), shootOption); Shots.Flush(); isShooting = true; option = shootOption; } else { isShooting = false; possessor = receiver; } return(option); }