Skip to content

Commit 4b16081

Browse files
added web content
1 parent 672b331 commit 4b16081

File tree

121 files changed

+24329
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+24329
-30
lines changed

Diff for: docs/index.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<title>Clever Algorithms: Nature-Inspired Programming Recipes</title>
44
</head>
55
<body>
6-
<center>
6+
7+
<!-- Start Header -->
8+
<center>
79
<h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1>
810
<em>A book by <a href="https://JasonBrownlee.me">Jason Brownlee</a></em>
911

@@ -19,6 +21,8 @@ <h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1>
1921
</center>
2022
<hr/>
2123
<br/>
24+
<!-- End Header -->
25+
2226
</div>
2327
<!-- content -->
2428
<center>

Diff for: docs/nature-inspired/acknowledgments.html

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<html>
2+
<head>
3+
<title>Acknowledgments | Clever Algorithms</title><link href="clever.css" media="screen" rel="stylesheet" type="text/css"/>
4+
<!-- support to display ruby source nicely -->
5+
<link href="prettify.css" type="text/css" rel="stylesheet"/>
6+
<script type="text/javascript" src="prettify.js"></script>
7+
</head>
8+
<!-- call to display ruby source nicely -->
9+
<body onload="prettyPrint()"><!-- Start Header -->
10+
<center>
11+
<h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1>
12+
<em>A book by Jason Brownlee</em>
13+
14+
<p>
15+
<a href="/">Home</a> |
16+
<a href="https://amzn.to/4iKM9uc">Amazon</a> |
17+
<a href="https://www.goodreads.com/book/show/10321060-clever-algorithms">GoodReads</a> |
18+
<a href="https://www.google.com.au/books/edition/Clever_Algorithms/SESWXQphCUkC">Google Books</a> |
19+
<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>) |
20+
<a href="https://github.com/Jason2Brownlee/CleverAlgorithms">GitHub</a>
21+
</p>
22+
23+
</center>
24+
<hr/>
25+
<br/>
26+
<!-- End Header --><div class='breadcrumb'>
27+
<a href='index.html'>Table of Contents</a>
28+
&gt;&gt;
29+
<a href='acknowledgments.html'>Acknowledgments</a>
30+
</div>
31+
<h1><a name='acknowledgments'>Acknowledgments</a></h1>
32+
<p>
33+
This book could not have been completed without the commitment, passion, and hard work from a large group of editors and supporters.
34+
</p>
35+
<p>
36+
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.
37+
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.
38+
</p>
39+
<p>
40+
In no particular order, thanks to:
41+
Juan Ojeda,
42+
Martin Goddard,
43+
David Howden,
44+
Sean Luke,
45+
David Zappia,
46+
Jeremy Wazny,
47+
Andrew Murray,
48+
John Wise,
49+
Patrick Boehnke,
50+
Martin-Louis Bright,
51+
Leif Wickland,
52+
Andrew Myers,
53+
Paul Chinnery,
54+
Donald Doherty,
55+
Brook Tamir,
56+
Zach Scott,
57+
Diego Noble,
58+
Jason Davies,
59+
Mark Chenoweth,
60+
Markus Stokmaier,
61+
Stefan Pauleweit,
62+
Lai Yu-Hsuan,
63+
Stephan Williams,
64+
and
65+
Ricardo Colome.
66+
</p>
67+
<p>
68+
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!
69+
</p>
70+
<p>
71+
Finally, I would like to thank my beautiful wife Ying Liu for her unrelenting support and patience throughout the project.
72+
</p>
73+
74+
75+
<!-- generate math images from latex math snippets -->
76+
<script type="text/javascript" src="https://mathcache.s3.amazonaws.com/replacemath.js"></script>
77+
<script type="text/javascript">replaceMath(document.body);</script>
78+
</body>
79+
</html>

Diff for: docs/nature-inspired/advanced.html

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<html>
2+
<head>
3+
<title>Advanced Topics | Clever Algorithms</title><link href="clever.css" media="screen" rel="stylesheet" type="text/css"/>
4+
<!-- support to display ruby source nicely -->
5+
<link href="prettify.css" type="text/css" rel="stylesheet"/>
6+
<script type="text/javascript" src="prettify.js"></script>
7+
</head>
8+
<!-- call to display ruby source nicely -->
9+
<body onload="prettyPrint()"><!-- Start Header -->
10+
<center>
11+
<h1>Clever Algorithms: Nature-Inspired Programming Recipes</h1>
12+
<em>A book by Jason Brownlee</em>
13+
14+
<p>
15+
<a href="/">Home</a> |
16+
<a href="https://amzn.to/4iKM9uc">Amazon</a> |
17+
<a href="https://www.goodreads.com/book/show/10321060-clever-algorithms">GoodReads</a> |
18+
<a href="https://www.google.com.au/books/edition/Clever_Algorithms/SESWXQphCUkC">Google Books</a> |
19+
<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>) |
20+
<a href="https://github.com/Jason2Brownlee/CleverAlgorithms">GitHub</a>
21+
</p>
22+
23+
</center>
24+
<hr/>
25+
<br/>
26+
<!-- End Header --><div class='breadcrumb'>
27+
<a href='index.html'>Table of Contents</a>
28+
&gt;&gt;
29+
<a href='advanced.html'>Advanced Topics</a>
30+
</div>
31+
<h1><a name='advanced_topics'>Advanced Topics</a></h1>
32+
<p>
33+
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.
34+
</p>
35+
<p>
36+
The topics in this section consider some practical concerns such as:
37+
</p>
38+
<ul>
39+
<li> How to implement an algorithm using a different programming paradigm.</li>
40+
<li> How to devise and investigate a new biologically-inspired algorithm.</li>
41+
<li> How to test algorithm implementations to ensure they are implemented correctly.</li>
42+
<li> How to visualize problems, algorithm behavior and candidate solutions.</li>
43+
<li> How to direct these algorithms toward practical problem solving.</li>
44+
<li> Issues to consider when benchmarking and comparing the capabilities of algorithms.</li>
45+
</ul>
46+
<p>
47+
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.
48+
</p>
49+
50+
51+
<h3>Advanced Topics</h3>
52+
<ul>
53+
<li><a href='advanced/paradigms.html'>Programming Paradigms</a></li>
54+
<li><a href='advanced/new_algorithms.html'>Devising New Algorithms</a></li>
55+
<li><a href='advanced/testing_algorithms.html'>Testing Algorithms</a></li>
56+
<li><a href='advanced/visualizing_algorithms.html'>Visualizing Algorithms</a></li>
57+
<li><a href='advanced/problem_solving.html'>Problem Solving Strategies</a></li>
58+
<li><a href='advanced/racing_algorithms.html'>Benchmarking Algorithms</a></li>
59+
</ul>
60+
<!-- generate math images from latex math snippets -->
61+
<script type="text/javascript" src="https://mathcache.s3.amazonaws.com/replacemath.js"></script>
62+
<script type="text/javascript">replaceMath(document.body);</script>
63+
</body>
64+
</html>

Diff for: docs/nature-inspired/advanced/flow.rb

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Genetic Algorithm in the Ruby Programming Language: Flow Programming
2+
3+
# The Clever Algorithms Project: http://www.CleverAlgorithms.com
4+
# (c) Copyright 2010 Jason Brownlee. Some Rights Reserved.
5+
# This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Australia License.
6+
7+
require 'thread'
8+
9+
# Generic flow unit
10+
class FlowUnit
11+
attr_reader :queue_in, :queue_out, :thread
12+
13+
def initialize(q_in=Queue.new, q_out=Queue.new)
14+
@queue_in, @queue_out = q_in, q_out
15+
start()
16+
end
17+
18+
def execute
19+
raise "FlowUnit not defined!"
20+
end
21+
22+
def start
23+
puts "Starting flow unit: #{self.class.name}!"
24+
@thread = Thread.new do
25+
execute() while true
26+
end
27+
end
28+
end
29+
30+
# Evaluation of solutions flow unit
31+
class EvalFlowUnit < FlowUnit
32+
def onemax(bitstring)
33+
sum = 0
34+
bitstring.size.times {|i| sum+=1 if bitstring[i].chr=='1'}
35+
return sum
36+
end
37+
38+
def execute
39+
data = @queue_in.pop
40+
data[:fitness] = onemax(data[:bitstring])
41+
@queue_out.push(data)
42+
end
43+
end
44+
45+
# Stop condition flow unit
46+
class StopConditionUnit < FlowUnit
47+
attr_reader :best, :num_bits, :max_evaluations, :evals
48+
49+
def initialize(q_in=Queue.new, q_out=Queue.new, max_evaluations=10000, num_bits=64)
50+
@best, @evals = nil, 0
51+
@num_bits = num_bits
52+
@max_evaluations = max_evaluations
53+
super(q_in, q_out)
54+
end
55+
56+
def execute
57+
data = @queue_in.pop
58+
if @best.nil? or data[:fitness] > @best[:fitness]
59+
@best = data
60+
puts " >new best: #{@best[:fitness]}, #{@best[:bitstring]}"
61+
end
62+
@evals += 1
63+
if @best[:fitness]==@num_bits or @evals>=@max_evaluations
64+
puts "done! Solution: f=#{@best[:fitness]}, s=#{@best[:bitstring]}"
65+
@thread.exit()
66+
end
67+
@queue_out.push(data)
68+
end
69+
end
70+
71+
# Fitness-based selection flow unit
72+
class SelectFlowUnit < FlowUnit
73+
def initialize(q_in=Queue.new, q_out=Queue.new, pop_size=100)
74+
@pop_size = pop_size
75+
super(q_in, q_out)
76+
end
77+
78+
def binary_tournament(pop)
79+
i, j = rand(pop.size), rand(pop.size)
80+
j = rand(pop.size) while j==i
81+
return (pop[i][:fitness] > pop[j][:fitness]) ? pop[i] : pop[j]
82+
end
83+
84+
def execute
85+
population = Array.new
86+
population << @queue_in.pop while population.size < @pop_size
87+
@pop_size.times do |i|
88+
@queue_out.push(binary_tournament(population))
89+
end
90+
end
91+
end
92+
93+
# Variation flow unit
94+
class VariationFlowUnit < FlowUnit
95+
def initialize(q_in=Queue.new, q_out=Queue.new, crossover=0.98, mutation=1.0/64.0)
96+
@p_crossover = crossover
97+
@p_mutation = mutation
98+
super(q_in, q_out)
99+
end
100+
101+
def uniform_crossover(parent1, parent2)
102+
return ""+parent1 if rand()>=@p_crossover
103+
child = ""
104+
parent1.length.times do |i|
105+
child << ((rand()<0.5) ? parent1[i].chr : parent2[i].chr)
106+
end
107+
return child
108+
end
109+
110+
def point_mutation(bitstring)
111+
child = ""
112+
bitstring.size.times do |i|
113+
bit = bitstring[i].chr
114+
child << ((rand()<@p_mutation) ? ((bit=='1') ? "0" : "1") : bit)
115+
end
116+
return child
117+
end
118+
119+
def reproduce(p1, p2)
120+
child = {}
121+
child[:bitstring] = uniform_crossover(p1[:bitstring], p2[:bitstring])
122+
child[:bitstring] = point_mutation(child[:bitstring])
123+
return child
124+
end
125+
126+
def execute
127+
parent1 = @queue_in.pop
128+
parent2 = @queue_in.pop
129+
@queue_out.push(reproduce(parent1, parent2))
130+
@queue_out.push(reproduce(parent2, parent1))
131+
end
132+
end
133+
134+
def random_bitstring(num_bits)
135+
return (0...num_bits).inject(""){|s,i| s<<((rand<0.5) ? "1" : "0")}
136+
end
137+
138+
def search(population_size=100, num_bits=64)
139+
# create the pipeline
140+
eval = EvalFlowUnit.new
141+
stopcondition = StopConditionUnit.new(eval.queue_out)
142+
selection = SelectFlowUnit.new(stopcondition.queue_out)
143+
variation = VariationFlowUnit.new(selection.queue_out, eval.queue_in)
144+
# push random solutions into the pipeline
145+
population_size.times do
146+
solution = {:bitstring=>random_bitstring(num_bits)}
147+
eval.queue_in.push(solution)
148+
end
149+
stopcondition.thread.join
150+
return stopcondition.best
151+
end
152+
153+
if __FILE__ == $0
154+
best = search()
155+
puts "done! Solution: f=#{best[:fitness]}, s=#{best[:bitstring]}"
156+
end

0 commit comments

Comments
 (0)