Advertisement
ItsNoah

lua genetic algorithm

Feb 6th, 2023
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. function table_print (tt, indent, done)
  2.   done = done or {}
  3.   indent = indent or 0
  4.   if type(tt) == "table" then
  5.     local sb = {}
  6.     for key, value in pairs (tt) do
  7.       table.insert(sb, string.rep (" ", indent)) -- indent it
  8.       if type (value) == "table" and not done [value] then
  9.         done [value] = true
  10.         table.insert(sb, key .. " = {\n");
  11.         table.insert(sb, table_print (value, indent + 2, done))
  12.         table.insert(sb, string.rep (" ", indent)) -- indent it
  13.         table.insert(sb, "}\n");
  14.       elseif "number" == type(key) then
  15.         table.insert(sb, string.format("\"%s\"\n", tostring(value)))
  16.       else
  17.         table.insert(sb, string.format(
  18.             "%s = \"%s\"\n", tostring (key), tostring(value)))
  19.        end
  20.     end
  21.     return table.concat(sb)
  22.   else
  23.     return tt .. "\n"
  24.   end
  25. end
  26.  
  27. function to_string( tbl )
  28.     if  "nil"       == type( tbl ) then
  29.         return tostring(nil)
  30.     elseif  "table" == type( tbl ) then
  31.         return table_print(tbl)
  32.     elseif  "string" == type( tbl ) then
  33.         return tbl
  34.     else
  35.         return tostring(tbl)
  36.     end
  37. end
  38.  
  39. function create_population(pop_size, gene_len)
  40.   local population = {}
  41.   for i = 1, pop_size do
  42.     local individual = {}
  43.     for j = 1, gene_len do
  44.       individual[j] = math.random(0, 1)
  45.     end
  46.     population[i] = individual
  47.   end
  48.   return population
  49. end
  50.  
  51. function evaluate_fitness(pop)
  52.   local fitness = {}
  53.   for i, ind in ipairs(pop) do
  54.     local sum = 0
  55.     for j, gene in ipairs(ind) do
  56.       sum = sum + gene
  57.     end
  58.     fitness[i] = sum
  59.   end
  60.   return fitness
  61. end
  62.  
  63. function select_parents(pop, fit)
  64.   local parents = {}
  65.   for i = 1, #pop do
  66.     local p1 = math.random(1, #pop)
  67.     local p2 = math.random(1, #pop)
  68.     if fit[p1] > fit[p2] then
  69.       parents[i] = pop[p1]
  70.     else
  71.       parents[i] = pop[p2]
  72.     end
  73.   end
  74.   return parents
  75. end
  76.  
  77. function crossover(par, pop_size, gene_len)
  78.   local offspring = {}
  79.   for i = 1, pop_size, 2 do
  80.     local p1 = par[i]
  81.     local p2 = par[i+1]
  82.     local o1 = {}
  83.     local o2 = {}
  84.     for j = 1, gene_len do
  85.       if math.random(0, 1) == 0 then
  86.         o1[j] = p1[j]
  87.         o2[j] = p2[j]
  88.       else
  89.         o1[j] = p2[j]
  90.         o2[j] = p1[j]
  91.       end
  92.     end
  93.     offspring[i] = o1
  94.     offspring[i+1] = o2
  95.   end
  96.   return offspring
  97. end
  98.  
  99. function mutation(off, mut_rate, gene_len)
  100.   for i, ind in ipairs(off) do
  101.     for j = 1, gene_len do
  102.       if math.random() < mut_rate then
  103.         ind[j] = 1 - ind[j]
  104.       end
  105.     end
  106.   end
  107.   return off
  108. end
  109.  
  110. function genetic_algorithm(pop_size, gene_len, mut_rate, generations)
  111.   local pop = create_population(pop_size, gene_len)
  112.   for i = 1, generations do
  113.     local fit = evaluate_fitness(pop)
  114.     local par = select_parents(pop, fit)
  115.     local off = crossover(par, pop_size, gene_len)
  116.     pop = mutation(off, mut_rate, gene_len)
  117.   end
  118.   return pop
  119. end
  120.  
  121. result = genetic_algorithm(8, 4, 0.01, 100)
  122.  
  123. print(to_string(result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement