Skip to content

Commit a9f5a4f

Browse files
authored
Drhuffman12/add team utils part 2 (#57)
* drhuffman12/add_team_utils_part_2 code cleanup * drhuffman12/add_team_utils_part_2 code cleanup * drhuffman12/add_team_utils_part_2 refactoring and code cleanup * drhuffman12/add_team_utils_part_2 code cleanup * drhuffman12/add_team_utils_part_2 reorg properties and initializers * drhuffman12/add_team_utils_part_2 reorg properties and initializers * drhuffman12/add_team_utils_part_2 convert SafeCounter to use a class variable; add testes * drhuffman12/add_team_utils_part_2 add MiniNetManager * drhuffman12/add_team_utils_part_2 test cleanup with more debugging/logging * drhuffman12/add_team_utils_part_2 add Ai4cr::Data::Utils.rand_excluding and use for tests re MiniNetManager * drhuffman12/add_team_utils_part_2 add Ai4cr::Data::Utils.rand_excluding related convenience methods and expand usage * drhuffman12/add_team_utils_part_2 contantize some values in Ai4cr::Data::Utils * drhuffman12/add_team_utils_part_2 bump version from '0.1.19' to '0.1.20'
1 parent b737173 commit a9f5a4f

22 files changed

+868
-433
lines changed

Dockerfile

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
# FROM crystallang/crystal:nightly-build
21
FROM crystallang/crystal:nightly-alpine-build
32

43
WORKDIR /app
54
ADD . /app
65

7-
# RUN shards install
8-
# RUN shards update
9-
# RUN shards build
10-
116
RUN shards install --ignore-crystal-version
127
RUN shards update --ignore-crystal-version
13-
RUN shards build
8+
RUN shards build

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ net2 = Ai4cr::NeuralNetwork::Backpropagation.from_json(json)
4242
assert_approximate_equality_of_nested_list net.weights, net2.weights, 0.000000001
4343
```
4444

45-
## Multithreading
45+
## Optimiaztions
4646

47-
Use the `-Dpreview_mt` flag for multithreading.
47+
* Compiler
48+
49+
Use `--release` for more code optimizations during the compilation steps.
50+
51+
e.g.: `time crystal spec --release`
52+
53+
* Multithreading
54+
55+
Use the `-Dpreview_mt` (or `-D preview_mt`) flag for multithreading.
4856

4957
See also:
5058
* https://crystal-lang.org/2019/09/23/crystal-0.31.0-released.html

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: ai4cr
2-
version: 0.1.19
2+
version: 0.1.20
33

44
authors:
55
- Daniel Huffman <[email protected]>

spec/ai4cr/breed/manager_spec.cr

+99-19
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class MyBreed
1515
property some_value : Float64 = -1.0
1616
property some_array = Array(Float64).new(2) { rand }
1717

18-
ALLOWED_STRING_FIRST = "a" # 'a' # .ord
19-
ALLOWED_STRING_LAST = "z" # 'z' # .ord
18+
ALLOWED_STRING_FIRST = "a"
19+
ALLOWED_STRING_LAST = "z"
2020
ALLOWED_STRINGS = (ALLOWED_STRING_FIRST..ALLOWED_STRING_LAST).to_a
2121
property some_string : String = (ALLOWED_STRINGS.sample) * 2
2222

2323
def initialize(@name, @some_value)
2424
end
2525
end
2626

27-
class MyBreeder < Ai4cr::Breed::Manager(MyBreed)
27+
class MyBreedManager < Ai4cr::Breed::Manager(MyBreed)
2828
def mix_parts(child : T, parent_a : T, parent_b : T, delta)
2929
some_value = mix_one_part_number(parent_a.some_value, parent_b.some_value, delta)
3030
child.some_value = some_value
@@ -44,20 +44,18 @@ def puts_debug(message = "")
4444
end
4545

4646
Spectator.describe Ai4cr::Breed::Manager do
47+
let(my_breed_manager) { MyBreedManager.new }
48+
4749
describe "For Adam and Eve examples" do
4850
# TODO: Split this up into smaller tests!
49-
50-
let(my_breeder) { MyBreeder.new }
51-
let(parallel_universe_breeder) { MyBreeder.new }
5251
let(delta_child_1) { (rand*2 - 0.5) }
5352
let(delta_child_2) { (rand*2 - 0.5) }
5453

5554
let(ancestor_adam_value) { 0.0 }
5655
let(ancestor_eve_value) { 1.0 }
5756

58-
let(ancestor_adam_in_parallel_universe) { parallel_universe_breeder.create(name: "Adam", some_value: ancestor_adam_value) }
59-
let(ancestor_adam) { my_breeder.create(name: "Adam", some_value: ancestor_adam_value) }
60-
let(ancestor_eve) { my_breeder.create(name: "Eve", some_value: ancestor_eve_value) }
57+
let(ancestor_adam) { my_breed_manager.create(name: "Adam", some_value: ancestor_adam_value) }
58+
let(ancestor_eve) { my_breed_manager.create(name: "Eve", some_value: ancestor_eve_value) }
6159

6260
let(some_array_expected_1) {
6361
ancestor_adam.some_array.map_with_index do |sa, i|
@@ -91,30 +89,24 @@ Spectator.describe Ai4cr::Breed::Manager do
9189
delta_child_2 < 0.5 ? parent_a_part : parent_b_part
9290
}
9391

94-
it "birth_id's are in the correct order (when birthed in correct order" do
92+
it "birth_id's are in the consistent order (when birthed in order" do
9593
expected_birth_counter = 0
9694
puts_debug
97-
puts_debug "ancestor_adam_in_parallel_universe: #{ancestor_adam_in_parallel_universe.to_json}"
9895
puts_debug "ancestor_adam: #{ancestor_adam.to_json}"
9996
puts_debug
10097
puts_debug "ancestor_eve: #{ancestor_eve.to_json}"
10198

10299
expected_birth_counter += 1
103100

104-
# NOTE: We probably should convert the 'birth_id' from an instance variable to a class variable!
105-
# Otherwise, you could get multiple instances with separate counters,
106-
# which might or might not be desirable:
107-
expect(ancestor_adam_in_parallel_universe.birth_id).to eq(expected_birth_counter)
108101
expect(ancestor_adam.birth_id).to eq(expected_birth_counter)
109-
110102
expect(ancestor_adam.some_value).to eq(ancestor_adam_value)
111103

112104
expected_birth_counter += 1
113105
expect(ancestor_eve.birth_id).to eq(expected_birth_counter)
114106
expect(ancestor_eve.some_value).to eq(ancestor_eve_value)
115107

116108
# cain
117-
child_1 = my_breeder.breed(ancestor_adam, ancestor_eve, delta: delta_child_1)
109+
child_1 = my_breed_manager.breed(ancestor_adam, ancestor_eve, delta: delta_child_1)
118110
child_1.name = "Cain, child of #{child_1.name} and #{ancestor_eve.name}"
119111

120112
puts_debug "child_1: #{child_1.to_json}"
@@ -126,7 +118,7 @@ Spectator.describe Ai4cr::Breed::Manager do
126118
expect(child_1.some_string).to eq(some_string_expected_1)
127119

128120
# abel
129-
child_2 = my_breeder.breed(ancestor_adam, ancestor_eve, delta: delta_child_2)
121+
child_2 = my_breed_manager.breed(ancestor_adam, ancestor_eve, delta: delta_child_2)
130122
child_2.name = "Abel, child of #{child_2.name} and #{ancestor_eve.name}"
131123

132124
puts_debug "child_2: #{child_2.to_json}"
@@ -140,10 +132,98 @@ Spectator.describe Ai4cr::Breed::Manager do
140132
puts_debug
141133
puts_debug "Now, in order or youngest to oldest:"
142134
[ancestor_adam, ancestor_eve, child_1, child_2].sort_by do |person|
143-
-person.birth_id
135+
(-person.birth_id)
144136
end.each do |person|
145137
puts_debug "person: #{person.to_json}"
146138
end
147139
end
148140
end
141+
142+
describe "#estimate_better_delta" do
143+
let(delta_estimated) { my_breed_manager.estimate_better_delta(error_a, error_b) }
144+
let(zero_estimated) { error_a + delta_estimated * (error_b - error_a) }
145+
let(zero) { 0.0 }
146+
147+
describe "a before b" do
148+
context "0 a b" do
149+
let(error_a) { 0.25 }
150+
let(error_b) { 0.75 }
151+
let(delta_expected) { -0.5 }
152+
153+
it "estimates a delta" do
154+
expect(delta_estimated).to eq(delta_expected)
155+
end
156+
it "estimates a delta that estimates zero" do
157+
expect(zero_estimated).to eq(zero)
158+
end
159+
end
160+
161+
context "a 0 b" do
162+
let(error_a) { -0.5 }
163+
let(error_b) { 0.5 }
164+
let(delta_expected) { 0.5 }
165+
166+
it "estimates a delta" do
167+
expect(delta_estimated).to eq(delta_expected)
168+
end
169+
it "estimates a delta that estimates zero" do
170+
expect(zero_estimated).to eq(zero)
171+
end
172+
end
173+
174+
context "a b 0" do
175+
let(error_a) { -0.75 }
176+
let(error_b) { -0.25 }
177+
let(delta_expected) { 1.5 }
178+
179+
it "estimates a delta" do
180+
expect(delta_estimated).to eq(delta_expected)
181+
end
182+
it "estimates a delta that estimates zero" do
183+
expect(zero_estimated).to eq(zero)
184+
end
185+
end
186+
end
187+
188+
describe "b before a" do
189+
context "0 b a" do
190+
let(error_a) { 0.75 }
191+
let(error_b) { 0.25 }
192+
let(delta_expected) { 1.5 }
193+
194+
it "estimates a delta" do
195+
expect(delta_estimated).to eq(delta_expected)
196+
end
197+
it "estimates a delta that estimates zero" do
198+
expect(zero_estimated).to eq(zero)
199+
end
200+
end
201+
202+
context "a 0 b" do
203+
let(error_a) { 0.5 }
204+
let(error_b) { -0.5 }
205+
let(delta_expected) { 0.5 }
206+
207+
it "estimates a delta" do
208+
expect(delta_estimated).to eq(delta_expected)
209+
end
210+
it "estimates a delta that estimates zero" do
211+
expect(zero_estimated).to eq(zero)
212+
end
213+
end
214+
215+
context "b a 0" do
216+
let(error_a) { -0.25 }
217+
let(error_b) { -0.75 }
218+
let(delta_expected) { -0.5 }
219+
220+
it "estimates a delta" do
221+
expect(delta_estimated).to eq(delta_expected)
222+
end
223+
it "estimates a delta that estimates zero" do
224+
expect(zero_estimated).to eq(zero)
225+
end
226+
end
227+
end
228+
end
149229
end

0 commit comments

Comments
 (0)