//finds the best mapping between selected moveTo coordinates and the real robots private int[] MapRobotsToMoveTo(Storage storage, GridRule rule) { int[] bestPerm = new int[4]; int[][] indexes = { new int[] { 1, 2, 3, 0 }, new int[] { 1, 2, 0, 3 }, new int[] { 1, 3, 2, 0 }, new int[] { 1, 3, 0, 2 }, new int[] { 1, 0, 2, 3 }, new int[] { 1, 0, 3, 2 }, new int[] { 2, 1, 3, 0 }, new int[] { 2, 1, 0, 3 }, new int[] { 2, 3, 1, 0 }, new int[] { 2, 3, 0, 1 }, new int[] { 2, 0, 1, 3 }, new int[] { 2, 0, 3, 1 }, new int[] { 3, 1, 2, 0 }, new int[] { 3, 1, 0, 2 }, new int[] { 3, 2, 1, 0 }, new int[] { 3, 2, 0, 1 }, new int[] { 3, 0, 1, 2 }, new int[] { 3, 0, 2, 1 }, new int[] { 0, 1, 2, 3 }, new int[] { 0, 1, 3, 2 }, new int[] { 0, 2, 1, 3 }, new int[] { 0, 2, 3, 1 }, new int[] { 0, 3, 1, 2 }, new int[] { 0, 3, 2, 1 } }; double min = double.MaxValue; foreach (int[] i in indexes) { double moveRuleSize = gStrategy.GridToReal(rule.Move[0], gameSetting).DistanceFrom(storage.RightRobots[i[0]].Position) + gStrategy.GridToReal(rule.Move[1], gameSetting).DistanceFrom(storage.RightRobots[i[1]].Position) + gStrategy.GridToReal(rule.Move[2], gameSetting).DistanceFrom(storage.RightRobots[i[2]].Position) + gStrategy.GridToReal(rule.Move[3], gameSetting).DistanceFrom(storage.RightRobots[i[3]].Position); if (moveRuleSize < min) { min = moveRuleSize; bestPerm = i; } } return(bestPerm); }
// tik strategie urcujici co se ma pri kazdem kroku vykonat public override void TickStrategy(Storage storage) { SetGoalkeeperStrategy(storage.RightRobots[gameSetting.NUMBER_OF_ROBOTS - 1], storage); // strategie //if (RobotsInDesiredGrid(storage) >= gStrategy.RuleException) //currentRule = FindBestRule(ref ruleIndex, storage); //currentRule = FindBestRule4(storage); currentRule = FindBestRuleGraph(storage); SharedMutex.getMutex().WaitOne(); if (currentRule != null) { //for (int i = 0; i < gameSetting.NUMBER_OF_ROBOTS - 1; i++) // storage.RightRobots[ruleIndex[i]].PositionMove = gStrategy.GridToReal(currentRule.Move[i], gameSetting); int[] mapping = MapRobotsToMoveTo(storage, currentRule); for (int i = 0; i < gameSetting.NUMBER_OF_ROBOTS - 1; i++) { storage.RightRobots[mapping[i]].PositionMove = gStrategy.GridToReal(currentRule.Move[i], gameSetting); } } SharedMutex.getMutex().ReleaseMutex(); //taktiky SharedMutex.getMutex().WaitOne(); tactic.chooseTactic(storage.changeRobots()); SharedMutex.getMutex().ReleaseMutex(); //TACTIC if (currentRule != null) //{ // for (int i = 0; i < gameSetting.NUMBER_OF_ROBOTS - 1; i++) // { // if (!storage.MyRobots[ruleIndex[i]].tactic && // gStrategy.RealToGrid(storage.MyRobots[ruleIndex[i]].Position) != currentRule.Move[i]) // { // storage.MyRobots[ruleIndex[i]].GoTo(storage.MyRobots[ruleIndex[i]].Strat_pos); // } // } //} }
// konstruktor zajistujici spravne rozparsrovani vsech dat ze souboru a nacteni do promennych a pripadne x-ove preklopeni pravidel public GridStrategy(String fileName, bool mirror) { CultureInfo nfi = new CultureInfo("en-US", false); StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open)); this.name = getValue(reader.ReadLine(), "Strategy"); this.algorithm = getValue(reader.ReadLine(), "Algorithm"); this.author = getValue(reader.ReadLine(), "Author"); this.date = getValue(reader.ReadLine(), "Date"); String[] s_size = getValue(reader.ReadLine(), "Size").Split(new char[] { ',' }); this.size = new Vector2D(Convert.ToDouble(s_size[0], nfi), Convert.ToDouble(s_size[1], nfi)); this.attackerMin = Int32.Parse(getValue(reader.ReadLine(), "AttackerMin")); this.ruleException = Int32.Parse(getValue(reader.ReadLine(), "RuleException")); String[] s_priorityMine = getValue(reader.ReadLine(), "PriorityMine").Split(new char[] { ' ', '\t' }); this.priorityMine = new int[] { Int32.Parse(s_priorityMine[0]), Int32.Parse(s_priorityMine[1]), Int32.Parse(s_priorityMine[2]), Int32.Parse(s_priorityMine[3]) }; String[] s_priorityMineD = getValue(reader.ReadLine(), "PriorityMineD").Split(new char[] { ' ', '\t' }); this.priorityMineD = new int[] { Int32.Parse(s_priorityMineD[0]), Int32.Parse(s_priorityMineD[1]), Int32.Parse(s_priorityMineD[2]), Int32.Parse(s_priorityMineD[3]) }; String[] s_priorityOpponent = getValue(reader.ReadLine(), "PriorityOpponent").Split(new char[] { ' ', '\t' }); this.priorityOpponent = new int[] { Int32.Parse(s_priorityOpponent[0]), Int32.Parse(s_priorityOpponent[1]), Int32.Parse(s_priorityOpponent[2]), Int32.Parse(s_priorityOpponent[3]) }; String[] s_priorityOpponentD = getValue(reader.ReadLine(), "PriorityOpponentD").Split(new char[] { ' ', '\t' }); this.priorityOpponentD = new int[] { Int32.Parse(s_priorityOpponentD[0]), Int32.Parse(s_priorityOpponentD[1]), Int32.Parse(s_priorityOpponentD[2]), Int32.Parse(s_priorityOpponentD[3]) }; this.priorityBall = Int32.Parse(getValue(reader.ReadLine(), "PriorityBall")); rules = new List <GridRule>(); try { while (!reader.EndOfStream) { reader.ReadLine(); GridRule r = new GridRule(reader.ReadLine(), reader.ReadLine(), reader.ReadLine(), reader.ReadLine(), reader.ReadLine()); if (mirror) { r.Mirror((int)size.X); } r.fillZOrder(); rules.Add(r); } } catch (Exception) { } reader.Close(); }
public void Fill_LeftStrg31(GridStrategy gStrategy, GameSetting gameSetting) { //double _treshold = 0.7; double _treshold = 0; StringBuilder sb = new StringBuilder(); //nodes for (int i = 0; i < gStrategy.Rules.Count; i++) { AddNode(new Node(i, gStrategy.Rules[i])); } GridStrategy offensiveM = new GridStrategy(); offensiveM.Rules.Add(gStrategy.Rules[0]); offensiveM.Rules.Add(gStrategy.Rules[1]); offensiveM.Rules.Add(gStrategy.Rules[2]); offensiveM.Rules.Add(gStrategy.Rules[3]); GridStrategy offensiveR = new GridStrategy(); offensiveR.Rules.Add(gStrategy.Rules[4]); offensiveR.Rules.Add(gStrategy.Rules[5]); offensiveR.Rules.Add(gStrategy.Rules[6]); offensiveR.Rules.Add(gStrategy.Rules[7]); offensiveR.Rules.Add(gStrategy.Rules[8]); offensiveR.Rules.Add(gStrategy.Rules[9]); GridStrategy offensiveL = new GridStrategy(); offensiveL.Rules.Add(gStrategy.Rules[10]); offensiveL.Rules.Add(gStrategy.Rules[11]); offensiveL.Rules.Add(gStrategy.Rules[12]); offensiveL.Rules.Add(gStrategy.Rules[13]); offensiveL.Rules.Add(gStrategy.Rules[14]); offensiveL.Rules.Add(gStrategy.Rules[15]); GridStrategy defensiveM = new GridStrategy(); defensiveM.Rules.Add(gStrategy.Rules[16]); defensiveM.Rules.Add(gStrategy.Rules[17]); defensiveM.Rules.Add(gStrategy.Rules[18]); defensiveM.Rules.Add(gStrategy.Rules[19]); GridStrategy defensiveR = new GridStrategy(); defensiveR.Rules.Add(gStrategy.Rules[20]); defensiveR.Rules.Add(gStrategy.Rules[21]); defensiveR.Rules.Add(gStrategy.Rules[22]); defensiveR.Rules.Add(gStrategy.Rules[23]); defensiveR.Rules.Add(gStrategy.Rules[24]); defensiveR.Rules.Add(gStrategy.Rules[25]); GridStrategy defensiveL = new GridStrategy(); defensiveL.Rules.Add(gStrategy.Rules[26]); defensiveL.Rules.Add(gStrategy.Rules[27]); defensiveL.Rules.Add(gStrategy.Rules[28]); defensiveL.Rules.Add(gStrategy.Rules[29]); defensiveL.Rules.Add(gStrategy.Rules[30]); List <GridStrategy> lStrategy = new List <GridStrategy>(); lStrategy.Add(offensiveM); lStrategy.Add(offensiveR); lStrategy.Add(offensiveL); lStrategy.Add(defensiveM); lStrategy.Add(defensiveR); lStrategy.Add(defensiveL); //edges double max = -1; List <Triple> values = new List <Triple>(); foreach (GridStrategy tempStrg in lStrategy) { for (int i = 0; i < tempStrg.Rules.Count - 1; i++) { for (int j = i + 1; j < tempStrg.Rules.Count; j++) { GridRule r1 = tempStrg.Rules[i]; GridRule r2 = tempStrg.Rules[j]; //ball double ballRuleSize = gStrategy.GridToReal(r1.Ball, gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Ball, gameSetting) ); //mine double mineSize = 0; for (int k = 0; k < gameSetting.NUMBER_OF_ROBOTS - 1; k++) { mineSize += gStrategy.GridToReal(r1.Mine[r1.ZMine[k]], gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Mine[r2.ZMine[k]], gameSetting) ); } //oppnt double oppntSize = 0; for (int k = 0; k < gameSetting.NUMBER_OF_ROBOTS - 1; k++) { oppntSize += gStrategy.GridToReal(r1.Oppnt[r1.ZOppnt[k]], gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Oppnt[r2.ZOppnt[k]], gameSetting) ); } if (ballRuleSize + mineSize + oppntSize > max) { max = ballRuleSize + mineSize + oppntSize; } int ii = gStrategy.Rules.IndexOf(tempStrg.Rules[i]); int ij = gStrategy.Rules.IndexOf(tempStrg.Rules[j]); values.Add(new Triple(ii, ij, ballRuleSize + mineSize + oppntSize)); } } } for (int i = 0; i < values.Count; i++) { double norm = 1 - (values[i].z / max); sb.AppendLine(values[i].x + " " + values[i].y + " " + Math.Round(norm, 3).ToString().Replace(',', '.')); //Console.WriteLine("{0} {1} {2} {3}", values[i].x, values[i].y, values[i].z, norm); if (norm >= _treshold) { AddEdge(nodes[values[i].x], nodes[values[i].y], norm); } } string filename = "GRAPH_" + gStrategy.Name + ".txt"; File.Delete(filename); using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew))) { sw.Write(sb.ToString()); } }
public void Fill(GridStrategy gStrategy, GameSetting gameSetting, double _treshold) { StringBuilder sb = new StringBuilder(); //nodes for (int i = 0; i < gStrategy.Rules.Count; i++) { AddNode(new Node(i, gStrategy.Rules[i])); } //edges double max = -1; List <Triple> values = new List <Triple>(); for (int i = 0; i < gStrategy.Rules.Count - 1; i++) { for (int j = i + 1; j < gStrategy.Rules.Count; j++) { GridRule r1 = gStrategy.Rules[i]; GridRule r2 = gStrategy.Rules[j]; //ball double ballRuleSize = gStrategy.GridToReal(r1.Ball, gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Ball, gameSetting) ); //mine double mineSize = 0; for (int k = 0; k < gameSetting.NUMBER_OF_ROBOTS - 1; k++) { mineSize += gStrategy.GridToReal(r1.Mine[r1.ZMine[k]], gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Mine[r2.ZMine[k]], gameSetting) ); } //oppnt double oppntSize = 0; for (int k = 0; k < gameSetting.NUMBER_OF_ROBOTS - 1; k++) { oppntSize += gStrategy.GridToReal(r1.Oppnt[r1.ZOppnt[k]], gameSetting).DistanceFrom( gStrategy.GridToReal(r2.Oppnt[r2.ZOppnt[k]], gameSetting) ); } if (ballRuleSize + mineSize + oppntSize > max) { max = ballRuleSize + mineSize + oppntSize; } values.Add(new Triple(i, j, ballRuleSize + mineSize + oppntSize)); } } for (int i = 0; i < values.Count; i++) { double norm = 1 - (values[i].z / max); //Console.WriteLine("{0} {1} {2} {3}", values[i].x, values[i].y, values[i].z, norm); if (norm >= _treshold) { AddEdge(nodes[values[i].x], nodes[values[i].y], norm); sb.AppendLine(values[i].x + " " + values[i].y + " " + Math.Round(norm, 3).ToString().Replace(',', '.')); } } /** * string filename = "GRAPH_" + gStrategy.Name + ".txt"; * File.Delete(filename); * using (StreamWriter sw = new StreamWriter(new FileStream(filename, FileMode.CreateNew))) * { * sw.Write(sb.ToString()); * sw.Close(); * } */ }
//################################################################################################################################################ #region funkce pro strategie private GridRule FindBestRuleGraph(Storage storage) { GridRule bestRule = null; int x, y; //mine matrix = new List <int> [6, 4]; for (int i = 0; i < storage.RightRobots.Length - 1; i++) { Vector2D v = gStrategy.RealToGrid(storage.RightRobots[i].Position, gameSetting); x = Convert.ToInt32(v.X); y = Convert.ToInt32(v.Y); if (matrix[x, y] != null) { matrix[x, y].Add(i); } else { matrix[x, y] = new List <int>() { i } }; } List <int> actMine = zorder.zOrderMain(matrix, 2); //oppnt matrix = new List <int> [6, 4]; for (int i = 0; i < storage.LeftRobots.Length - 1; i++) { Vector2D v = gStrategy.RealToGrid(storage.LeftRobots[i].Position, gameSetting); x = Convert.ToInt32(v.X); y = Convert.ToInt32(v.Y); if (matrix[x, y] != null) { matrix[x, y].Add(i); } else { matrix[x, y] = new List <int>() { i } }; } List <int> actOppnt = zorder.zOrderMain(matrix, 2); //nejpodobnejsi pravidlo double min = Double.MaxValue; if (lastRule == -1) { GridRule rule; for (int r = 0; r < gStrategy.Rules.Count; r++) { //nesmysl //if (graph.Nodes[r].neighbours.Count == 0) //{ // if (gStrategy.Rules.Count > 0) // { // bestRule = gStrategy.Rules[0]; // lastRule = 0; // } // break; //} rule = gStrategy.Rules[r]; double ballRuleSize = gStrategy.GridToReal(rule.Ball, gameSetting).DistanceFrom(storage.Ball.Position); double mineSize = 0; for (int i = 0; i < actMine.Count; i++) { mineSize += gStrategy.GridToReal(rule.Mine[rule.ZMine[i]], gameSetting).DistanceFrom(storage.LeftRobots[actMine[i]].Position); } double oppntSize = 0; for (int i = 0; i < actOppnt.Count; i++) { oppntSize += gStrategy.GridToReal(rule.Oppnt[rule.ZOppnt[i]], gameSetting).DistanceFrom(storage.RightRobots[actOppnt[i]].Position); } if (ballRuleSize + mineSize + oppntSize < min) { min = ballRuleSize + mineSize + oppntSize; bestRule = rule; lastRule = r; } } } else { Node actNode = graph.Nodes[lastRule]; GridRule rule; List <int> tempKeys = new List <int>(actNode.neighbours.Keys); tempKeys.Add(lastRule); //foreach (int k in actNode.neighbours.Keys) foreach (int k in tempKeys) { rule = (GridRule)graph.Nodes[k].data; double ballRuleSize = gStrategy.GridToReal(rule.Ball, gameSetting).DistanceFrom(storage.Ball.Position); double mineSize = 0; for (int i = 0; i < actMine.Count; i++) { mineSize += gStrategy.GridToReal(rule.Mine[rule.ZMine[i]], gameSetting).DistanceFrom(storage.RightRobots[actMine[i]].Position); } double oppntSize = 0; for (int i = 0; i < actOppnt.Count; i++) { oppntSize += gStrategy.GridToReal(rule.Oppnt[rule.ZOppnt[i]], gameSetting).DistanceFrom(storage.LeftRobots[actOppnt[i]].Position); } if (ballRuleSize + mineSize + oppntSize < min) { min = ballRuleSize + mineSize + oppntSize; bestRule = rule; lastRule = k; } } } return(bestRule); }