Small Hangup in Corewars Evolver

Discussion in 'Ruby' started by Brad G., Feb 10, 2011.

  1. Brad G.

    Brad G. Guest

    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)

    --
    Posted via http://www.ruby-forum.com/.
     
    Brad G., Feb 10, 2011
    #1
    1. Advertising

  2. Brad G.

    Tony Arcieri Guest

    [Note: parts of this message were removed to make it a legal post.]

    Do you have a Ruby Corewars implementation? I've been working on a
    Corewars-related project off and on, and it'd be nice if there were another
    implementation I could go off of that wasn't Digital MARS.

    Here's my project:

    https://github.com/tarcieri/badgewars

    On Thu, Feb 10, 2011 at 1:46 PM, Brad G. <>wrote:

    > 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)
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Tony Arcieri
    Medioh! Kudelski
     
    Tony Arcieri, Feb 10, 2011
    #2
    1. Advertising

  3. Brad G.

    Brad G. Guest

    Tony Arcieri wrote in post #980956:
    > Do you have a Ruby Corewars implementation? I've been working on a
    > Corewars-related project off and on, and it'd be nice if there were
    > another
    > implementation I could go off of that wasn't Digital MARS.
    >
    > Here's my project:
    >
    > https://github.com/tarcieri/badgewars


    No I don't have a Ruby Corewars implementation, however if you do end up
    completing yours I'd very much like to use it in my evolver to eliminate
    the need for it to interact with an external mars.

    --
    Posted via http://www.ruby-forum.com/.
     
    Brad G., Feb 10, 2011
    #3
  4. Brad G.

    Tony Arcieri Guest

    [Note: parts of this message were removed to make it a legal post.]

    How are you doing that? I'd be curious. I need to check my implementation
    against pMARS, and right now I'm doing it manually :/

    On Thu, Feb 10, 2011 at 2:24 PM, Brad G. <>wrote:

    > Tony Arcieri wrote in post #980956:
    > > Do you have a Ruby Corewars implementation? I've been working on a
    > > Corewars-related project off and on, and it'd be nice if there were
    > > another
    > > implementation I could go off of that wasn't Digital MARS.
    > >
    > > Here's my project:
    > >
    > > https://github.com/tarcieri/badgewars

    >
    > No I don't have a Ruby Corewars implementation, however if you do end up
    > completing yours I'd very much like to use it in my evolver to eliminate
    > the need for it to interact with an external mars.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Tony Arcieri
    Medioh! Kudelski
     
    Tony Arcieri, Feb 10, 2011
    #4
  5. Brad G.

    Brad G. Guest

    well I'm not done coding that yet but in ruby it's relatively easy as
    you can pass system commands and recieve the returns quite easily.

    --
    Posted via http://www.ruby-forum.com/.
     
    Brad G., Feb 10, 2011
    #5
  6. Brad G.

    Tony Arcieri Guest

    [Note: parts of this message were removed to make it a legal post.]

    I'll have to check that out. Do you have any examples of your pMARS
    interfacing you can share?

    On Thu, Feb 10, 2011 at 3:11 PM, Brad G. <>wrote:

    > well I'm not done coding that yet but in ruby it's relatively easy as
    > you can pass system commands and recieve the returns quite easily.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Tony Arcieri
    Medioh! Kudelski
     
    Tony Arcieri, Feb 10, 2011
    #6
  7. [Note: parts of this message were removed to make it a legal post.]

    Ok, so it looks like this is the only really relevant code:

    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

    nop.breed(20, 10, 10, 10,
    "/home/brad/NetBeansProjects/BGEvolver/lib/dumpingfolder", 0)

    If I'm understanding this right, it will take the current warrior and breed
    20 new, mutated versions of it. How is this different from what you want?

    On Thu, Feb 10, 2011 at 3:46 PM, Brad G. <>wrote:

    > 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)
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Andrew Wagner, Feb 11, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manuel Bua

    Visual Studio .Net 2003 IDE Hangup

    Manuel Bua, Oct 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    446
    Manuel Bua
    Oct 20, 2003
  2. =?Utf-8?B?Y2xzbWl0aDY2?=

    Treeview causing hangup

    =?Utf-8?B?Y2xzbWl0aDY2?=, Dec 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    =?Utf-8?B?Y2xzbWl0aDY2?=
    Dec 9, 2004
  3. Pattt
    Replies:
    1
    Views:
    416
    Unforgiven
    Oct 2, 2004
  4. Merrigan

    DB Query Parse Hangup

    Merrigan, Dec 14, 2007, in forum: Python
    Replies:
    1
    Views:
    280
    Gabriel Genellina
    Dec 17, 2007
  5. CJ
    Replies:
    0
    Views:
    1,133
Loading...

Share This Page