Ejemplo n.º 1
0
        public override string[] Execute()
        {
            resultComment.Remove(0, resultComment.Length);
            List <string> resultSet = new List <string>();

            foreach (Strategies.Wins w in strategies.WinsList)
            {
                // Check if w is in alpha-kernel
                bool inAlphaKernel = true;
                for (int t = 1; t < (1 << strategies.PlayerCount) - 1; t++)
                {
                    // t is a coalition.
                    int[] coalition     = new int[CountBits(t)];
                    int[] complementary = new int[strategies.PlayerCount - CountBits(t)];
                    int[] index         = new int[strategies.PlayerCount];

                    List <string> tmp = new List <string>(w.Key.Replace(',', ' ').Split(' '));
                    tmp.RemoveAll(string.IsNullOrEmpty);
                    string[] st = tmp.ToArray();

                    for (int j = 0; j < index.Length; j++)
                    {
                        index[j] = strategies.GetStrategies(j).IndexOf(st[j]);
                    }

                    int p = 0, q = 0;
                    int coalitionVariants = 1;
                    for (int j = 0; j < strategies.PlayerCount; j++)
                    {
                        if ((t & (1 << j)) != 0)
                        {
                            coalition[p++]     = j;
                            coalitionVariants *= strategies.GetStrategies(j).Count;
                        }
                        else
                        {
                            complementary[q++] = j;
                        }
                    }

                    int[] newIndex        = new int[strategies.PlayerCount];
                    for (int newT = 0; newT < coalitionVariants; newT++)
                    {
                        int k;
                        k = newT;
                        for (int i = 0; i < coalition.Length; i++)
                        {
                            int v = strategies.GetStrategies(coalition[i]).Count;
                            newIndex[coalition[i]] = k % v;
                            k /= v;
                        }

                        bool isBetter = true;
                        for (int threat = 0; threat < strategies.WinsList.Count / coalitionVariants; threat++)
                        {
                            k = threat;
                            for (int i = 0; i < complementary.Length; i++)
                            {
                                int v = strategies.GetStrategies(complementary[i]).Count;
                                newIndex[complementary[i]] = k % v;
                                k /= v;
                            }

                            Strategies.Wins w1       = strategies.GetWins(IndexArrayToKey(newIndex));
                            bool            allEqual = true;
                            for (int i = 0; i < coalition.Length; i++)
                            {
                                int c = coalition[i];
                                allEqual &= w1[c] == w[c];
                                if (w1[c] < w[c])
                                {
                                    isBetter = false;
                                    break;
                                }
                            }
                            if (isBetter && allEqual)
                            {
                                isBetter = false;
                            }
                            if (!isBetter)
                            {
                                break; // Already found good threat.
                            }
                        }

                        if (isBetter)
                        {
                            inAlphaKernel = false;
                            resultComment.AppendFormat("{0} не належить α-ядру гри, так як гравці {{1}} можуть утворити коаліцію", w.Key, PlayerSet(coalition));
                            break;
                        }
                    }
                    if (!inAlphaKernel)
                    {
                        break;
                    }
                }

                if (inAlphaKernel)
                {
                    resultComment.Append(w.Key + " належить α-ядру гри\r\n");
                    resultSet.Add(w.Key);
                }
            }
            return(resultSet.ToArray());
        }
Ejemplo n.º 2
0
        public override string[] Execute()
        {
            resultComment.Remove(0, resultComment.Length);
            List <string> resultSet = new List <string>();

            foreach (Strategies.Wins w in strategies.WinsList)
            {
                // Check if w is in beta-kernel
                bool inBetaKernel = true;
                for (int t = 1; t < (1 << strategies.PlayerCount) - 1; t++)
                {
                    // t is a coalition.
                    int[] coalition     = new int[CountBits(t)];
                    int[] complementary = new int[strategies.PlayerCount - CountBits(t)];
                    int[] index         = new int[strategies.PlayerCount];

                    List <string> tmp = new List <string>(w.Key.Replace(',', ' ').Split(' '));
                    tmp.RemoveAll(string.IsNullOrEmpty);
                    string[] st = tmp.ToArray();

                    for (int j = 0; j < index.Length; j++)
                    {
                        index[j] = strategies.GetStrategies(j).IndexOf(st[j]);
                    }

                    int p = 0, q = 0;
                    int coalitionVariants = 1;
                    for (int j = 0; j < strategies.PlayerCount; j++)
                    {
                        if ((t & (1 << j)) != 0)
                        {
                            coalition[p++]     = j;
                            coalitionVariants *= strategies.GetStrategies(j).Count;
                        }
                        else
                        {
                            complementary[q++] = j;
                        }
                    }

                    int[] newIndex        = new int[strategies.PlayerCount];
                    for (int newT = 0; newT < strategies.WinsList.Count / coalitionVariants; newT++)
                    {
                        int k;
                        k = newT;
                        for (int i = 0; i < complementary.Length; i++)
                        {
                            int v = strategies.GetStrategies(complementary[i]).Count;
                            newIndex[complementary[i]] = k % v;
                            k /= v;
                        }

                        bool isGoodThreateningStrategy = true;
                        for (int forw = 0; forw < coalitionVariants; forw++)
                        {
                            k = forw;
                            for (int i = 0; i < coalition.Length; i++)
                            {
                                int v = strategies.GetStrategies(coalition[i]).Count;
                                newIndex[coalition[i]] = k % v;
                                k /= v;
                            }

                            Strategies.Wins w1       = strategies.GetWins(IndexArrayToKey(newIndex));
                            bool            allEqual = true;
                            for (int i = 0; i < coalition.Length; i++)
                            {
                                int c = coalition[i];
                                allEqual &= w1[c] == w[c];
                                if (w1[c] < w[c])
                                {
                                    isGoodThreateningStrategy = false;
                                    break;
                                }
                            }
                            if (isGoodThreateningStrategy && allEqual)
                            {
                                isGoodThreateningStrategy = false;
                            }
                            if (!isGoodThreateningStrategy)
                            {
                                break; // Already found good threat.
                            }
                        }

                        if (!isGoodThreateningStrategy)
                        {
                            inBetaKernel = false;
//                            resultComment.AppendFormat("{0} не належить β-ядру гри", w.Key);
                            break;
                        }
                    }
                    if (!inBetaKernel)
                    {
                        break;
                    }
                }

                if (inBetaKernel)
                {
                    //                  resultComment.Append(w.Key + " належить β-ядру гри\r\n");
                    resultSet.Add(w.Key);
                }
            }

            if (resultSet.Count > 0)
            {
                resultComment.Append("β-ядро гри: {");
                for (int i = 0; i < resultSet.Count; i++)
                {
                    if (i > 0)
                    {
                        resultComment.Append(", ");
                    }
                    resultComment.Append(resultSet[i]);
                }
                resultComment.Append("}");
            }
            else
            {
                resultComment.Append("β-ядро гри порожнє");
            }

            return(resultSet.ToArray());
        }
Ejemplo n.º 3
0
        public override string[] Execute()
        {
            resultComment.Remove(0, resultComment.Length);
            List <string> resultSet = new List <string>();

            foreach (Strategies.Wins w in strategies.WinsList)
            {
                // Check if w is Strong Nash Equibalance
                bool isStrongNash = true;
                for (int t = 0; t < (1 << strategies.PlayerCount) - 1; t++)
                {
                    int[] group = new int[CountBits(t)];
                    int[] index = new int[strategies.PlayerCount];

                    List <string> tmp = new List <string>(w.Key.Replace(',', ' ').Split(' '));
                    tmp.RemoveAll(string.IsNullOrEmpty);
                    string[] st = tmp.ToArray();

                    for (int j = 0; j < index.Length; j++)
                    {
                        index[j] = strategies.GetStrategies(j).IndexOf(st[j]);
                    }

                    int p = 0;
                    for (int j = 0; j < strategies.PlayerCount; j++)
                    {
                        if ((t & (1 << j)) != 0)
                        {
                            group[p++] = j;
                        }
                    }

                    int v = 1;
                    for (int j = 0; j < group.Length; j++)
                    {
                        v *= strategies.Count(group[j]);
                    }

                    for (int i = 0; i < v; i++)
                    {
                        int k = i;
                        for (int j = 0; j < group.Length; j++)
                        {
                            index[group[j]] = k % strategies.Count(group[j]);
                            k /= strategies.Count(group[j]);
                        }

                        string key = IndexArrayToKey(index);

                        Strategies.Wins w1 = strategies.GetWins(key);
                        bool            allNotWorse = true, haveBetter = false;
                        for (int j = 0; j < group.Length; j++)
                        {
                            if (w1[j] < w[j])
                            {
                                allNotWorse = false;
                                break;
                            }
                            else
                            if (w1[j] > w[j])
                            {
                                haveBetter = true;
                            }
                        }

                        if (allNotWorse && haveBetter)
                        {
                            isStrongNash = false;
                            resultComment.Append(w.Key);
                            resultComment.Append(" не є Сильною Рівновагою Неша, бо коаліція {");
                            for (int j = 0; j < group.Length; j++)
                            {
                                if (j > 0)
                                {
                                    resultComment.Append(", ");
                                }
                                resultComment.Append(group[j] + 1);
                            }
                            resultComment.Append("} змінює стратегію на ");
                            resultComment.Append(w1.Key);
                            resultComment.Append("\r\n");
                            break;
                        }
                    }
                    if (!isStrongNash)
                    {
                        break;
                    }
                }
                if (isStrongNash)
                {
                    resultComment.Append(w.Key + " є Сильною Рівновагою Неша\r\n");
                    resultSet.Add(w.Key);
                }
            }
            return(resultSet.ToArray());
        }