-
Notifications
You must be signed in to change notification settings - Fork 336
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
1 parent
672b331
commit 4b16081
Showing
121 changed files
with
24,329 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<html> | ||
<head> | ||
<title>Acknowledgments | Clever Algorithms</title><link href="clever.css" media="screen" rel="stylesheet" type="text/css"/> | ||
<!-- support to display ruby source nicely --> | ||
<link href="prettify.css" type="text/css" rel="stylesheet"/> | ||
<script type="text/javascript" src="prettify.js"></script> | ||
</head> | ||
<!-- call to display ruby source nicely --> | ||
<body onload="prettyPrint()"><!-- Start Header --> | ||
<center> | ||
<h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1> | ||
<em>A book by Jason Brownlee</em> | ||
|
||
<p> | ||
<a href="/">Home</a> | | ||
<a href="https://amzn.to/4iKM9uc">Amazon</a> | | ||
<a href="https://www.goodreads.com/book/show/10321060-clever-algorithms">GoodReads</a> | | ||
<a href="https://www.google.com.au/books/edition/Clever_Algorithms/SESWXQphCUkC">Google Books</a> | | ||
<a href="https://raw.githubusercontent.com/Jason2Brownlee/CleverAlgorithms/master/release/clever_algorithms.pdf">PDF</a> (<a href="https://raw.githubusercontent.com/Jason2Brownlee/CleverAlgorithms/master/release/clever_algorithms-src.zip">code</a>) | | ||
<a href="https://github.com/Jason2Brownlee/CleverAlgorithms">GitHub</a> | ||
</p> | ||
|
||
</center> | ||
<hr/> | ||
<br/> | ||
<!-- End Header --><div class='breadcrumb'> | ||
<a href='index.html'>Table of Contents</a> | ||
>> | ||
<a href='acknowledgments.html'>Acknowledgments</a> | ||
</div> | ||
<h1><a name='acknowledgments'>Acknowledgments</a></h1> | ||
<p> | ||
This book could not have been completed without the commitment, passion, and hard work from a large group of editors and supporters. | ||
</p> | ||
<p> | ||
A special thanks to Steve Dower for his incredible attention to detail in providing technical and copy edits for large portions of this book, and for his enthusiasm for the subject area. | ||
Also, a special thanks to Daniel Angus for the discussions around the genesis of the project, his continued support with the idea of an 'algorithms atlas' and for his attention to detail in providing technical and copy edits for key chapters. | ||
</p> | ||
<p> | ||
In no particular order, thanks to: | ||
Juan Ojeda, | ||
Martin Goddard, | ||
David Howden, | ||
Sean Luke, | ||
David Zappia, | ||
Jeremy Wazny, | ||
Andrew Murray, | ||
John Wise, | ||
Patrick Boehnke, | ||
Martin-Louis Bright, | ||
Leif Wickland, | ||
Andrew Myers, | ||
Paul Chinnery, | ||
Donald Doherty, | ||
Brook Tamir, | ||
Zach Scott, | ||
Diego Noble, | ||
Jason Davies, | ||
Mark Chenoweth, | ||
Markus Stokmaier, | ||
Stefan Pauleweit, | ||
Lai Yu-Hsuan, | ||
Stephan Williams, | ||
and | ||
Ricardo Colome. | ||
</p> | ||
<p> | ||
Thanks to the hundreds of machine learning enthusiasts who voted on potential covers and helped shape what this book became. You know who you are! | ||
</p> | ||
<p> | ||
Finally, I would like to thank my beautiful wife Ying Liu for her unrelenting support and patience throughout the project. | ||
</p> | ||
|
||
|
||
<!-- generate math images from latex math snippets --> | ||
<script type="text/javascript" src="https://mathcache.s3.amazonaws.com/replacemath.js"></script> | ||
<script type="text/javascript">replaceMath(document.body);</script> | ||
</body> | ||
</html> |
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,64 @@ | ||
<html> | ||
<head> | ||
<title>Advanced Topics | Clever Algorithms</title><link href="clever.css" media="screen" rel="stylesheet" type="text/css"/> | ||
<!-- support to display ruby source nicely --> | ||
<link href="prettify.css" type="text/css" rel="stylesheet"/> | ||
<script type="text/javascript" src="prettify.js"></script> | ||
</head> | ||
<!-- call to display ruby source nicely --> | ||
<body onload="prettyPrint()"><!-- Start Header --> | ||
<center> | ||
<h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1> | ||
<em>A book by Jason Brownlee</em> | ||
|
||
<p> | ||
<a href="/">Home</a> | | ||
<a href="https://amzn.to/4iKM9uc">Amazon</a> | | ||
<a href="https://www.goodreads.com/book/show/10321060-clever-algorithms">GoodReads</a> | | ||
<a href="https://www.google.com.au/books/edition/Clever_Algorithms/SESWXQphCUkC">Google Books</a> | | ||
<a href="https://raw.githubusercontent.com/Jason2Brownlee/CleverAlgorithms/master/release/clever_algorithms.pdf">PDF</a> (<a href="https://raw.githubusercontent.com/Jason2Brownlee/CleverAlgorithms/master/release/clever_algorithms-src.zip">code</a>) | | ||
<a href="https://github.com/Jason2Brownlee/CleverAlgorithms">GitHub</a> | ||
</p> | ||
|
||
</center> | ||
<hr/> | ||
<br/> | ||
<!-- End Header --><div class='breadcrumb'> | ||
<a href='index.html'>Table of Contents</a> | ||
>> | ||
<a href='advanced.html'>Advanced Topics</a> | ||
</div> | ||
<h1><a name='advanced_topics'>Advanced Topics</a></h1> | ||
<p> | ||
This chapter discusses a number of advanced topics that may be considered once one or more of the algorithms described in this book have been mastered. | ||
</p> | ||
<p> | ||
The topics in this section consider some practical concerns such as: | ||
</p> | ||
<ul> | ||
<li> How to implement an algorithm using a different programming paradigm.</li> | ||
<li> How to devise and investigate a new biologically-inspired algorithm.</li> | ||
<li> How to test algorithm implementations to ensure they are implemented correctly.</li> | ||
<li> How to visualize problems, algorithm behavior and candidate solutions.</li> | ||
<li> How to direct these algorithms toward practical problem solving.</li> | ||
<li> Issues to consider when benchmarking and comparing the capabilities of algorithms.</li> | ||
</ul> | ||
<p> | ||
The objective of this chapter is to illustrate the concerns and skills necessary for taking the algorithms described in this book into the real-world. | ||
</p> | ||
|
||
|
||
<h3>Advanced Topics</h3> | ||
<ul> | ||
<li><a href='advanced/paradigms.html'>Programming Paradigms</a></li> | ||
<li><a href='advanced/new_algorithms.html'>Devising New Algorithms</a></li> | ||
<li><a href='advanced/testing_algorithms.html'>Testing Algorithms</a></li> | ||
<li><a href='advanced/visualizing_algorithms.html'>Visualizing Algorithms</a></li> | ||
<li><a href='advanced/problem_solving.html'>Problem Solving Strategies</a></li> | ||
<li><a href='advanced/racing_algorithms.html'>Benchmarking Algorithms</a></li> | ||
</ul> | ||
<!-- generate math images from latex math snippets --> | ||
<script type="text/javascript" src="https://mathcache.s3.amazonaws.com/replacemath.js"></script> | ||
<script type="text/javascript">replaceMath(document.body);</script> | ||
</body> | ||
</html> |
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,156 @@ | ||
# Genetic Algorithm in the Ruby Programming Language: Flow Programming | ||
|
||
# The Clever Algorithms Project: http://www.CleverAlgorithms.com | ||
# (c) Copyright 2010 Jason Brownlee. Some Rights Reserved. | ||
# This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Australia License. | ||
|
||
require 'thread' | ||
|
||
# Generic flow unit | ||
class FlowUnit | ||
attr_reader :queue_in, :queue_out, :thread | ||
|
||
def initialize(q_in=Queue.new, q_out=Queue.new) | ||
@queue_in, @queue_out = q_in, q_out | ||
start() | ||
end | ||
|
||
def execute | ||
raise "FlowUnit not defined!" | ||
end | ||
|
||
def start | ||
puts "Starting flow unit: #{self.class.name}!" | ||
@thread = Thread.new do | ||
execute() while true | ||
end | ||
end | ||
end | ||
|
||
# Evaluation of solutions flow unit | ||
class EvalFlowUnit < FlowUnit | ||
def onemax(bitstring) | ||
sum = 0 | ||
bitstring.size.times {|i| sum+=1 if bitstring[i].chr=='1'} | ||
return sum | ||
end | ||
|
||
def execute | ||
data = @queue_in.pop | ||
data[:fitness] = onemax(data[:bitstring]) | ||
@queue_out.push(data) | ||
end | ||
end | ||
|
||
# Stop condition flow unit | ||
class StopConditionUnit < FlowUnit | ||
attr_reader :best, :num_bits, :max_evaluations, :evals | ||
|
||
def initialize(q_in=Queue.new, q_out=Queue.new, max_evaluations=10000, num_bits=64) | ||
@best, @evals = nil, 0 | ||
@num_bits = num_bits | ||
@max_evaluations = max_evaluations | ||
super(q_in, q_out) | ||
end | ||
|
||
def execute | ||
data = @queue_in.pop | ||
if @best.nil? or data[:fitness] > @best[:fitness] | ||
@best = data | ||
puts " >new best: #{@best[:fitness]}, #{@best[:bitstring]}" | ||
end | ||
@evals += 1 | ||
if @best[:fitness]==@num_bits or @evals>=@max_evaluations | ||
puts "done! Solution: f=#{@best[:fitness]}, s=#{@best[:bitstring]}" | ||
@thread.exit() | ||
end | ||
@queue_out.push(data) | ||
end | ||
end | ||
|
||
# Fitness-based selection flow unit | ||
class SelectFlowUnit < FlowUnit | ||
def initialize(q_in=Queue.new, q_out=Queue.new, pop_size=100) | ||
@pop_size = pop_size | ||
super(q_in, q_out) | ||
end | ||
|
||
def binary_tournament(pop) | ||
i, j = rand(pop.size), rand(pop.size) | ||
j = rand(pop.size) while j==i | ||
return (pop[i][:fitness] > pop[j][:fitness]) ? pop[i] : pop[j] | ||
end | ||
|
||
def execute | ||
population = Array.new | ||
population << @queue_in.pop while population.size < @pop_size | ||
@pop_size.times do |i| | ||
@queue_out.push(binary_tournament(population)) | ||
end | ||
end | ||
end | ||
|
||
# Variation flow unit | ||
class VariationFlowUnit < FlowUnit | ||
def initialize(q_in=Queue.new, q_out=Queue.new, crossover=0.98, mutation=1.0/64.0) | ||
@p_crossover = crossover | ||
@p_mutation = mutation | ||
super(q_in, q_out) | ||
end | ||
|
||
def uniform_crossover(parent1, parent2) | ||
return ""+parent1 if rand()>=@p_crossover | ||
child = "" | ||
parent1.length.times do |i| | ||
child << ((rand()<0.5) ? parent1[i].chr : parent2[i].chr) | ||
end | ||
return child | ||
end | ||
|
||
def point_mutation(bitstring) | ||
child = "" | ||
bitstring.size.times do |i| | ||
bit = bitstring[i].chr | ||
child << ((rand()<@p_mutation) ? ((bit=='1') ? "0" : "1") : bit) | ||
end | ||
return child | ||
end | ||
|
||
def reproduce(p1, p2) | ||
child = {} | ||
child[:bitstring] = uniform_crossover(p1[:bitstring], p2[:bitstring]) | ||
child[:bitstring] = point_mutation(child[:bitstring]) | ||
return child | ||
end | ||
|
||
def execute | ||
parent1 = @queue_in.pop | ||
parent2 = @queue_in.pop | ||
@queue_out.push(reproduce(parent1, parent2)) | ||
@queue_out.push(reproduce(parent2, parent1)) | ||
end | ||
end | ||
|
||
def random_bitstring(num_bits) | ||
return (0...num_bits).inject(""){|s,i| s<<((rand<0.5) ? "1" : "0")} | ||
end | ||
|
||
def search(population_size=100, num_bits=64) | ||
# create the pipeline | ||
eval = EvalFlowUnit.new | ||
stopcondition = StopConditionUnit.new(eval.queue_out) | ||
selection = SelectFlowUnit.new(stopcondition.queue_out) | ||
variation = VariationFlowUnit.new(selection.queue_out, eval.queue_in) | ||
# push random solutions into the pipeline | ||
population_size.times do | ||
solution = {:bitstring=>random_bitstring(num_bits)} | ||
eval.queue_in.push(solution) | ||
end | ||
stopcondition.thread.join | ||
return stopcondition.best | ||
end | ||
|
||
if __FILE__ == $0 | ||
best = search() | ||
puts "done! Solution: f=#{best[:fitness]}, s=#{best[:bitstring]}" | ||
end |
Oops, something went wrong.