Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This genetic algorithm is kinda silly, but it makes an "imaginary" house where all the materials have its own strength which will translate to fitness but, materials require structure so it requires nails or tape. So the algorithm will try to make a balance of all materials for the best possible score.
- #Make a script called whatever you want, and use following.
- import Population
- import time
- import math
- GeneLen = 49
- PopulationSize = 25
- MutationRate = 0.01
- Population = Population.Population(PopulationSize, MutationRate, GeneLen)
- Score = 0
- Materials = None
- t = time.time()
- for i in range(25):
- Population.NaturalSelection()
- Population.NextGen()
- Population.CalcFitness()
- Score, Materials = Population.BestIndevidual()
- print( "Avarage:"+str(Population.Avarage),"Generation:"+str(Population.Gen))
- print("")
- print("_______________Result________________")
- print("Generation:"+str(Population.Gen))
- print("Worst Score:"+str(Population.WorstScore))
- print("Avarage:"+str(Population.Avarage))
- print("Best Score:"+str(Score), "Record:"+str(Population.Record))
- print("Time Taken(Seconds):"+str(math.floor(time.time()-t)))
- print("_______________DATA________________")
- print("Generation Size:"+str(PopulationSize))
- print("Mutation Rate(%):"+str(MutationRate*100))
- print("Material Slots:"+str(GeneLen))
- print("_______________Materials________________")
- MaterialsInfo = Materials.Content()
- for i in MaterialsInfo:
- print(i+":", MaterialsInfo[i])
- #Make a new script called DNA and paste following, note all scripts needs to be in the same folder.
- import Materials
- import random
- import math
- Materials = [Materials.Nail, Materials.SteelNail, Materials.Stone, Materials.Wood, Materials.Paper, Materials.Grass, Materials.Metall, Materials.Tape, Materials.Graphene, Materials.Steel, Materials.Glass, Materials.Carbonfiber, Materials.MagicMix, Materials.Plastic]
- class DNA:
- def __init__(self, length):
- self.Gene = []
- self.length = length
- for i in range(0, length):
- list.append(self.Gene, Materials[random.randint(0, len(Materials)-1)]())
- def Content(self):
- MaterialsCount = {"Wood": 0,
- "Nail": 0,
- "Metall": 0,
- "Stone": 0,
- "Paper": 0,
- "Grass": 0,
- "Tape": 0,
- "Steel": 0,
- "Glass": 0,
- "Carbonfiber": 0,
- "Graphene": 0,
- "MagicMix": 0,
- "Plastic": 0,
- "SteelNail": 0
- }
- for i in self.Gene:
- MaterialsCount[i.__class__.__name__] += 1
- return MaterialsCount
- def CalcFitness(self):
- Strength = 0
- Structure = 0
- for i in self.Gene:
- Strength += i.Strength
- Structure += i.Structure
- Durability = Strength*0.8/100
- Score = ((Strength+(Structure*1.5))*Durability)/364#durability
- self.Fitness = Score
- def Crossover(self, Partner):
- Cross = math.floor((len(Partner.Gene)-1)/2)
- child = DNA(self.length)
- for i in range(self.length-1):
- if i > Cross:
- child.Gene[i] = self.Gene[i]
- else:
- child.Gene[i] = Partner.Gene[i]
- return child
- def Mutate(self, MutationRate):
- for i in range(len(self.Gene)-1):
- if random.random() <= MutationRate:
- self.Gene[i] = Materials[random.randint(0, len(Materials)-1)]()
- #Make a new script called Materials and paste following, the values on these can be customized if you want.
- class Wood():
- def __init__(self):
- self.Strength = 65
- self.Structure = -30
- class Stone():
- def __init__(self):
- self.Strength = 175
- self.Structure = -90
- class Nail():
- def __init__(self):
- self.Strength = 5
- self.Structure = 100
- class Metall():
- def __init__(self):
- self.Strength = 155
- self.Structure = -65
- class Grass():
- def __init__(self):
- self.Strength = 8
- self.Structure = -3
- class Tape():
- def __init__(self):
- self.Strength = 1
- self.Structure = 40
- class Paper():
- def __init__(self):
- self.Strength = 20
- self.Structure = -10
- class Glass():
- def __init__(self):
- self.Strength = 35
- self.Structure = -20
- class Graphene():
- def __init__(self):
- self.Strength = 100
- self.Structure = 5
- class Carbonfiber():
- def __init__(self):
- self.Strength = 85
- self.Structure = -15
- class Steel():
- def __init__(self):
- self.Strength = 250
- self.Structure = -110
- class Plastic():
- def __init__(self):
- self.Strength = 70
- self.Structure = -40
- class MagicMix():
- def __init__(self):
- self.Strength = 199.99
- self.Structure = -79
- class SteelNail:
- def __init__(self):
- self.Strength = 15
- self.Structure = 210
- #Make a new script called Population and paste following.
- import DNA
- import math
- import random
- class Population:
- def __init__(self, Size, MR, Length):
- self.MutationRate = MR
- self.Size = Size
- self.Population = []
- self.MatingPool = []
- self.Gen = 0
- self.Length = Length
- self.WorstScore = 0
- self.WorstScore2 = 0
- self.Record = 0
- self.Record2 = None
- for i in range(Size-1):
- self.Population.append(DNA.DNA(self.Length))
- self.CalcFitness()
- def CalcFitness(self):
- for i in self.Population:
- i.CalcFitness()
- def NaturalSelection(self):
- self.MatingPool = []
- self.MaxFitness = 0
- totallScore = 0
- for i in self.Population:
- if i.Fitness > self.MaxFitness:
- MaxFitness = i.Fitness
- totallScore += i.Fitness
- avarage = totallScore/len(self.Population)
- for i in self.Population:
- normalized = (i.Fitness-0.1)/(MaxFitness-0.1)
- x = math.floor(normalized * 100)
- if i.Fitness > avarage:
- for n in range(0, x):
- self.MatingPool.append(i)
- def NextGen(self):
- for i in range(0, len(self.Population)-1):
- x = self.MatingPool[random.randint(0, len(self.MatingPool)-1)]
- y = self.MatingPool[random.randint(0, len(self.MatingPool)-1)]
- child = x.Crossover(y)
- child.Mutate(self.MutationRate)
- self.Population[i] = child
- self.Gen += 1
- def BestIndevidual(self):
- Best = 0
- Best2 = None
- TotallScore = 0
- for i in self.Population:
- TotallScore += i.Fitness
- if self.WorstScore == 0:
- self.WorstScore = i.Fitness
- self.WorstScore2 = i
- elif self.WorstScore > i.Fitness:
- self.WorstScore = i.Fitness
- self.WorstScore2 = i
- if i.Fitness > Best:
- Best = i.Fitness
- Best2 = i
- self.Avarage = TotallScore/len(self.Population)
- if Best > self.Record:
- self.Record = Best
- self.Record2 = Best2
- return Best, Best2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement