public void addPlayer(Player player)
 {
     if (player.Materials.Length > materials.Length)
     {
         throw new InputException("Wrong input, materials have different length.");
     }
     players.Add(player);
     materials = materials.Zip(player.Materials, (x, y) => x + y).ToArray<int>();
     maximumCalculated = false;
 }
 public void init(Player[] players, int m)
 {
     this.players = players;
     this.n = players.Length;
     l = new Coalition[n][];
     var combinations = getCombinations(n, players, m);
     for (var i = 0; i < n; i++)
     {
         l[i] = combinations[i].ToArray();
     }
     coalitionStructuresFound = new List<Coalition[]>();
 }
示例#3
0
        public void CoalitionMaxTest()
        {
            Coalition     c  = new Coalition(5);
            ValueFunction vf = new ValueFunction();

            int[] materials = new int[5];
            int   max       = 0;

            materials[0] = 1;
            materials[1] = 5;
            materials[2] = 3;
            materials[3] = 8;
            materials[4] = 4;

            /*int[] materials2 = new int[3];
             * materials[0] = 1;
             * materials[1] = 3;
             * materials[2] = 5; */
            /*int[] materials3 = new int[4];
             * materials[0] = 10;
             * materials[1] = 9;
             * materials[2] = 3;
             * materials[3] = 4;*/
            Dynamic_Games.coop.models.Player p1 = new Dynamic_Games.coop.models.Player(vf, materials);
            //Dynamic_Games.coop.models.Player p2 = new Dynamic_Games.coop.models.Player();
            //Dynamic_Games.coop.models.Player p3 = new Dynamic_Games.coop.models.Player();
            c.addPlayer(p1);
            //c.addPlayer(p2);
            //c.addPlayer(p3);
            p1.Materials = materials;
            //p2.Materials = materials2;
            //p3.Materials = materials3;

            for (int i = 0; i < materials.Length; i++)
            {
                if (materials[i] > max)
                {
                    max = materials[i];
                }
            }
            int res = c.calculateMaximumValue();

            Assert.AreEqual(max, res);
        }
        public void CoalitionMaxTest()
        {
            Coalition c = new Coalition(5);
            ValueFunction vf = new ValueFunction();
            int[] materials = new int[5];
            int max = 0;
            materials[0] = 1;
            materials[1] = 5;
            materials[2] = 3;
            materials[3] = 8;
            materials[4] = 4;
            /*int[] materials2 = new int[3];
            materials[0] = 1;
            materials[1] = 3;
            materials[2] = 5; */
            /*int[] materials3 = new int[4];
            materials[0] = 10;
            materials[1] = 9;
            materials[2] = 3;
            materials[3] = 4;*/
            Dynamic_Games.coop.models.Player p1 = new Dynamic_Games.coop.models.Player(vf, materials);
            //Dynamic_Games.coop.models.Player p2 = new Dynamic_Games.coop.models.Player();
            //Dynamic_Games.coop.models.Player p3 = new Dynamic_Games.coop.models.Player();
            c.addPlayer(p1);
            //c.addPlayer(p2);
            //c.addPlayer(p3);
            p1.Materials = materials;
            //p2.Materials = materials2;
            //p3.Materials = materials3;

            for (int i = 0; i < materials.Length; i++)
            {
                if (materials[i] > max)
                {
                    max = materials[i];
                }
            }
            int res = c.calculateMaximumValue();
            Assert.AreEqual(max, res);
        }
 public void removePlayer(Player player)
 {
     players.Remove(player);
     materials = materials.Zip(player.Materials, (x, y) => x - y).ToArray<int>();
     maximumCalculated = false;
 }
        private void init(String filename)
        {
            try
            {
                using (TextReader reader = File.OpenText(filename))
                {
                    string text = reader.ReadLine();
                    string[] bits = text.Split(' ');
                    int n = int.Parse(bits[0]);
                    int m = int.Parse(bits[1]);
                    Player[] players = new Player[n];
                    for (var i = 0; i < n; i++)
                    {
                        players[i] = new Player();
                        players[i].Name = "P" + i;
                        text = reader.ReadLine();
                        players[i].ValueFunction = new ValueFunction(text);
                    }
                    for (var i = 0; i < n; i++)
                    {
                        text = reader.ReadLine();
                        bits = text.Split(' ');
                        var materials = new int[m];
                        for (var j = 0; j < m; j++)
                        {
                            materials[j] = int.Parse(bits[j]);
                        }
                        players[i].Materials = materials;
                    }

                    calculator.init(players, m);
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
        public void init(string[] players, int[][] materials)
        {
            Player[] agents = new Player[players.Length];
            if (players.Length != materials.Length)
            {
                throw new InputException("False number of materials or players");
            }
            int m = materials[0].Length;
            for (var i = 0; i < players.Length; i++)
            {
                if (materials[i].Length != m)
                {
                    throw new InputException("False materials");
                }
                agents[i] = new Player(new ValueFunction(players[i]), materials[i]);
                agents[i].Name = Player.prefix + (i + 1);
                agents[i].ValueFunction.getValue(agents[i].Materials);
            }

            selectCalculator();
            calculator.init(agents, materials[0].Length);
            calculatorThread = new Thread(calculator.start);
            calculatorThread.Start();
        }
 //calculate l, combinations of Coalitions
 private List<Coalition>[] getCombinations(int n, Player[] players, int m)
 {
     var result = new List<Coalition>[n];
     var stack = new int[n];
     for (int i = 1; i <= n; i++)
     {
         stack[0] = -1;
         result[i - 1] = new List<Coalition>();
         int l = 0;
         while (l >= 0)
         {
             stack[l]++;
             if (stack[l] >= n)
             {
                 l--;
             }
             else
             {
                 if (l == i - 1)
                 {
                     Coalition coalition = new Coalition(m);
                     for (var j = 0; j < i; j++)
                     {
                         coalition.addPlayer(players[stack[j]]);
                     }
                     result[i - 1].Add(coalition);
                 }
                 else
                 {
                     l++;
                     stack[l] = stack[l - 1];
                 }
             }
         }
     }
     return result;
 }
 //calculate MSet, MSet[i] contains all possible coalition at ith level of searching
 // g number partitions, k level, gk length of coalition, a: players at k level, alpha: lower bound
 private List<Coalition> getMSet(int[] g, int k, int gk, Player[] a, int alpha)
 {
     List<Coalition> result = new List<Coalition>();
     var end = n + 1;
     for (var i = 0; i <= k; i++)
         end = end - g[i]; //calculate upper bound
     for (var i = alpha; i <= end; i++)
     {
         foreach (Coalition c in l[gk]) //every gk long coalition
         {
             if (c.Players[0] == a[i - 1]) //first player is in current available players
             {
                 Boolean correct = true;
                 foreach (var p in c.Players)
                 {
                     if (!a.Contains(p)) correct = false;
                 }
                 if (correct)
                 {
                     result.Add(c);
                 }
             }
         }
     }
     return result;
 }