[SOLVED] Best Balance calculate

Post here if you want to help developing something for FAF.

[SOLVED] Best Balance calculate

Postby Xinnony » 13 Jan 2014, 14:52

Hi, i need your help for finish my script for calculate the best rating with 2v2 players (for the moment).

Spoiler: show
Code: Select all
--

function joinMyTables(t1, t2)
  for k,v in ipairs(t2) do
    table.insert(t1, v)
  end
  return t1
end

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

local function permutation(a, n, cb)
  if n == 0 then
      cb(a)
   else
      for i = 1, n do
         a[i], a[n] = a[n], a[i]
      print(i)
         permutation(a, n - 1, cb)
         a[i], a[n] = a[n], a[i]
      end
   end
end

--Usage
local function callback(a, i)
   print('{'..table.concat(a, ', ')..'}')
  --
  local TotalA = a[1]+a[2]
  local TotalB = a[3]+a[4]
  local Total = TotalA+TotalB
  local PurcentA = ((TotalA)*100)/Total
  local PurcentB = ((TotalB)*100)/Total
  local PurcentRatio = ''
  if TotalA < TotalB then
     PurcentRatio = (TotalA/TotalB)*100
   else
      PurcentRatio = (TotalB/TotalA)*100
   end
  if maxx < PurcentRatio then
    maxx = PurcentRatio
    --max_index = i -- PROBLEM HERE, 'i' return Nil
  end
  --
  print('TotalA : '..TotalA..' / TotalB : '..TotalB)
  print('Total : '..Total)
  print('A : '..round(PurcentA)..'% / B : '..round(PurcentB)..'%')
  print('Ratio : '..round(PurcentRatio)..'%')
  print('')
end

--

local tableA = {}                                 -- Team A
local tableB = {}                                 -- Team B
local number = 4                                  -- Number of Player
maxx = 0                                          -- Max rating
max_index = ''                                    -- Index du meilleur rating
i = 0

tableA[0] = {10, 2}                               -- DEV
tableA[1] = {'A', 'B'}                            -- PlayerName
tableB[0] = {5, 5}                                -- DEV
tableB[1] = {'C', 'D'}                            -- PlayerName

local tableAB = {}
tableAB[0] = joinMyTables(tableA[0], tableB[0])   -- ALL DEV
tableAB[1] = joinMyTables(tableA[1], tableB[1])   -- ALL PlayerName

--

permutation(tableAB[0], number, callback)
print('Meilleur rating : '..round(maxx, 2)..'%')
print('Index du meilleur rating : '..max_index..'NEED INFO HERE')

--


You can test my script here : http://www.compileonline.com/execute_lua_online.php
Last edited by Xinnony on 13 Jan 2014, 16:49, edited 2 times in total.
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony

Re: Best Balance calculate

Postby Lame » 13 Jan 2014, 15:26

what exactly is the point of this?

the bets way to balance 2 vs 2 is always #1 and #4 vs #2,#3
Lame
Banned
 
Posts: 189
Joined: 05 Jul 2013, 21:38
Has liked: 13 times
Been liked: 10 times
FAF User Name: Lame

Re: Best Balance calculate

Postby Xinnony » 13 Jan 2014, 15:30

Lame wrote:what exactly is the point of this?

the bets way to balance 2 vs 2 is always #1 and #4 vs #2,#3


If #1&#4 vs #2&#3 have better Game Quality to #1&#2 vs #3&#4.

New change :

Spoiler: show
Code: Select all
--

function joinMyTables(t1, t2)
  for k,v in ipairs(t2) do
    table.insert(t1, v)
  end
  return t1
end

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

local function permutation(a, n, cb)
  if n == 0 then
      cb(a)
   else
      for i = 1, n do
         a[i], a[n] = a[n], a[i]
         permutation(a, n - 1, cb)
         a[i], a[n] = a[n], a[i]
      end
   end
end

--Usage
local function callback(a, i)
   print('{'..table.concat(a, ', ')..'}')
  --
  index = index + 1
  print('Index : '..index)
  local TotalA = a[1]+a[2]
  tableA[2] = TotalA
  local TotalB = a[3]+a[4]
  tableB[2] = TotalB
  local Total = TotalA+TotalB
  tableAB[2] = Total
  local PurcentA = ((TotalA)*100)/Total
  tableA[3] = PurcentA
  local PurcentB = ((TotalB)*100)/Total
  tableB[3] = PurcentB
  local PurcentRatio = ''
  if TotalA < TotalB then
     PurcentRatio = (TotalA/TotalB)*100
   else
      PurcentRatio = (TotalB/TotalA)*100
   end
  tableAB[3] = PurcentRatio
  if maxx < PurcentRatio then
    maxx = PurcentRatio
    if table.concat(a) == table.concat(tableAB[0]) then
      print('ALL DEV : '..tableAB[0][1]..tableAB[0][2]..tableAB[0][3]..tableAB[0][4])
    end
    --max_index = i -- PROBLEM HERE, 'i' return Nil
  end
  --
  --print('TotalA : '..TotalA..' / TotalB : '..TotalB)
  --print('Total : '..Total)
  print('A : '..round(PurcentA)..'% / B : '..round(PurcentB)..'%')
  print('Ratio : '..round(PurcentRatio)..'%')
  print('')
end

--

tableA = {}                                       -- Team A
tableB = {{}}                                       -- Team B
number = 4                                        -- Number of Player
maxx = 0                                          -- Max rating
max_index = ''                                    -- Index du meilleur rating
index = 0
i = 0

tableA[0] = {10, 2}                               -- DEV
tableA[1] = {'A', 'B'}                            -- PlayerName
--tableA[2]                                       -- Total A
--tableA[3]                                       -- % A

tableB[0] = {5, 5}                                -- DEV
tableB[1] = {'C', 'D'}                            -- PlayerName
--tableB[2]                                       -- Total B
--tableB[3]                                       -- % B

tableAB = {}
tableAB[0] = joinMyTables(tableA[0], tableB[0])   -- ALL DEV
tableAB[1] = joinMyTables(tableA[1], tableB[1])   -- ALL PlayerName
--tableAB[2]                                      -- Total
--tableAB[3]                                       -- % Best Rating

--

permutation(tableAB[0], number, callback)
print('Meilleur rating : '..round(maxx, 2)..'%')
print('Index du meilleur rating : '..max_index..'NEED INFO HERE')

--
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony

Re: Best Balance calculate

Postby Xinnony » 13 Jan 2014, 15:42

Soon finished ...

Edit :
Spoiler: show
Code: Select all
--

function joinMyTables(t1, t2)
  for k,v in ipairs(t2) do
    table.insert(t1, v)
  end
  return t1
end

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

local function permutation(a, n, cb)
  if n == 0 then
      cb(a)
   else
      for i = 1, n do
         a[i], a[n] = a[n], a[i]
         permutation(a, n - 1, cb)
         a[i], a[n] = a[n], a[i]
      end
   end
end

--Usage
local function callback(a)
  index = index + 1
  local TotalA = a[1]+a[2]
  tableA[2] = TotalA
  local TotalB = a[3]+a[4]
  tableB[2] = TotalB
  local Total = TotalA+TotalB
  tableAB[2] = Total
  local PurcentA = ((TotalA)*100)/Total
  tableA[3] = PurcentA
  local PurcentB = ((TotalB)*100)/Total
  tableB[3] = PurcentB
  local PurcentRatio = ''
  if TotalA < TotalB then
     PurcentRatio = (TotalA/TotalB)*100
   else
      PurcentRatio = (TotalB/TotalA)*100
   end
  tableAB[3] = PurcentRatio
  --
  if maxx < PurcentRatio then
    print('{'..table.concat(a, ', ')..'}')
    print('Index : '..index)
    if table.concat(a, '.') == table.concat(tableAB[0], '.') then
      best_dev_permut = table.concat(tableAB[0], '.')
      print('ALL DEV : '..best_dev_permut)
      max_index = index
      maxx = PurcentRatio
      --print('ALL DEV : '..tableAB[0][1]..tableAB[0][2]..tableAB[0][3]..tableAB[0][4])
    end
    --print('TotalA : '..TotalA..' / TotalB : '..TotalB)
    --print('Total : '..Total)
    print('A : '..round(PurcentA)..'% / B : '..round(PurcentB)..'%')
    print('Ratio : '..round(PurcentRatio)..'%')
    print('')
  end
end

--

tableA = {}                                       -- Team A
tableB = {}                                       -- Team B
number = 4                                        -- Number of Player
maxx = 0                                          -- Max rating
best_dev_permut = ''                              -- Best permut
max_index = ''                                    -- Index best rating
index = 0

tableA[0] = {10, 2}                               -- DEV
tableA[1] = {'A', 'B'}                            -- PlayerName
--tableA[2]                                       -- Total A
--tableA[3]                                       -- % A

tableB[0] = {5, 5}                                -- DEV
tableB[1] = {'C', 'D'}                            -- PlayerName
--tableB[2]                                       -- Total B
--tableB[3]                                       -- % B

tableAB = {}
tableAB[0] = joinMyTables(tableA[0], tableB[0])   -- ALL DEV
tableAB[1] = joinMyTables(tableA[1], tableB[1])   -- ALL PlayerName
--tableAB[2]                                      -- Total
--tableAB[3]                                       -- % Best Rating

--

permutation(tableAB[0], number, callback)
print('Best rating : '..round(maxx, 2)..'%')
print('Best permut : '..best_dev_permut)
print('Index best rating : '..max_index)

--


Result :
Code: Select all
{2, 5, 5, 10}
Index : 1
ALL DEV : 2.5.5.10
A : 32% / B : 68%
Ratio : 47%

{5, 5, 2, 10}
Index : 3
ALL DEV : 5.5.2.10
A : 45% / B : 55%
Ratio : 83%

Best rating : 83.33%
Best permut : 5.5.2.10
Index best rating : 3
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony

Re: Best Balance calculate

Postby Lame » 13 Jan 2014, 15:48

why do you need that script?

just sort players by rating and let the highest and lowest team up
Lame
Banned
 
Posts: 189
Joined: 05 Jul 2013, 21:38
Has liked: 13 times
Been liked: 10 times
FAF User Name: Lame

Re: Best Balance calculate

Postby Xinnony » 13 Jan 2014, 15:58

For create a function to move all player for have the best balance in two team.

Spoiler: show
Code: Select all
--

function joinMyTables(t1, t2)
  for k,v in ipairs(t2) do
    table.insert(t1, v)
  end
  return t1
end

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

local function permutation(a, n, cb)
  if n == 0 then
      cb(a)
   else
      for i = 1, n do
         a[i], a[n] = a[n], a[i]
         permutation(a, n - 1, cb)
         a[i], a[n] = a[n], a[i]
      end
   end
end

--Usage
local function callback(a)
  index = index + 1
  local TotalA = a[1]+a[2]
  table_A[2] = TotalA
  local TotalB = a[3]+a[4]
  table_B[2] = TotalB
  local Total = TotalA+TotalB
  tableAB[2] = Total
  local PurcentA = ((TotalA)*100)/Total
  table_A[3] = PurcentA
  local PurcentB = ((TotalB)*100)/Total
  table_B[3] = PurcentB
  local PurcentRatio = ''
  if TotalA < TotalB then
     PurcentRatio = (TotalA/TotalB)*100
   else
      PurcentRatio = (TotalB/TotalA)*100
   end
  tableAB[3] = PurcentRatio
  --
  if maxx < PurcentRatio then
    print('{'..table.concat(a, ', ')..'}')
    print('Index : '..index)
    if table.concat(a, '.') == table.concat(tableAB[0], '.') then
      best_dev_permut_A = table.concat(table_A[0], '.')
      best_dev_permut_B = table.concat(table_B[0], '.')
      best_dev_permutAB = table.concat(tableAB[0], '.')
      print('A DEV : '..best_dev_permut_A..' / Name : '..table.concat(table_A[1], '.'))
      print('B DEV : '..best_dev_permut_B..' / Name : '..table.concat(table_B[1], '.'))
      print('ALL DEV : '..best_dev_permutAB)
      max_index = index
      maxx = PurcentRatio
      --print('ALL DEV : '..tableAB[0][1]..tableAB[0][2]..tableAB[0][3]..tableAB[0][4])
    end
    --print('TotalA : '..TotalA..' / TotalB : '..TotalB)
    --print('Total : '..Total)
    print('A : '..round(PurcentA)..'% / B : '..round(PurcentB)..'%')
    print('Ratio : '..round(PurcentRatio)..'%')
    print('')
  end
end

--

table_A = {}                                       -- Team A
table_B = {}                                       -- Team B
number = 4                                        -- Number of Player
maxx = 0                                          -- Max rating
best_dev_permut_A = ''                             -- Best permut A
best_dev_permut_B = ''                             -- Best permut B
best_dev_permutAB = ''                            -- Best permut AB
max_index = ''                                    -- Index best rating
index = 0

table_A[0] = {10, 2}                               -- DEV
table_A[1] = {'A', 'B'}                            -- PlayerName
--table_A[2]                                       -- Total A
--table_A[3]                                       -- % A

table_B[0] = {5, 5}                                -- DEV
table_B[1] = {'C', 'D'}                            -- PlayerName
--table_B[2]                                       -- Total B
--table_B[3]                                       -- % B

tableAB = {}
tableAB[0] = joinMyTables(table_A[0], table_B[0])   -- ALL DEV
tableAB[1] = joinMyTables(table_A[1], table_B[1])   -- ALL PlayerName
--tableAB[2]                                      -- Total
--tableAB[3]                                       -- % Best Rating

--

permutation(tableAB[0], number, callback)
print('Best rating : '..round(maxx, 2)..'%')
print('Best permut A : '..best_dev_permut_A)
print('Best permut B : '..best_dev_permut_B)
print('Best permut AB : '..best_dev_permutAB)
print('Index best rating : '..max_index)

--


Now i have a problem, my script return :
Code: Select all
{2, 5, 5, 10}
Index : 1
A DEV : 2.5.5.10 / Name : A.B.C.D
B DEV : 5.5 / Name : C.D
ALL DEV : 2.5.5.10
A : 32% / B : 68%
Ratio : 47%

{5, 5, 2, 10}
Index : 3
A DEV : 5.5.2.10 / Name : A.B.C.D
B DEV : 5.5 / Name : C.D
ALL DEV : 5.5.2.10
A : 45% / B : 55%
Ratio : 83%

Best rating : 83.33%
Best permut A : 5.5.2.10
Best permut B : 5.5
Best permut AB : 5.5.2.10
Index best rating : 3


But the first 'A DEV' normaly return just '2.5' and 'B DEV' return '5.10' normally.

Edit : I find the problem.
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony

Re: Best Balance calculate

Postby Xinnony » 13 Jan 2014, 16:48

Not needed ultimately helps

Script finish !

Speed :
6,3s for 6v6
5,4s for 5v5
1,6s for 4v4
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony

Re: [SOLVED] Best Balance calculate

Postby SC-Account » 23 Jan 2014, 06:40

Wait.. you are doing a brute force algorithm trying out all combinations and select the max? Why would you do that? Should run in like 0.01 seconds if you just put lowest & highest in one team...
SC-Account
Evaluator
 
Posts: 541
Joined: 07 Feb 2013, 05:10
Location: Colonial district of Germany
Has liked: 48 times
Been liked: 77 times
FAF User Name: Tnuoccacs

Re: [SOLVED] Best Balance calculate

Postby Xinnony » 23 Jan 2014, 18:07

I rework my script

Permutation is not fast (6.3s 6v6)

Now i use Combination/Iteration, 1 second for 100.000 run :)
User avatar
Xinnony
Contributor
 
Posts: 551
Joined: 19 Feb 2012, 02:49
Has liked: 70 times
Been liked: 34 times
FAF User Name: Xinnony


Return to Contributors

Who is online

Users browsing this forum: No registered users and 1 guest