Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harry Brundage
committed
Sep 13, 2010
1 parent
077abe4
commit 5b3a3b2
Showing
25 changed files
with
557 additions
and
76,416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'ai4r' | ||
require 'darwin/chromosome' | ||
require 'darwin/game_player' | ||
|
||
task :evolve do | ||
search = Ai4r::GeneticAlgorithm::GeneticSearch.new(2, 3) | ||
result = search.run | ||
puts result.inspect | ||
puts result.data | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module Finch | ||
class Chromosome | ||
|
||
attr_accessor :data | ||
attr_accessor :normalized_fitness | ||
attr_accessor :fitness | ||
|
||
def initialize(data) | ||
@data = data | ||
end | ||
|
||
# The fitness method quantifies the optimality of a solution | ||
# (that is, a chromosome) in a genetic algorithm so that that particular | ||
# chromosome may be ranked against all the other chromosomes. | ||
# | ||
# Optimal chromosomes, or at least chromosomes which are more optimal, | ||
# are allowed to breed and mix their datasets by any of several techniques, | ||
# producing a new generation that will (hopefully) be even better. | ||
def fitness | ||
return @fitness if @fitness | ||
maps = Array.new(99) do |i| | ||
"maps/map#{i+1}.txt" | ||
end | ||
|
||
bots = ["RandomBot", "BullyBot", "DualBot", "ProspectorBot", "RageBot"].collect do |s| | ||
"java -jar example_bots/#{s}.jar" | ||
end | ||
finch = "/Users/hornairs/.rvm/rubies/ruby-1.9.2-rc2/bin/ruby mybot.rb #{self.data.join(" ")}" | ||
scores = [] | ||
maps[0..2].each do |map| | ||
bots[1..3].each do |bot| | ||
scores.push GamePlayer.new.play(bot, finch, map) | ||
end | ||
end | ||
|
||
@fitness = scores.sum / scores.length | ||
return @fitness | ||
end | ||
|
||
def self.mutate(chromosome) | ||
if chromosome.normalized_fitness && rand < ((1 - chromosome.normalized_fitness) * 0.3) | ||
data = chromosome.data | ||
index = rand(data.length-1) | ||
data[index] = data[index] + ((rand - 0.5) * data[index]) | ||
chromosome.data = data | ||
chromosome.fitness = nil | ||
end | ||
end | ||
|
||
def self.reproduce(a, b) | ||
data_size = Finch.parameter_count-2 | ||
crossover = rand(data_size) | ||
spawn = a.data[0..crossover] + b.data[crossover+1..-1] | ||
return self.class.new(spawn) | ||
end | ||
|
||
# Initializes an individual solution (chromosome) for the initial | ||
# population. Usually the chromosome is generated randomly, but you can | ||
# use some problem domain knowledge, to generate a | ||
# (probably) better initial solution. | ||
def self.seed | ||
seed = Array.new(Finch.parameter_count) do |i| | ||
rand | ||
end | ||
return Chromosome.new(seed) | ||
end | ||
end | ||
end | ||
|
||
module Ai4r | ||
module GeneticAlgorithm | ||
class GeneticSearch | ||
def generate_initial_population | ||
@population = [] | ||
@population_size.times do | ||
population << Finch::Chromosome.seed | ||
end | ||
end | ||
|
||
def reproduction(selected_to_breed) | ||
offsprings = [] | ||
0.upto(selected_to_breed.length/2-1) do |i| | ||
offsprings << Finch::Chromosome.reproduce(selected_to_breed[2*i], selected_to_breed[2*i+1]) | ||
end | ||
@population.each do |individual| | ||
Finch::Chromosome.mutate(individual) | ||
end | ||
return offsprings | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "open3" | ||
module Finch | ||
class GamePlayer | ||
def play(bot1, bot2, map) | ||
str = "java -jar tools/PlayGame.jar #{map} 1000 1000 logs/log#{Time.now.to_i}.txt \"#{bot1}\" \"#{bot2}\"" | ||
puts str | ||
output = "" | ||
Open3.popen3(str) { |stdin, stdout, stderr| | ||
output = stdout.read | ||
errors = stderr.read | ||
puts errors | ||
} | ||
self.parse_output(output) | ||
end | ||
|
||
def parse_output(string) | ||
if string.match("WARNING: player 2 timed out") | ||
return -1001 | ||
else | ||
turns = string.scan(/Turn \d{1,3}/).length | ||
if string.match("Player 2 Wins!") | ||
winner = 1 | ||
else | ||
winner = -1 | ||
end | ||
return (1000 - turns) * winner | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Finch | ||
class CripplingAngle < Angle | ||
def opinion | ||
if destination.owner == $ENEMY | ||
destination.num_ships / Finch.pw.distance(source, destination) | ||
else | ||
0 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Finch | ||
class DefendableAngle < Angle | ||
def opinion | ||
source.num_ships - destination.num_ships | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
module Finch | ||
class InvestmentAngle < Angle | ||
def find_best_investments | ||
def self.calculate_investments | ||
calculations = [] | ||
Finch.pw.my_planets.each do |src| | ||
calculations[src.id] = [] | ||
Finch.pw.not_my_planets.each do |dest| | ||
# How good is the growth rate in comparison to how many ships would be generated while not doing so | ||
calculations[src.id][dest.id] = dest.growth_rate / (src.growth_rate * Finch.pw.distance(src, dest)) | ||
# calculations[src.id][dest.id] = (dest.growth_rate * 10) / ((src.growth_rate * Finch.pw.distance(src, dest))) | ||
calculations[src.id][dest.id] = dest.growth_rate / (src.growth_rate + 1) | ||
end | ||
end | ||
calculations | ||
end | ||
def opinion(source, destination) | ||
@calculations ||= self.find_best_investments | ||
@calculations[source.id][destination.id] || 0 | ||
|
||
def opinion | ||
Finch.pw.calculations[:investments] ||= self.class.calculate_investments | ||
calcs = Finch.pw.calculations[:investments] | ||
if(calcs[source.id][destination.id]) | ||
reserve_conquering_fleet! | ||
calcs[source.id][destination.id] | ||
else | ||
0 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.