Exemplo n.º 1
0
        private static double H(int i, GlobalState s, int[] a)
        {
            double R          = 4;
            int    lipos      = 0; //if li>0
            int    aizer      = 0; //if ai=0
            double ajzercount = 0;
            AState aS         = (AState)s.getLocalStates()[i];
            int    li         = aS.getL();

            if (li > 0)
            {
                lipos = 1;
            }
            if (a[i] == 0)
            {
                aizer = 1;
            }

            if (lipos * aizer == 1)
            {
                for (int j = 0; j < n; j++)
                {
                    if (a[j] == 0)
                    {
                        ajzercount++;
                    }
                }
                return(R / ajzercount);
            }
            else
            {
                return(0);
            }
            //int c = -2, r = 2, w = -1;
            //// only valid state action pairs are accepted
            //int z_i = s.getLocalStates()[i].getZ();//get z_i
            //int y_i = s.getLocalStates()[i].getY();//get y_i
            //if (z_i == 0 && a[i] == 1)
            //{
            //    return c;
            //}
            //else if (z_i == 1 && a[i] == 0 && !gamma(i, s))//stay in finished state if the job not completed
            //{
            //    return w;
            //}
            //else if (z_i == 1 && a[i] == 1 && gamma(i, s))//go to completed state if a job completed
            //{
            //    return r;
            //}
            //else
            //{
            //    return 0;
            //}
            return(0);
        }
Exemplo n.º 2
0
        private static GlobalState BT(GlobalState s, int[] a, List <GlobalState> gstates)
        {
            State[] localstates = new AState[n];
            for (int i = 0; i < n; i++)
            {
                AState aS     = (AState)(s.getLocalStates()[i]);
                int    label  = aS.getLabel();
                int    l      = aS.getL();
                int[]  c      = aS.getC();
                int[]  aSData = new int[n + 2];
                if (label != 2)
                {
                    int shootcount = 0;

                    for (int j = 0; j < n; j++)
                    {
                        if (j != i && a[j] == (i + 1))//convert zero based index of players to one based index for shooting
                        {
                            shootcount++;
                            if (c[j] > 0)//lose confidence with shootings
                            {
                                c[j]--;
                            }
                        }
                        aSData[j] = c[j];
                    }
                    l             = Math.Max(0, l - shootcount);
                    label         = (label + 1) % 3;
                    aSData[n]     = l;
                    aSData[n + 1] = label;
                }
                else
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (j != (i))//update confidence
                        {
                            c[j] = Math.Min(2, c[j] + 1);
                        }
                        aSData[j] = c[j];
                    }
                    l             = 2;//restore life
                    label         = (label + 1) % 3;
                    aSData[n]     = l;
                    aSData[n + 1] = label;
                }
                localstates[i] = new AState(i, aSData);
            }

            return(gstates[getGlobalStateIndex(localstates)]);
        }
Exemplo n.º 3
0
        private static double Pr(int i, GlobalState s, int[] a)
        {
            AState aS = (AState)(s.getLocalStates()[i]); //retrive the state of player i

            int[]  c          = aS.getC();               //retrive confidence list of player i
            double normal_sum = 0;
            double maxcij     = 0;

            for (int j = 0; j < n; j++)
            {
                AState aSj = (AState)(s.getLocalStates()[j]);
                if (j != i)
                {
                    if (aSj.getL() > 0)    //only the living ones are considered for shooting
                    {
                        if (c[j] > maxcij) //find max confidence value
                        {
                            maxcij = c[j];
                        }
                        normal_sum += c[j];
                    }
                }
            }
            if (normal_sum > 0)//if some other player is alive
            {
                if (a[i] == 0)
                {
                    return(1 - maxcij / (normal_sum));
                }
                else
                {
                    for (int j = 1; j <= n; j++)
                    {
                        if (a[i] == j)
                        {
                            AState aSj = (AState)(s.getLocalStates()[j - 1]);//shift the index of j
                            int    lj  = aSj.getL();
                            if (lj == 0)
                            {
                                return(0);
                            }
                            else
                            {
                                return((maxcij * c[j - 1]) / normal_sum);//shifting the index to zero based
                            }
                        }
                    }
                }
            }
            else //if no other player is alive
            {
                if (a[i] == 0)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }


            return(0);
        }
Exemplo n.º 4
0
        private static String Nashepdeltamul(List <double>[] vcurrent, List <double>[] vprev, List <double>[] ucurrent, List <double>[] uprev, int i0, int j0)
        {
            String     result = "";
            double     min_distance = 20, deltastar = 0;
            List <int> ratlist = new List <int>();

            ratlist.Add(i0);
            ratlist.Add(j0);

            while (result.Equals("undecided") || result.Equals(""))
            {
                //initialize vi and ui
                for (int i = 0; i < n; i++)
                {
                    vcurrent[i] = new List <double>();
                    vprev[i]    = new List <double>();
                    ucurrent[i] = new List <double>();
                    uprev[i]    = new List <double>();
                    for (int s = 0; s < gstates.Count; s++)
                    {
                        vcurrent[i].Add(0);
                        vprev[i].Add(0);
                        ucurrent[i].Add(0);
                        uprev[i].Add(0);
                    }
                }

                //algo


                double M = 4;
                //beta^k < (1-beta)*delta/5M
                //k>log((1-beta)*delta/5M)/log(beta)
                double k   = Math.Ceiling(Math.Log(((1 - beta) * delta) / (24 * M)) / Math.Log(beta));
                double EiK = (6 * M * Math.Pow(beta, k)) / (1 - beta);

                System.Diagnostics.Debug.WriteLine("k =" + k);
                Parallel.For(0, gstates.Count, index =>
                             //    Parallel.For(0, 729, index =>
                {
                    GlobalState s = gstates[(index * 128) % gstates.Count];
                    //  GlobalState s = gstates[(index * 128) % 729];
                    for (int t = 0; t < k; t++)
                    {
                        //    foreach (int i in ratlist)//i,j rationals
                        //    {//for each altruistic player, player of interest is rational
                        bybool[i0] = true; //make i byzantine
                        bybool[j0] = true; //make j byzantine

                        double max = 0;

                        foreach (GlobalAction a_ij in multiAc)//for each action of rational ij 2
                        {
                            double minbyzi = 20;
                            double minbyzj = 20;
                            foreach (GlobalAction a_z in byzAc)//for each byz action Z
                            {
                                double rewardi = 0;
                                double rewardj = 0;
                                foreach (GlobalAction a_al in AlmrAc)         //for each altruistic action,two rat players Al-2
                                {
                                    setglobalAction(i0, j0, a_ij, a_z, a_al); //call overridden method
                                    if (BT(s, Actions, gstates) != null)
                                    {
                                        GlobalState snext = BT(s, Actions, gstates);
                                        rewardi          += GlobalPr(s, Actions) * (H(i0, s, Actions) + beta * vprev[i0][getGlobalStateIndex(snext.getLocalStates())]);
                                        rewardj          += GlobalPr(s, Actions) * (H(j0, s, Actions) + beta * vprev[j0][getGlobalStateIndex(snext.getLocalStates())]);
                                    }
                                }//expected reward calculated for a_z,a_ij
                                if (AlmrAc.Count == 0)
                                {
                                    setglobalAction(i0, j0, a_ij, a_z, null);//call overridden method
                                    if (BT(s, Actions, gstates) != null)
                                    {
                                        GlobalState snext = BT(s, Actions, gstates);
                                        rewardi           = (H(i0, s, Actions) + beta * vprev[i0][getGlobalStateIndex(snext.getLocalStates())]);
                                        rewardj           = (H(j0, s, Actions) + beta * vprev[j0][getGlobalStateIndex(snext.getLocalStates())]);
                                    }
                                }
                                if (rewardi < minbyzi)
                                {
                                    minbyzi = rewardi;
                                }
                                if (rewardj < minbyzj)
                                {
                                    minbyzj = rewardj;
                                }
                                //min reward calculated for each a_ij
                                //     System.Diagnostics.Debug.WriteLine("reward =" + minbyz);
                            }//minbyz calculated
                            if (byZ.Count == 0)
                            {
                                double rewardi = 0;
                                double rewardj = 0;
                                foreach (GlobalAction a_al in AlmrAc)          //for each altruistic action,two rat players Al-2
                                {
                                    setglobalAction(i0, j0, a_ij, null, a_al); //call overridden method
                                    if (BT(s, Actions, gstates) != null)
                                    {
                                        GlobalState snext = BT(s, Actions, gstates);
                                        rewardi          += GlobalPr(s, Actions) * (H(i0, s, Actions) + beta * vprev[i0][getGlobalStateIndex(snext.getLocalStates())]);
                                        rewardj          += GlobalPr(s, Actions) * (H(j0, s, Actions) + beta * vprev[j0][getGlobalStateIndex(snext.getLocalStates())]);
                                    }
                                } //expected reward calculated for a_z,a_ij
                                if (AlmrAc.Count == 0)
                                {
                                    setglobalAction(i0, j0, a_ij, null, null); //call overridden method
                                    if (BT(s, Actions, gstates) != null)
                                    {
                                        GlobalState snext = BT(s, Actions, gstates);
                                        rewardi           = (H(i0, s, Actions) + beta * vprev[i0][getGlobalStateIndex(snext.getLocalStates())]);
                                        rewardj           = (H(j0, s, Actions) + beta * vprev[j0][getGlobalStateIndex(snext.getLocalStates())]);
                                    }
                                }

                                minbyzi = rewardi;
                                minbyzj = rewardj;
                                //min reward calculated for each a_ij
                                //     System.Diagnostics.Debug.WriteLine("reward =" + minbyz);
                            }

                            /* if (minbyz > max)
                             *   max = minbyz;
                             * exp += Pr(i, s, Actions) * minbyz;*/
                            //min reward is used to maximize a different reward function
                            if (alpha * minbyzi + (1 - alpha) * minbyzj > max)
                            {
                                max = alpha * minbyzi + (1 - alpha) * minbyzj;
                                vcurrent[i0][getGlobalStateIndex(s.getLocalStates())] = minbyzi;
                                vcurrent[j0][getGlobalStateIndex(s.getLocalStates())] = minbyzj;
                            }
                        }//maxrat calculated
                         // vcurrent[i0][getGlobalStateIndex(s.getLocalStates())] = max;//v^k_i(s)=maxminE
                         // ucurrent[i][getGlobalStateIndex(s.getLocalStates())] = exp;//v^k_i(s)=maxminE

                        bybool[i0] = false; //remove byzantine role from i
                        bybool[j0] = false; //remove byzantine role from i
                        vprev[i0]  = vcurrent[i0];
                        vprev[j0]  = vcurrent[j0];
                    }
                });
                double[] bigDelta = new double[n];
                double   ep1, ep2;
                int      countfail = 0;
                int      countpass = 0;
                double   distance  = 0;
                double   minxy     = 20;

                bigDelta[ratlist[0]] = vcurrent[ratlist[0]][getGlobalStateIndex(gstates[0].getLocalStates())] - ucurrent[ratlist[0]][getGlobalStateIndex(gstates[0].getLocalStates())];
                bigDelta[ratlist[1]] = vcurrent[ratlist[1]][getGlobalStateIndex(gstates[0].getLocalStates())] - ucurrent[ratlist[1]][getGlobalStateIndex(gstates[0].getLocalStates())];
                double epi1, epi2, epj1, epj2;
                epi1 = bigDelta[ratlist[0]] - 2 * EiK;
                epi2 = bigDelta[ratlist[0]] + 2 * EiK;
                epj1 = bigDelta[ratlist[1]] - 2 * EiK;
                epj2 = bigDelta[ratlist[1]] + 2 * EiK;


                if ((epi1 >= 0 && epj1 > eps) || (epj1 >= 0 && epi1 > eps))
                {
                    return("unstable");
                }
                else if (epi2 <= eps && epj2 <= eps)
                {
                    return("stable with eps" + eps);
                }
                else if (epi1 <= eps && epj1 <= eps)
                {
                    return("stable with epsdel" + (eps + delta));//if (eps <= epi2 || eps <= epj2)
                }
                else
                {
                    distance = Math.Sqrt(Math.Pow(epi2 - eps, 2) + Math.Pow(epj2 - eps, 2));
                    if (distance < min_distance)
                    {
                        min_distance = distance;
                        deltastar    = Math.Min(Math.Abs(epi2 - eps), Math.Abs(epj2 - eps));
                    }
                    result = "undecided";
                }

                alpha += 0.2;
                if (alpha >= 1)
                {
                    return("NEepsdelta*" + " " + deltastar);
                }
            }


            // System.Diagnostics.Debug.WriteLine(result);
            return(result);
        }
Exemplo n.º 5
0
        private static void reward_calc(List <double>[] prev, List <double>[] current, int i, bool biz)
        {
            /* Parallel.ForEach(gstates, (s) =>
             * {*/
            Parallel.For(0, gstates.Count, index =>
            {
                if (_debug)
                {
                    Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
                }
                GlobalState s = gstates[(index * 128) % gstates.Count];
                //GlobalState s = gstates[(index * 128)%59049];
                double max = 0;
                double exp = 0;
                foreach (int a_i in localactionset[i])//for each action of rational i
                {
                    double minbyz = 20;
                    if (byzAc.Count == 0)// if no byzantines
                    {
                        double reward = 0;
                        // double ureward = 0;
                        foreach (GlobalAction a_al in AlrAc)//for each altruistic action Al-i
                        {
                            setglobalAction(i, a_i, a_al);
                            if (BT(s, Actions, gstates) != null)
                            {
                                GlobalState snext = BT(s, Actions, gstates);
                                reward           += GlobalPr(s, Actions) * (H(i, s, Actions) + beta * prev[i][getGlobalStateIndex(snext.getLocalStates())]);
                                //    ureward += GlobalPr(s, Actions) * (H(i, s, Actions) + beta * uprev[i][getGlobalStateIndex(snext.getLocalStates())]);
                            }
                        }//expected reward calculated for a_z,a_i
                        minbyz = reward;
                    }
                    else
                    {
                        foreach (GlobalAction a_z in byzAc)//for each byz action Z
                        {
                            double reward = 0;
                            // double ureward = 0;
                            foreach (GlobalAction a_al in AlrAc)//for each altruistic action Al-i
                            {
                                setglobalAction(i, a_i, a_z, a_al);
                                if (BT(s, Actions, gstates) != null)
                                {
                                    GlobalState snext = BT(s, Actions, gstates);
                                    reward           += GlobalPr(s, Actions) * (H(i, s, Actions) + beta * prev[i][getGlobalStateIndex(snext.getLocalStates())]);
                                    //    ureward += GlobalPr(s, Actions) * (H(i, s, Actions) + beta * uprev[i][getGlobalStateIndex(snext.getLocalStates())]);
                                }
                            }//expected reward calculated for a_z,a_i
                            if (reward < minbyz)
                            {
                                minbyz = reward;
                            }
                            //     System.Diagnostics.Debug.WriteLine("reward =" + minbyz);
                        }//minbyz calculated
                    }


                    if (minbyz > max)
                    {
                        max = minbyz;
                    }
                    exp += Pr(i, s, Actions) * minbyz;
                }//maxrat calculated
                if (biz)
                {
                    current[i][getGlobalStateIndex(s.getLocalStates())] = max;//v^k_i(s)=maxminE
                }
                else
                {
                    current[i][getGlobalStateIndex(s.getLocalStates())] = exp;//v^k_i(s)=maxminE
                }
            });
        }