Skip to content

Commit

Permalink
Drhuffman12/add team utils part 2 (#57)
Browse files Browse the repository at this point in the history
* 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'
  • Loading branch information
drhuffman12 committed Feb 2, 2021
1 parent b737173 commit a9f5a4f
Show file tree
Hide file tree
Showing 22 changed files with 868 additions and 433 deletions.
7 changes: 1 addition & 6 deletions Dockerfile
@@ -1,13 +1,8 @@
# FROM crystallang/crystal:nightly-build
FROM crystallang/crystal:nightly-alpine-build

WORKDIR /app
ADD . /app

# RUN shards install
# RUN shards update
# RUN shards build

RUN shards install --ignore-crystal-version
RUN shards update --ignore-crystal-version
RUN shards build
RUN shards build
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -42,9 +42,17 @@ net2 = Ai4cr::NeuralNetwork::Backpropagation.from_json(json)
assert_approximate_equality_of_nested_list net.weights, net2.weights, 0.000000001
```

## Multithreading
## Optimiaztions

Use the `-Dpreview_mt` flag for multithreading.
* Compiler

Use `--release` for more code optimizations during the compilation steps.

e.g.: `time crystal spec --release`

* Multithreading

Use the `-Dpreview_mt` (or `-D preview_mt`) flag for multithreading.

See also:
* https://crystal-lang.org/2019/09/23/crystal-0.31.0-released.html
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
@@ -1,5 +1,5 @@
name: ai4cr
version: 0.1.19
version: 0.1.20

authors:
- Daniel Huffman <drhuffman12@yahoo.com>
Expand Down
118 changes: 99 additions & 19 deletions spec/ai4cr/breed/manager_spec.cr
Expand Up @@ -15,16 +15,16 @@ class MyBreed
property some_value : Float64 = -1.0
property some_array = Array(Float64).new(2) { rand }

ALLOWED_STRING_FIRST = "a" # 'a' # .ord
ALLOWED_STRING_LAST = "z" # 'z' # .ord
ALLOWED_STRING_FIRST = "a"
ALLOWED_STRING_LAST = "z"
ALLOWED_STRINGS = (ALLOWED_STRING_FIRST..ALLOWED_STRING_LAST).to_a
property some_string : String = (ALLOWED_STRINGS.sample) * 2

def initialize(@name, @some_value)
end
end

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

Spectator.describe Ai4cr::Breed::Manager do
let(my_breed_manager) { MyBreedManager.new }

describe "For Adam and Eve examples" do
# TODO: Split this up into smaller tests!

let(my_breeder) { MyBreeder.new }
let(parallel_universe_breeder) { MyBreeder.new }
let(delta_child_1) { (rand*2 - 0.5) }
let(delta_child_2) { (rand*2 - 0.5) }

let(ancestor_adam_value) { 0.0 }
let(ancestor_eve_value) { 1.0 }

let(ancestor_adam_in_parallel_universe) { parallel_universe_breeder.create(name: "Adam", some_value: ancestor_adam_value) }
let(ancestor_adam) { my_breeder.create(name: "Adam", some_value: ancestor_adam_value) }
let(ancestor_eve) { my_breeder.create(name: "Eve", some_value: ancestor_eve_value) }
let(ancestor_adam) { my_breed_manager.create(name: "Adam", some_value: ancestor_adam_value) }
let(ancestor_eve) { my_breed_manager.create(name: "Eve", some_value: ancestor_eve_value) }

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

it "birth_id's are in the correct order (when birthed in correct order" do
it "birth_id's are in the consistent order (when birthed in order" do
expected_birth_counter = 0
puts_debug
puts_debug "ancestor_adam_in_parallel_universe: #{ancestor_adam_in_parallel_universe.to_json}"
puts_debug "ancestor_adam: #{ancestor_adam.to_json}"
puts_debug
puts_debug "ancestor_eve: #{ancestor_eve.to_json}"

expected_birth_counter += 1

# NOTE: We probably should convert the 'birth_id' from an instance variable to a class variable!
# Otherwise, you could get multiple instances with separate counters,
# which might or might not be desirable:
expect(ancestor_adam_in_parallel_universe.birth_id).to eq(expected_birth_counter)
expect(ancestor_adam.birth_id).to eq(expected_birth_counter)

expect(ancestor_adam.some_value).to eq(ancestor_adam_value)

expected_birth_counter += 1
expect(ancestor_eve.birth_id).to eq(expected_birth_counter)
expect(ancestor_eve.some_value).to eq(ancestor_eve_value)

# cain
child_1 = my_breeder.breed(ancestor_adam, ancestor_eve, delta: delta_child_1)
child_1 = my_breed_manager.breed(ancestor_adam, ancestor_eve, delta: delta_child_1)
child_1.name = "Cain, child of #{child_1.name} and #{ancestor_eve.name}"

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

# abel
child_2 = my_breeder.breed(ancestor_adam, ancestor_eve, delta: delta_child_2)
child_2 = my_breed_manager.breed(ancestor_adam, ancestor_eve, delta: delta_child_2)
child_2.name = "Abel, child of #{child_2.name} and #{ancestor_eve.name}"

puts_debug "child_2: #{child_2.to_json}"
Expand All @@ -140,10 +132,98 @@ Spectator.describe Ai4cr::Breed::Manager do
puts_debug
puts_debug "Now, in order or youngest to oldest:"
[ancestor_adam, ancestor_eve, child_1, child_2].sort_by do |person|
-person.birth_id
(-person.birth_id)
end.each do |person|
puts_debug "person: #{person.to_json}"
end
end
end

describe "#estimate_better_delta" do
let(delta_estimated) { my_breed_manager.estimate_better_delta(error_a, error_b) }
let(zero_estimated) { error_a + delta_estimated * (error_b - error_a) }
let(zero) { 0.0 }

describe "a before b" do
context "0 a b" do
let(error_a) { 0.25 }
let(error_b) { 0.75 }
let(delta_expected) { -0.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end

context "a 0 b" do
let(error_a) { -0.5 }
let(error_b) { 0.5 }
let(delta_expected) { 0.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end

context "a b 0" do
let(error_a) { -0.75 }
let(error_b) { -0.25 }
let(delta_expected) { 1.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end
end

describe "b before a" do
context "0 b a" do
let(error_a) { 0.75 }
let(error_b) { 0.25 }
let(delta_expected) { 1.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end

context "a 0 b" do
let(error_a) { 0.5 }
let(error_b) { -0.5 }
let(delta_expected) { 0.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end

context "b a 0" do
let(error_a) { -0.25 }
let(error_b) { -0.75 }
let(delta_expected) { -0.5 }

it "estimates a delta" do
expect(delta_estimated).to eq(delta_expected)
end
it "estimates a delta that estimates zero" do
expect(zero_estimated).to eq(zero)
end
end
end
end
end

0 comments on commit a9f5a4f

Please sign in to comment.