From 73e6c11f02d26c8c92bad7a0c631cdae5f1779a1 Mon Sep 17 00:00:00 2001 From: monkstone Date: Fri, 26 Jun 2015 16:42:05 +0100 Subject: [PATCH] various tidy ups --- .../NOC_9_01_GA_Shakespeare.rb | 8 +++---- .../smart_rocket_basic.rb | 10 ++++---- ...ionEcosystem.rb => evolution_ecosystem.rb} | 7 ++---- chp09_ga/NOC_9_05_EvolutionEcosystem/world.rb | 23 ++++++++----------- 4 files changed, 21 insertions(+), 27 deletions(-) rename chp09_ga/NOC_9_05_EvolutionEcosystem/{NOC_9_05_EvolutionEcosystem.rb => evolution_ecosystem.rb} (92%) diff --git a/chp09_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.rb b/chp09_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.rb index 422dc6e..b02a21c 100644 --- a/chp09_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.rb +++ b/chp09_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.rb @@ -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 diff --git a/chp09_ga/NOC_9_02_SmartRockets_superbasic/smart_rocket_basic.rb b/chp09_ga/NOC_9_02_SmartRockets_superbasic/smart_rocket_basic.rb index 5acd860..6746c65 100644 --- a/chp09_ga/NOC_9_02_SmartRockets_superbasic/smart_rocket_basic.rb +++ b/chp09_ga/NOC_9_02_SmartRockets_superbasic/smart_rocket_basic.rb @@ -7,8 +7,8 @@ # 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 @@ -16,14 +16,14 @@ 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 @@ -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 diff --git a/chp09_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.rb b/chp09_ga/NOC_9_05_EvolutionEcosystem/evolution_ecosystem.rb similarity index 92% rename from chp09_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.rb rename to chp09_ga/NOC_9_05_EvolutionEcosystem/evolution_ecosystem.rb index b8de82f..93e1389 100644 --- a/chp09_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.rb +++ b/chp09_ga/NOC_9_05_EvolutionEcosystem/evolution_ecosystem.rb @@ -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 @@ -10,7 +9,7 @@ load_library :vecmath -require_relative './world' +require_relative 'world' include Eco @@ -34,5 +33,3 @@ def mouse_pressed def mouse_dragged world.born(mouse_x, mouse_y) end - - diff --git a/chp09_ga/NOC_9_05_EvolutionEcosystem/world.rb b/chp09_ga/NOC_9_05_EvolutionEcosystem/world.rb index 4439752..25924ba 100644 --- a/chp09_ga/NOC_9_05_EvolutionEcosystem/world.rb +++ b/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 @@ -29,7 +28,6 @@ def run end # Creature class - class Bloop include Processing::Proxy attr_reader :width, :height, :health, :dna, :location @@ -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 @@ -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 @@ -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 @@ -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