public IEnumerator SolveGame(Game game) { yield return(null); LogsManager.ins.AddLog(string.Format("{0} ma dokładnie dwie strategie.", game.gameData.playerB)); LogsManager.ins.AddLog("Wyszukiwanie strategii dającej największą przegraną dla gracza"); yield return(LogsManager.Wait()); id = IDTheLargestVal(game.gameData); yield return(CrossOverMax(id, game.gameData)); subGame[1, 0] = game.gameData.matrix[point.strategyA_ID, 0]; subGame[1, 1] = game.gameData.matrix[point.strategyA_ID, 1]; subGame[0, 0] = game.gameData.matrix[point.strategyB_ID, 0]; subGame[0, 1] = game.gameData.matrix[point.strategyB_ID, 1]; char[] useingStrategies = new char[2] { game.gameData.rowStrategies[point.strategyA_ID], game.gameData.rowStrategies[point.strategyB_ID] }; game.gameData.rowStrategies = useingStrategies.ToList(); game.gameData.matrix = subGame; }
public IEnumerator SolveGame(Game game) { sbyte minInRow, maxInCol; byte cellID = 0; LogsManager.ins.AddLog("Zaznaczanie punktów siodłowych."); yield return(MinMaxPanel.ins.SetUp(true)); yield return(LogsManager.Wait()); TableManager.ins.SelectCell(0, EColors.Default, true); for (byte i = 0; i < game.gameData.matrix.GetLength(0); i++) { for (byte j = 0; j < game.gameData.matrix.GetLength(1); j++) { if (game.gameData.matrix[i, j] == (sbyte)valueOfGame) { minInRow = game.GetRow(i).Min(); maxInCol = game.GetColumn(j).Max(); if (minInRow == maxInCol) { LogsManager.ins.AddLog("Zaznaczono punkt siodłowy."); yield return(LogsManager.Wait()); TableManager.ins.SelectCell(cellID, EColors.Green); } } cellID++; } } }
private IEnumerator WriteLines(int[] strA, int[] strB) { LogsManager.ins.AddLog("Trwa wypisywanie strategii drugiego gracza w postaci prostych."); yield return(LogsManager.Wait()); Rect rect = valuesList.GetComponent <RectTransform>().rect; int valA, valB; float height = rect.height / leftValues.childCount; float width = rect.width; float halfOnce = height / 2; for (int i = 0; i < strA.Length; i++) { valA = strA[i]; valB = strB[i]; UILineRenderer lR = Instantiate(linePrefab, valuesList).GetComponent <UILineRenderer>(); lR.Points[0] = new Vector2(0, rect.height - (GetChildID(valA) * height) - halfOnce); lR.Points[1] = new Vector2(width, rect.height - (GetChildID(valB) * height) - halfOnce); lines.Add(lR); LogsManager.ins.AddLog("Wypisano strategię."); yield return(LogsManager.Wait()); } }
public IEnumerator SetUp(int[,] matrix) { //cG.alpha = 1; this.matrix = matrix; yield return(null); int[] strategyA = (matrix.GetLength(0) == 2) ? GetRow(0) : GetColumn(0); int[] strategyB = (matrix.GetLength(0) == 2) ? GetRow(1) : GetColumn(1); #region Wyszukiwanie najmniejszej i największej wartości int min, max; int minA = strategyA.Min(); int minB = strategyB.Min(); int maxA = strategyA.Max(); int maxB = strategyB.Max(); min = (minA < minB) ? minA : minB; max = (maxA > maxB) ? maxA : maxB; #endregion LogsManager.ins.AddLog("Trwa wypisywanie wartości dla gracza, który posiada dokładnie dwie strategie."); yield return(LogsManager.Wait()); WriteValues(min, max); yield return(WriteLines(strategyA, strategyB)); }
private IEnumerator RowsDomination() { sbyte[] rowA, rowB; yield return(LogsManager.Wait()); for (byte i = 0; i < gameData.matrix.GetLength(0) - 1; i++) { rowA = GetRow(i); for (byte j = (byte)(i + 1); j < gameData.matrix.GetLength(0); j++) { LogsManager.ins.AddLog(string.Format("Porównuję strategię gracza: {2} - {0} z {1}", gameData.rowStrategies[i].ToString(), gameData.rowStrategies[j].ToString(), gameData.playerA)); rowB = GetRow(j); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), i, EColors.Green, true); yield return(LogsManager.Wait()); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), j, EColors.Blue); yield return(LogsManager.Wait()); // -1 = rowA dominuje rowB; 1 = rowB dominuje rowA; 0 = brak dominacji sbyte domination = Compare(rowA, rowB); if (domination == -1) { LogsManager.ins.AddLog(string.Format("Strategia {0} jest dominowana przez {1}", gameData.rowStrategies[j].ToString(), gameData.rowStrategies[i].ToString())); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), j, EColors.Red, true); yield return(LogsManager.Wait()); gameData.matrix = RemoveRowFromMatrix(j--); TableManager.ins.DisplayTable(gameData); yield return(LogsManager.Wait()); } else if (domination == 1) { LogsManager.ins.AddLog(string.Format("Strategia {0} dominuje strategię {1}", gameData.rowStrategies[j].ToString(), gameData.rowStrategies[i].ToString())); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), i, EColors.Red, true); yield return(LogsManager.Wait()); gameData.matrix = RemoveRowFromMatrix(i--); TableManager.ins.DisplayTable(gameData); yield return(LogsManager.Wait()); break; } else { LogsManager.ins.AddLog("Brak dominacji pomiędzy strategiami."); TableManager.ins.SelectCell(0, EColors.Default, true); yield return(LogsManager.Wait()); } } } yield return(null); }
private IEnumerator ColumnsDomination() { sbyte[] colA, colB; yield return(LogsManager.Wait()); for (byte i = 0; i < gameData.matrix.GetLength(1) - 1; i++) { colA = GetColumn(i); for (byte j = (byte)(i + 1); j < gameData.matrix.GetLength(1); j++) { colB = GetColumn(j); LogsManager.ins.AddLog(string.Format("Porównuję strategię gracza: {2} - {0} z {1}", gameData.colStrategies[i].ToString(), gameData.colStrategies[j].ToString(), gameData.playerB)); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), i, EColors.Green, true); yield return(LogsManager.Wait()); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), j, EColors.Blue); yield return(LogsManager.Wait()); // 1 = colA dominuje colB; 1 = colB dominuje colA; 0 = brak dominacji sbyte domination = Compare(colA, colB); if (domination == 1) { LogsManager.ins.AddLog(string.Format("Strategia {0} jest dominowana przez {1}", gameData.colStrategies[j].ToString(), gameData.colStrategies[i].ToString())); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), j, EColors.Red, true); yield return(LogsManager.Wait()); gameData.matrix = RemoveColumnFromMatrix(j--); TableManager.ins.DisplayTable(gameData); yield return(LogsManager.Wait()); } else if (domination == -1) { LogsManager.ins.AddLog(string.Format("Strategia {0} dominuje strategię {1}", gameData.colStrategies[j].ToString(), gameData.colStrategies[i]).ToString()); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), i, EColors.Red, true); yield return(LogsManager.Wait()); gameData.matrix = RemoveColumnFromMatrix(i--); TableManager.ins.DisplayTable(gameData); yield return(LogsManager.Wait()); break; } else { TableManager.ins.SelectCell(0, EColors.Default, true); LogsManager.ins.AddLog("Brak dominacji pomiędzy strategiami"); yield return(LogsManager.Wait()); } } } yield return(null); }
public IEnumerator SetUp(bool succes) { CanvasGroup panel = (succes) ? successPanel : failedPanel; panel.alpha = 1; blurImage.enabled = true; yield return(LogsManager.Wait(3)); blurImage.enabled = false; panel.alpha = 0; }
public IEnumerator CheckDominations() { if (stateSolver.GetType() == typeof(StateNoSaddlePoint)) { LogsManager.ins.AddLog("Sprawdzanie strategii zdominowanych."); yield return(RemoveDominatedStrategies()); } else { yield return(LogsManager.Wait()); } }
private IEnumerator RemoveDominatedStrategies() { yield return(LogsManager.Wait()); byte oldCols; // ile wierszy i kolumn posiada macierz przed dominacją do { oldCols = (byte)gameData.matrix.GetLength(1); yield return(RowsDomination()); yield return(ColumnsDomination()); } while (oldCols != gameData.matrix.GetLength(1)); }
private IEnumerator FindMinMax() { yield return(LogsManager.Wait()); sbyte max = sbyte.MinValue; sbyte minInRow; sbyte[] row; for (byte i = 0; i < gameData.matrix.GetLength(0); i++) { row = GetRow(i); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), i, EColors.Green, true); minInRow = row.Min(); MiniMaksManager.ins.ChangeMinVal(i, minInRow); LogsManager.ins.AddLog(string.Format("Znaleziono najmniejszą wartość w strategii {0} gracza: {1}", gameData.rowStrategies[i].ToString(), gameData.playerA)); yield return(LogsManager.Wait()); if (minInRow > max) { max = minInRow; LogsManager.ins.AddLog("Znaleziony min wiersza jest największy."); MiniMaksManager.ins.SelectMin(i, EColors.Red, true); yield return(LogsManager.Wait()); } else if (minInRow == max) { LogsManager.ins.AddLog("Znaleziono kolejny min wiersza"); MiniMaksManager.ins.SelectMin(i, EColors.Red); yield return(LogsManager.Wait()); } else { LogsManager.ins.AddLog("Znaleziona strategia nie jest największa spośród minimów"); yield return(LogsManager.Wait()); } } minMax = max; }
private IEnumerator FindMaxMin() { yield return(LogsManager.Wait()); sbyte min = sbyte.MaxValue; sbyte maxInColumn; sbyte[] column; for (byte i = 0; i < gameData.matrix.GetLength(1); i++) { column = GetColumn(i); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), i, EColors.Green, true); maxInColumn = column.Max(); MiniMaksManager.ins.ChangeMaxVal(i, maxInColumn); LogsManager.ins.AddLog(string.Format("Znaleziono największą wartość w strategii {0} gracza: {1}", gameData.colStrategies[i].ToString(), gameData.playerB)); yield return(LogsManager.Wait()); if (maxInColumn < min) { min = maxInColumn; LogsManager.ins.AddLog("Znaleziony maks kolumny jest najmniejszy."); MiniMaksManager.ins.SelectMaks(i, EColors.Red, true); yield return(LogsManager.Wait()); } else if (maxInColumn == min) { LogsManager.ins.AddLog("Znaleziono kolejny maks kolumny"); MiniMaksManager.ins.SelectMaks(i, EColors.Red); yield return(LogsManager.Wait()); } else { LogsManager.ins.AddLog("Znaleziona strategia nie jest najmniejsza spośród maksimów"); yield return(LogsManager.Wait()); } } maxMin = min; }
public IEnumerator CheckSaddlePoint() { LogsManager.ins.AddLog("Sprawdzanie punktów siodłowych."); MiniMaksManager.ins.SetUp(gameData); yield return(LogsManager.Wait()); yield return(FindMinMax()); yield return(FindMaxMin()); if (minMax == maxMin) { LogsManager.ins.AddLog("Znaleziono punkt siodłowy.", EColors.Green); stateSolver = new StateSaddlePoint(minMax); yield return(LogsManager.Wait()); } else { LogsManager.ins.AddLog("Brak punktów siodłowych.", EColors.Red); stateSolver = new StateNoSaddlePoint(); yield return(LogsManager.Wait()); } }
public IEnumerator SolveGame(Game game) { yield return(null); LogsManager.ins.AddLog(string.Format("{0} ma dokładnie dwie strategie.", game.gameData.playerA)); yield return(LogsManager.Wait()); id = IdTheSmalestVal(game.gameData); yield return(CrossOverMin(id, game.gameData)); subGame[0, 1] = game.gameData.matrix[0, point.strategyA_ID]; subGame[1, 1] = game.gameData.matrix[1, point.strategyA_ID]; subGame[0, 0] = game.gameData.matrix[0, point.strategyB_ID]; subGame[1, 0] = game.gameData.matrix[1, point.strategyB_ID]; char[] useingStrategies = new char[2] { game.gameData.colStrategies[point.strategyA_ID], game.gameData.colStrategies[point.strategyB_ID] }; game.gameData.colStrategies = useingStrategies.ToList(); game.gameData.matrix = subGame; }
private IEnumerator CrossOverMax(byte idLargest, GameData gameData, double max = double.MaxValue) { LogsManager.ins.AddLog("Strategia znaleziona: " + gameData.rowStrategies[idLargest]); GraphicalManager.ins.SelectLine(idLargest, EColors.Red, true); yield return(LogsManager.Wait()); byte newID = 0; Vector strategyA_start = new Vector(gameData.matrix[idLargest, 0], 0); Vector strategyA_end = new Vector(gameData.matrix[idLargest, 1], 1); if (GraphicalManager.ins.highLine.Points.Length == 0) { GraphicalManager.ins.SetHighLinePoint(new Vector2((float)strategyA_start.X, (float)strategyA_start.Y)); } Vector strategyB_start, strategyB_end; Vector maxIntersaction = new Vector(double.MinValue, 0); Vector intersection; for (byte i = 0; i < gameData.matrix.GetLength(0); i++) { if (strategyA_start.X < strategyA_end.X) { break; } strategyB_start = new Vector(gameData.matrix[i, 0], 0); strategyB_end = new Vector(gameData.matrix[i, 1], 1); if (strategyA_start.Equals(strategyB_start) && strategyA_end.Equals(strategyB_end)) { continue; } bool actual = Vector.Intersect(strategyA_start, strategyA_end, strategyB_start, strategyB_end, out intersection); if (actual && intersection.X > maxIntersaction.X && Math.Round(intersection.X, 2) < Math.Round(max, 2)) { maxIntersaction = intersection; newID = i; } } if (maxIntersaction.X > double.MinValue) { Cross2x2 c = new Cross2x2(idLargest, newID, maxIntersaction); if (point == null || c.point.X < point.point.X) { point = c; } GraphicalManager.ins.SetHighLinePoint(new Vector2((float)point.point.X, (float)point.point.Y)); yield return(CrossOverMax(newID, gameData, maxIntersaction.X)); } else { GraphicalManager.ins.SetHighLinePoint(new Vector2((float)strategyA_end.X, (float)strategyA_end.Y)); GraphicalManager.ins.SelectLine(0, EColors.Default, true); } }
public override IEnumerator SolveGame(Game game) { LogsManager.ins.AddLog("Rozwiazywanie gry w strategiach mieszanych 2x2"); FrequencyManager.ins.SetUp(game.gameData); yield return(LogsManager.Wait()); game.gameData.rowFreq = new int[2]; game.gameData.colFreq = new int[2]; sbyte[] row; for (byte i = 0; i < 2; i++) { row = game.GetRow(i); TableManager.ins.SelectRow(2, i, EColors.Green, true); LogsManager.ins.AddLog("Wybieram wiersz " + game.gameData.rowStrategies[i].ToString(), EColors.Blue); yield return(LogsManager.Wait()); LogsManager.ins.AddLog(string.Format("Wykonując obliczenia: |{0}-{1}|=|{2}|={3}. Zapisuje w {4} wierszu dla częstotliwości.", row[0], row[1], row[0] - row[1], Math.Abs(row[0] - row[1]), 1 - i), EColors.Default); yield return(LogsManager.Wait()); game.gameData.rowFreq[1 - i] = Math.Abs(row[0] - row[1]); FrequencyManager.ins.ChangeRowFreq((sbyte)(1 - i), game.gameData.rowFreq[1 - i]); yield return(LogsManager.Wait()); } LogsManager.ins.AddLog("Optymalne skracanie wyników.", EColors.Blue); yield return(LogsManager.Wait()); game.gameData.rowFreq = Game.DivisorRelativeRequences(game.gameData.rowFreq); for (sbyte i = 0; i < game.gameData.rowFreq.Length; i++) { FrequencyManager.ins.ChangeRowFreq(i, game.gameData.rowFreq[i]); yield return(LogsManager.Wait()); } sbyte[] column; for (byte i = 0; i < 2; i++) { column = game.GetColumn(i); TableManager.ins.SelectColumn(2, i, EColors.Green, true); LogsManager.ins.AddLog("Wybieram kolumnę " + game.gameData.colStrategies[i].ToString(), EColors.Blue); yield return(LogsManager.Wait()); LogsManager.ins.AddLog(string.Format("Wykonując obliczenia: |{0}-{1}|=|{2}|={3}. Zapisuje w {4} kolumnie dla częstotliwości.", column[0], column[1], column[0] - column[1], Math.Abs(column[0] - column[1]), 1 - i), EColors.Default); yield return(LogsManager.Wait()); game.gameData.colFreq[1 - i] = Math.Abs(column[0] - column[1]); FrequencyManager.ins.ChangeColFreq((sbyte)(1 - i), game.gameData.colFreq[1 - i]); yield return(LogsManager.Wait()); } TableManager.ins.SelectCell(0, EColors.Default, true); LogsManager.ins.AddLog("Optymalne skracanie wyników.", EColors.Blue); yield return(LogsManager.Wait()); game.gameData.colFreq = Game.DivisorRelativeRequences(game.gameData.colFreq); for (sbyte i = 0; i < game.gameData.colFreq.Length; i++) { FrequencyManager.ins.ChangeColFreq(i, game.gameData.colFreq[i]); yield return(LogsManager.Wait()); } }
private IEnumerator CrossOverMin(byte idSmallest, GameData gameData, double min = double.MinValue) { LogsManager.ins.AddLog("Strategia znaleziona: " + gameData.colStrategies[idSmallest].ToString()); GraphicalManager.ins.SelectLine(idSmallest, EColors.Red, true); yield return(LogsManager.Wait()); byte newID = 0; Vector strategyA_start = new Vector(0, gameData.matrix[0, idSmallest]); Vector strategyA_end = new Vector(1, gameData.matrix[1, idSmallest]); if (GraphicalManager.ins.highLine.Points.Length == 0) { GraphicalManager.ins.SetHighLinePoint(new Vector2((float)strategyA_start.Y, (float)strategyA_start.X)); } Vector strategyB_start, strategyB_end; Vector minIntersaction = new Vector(0, double.MaxValue); Vector intersaction; for (byte i = 0; i < gameData.matrix.GetLength(1); i++) { if (strategyA_start.Y > strategyA_end.Y) { break; } strategyB_start = new Vector(0, gameData.matrix[0, i]); strategyB_end = new Vector(1, gameData.matrix[1, i]); if (strategyA_start.Equals(strategyB_start) && strategyA_end.Equals(strategyB_end)) { continue; } bool actual = Vector.Intersect(strategyA_start, strategyA_end, strategyB_start, strategyB_end, out intersaction); if (actual && intersaction.Y < minIntersaction.Y && Math.Round(intersaction.Y, 2) > Math.Round(min, 2)) { minIntersaction = intersaction; newID = i; } } if (minIntersaction.Y < double.MaxValue) { Cross2x2 c = new Cross2x2(idSmallest, newID, minIntersaction); if (point == null || c.point.Y > point.point.Y) { point = c; } GraphicalManager.ins.SetHighLinePoint(new Vector2((float)point.point.Y, (float)point.point.X)); yield return(CrossOverMin(newID, gameData, minIntersaction.Y)); } else { GraphicalManager.ins.SetHighLinePoint(new Vector2((float)strategyA_end.Y, (float)strategyA_end.X)); GraphicalManager.ins.SelectLine(0, EColors.Default, true); } }
public override IEnumerator SolveGame(Game game) { LogsManager.ins.AddLog("Rozwiązywanie gry metodą przybliżoną.", EColors.Blue); GameData gameData = game.gameData; FrequencyManager.ins.SetUp(gameData); SumManager.ins.SetUp(gameData); yield return(LogsManager.Wait()); int repeatCount = 0; byte id = 0; sbyte[] data; int minVal = 0; int maxVal = 0; gameData.rowFreq = new int[gameData.matrix.GetLength(0)]; gameData.colFreq = new int[gameData.matrix.GetLength(1)]; int[] lastRow = new int[gameData.matrix.GetLength(1)]; int[] lastCol = new int[gameData.matrix.GetLength(0)]; int stopCount = gameData.matrix.GetLength(0) * gameData.matrix.GetLength(1); LogsManager.ins.AddLog(string.Format("Liczba powtórzeń liczona na podstawie ilocznu liczby strategii {0} oraz {1} i wynosi: {2}", gameData.playerA, gameData.playerB, stopCount), EColors.Default); yield return(LogsManager.Wait()); while (repeatCount < stopCount) { data = game.GetRow(id); lastRow = AddToLast(lastRow, data); gameData.rowFreq[id]++; LogsManager.ins.AddLog(string.Format("Pobieram strategię {0} gracza {1} i dodaję do dodatkowej tablicy", gameData.rowStrategies[id].ToString(), gameData.playerA)); SumManager.ins.ChangeColSum(lastRow); TableManager.ins.SelectRow((byte)gameData.matrix.GetLength(0), id, EColors.Green, true); yield return(LogsManager.Wait()); LogsManager.ins.AddLog("Dla wybranej strategii zwiększam częstotliwość o 1"); FrequencyManager.ins.ChangeRowFreq((sbyte)id, gameData.rowFreq[id]); yield return(LogsManager.Wait()); minVal = lastRow.Min(); id = GetElementID(lastRow, minVal); LogsManager.ins.AddLog("Znajduję najmniejszą wartość w wybranym wierszu."); SumManager.ins.SelectRow(id); yield return(LogsManager.Wait()); data = game.GetColumn(id); lastCol = AddToLast(lastCol, data); gameData.colFreq[id]++; LogsManager.ins.AddLog(string.Format("Pobieram strategię {0} gracza {1} i dodaję do dodatkowej tablicy", gameData.colStrategies[id].ToString(), gameData.playerB)); SumManager.ins.ChangeRowSm(lastCol); TableManager.ins.SelectColumn((byte)gameData.matrix.GetLength(1), id, EColors.Green, true); yield return(LogsManager.Wait()); LogsManager.ins.AddLog("Dla wybranej strategii zwiększam częstotliwość o 1"); FrequencyManager.ins.ChangeColFreq((sbyte)id, gameData.colFreq[id]); yield return(LogsManager.Wait()); maxVal = lastCol.Max(); id = GetElementID(lastCol, maxVal); LogsManager.ins.AddLog("Znajduję najmniejszą wartość w wybranej kolumnie."); SumManager.ins.SelectColumn(id); yield return(LogsManager.Wait()); repeatCount++; LogsManager.ins.AddLog(string.Format("Powtórzenie: {0}/{1}.", repeatCount, stopCount), EColors.Yellow); } SumManager.ins.UnselectAll(); TableManager.ins.SelectCell(0, EColors.Default, true); LogsManager.ins.AddLog("Optymalne skracanie wyników.", EColors.Blue); gameData.rowFreq = Game.DivisorRelativeRequences(gameData.rowFreq); gameData.colFreq = Game.DivisorRelativeRequences(gameData.colFreq); for (sbyte i = 0; i < gameData.rowFreq.Length; i++) { FrequencyManager.ins.ChangeRowFreq(i, gameData.rowFreq[i]); } for (sbyte i = 0; i < gameData.colFreq.Length; i++) { FrequencyManager.ins.ChangeColFreq(i, gameData.colFreq[i]); } }