D_rawest16

GeneticAlgorithm

Feb 7th, 2021 (edited)
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. -- // VARIABLES \\ --
  2. local original = script.Parent.Part
  3. local spawnarea = script.Parent.SpawnArea
  4. local pos = spawnarea.CFrame
  5. local size = spawnarea.Size/2
  6. local RunService = game:GetService("RunService")
  7. local EndPos = script.Parent.EndPos.Position
  8.  
  9. -- // PHYSICS \\ --
  10. local physics = game:GetService("PhysicsService")
  11. local modelgroup = physics:CreateCollisionGroup('p')
  12. physics:CollisionGroupSetCollidable('p','p',false)
  13.  
  14. -- // MODULES \\ --
  15. local MutationRate = 0.1
  16. local amount = 100
  17. local Genes = {}
  18. local parts = {}
  19. local API = {}
  20. local lifeTime = 250
  21.  
  22. local num = 1
  23.  
  24. local pool = {}
  25.  
  26. local TopGene,SecGene
  27.  
  28. function rad()
  29.     return math.rad(math.random(-180,180))
  30. end
  31. function randomAngle()
  32.      return CFrame.Angles(rad(),rad(),rad())
  33. end
  34. API.AddForce = function(v)
  35.     -- // ADDING BODY MOVERS \\ --
  36.    
  37.     local force = Instance.new("BodyVelocity")
  38.     local gyro = Instance.new("BodyGyro")
  39.    
  40.     force.MaxForce = Vector3.new(5e4,5e4,5e4)
  41.     gyro.MaxTorque = Vector3.new(5e5,5e5,5e5)
  42.    
  43.     gyro.D = 200
  44.     force.Parent = v
  45.     gyro.Parent = v
  46. end
  47. -- // SPAWNING THEM \\ --
  48. for i = 1,amount do
  49.     local clone = original:Clone()
  50.    
  51.     clone.CFrame = pos * randomAngle()
  52.    
  53.     clone.Parent = workspace.NaturalSelection
  54.     physics:SetPartCollisionGroup(clone,'p')
  55.     API.AddForce(clone)
  56.    
  57.     Genes[clone] = {}
  58.     table.insert(parts,clone)
  59. end
  60.  
  61.  
  62. API.Fitness = function(part)
  63.     -- // GETTING FITNESS \\ --
  64.    
  65.     local FitnessLevel
  66.     local mag = (part.Position - EndPos).Magnitude
  67.    
  68.    
  69.     FitnessLevel = (1/mag)
  70.    
  71.     return FitnessLevel
  72. end
  73.  
  74. API.ShowPath = function(path)
  75.     -- // SHOWING PATH SOMETIMG TOOK \\ --
  76.    
  77.     for i,v in pairs(path) do
  78.         local part = Instance.new("Part")
  79.         part.Anchored = true
  80.         part.BrickColor = BrickColor.new("Really red")
  81.         part.Size = Vector3.new(0.2,0.2,0.2)
  82.         part.Material = Enum.Material.Neon
  83.         part.CFrame = v
  84.         part.CanCollide = false
  85.        
  86.         part.Parent = workspace.MoveFolder
  87.     end
  88.     delay(3,function()
  89.         for i,v in pairs(game.Workspace.MoveFolder:GetChildren()) do v:Destroy() end
  90.     end)
  91. end
  92. API.CrossGenes = function(v)
  93.     -- // CROSSING 2 GENES \\ --
  94.     Genes[v] = {}
  95.     local parent1 = pool[math.random(1,#pool)]
  96.     local parent2 = pool[math.random(1,#pool)]
  97.    
  98.     for i = 1,#parent1 do
  99.         if i >= #parent1/2 then
  100.             Genes[v][i] = parent2[i]
  101.         else
  102.             Genes[v][i] = parent1[i]
  103.         end
  104.     end
  105.     --API.ShowPath(Genes[v])
  106. end
  107. API.Reset = function()
  108.     local HighestFitness
  109.     local topgene
  110.    
  111.     for i = 1,#parts do
  112.        
  113.         local v = parts[i]
  114.         local CurrentFitness = API.Fitness(v)
  115.        
  116.         -- // GETTING TOP GENE \\ --
  117.         if not HighestFitness or CurrentFitness > HighestFitness then
  118.             HighestFitness = CurrentFitness
  119.             TopGene = Genes[v]
  120.         end
  121.         -- // ADDING AVERAGE \\ --
  122.        
  123.         for i = 1,CurrentFitness*100 do
  124.             table.insert(pool,Genes[v])
  125.         end
  126.         -- // FULL RESETTING \\ --
  127.         v.CFrame = pos * randomAngle()
  128.     end
  129.    
  130.     -- // SHOWING PATH \\ --
  131.     API.ShowPath(TopGene)
  132.    
  133.     -- // MULTIPLYING TOP GENE \\ --
  134.     for i = 1,HighestFitness*250 do
  135.         table.insert(pool,TopGene)
  136.     end
  137.     for i = 1,#parts do
  138.         API.CrossGenes(parts[i])
  139.     end
  140.    
  141.     num = 0
  142. end
  143.  
  144. local db = false
  145.  
  146. local function StartMovement()
  147.     if num <= lifeTime then
  148.         num = num + 1
  149.     else
  150.         if not db then
  151.             db = true
  152.             API.Reset()
  153.             db = false
  154.            
  155.         end
  156.         return
  157.     end
  158.    
  159.     for i = 1,#parts do
  160.         local v = parts[i]
  161.         local gyro = v.BodyGyro
  162.         local force = v.BodyVelocity
  163.        
  164.        
  165.        
  166.         if not pool[1] then
  167.             gyro.CFrame = CFrame.new(v.Position) * randomAngle()
  168.         elseif Genes[v][num] then
  169.             if math.random(1,10)/10 <= MutationRate then
  170.                 -- // MUTATION \\ --
  171.                 gyro.P = 2e4
  172.                 gyro.CFrame = CFrame.new(v.Position) * randomAngle()
  173.             else
  174.                 -- // NO MUTATION \\ --
  175.                 gyro.P = 5e3
  176.                 gyro.CFrame = Genes[v][num]
  177.             end
  178.         end
  179.         force.Velocity = v.CFrame.LookVector * 8
  180.        
  181.         table.insert(Genes[v],num,gyro.CFrame)
  182.     end
  183. end
  184. RunService.Heartbeat:Connect(StartMovement)
  185.  
Add Comment
Please, Sign In to add comment