Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local next=next
- local password = "test"
- local points={}
- --Brain--
- local point={
- new=function(self)
- return {passwordtest="", fitnessLevel=0}
- end
- }
- local calculator={
- calculateFitness=function(self, point)
- --Create fitness Variable
- local fitnessLevelz=0
- --Separate word based on single characters
- local letters=SplitString(point.passwordtest);
- local passLetters=SplitString(password); -- Separate letters of password
- --Now compare
- for i,v in next,letters do
- if passLetters[i]==v then--Compare password letter to random word letter
- fitnessLevelz=fitnessLevelz+1
- end
- end
- return fitnessLevelz
- end,
- randPW=function(self, point, length)
- local index, pw, rnd = 0, ""
- local chars = {
- "abcdefghijklmnopqrstuvwxyz"
- }
- repeat
- index = index + 1
- rnd = math.random(chars[index]:len())
- if math.random(2) == 1 then
- pw = pw .. chars[index]:sub(rnd, rnd)
- else
- pw = chars[index]:sub(rnd, rnd) .. pw
- end
- index = index % #chars
- until pw:len() >= length
- point.passwordtest=pw;
- local fl = self:calculateFitness(point);
- point.fitnessLevel = fl
- return pw
- end
- }
- --[[Helpful Functions]]--
- function SplitString(str)
- local letters={}
- for i = 1,str:len() do
- table.insert(letters,str:sub(i,i))
- end
- return letters
- end
- --Population
- local population={
- generationNum=1,
- start=function(self)
- self.generationNum=self.generationNum+1
- for i = 1,100 do
- local newPoint = point:new()
- table.insert(points, newPoint)
- calculator:randPW(newPoint,4)
- end
- return self
- end,
- GetBest=function(self)
- local fitnesslevels={}
- for i,v in next, points do
- table.insert(fitnesslevels,v.fitnessLevel);
- end
- table.sort(fitnesslevels);
- local best={}
- for i,v in next,fitnesslevels do
- if v==fitnesslevels[#fitnesslevels] then
- table.insert(best,v)
- end
- end
- return best
- end
- }
- for i = 1,100 do
- population:start();
- local bestPoints = population:GetBest();
- print(unpack(bestPoints))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement