B
Brad G.
Corewars is a neat little programming game where players develop
assembly like programs to battle in virtual memory. My evolver uses
selective pressure to evolve warriors through variance.
The problem is that currently the breed method is producing several
generations of grand parents instead of several parallel first
generation children.
Code:
module Utilities
class Line
attr_accessor :rawtext, :command, :number1, :number2
def initialize(rawtextsource)
rawtext = []
rawtext = rawtextsource.split(//)
@command = rawtext[0..2].join
@number1 = rawtext[4]
@number2 = rawtext[7]
end
end
class Warrior
attr_accessor :location, :lines, :filename, :rawtext,
:instructionset
def initialize(location)
code = File.open(location, 'r')
@filename = location
@rawtext = code.readlines()
@lines = []
for line in rawtext
@lines << Line.new(line)
@instructionset =
File.open("/home/brad/NetBeansProjects/BGEvolver/lib/instructionset.txt",
'r').readlines.map {|x| x.chomp}
end
end
def updateraw
counter = 0
while counter < self.rawtext.length
self.rawtext[counter] = self.lines[counter].command + " " +
self.lines[counter].number1 + ", " + self.lines[counter].number2 + "\n"
counter += 1
end
return self
end
def mutate(chancecommand, chancenumber1, chancenumber2)
for line in self.lines
dice = rand(100)
line.command =
self.instructionset[rand(self.instructionset.length)] if
Array(0..chancecommand).include?(dice)
line.number1 = (line.number1.to_i + rand(10)).to_s if
Array(20..(20+chancenumber1 / 2)).include?(dice)
line.number1 = (line.number1.to_i + rand(10) * -1).to_s if
Array(30..(30+chancenumber1 / 2)).include?(dice)
line.number2 = (line.number2.to_i + rand(10)).to_s if
Array(40..(40+chancenumber2 / 2)).include?(dice)
line.number2 = (line.number2.to_i + rand(10) * -1).to_s if
Array(50..(50+chancenumber2 / 2)).include?(dice)
end
self.updateraw
end
def save(location)
self.updateraw
File.open(location, 'w').puts(self.rawtext)
end
def breed(offspring, chancecommand, chancenumber1, chancenumber2,
destfolder, botidcounter)
counter = 0
while counter < offspring
tmp = self.dup
tmp.mutate(chancecommand, chancenumber1,
chancenumber2).save(destfolder+"/"+((botidcounter += 1).to_s)+".RED")
counter += 1
end
return botidcounter
end
end
end
and also main.rb:
require "/home/brad/NetBeansProjects/BGEvolver/lib/utilities.rb"
include Utilities
nop =
Warrior.new("/home/brad/NetBeansProjects/BGEvolver/lib/benchmarks/nop.red")
nop.breed(20, 10, 10, 10,
"/home/brad/NetBeansProjects/BGEvolver/lib/dumpingfolder", 0)
assembly like programs to battle in virtual memory. My evolver uses
selective pressure to evolve warriors through variance.
The problem is that currently the breed method is producing several
generations of grand parents instead of several parallel first
generation children.
Code:
module Utilities
class Line
attr_accessor :rawtext, :command, :number1, :number2
def initialize(rawtextsource)
rawtext = []
rawtext = rawtextsource.split(//)
@command = rawtext[0..2].join
@number1 = rawtext[4]
@number2 = rawtext[7]
end
end
class Warrior
attr_accessor :location, :lines, :filename, :rawtext,
:instructionset
def initialize(location)
code = File.open(location, 'r')
@filename = location
@rawtext = code.readlines()
@lines = []
for line in rawtext
@lines << Line.new(line)
@instructionset =
File.open("/home/brad/NetBeansProjects/BGEvolver/lib/instructionset.txt",
'r').readlines.map {|x| x.chomp}
end
end
def updateraw
counter = 0
while counter < self.rawtext.length
self.rawtext[counter] = self.lines[counter].command + " " +
self.lines[counter].number1 + ", " + self.lines[counter].number2 + "\n"
counter += 1
end
return self
end
def mutate(chancecommand, chancenumber1, chancenumber2)
for line in self.lines
dice = rand(100)
line.command =
self.instructionset[rand(self.instructionset.length)] if
Array(0..chancecommand).include?(dice)
line.number1 = (line.number1.to_i + rand(10)).to_s if
Array(20..(20+chancenumber1 / 2)).include?(dice)
line.number1 = (line.number1.to_i + rand(10) * -1).to_s if
Array(30..(30+chancenumber1 / 2)).include?(dice)
line.number2 = (line.number2.to_i + rand(10)).to_s if
Array(40..(40+chancenumber2 / 2)).include?(dice)
line.number2 = (line.number2.to_i + rand(10) * -1).to_s if
Array(50..(50+chancenumber2 / 2)).include?(dice)
end
self.updateraw
end
def save(location)
self.updateraw
File.open(location, 'w').puts(self.rawtext)
end
def breed(offspring, chancecommand, chancenumber1, chancenumber2,
destfolder, botidcounter)
counter = 0
while counter < offspring
tmp = self.dup
tmp.mutate(chancecommand, chancenumber1,
chancenumber2).save(destfolder+"/"+((botidcounter += 1).to_s)+".RED")
counter += 1
end
return botidcounter
end
end
end
and also main.rb:
require "/home/brad/NetBeansProjects/BGEvolver/lib/utilities.rb"
include Utilities
nop =
Warrior.new("/home/brad/NetBeansProjects/BGEvolver/lib/benchmarks/nop.red")
nop.breed(20, 10, 10, 10,
"/home/brad/NetBeansProjects/BGEvolver/lib/dumpingfolder", 0)