Skip to content

Commit

Permalink
various tidy ups
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Jun 26, 2015
1 parent d47f509 commit 73e6c11
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
8 changes: 4 additions & 4 deletions chp09_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.rb
Expand Up @@ -197,11 +197,11 @@ def display_info
text_size(40)
text(answer, 20, 100)
text_size(18)
text("total generations: #{@population.generations}", 20, 160)
text("average fitness: #{format('%.2f', @population.average_fitness)}",
text(format('total generations: %d', @population.generations), 20, 160)
text(format('average fitness: %.2f', @population.average_fitness),
20, 180)
text("total population: #{@popmax}", 20, 200)
text("mutation rate: #{(@mutation_rate * 100).to_i}%", 20, 220)
text(format('total population: %d', @popmax), 20, 200)
text(format('mutation rate: %d', (@mutation_rate * 100).to_i), 20, 220)
text_size(10)
text("All phrases:\n #{@population.all_phrases}", 500, 10)
end
10 changes: 5 additions & 5 deletions chp09_ga/NOC_9_02_SmartRockets_superbasic/smart_rocket_basic.rb
Expand Up @@ -7,23 +7,23 @@
# Each Rocket's DNA is an array of PVectors
# Each PVector acts as a force for each frame of animation
# Imagine an booster on the end of the rocket that can point in any direction
# and fire at any strength every frame
# The Rocket's fitness is a function of how close it gets to the target as well as how fast it gets there
# and fire at any strength every frame. The Rocket's fitness is a function of
# how close it gets to the target as well as how fast it gets there
# This example is inspired by Jer Thorp's Smart Rockets
# http://www.blprnt.com/smartrockets/
load_library :vecmath
require_relative 'population'
require_relative 'dna'
require_relative 'rocket'

attr_reader :lifetime, :population, :life_counter, :target, :mutation_rate
attr_reader :lifetime, :population, :life_counter, :target, :mutation_rate

def setup
size(640, 360)
# The number of cycles we will allow a generation to live
@lifetime = height
# Initialize variables
@life_counter = 0
@life_counter = 0
@target = Vec2D.new(width / 2, 24)
# Create a population with a mutation rate, and population max
@mutation_rate = 0.01
Expand All @@ -36,7 +36,7 @@ def draw
fill(0)
ellipse(target.x, target.y, 24, 24)
# If the generation hasn't ended yet
if (life_counter < lifetime)
if life_counter < lifetime
population.live
@life_counter += 1
# Otherwise a new generation
Expand Down
@@ -1,6 +1,5 @@
# Evolution EcoSystem
# EvolutionEcoSystem
# The Nature of Code

# A World of creatures that eat food
# The more they eat, the longer they survive
# The longer they survive, the more likely they are to reproduce
Expand All @@ -10,7 +9,7 @@

load_library :vecmath

require_relative './world'
require_relative 'world'

include Eco

Expand All @@ -34,5 +33,3 @@ def mouse_pressed
def mouse_dragged
world.born(mouse_x, mouse_y)
end


23 changes: 10 additions & 13 deletions chp09_ga/NOC_9_05_EvolutionEcosystem/world.rb
@@ -1,6 +1,5 @@
# Evolution EcoSystem
module Eco

# A collection of food in the world
class Food
include Processing::Proxy
Expand Down Expand Up @@ -29,7 +28,6 @@ def run
end

# Creature class

class Bloop
include Processing::Proxy
attr_reader :width, :height, :health, :dna, :location
Expand All @@ -40,8 +38,8 @@ def initialize(loc, dna)
@xoff = rand(1_000)
@yoff = rand(1_000)
@dna = dna
@maxspeed = map1d(dna.genes[0], (0 .. 1), (15 .. 0))
@r = map1d(dna.genes[0], (0 .. 1), (0 .. 50))
@maxspeed = map1d(dna.genes[0], (0..1), (15..0))
@r = map1d(dna.genes[0], (0..1), (0..50))
@width, @height = $app.width, $app.height
end

Expand All @@ -64,14 +62,14 @@ def eat(f)

def reproduce
return nil if rand >= 0.0005
childDNA = dna.copy
childDNA.mutate(0.01)
Bloop.new(location, childDNA)
child = dna.copy
child.mutate(0.01)
Bloop.new(location, child)
end

def update
vx = map1d(noise(@xoff), (0 .. 1.0), (-@maxspeed .. @maxspeed))
vy = map1d(noise(@yoff), (0 .. 1.0), (-@maxspeed .. @maxspeed))
vx = map1d(noise(@xoff), (0..1.0), (-@maxspeed..@maxspeed))
vy = map1d(noise(@yoff), (0..1.0), (-@maxspeed..@maxspeed))
velocity = Vec2D.new(vx, vy)
@xoff += 0.01
@yoff += 0.01
Expand All @@ -97,11 +95,11 @@ def dead?
end
end


# DNA class
class DNA
attr_reader :genes
def initialize(newgenes = [])
newgenes << rand(0 .. 1.0) if newgenes.empty?
newgenes << rand(0..1.0) if newgenes.empty?
@genes = newgenes
end

Expand All @@ -111,13 +109,12 @@ def copy

# this code doesn't make sense unless there is more than one gene
def mutate(m)
@genes.map! { |gene| (rand < m) ? rand(0 .. 1.0) : gene }
@genes.map! { |gene| (rand < m) ? rand(0..1.0) : gene }
end
end

# The World we live in
# Has bloops and food

class World
attr_reader :bloops, :food

Expand Down

0 comments on commit 73e6c11

Please sign in to comment.