예제 #1
0
        public List <NE_Profile> P1search_Max_Col(int colIndex, int rows, List <NE_Profile> p)
        {
            List <NE_Profile> results = new List <NE_Profile>(); //returned list
                                                                 //tempMax: used for comparison with the next cell
            NE_Profile TempNash = new NE_Profile(p.Find(n => (n.row == 0 && n.col == colIndex)).payoff1, p.Find(n => (n.row == 0 && n.col == colIndex)).payoff2, p.Find(n => (n.row == 0 && n.col == colIndex)).payoff3, 0, colIndex);

            for (int i = 1; i < rows; i++)//loop on rows of the selected col
            {
                if (TempNash.payoff1 > p.Find(n => (n.row == i && n.col == colIndex)).payoff1)
                {
                    List <NE_Profile> container = results.FindAll(n => (n.payoff1 == TempNash.payoff1)).ToList <NE_Profile>(); //if TempNash or/and an equal payoff was added before
                    if (container.Count == 0)                                                                                  //if not then TempNash is already in the list and no need to add it a sec time
                    {
                        results.Clear();
                        results.Add(TempNash);
                    }
                    continue;
                }
                else if (TempNash.payoff1 < p.Find(n => (n.row == i && n.col == colIndex)).payoff1)
                {
                    results.Clear();
                    TempNash.payoff1 = p.Find(n => (n.row == i && n.col == colIndex)).payoff1;
                    TempNash.payoff2 = p.Find(n => (n.row == i && n.col == colIndex)).payoff2;
                    TempNash.payoff3 = p.Find(n => (n.row == i && n.col == colIndex)).payoff3;
                    TempNash.row     = i;// same col but different row
                    results.Add(TempNash);
                    continue;
                }
                else if (TempNash.payoff1 == p.Find(n => (n.row == i && n.col == colIndex)).payoff1)                           //Add both profiles
                {
                    List <NE_Profile> container = results.FindAll(n => (n.payoff1 == TempNash.payoff1)).ToList <NE_Profile>(); //Detects prevsly added payoffs, whether of the same val of not
                    if (container.Count > 0)                                                                                   //TempNash is already in the list
                    {
                        results.Add(new NE_Profile(p.Find(n => (n.row == i && n.col == colIndex)).payoff1, p.Find(n => (n.row == i && n.col == colIndex)).payoff2, p.Find(n => (n.row == i && n.col == colIndex)).payoff3, i, colIndex));
                    }
                    else
                    {//no values were found or only TempNash was found
                        results.Clear();
                        results.Add(TempNash);
                        results.Add(new NE_Profile(p.Find(n => (n.row == i && n.col == colIndex)).payoff1, p.Find(n => (n.row == i && n.col == colIndex)).payoff2, p.Find(n => (n.row == i && n.col == colIndex)).payoff3, i, colIndex));
                    }
                    continue;
                }
            }
            return(results);
        }
예제 #2
0
        public List <NE_Profile> P2search_Max_Row(int StartCol, int rowIndex, int Numcols, List <NE_Profile> p)
        {
            List <NE_Profile> results = new List <NE_Profile>(); //returned list
                                                                 //temp for comparison
            NE_Profile TempNash = new NE_Profile(p.Find(n => (n.row == rowIndex && n.col == StartCol)).payoff1, p.Find(n => (n.row == rowIndex && n.col == StartCol)).payoff2, p.Find(n => (n.row == rowIndex && n.col == StartCol)).payoff3, rowIndex, StartCol);

            for (int i = StartCol + 1; i < Numcols + StartCol; i++)
            {
                if (TempNash.payoff2 > p.Find(n => (n.row == rowIndex && n.col == i)).payoff2)//Add TempNash
                {
                    List <NE_Profile> container = results.FindAll(n => (n.payoff2 == TempNash.payoff2)).ToList <NE_Profile>();
                    if (container.Count == 0)//if TempNash wasn't added before
                    {
                        results.Clear();
                        results.Add(TempNash);
                    }
                    continue;
                }
                else
                if (TempNash.payoff2 < p.Find(n => (n.row == rowIndex && n.col == i)).payoff2)    //Add p[row,i]
                {
                    results.Clear();
                    TempNash.payoff2 = p.Find(n => (n.row == rowIndex && n.col == i)).payoff2;
                    TempNash.payoff1 = p.Find(n => (n.row == rowIndex && n.col == i)).payoff1;
                    TempNash.payoff3 = p.Find(n => (n.row == rowIndex && n.col == i)).payoff3;
                    TempNash.col     = i;
                    results.Add(TempNash);
                    continue;
                }
                else if (p.Find(n => (n.row == rowIndex && n.col == i)).payoff2 == TempNash.payoff2)//Add both
                {
                    List <NE_Profile> container = results.FindAll(n => (n.payoff2 == TempNash.payoff2)).ToList <NE_Profile>();
                    if (container.Count > 0)
                    {//TempNash already exists
                        results.Add(new NE_Profile(p.Find(n => (n.row == rowIndex && n.col == i)).payoff1, p.Find(n => (n.row == rowIndex && n.col == i)).payoff2, p.Find(n => (n.row == rowIndex && n.col == i)).payoff3, rowIndex, i));
                    }
                    else
                    {//no values were found or only TempNash was found
                        results.Clear();
                        results.Add(TempNash);
                        results.Add(new NE_Profile(p.Find(n => (n.row == rowIndex && n.col == i)).payoff1, p.Find(n => (n.row == rowIndex && n.col == i)).payoff2, p.Find(n => (n.row == rowIndex && n.col == i)).payoff3, rowIndex, i));
                    }
                }
            }
            return(results);
        }
예제 #3
0
        }//RETURNS PF containing CP AND FINISHES

        public Dictionary <NE_Profile, bool> PreferencesGetter(string preferences, string username, List <NE_Profile> Max_Payoff)//gets the prefrences of each individual playre
        {
            foreach (var player in wrapper_TwoThree.playerInfo)
            {
                if (player.username == username)
                {
                    if (player.preferences.Count() == 0)
                    {
                        player.preferences = preferences;
                    }
                    else
                    {
                        return(new Dictionary <NE_Profile, bool>());
                    }
                }
            }

            bool check = false;

            foreach (var player in wrapper_TwoThree.playerInfo)
            {
                if (player.preferences == "")
                {
                    check = true;
                }
            }
            Dictionary <NE_Profile, bool> Returned_List = new Dictionary <NE_Profile, bool>();

            if (check == false)//all the players have their preferences filled
            {
                List <string> allpprefrences = new List <string>();
                for (int i = 0; i < wrapper_TwoThree.playerInfo.Count; i++)
                {
                    allpprefrences.Add(wrapper_TwoThree.playerInfo.Find(item => (item.orderInGame == i)).preferences);//order players' prefrences
                }

                PayOffs_generater(allpprefrences, wrapper_TwoThree.neprofiles); //splits preferences of each player and calls utility function and stores them in P

                Max_Payoff = new List <NE_Profile>();                           //Max payyoffs
                List <PF> OrderedPlayers = new List <PF>();
                for (int i = 0; i < wrapper_TwoThree.playerInfo.Count; i++)
                {
                    OrderedPlayers.Add(wrapper_TwoThree.playerInfo.Find(item => (item.orderInGame == i)));//order players
                }
                int p3_numStrategy = 0;
                if (OrderedPlayers.Count == 2)
                {
                    p3_numStrategy = 1;
                }
                else
                {
                    p3_numStrategy = OrderedPlayers[2].strategies.Count;
                }

                for (int i = 0; i < OrderedPlayers[0].strategies.Count; i++)//loop: rows/p2
                {
                    for (int j = 0; j < OrderedPlayers[1].strategies.Count * p3_numStrategy; j = j + OrderedPlayers[1].strategies.Count)
                    {
                        List <NE_Profile> tempRes = P2search_Max_Row(j, i, OrderedPlayers[1].strategies.Count, wrapper_TwoThree.neprofiles); // calls function and get MaxPayoff_row
                        Max_Payoff.AddRange(tempRes);                                                                                        // merging the results of the called function with Max_Payoff matrix
                    }
                }
                for (int j = 0; j < OrderedPlayers[1].strategies.Count * p3_numStrategy; j++)                                         //loop col/p1
                {
                    List <NE_Profile> tempRes = P1search_Max_Col(j, OrderedPlayers[1].strategies.Count, wrapper_TwoThree.neprofiles); // calls function and get MaxPayoff_col
                    Max_Payoff.AddRange(tempRes);                                                                                     // merging the results of the called function with Max_Payoff matrix
                }

                int  k         = 0;                                                                                                                                    // k is the index of the first element in the list and it's unchangable //NO TASTE
                bool flag_pair = false;                                                                                                                                // indicates if there is at least one Nash
                List <NE_Profile> PairedMax;                                                                                                                           //for p1_p12 results
                NE_Profile        element_maxPayoff;                                                                                                                   //temp for the first element of maxpayoff
                List <NE_Profile> Paired_p1_p2 = new List <NE_Profile>();                                                                                              //to be used to find the max for p3
                                                                                                                                                                       //List<NE_Profile> Finale = new List<NE_Profile>();//NASH EQUILIBRIUM PRIFLES ARE STORED HERE
                List <NE_Profile> Finale = new List <NE_Profile>();                                                                                                    //NASH EQUILIBRIUM PRIFLES ARE STORED HERE

                while (Max_Payoff.Count > 0)                                                                                                                           //pairing the results
                {
                    element_maxPayoff = new NE_Profile(Max_Payoff[k].payoff1, Max_Payoff[k].payoff2, /*Max_Payoff[k].payoff3,*/ Max_Payoff[k].row, Max_Payoff[k].col); //first node in list
                    PairedMax         = Max_Payoff.FindAll(n => (n.col == element_maxPayoff.col && n.row == element_maxPayoff.row)).ToList <NE_Profile>();             //Finding all matching profiles
                    Max_Payoff.RemoveAll(n => (n.col == element_maxPayoff.col && n.row == element_maxPayoff.row));                                                     //removed paired nodes
                    if (PairedMax.Count > 1)
                    {
                        Paired_p1_p2.Add(PairedMax[0]); //NOOO Duplication
                        flag_pair = true;               //at least one Nash was found
                    }
                }

                List <int> temp = new List <int>();
                if (!flag_pair)// no NE
                {
                    return(new Dictionary <NE_Profile, bool>());
                }
                //List<int> Temp = new List<int>();

                if (OrderedPlayers.Count == 3)
                {
                    Finale = P3search_Max_Cell(wrapper_TwoThree.neprofiles, Paired_p1_p2, OrderedPlayers[1].strategies.Count, p3_numStrategy);
                    //.Add(new NE_Profile(1, 1, 1, 1));
                    //return Finale;
                    bool flag_finale = false;                            //ture if point is a NE
                    foreach (var profile in wrapper_TwoThree.neprofiles) //fill returned list
                    {
                        foreach (var fin in Finale)
                        {
                            if (profile.col == fin.col || profile.row == fin.row || profile.payoff1 == fin.payoff1 || profile.payoff2 == fin.payoff2 || profile.payoff3 == fin.payoff3)//true if it's a nash point
                            {
                                flag_finale = true;
                                Returned_List.Add(profile, flag_finale);
                            } //nash
                        }     //p2p3
                        if (flag_finale == false)
                        {
                            Returned_List.Add(profile, flag_finale);
                        }
                    }//end foreach profile
                    wrapper_TwoThree.N_Players_Game = new GameFrame();
                    wrapper_TwoThree.neprofiles.Clear();
                    wrapper_TwoThree.playerInfo.Clear();
                    return(Returned_List);
                }
                //2p returned list
                bool flag_p2p3 = false;                              //true if point is a NE
                foreach (var profile in wrapper_TwoThree.neprofiles) //fill returned list
                {
                    foreach (var p2p3 in Paired_p1_p2)
                    {
                        if (profile.col == p2p3.col || profile.row == p2p3.row || profile.payoff1 == p2p3.payoff1 || profile.payoff2 == p2p3.payoff2 || profile.payoff3 == p2p3.payoff3)//true if it's a nash point
                        {
                            flag_p2p3 = true;
                            Returned_List.Add(profile, flag_p2p3);
                        }//nash
                    }     //p2p3
                    if (flag_p2p3 == false)
                    {
                        Returned_List.Add(profile, flag_p2p3);
                    }
                }//end foreach profile 2p

                wrapper_TwoThree.N_Players_Game = new GameFrame();
                wrapper_TwoThree.neprofiles.Clear();
                wrapper_TwoThree.playerInfo.Clear();
                return(Returned_List);
            }
            return(null);
        }
예제 #4
0
        public List <NE_Profile> P3search_Max_Cell(List <NE_Profile> p, List <NE_Profile> maxPayoff, int p2NumStrategies, int p3NumStrategies)
        {
            //List<NE_Profile> temp_maxPayoff = new List<NE_Profile>();//so we can delete without affecting the data
            //temp_maxPayoff.AddRange(maxPayoff);//copy by value
            List <NE_Profile> results      = new List <NE_Profile>(); //returned list
            List <NE_Profile> temp_results = new List <NE_Profile>(); //loop temp list
            List <NE_Profile> temp         = new List <NE_Profile>(); //find all profiles in row

            while (maxPayoff.Count > 0)
            {
                NE_Profile tempMax = new NE_Profile(maxPayoff[0].payoff1, maxPayoff[0].payoff2, maxPayoff[0].payoff3, maxPayoff[0].row, maxPayoff[0].col); //first node in list
                temp = p.FindAll(n => (Math.Abs(n.col - tempMax.col) == p2NumStrategies && n.row == tempMax.row)).ToList <NE_Profile>();                   //Finding all same row profiles
                for (int i = 0; i < temp.Count; i++)
                {
                    if (temp[i].payoff3 > tempMax.payoff3)
                    {
                        temp_results.Clear();
                        maxPayoff.RemoveAll(n => (n.col == tempMax.col && n.row == tempMax.row)); //removed becuase it's not the max in the row
                        if (maxPayoff.Find(n => (n.payoff1 == temp[i].payoff1 && n.payoff2 == temp[i].payoff2 && n.payoff3 == temp[i].payoff3 && n.col == temp[i].col && n.row == temp[i].row)) != null)
                        {                                                                         //if temp[i] is found in the Maxes list then add it to res and delete it
                            temp_results.Add(temp[i]);
                            tempMax = new NE_Profile(temp[i].payoff1, temp[i].payoff2, temp[i].payoff3, temp[i].row, temp[i].col);
                            maxPayoff.RemoveAll(n => (n.col == temp[i].col && n.row == temp[i].row));;
                        }
                        continue;
                    }
                    if (tempMax.payoff3 > temp[i].payoff3)
                    {
                        temp_results.Clear();
                        temp_results.Add(tempMax);
                        maxPayoff.RemoveAll(n => (n.col == tempMax.col && n.row == tempMax.row));; //removed
                        maxPayoff.RemoveAll(n => (n.col == temp[i].col && n.row == temp[i].row));; //removed if found
                        continue;
                    }
                    if (temp[i].payoff3 == tempMax.payoff3)//(REVIEW 2players)
                    {
                        List <NE_Profile> container = temp_results.FindAll(n => (n.payoff3 == tempMax.payoff3 && n.row == tempMax.row && n.col != tempMax.col)).ToList <NE_Profile>();
                        if (container.Count > 0)
                        {
                            temp_results.Add(tempMax);
                            if (maxPayoff.Find(n => (n.payoff1 == temp[i].payoff1 && n.payoff2 == temp[i].payoff2 && n.payoff3 == temp[i].payoff3 && n.col == temp[i].col && n.row == temp[i].row)) != null)
                            {                                                                             //if it was in the Maxes list then added the temp along side the tempMax
                                temp_results.Add(temp[i]);
                                maxPayoff.RemoveAll(n => (n.col == temp[i].col && n.row == temp[i].row)); //if found
                            }
                            maxPayoff.RemoveAll(n => (n.col == tempMax.col && n.row == tempMax.row));;
                        }
                        else
                        {
                            temp_results.Clear();
                            temp_results.Add(tempMax);
                            maxPayoff.RemoveAll(n => (n.col == tempMax.col && n.row == tempMax.row));
                            if (maxPayoff.Find(n => (n.payoff1 == temp[i].payoff1 && n.payoff2 == temp[i].payoff2 && n.payoff3 == temp[i].payoff3 && n.col == temp[i].col && n.row == temp[i].row)) != null)
                            {//if it was in the Maxes list then added the temp along side the tempMax
                                temp_results.Add(temp[i]);
                                maxPayoff.RemoveAll(n => (n.col == temp[i].col && n.row == temp[i].row));
                            }
                        }
                    } //if ==
                }     //end for
                results.AddRange(temp_results);//addig sub-results of a row to the returned list
                temp_results.Clear();
            }//end while

            return(results);
        }