Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shainet #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
app
worker
bundle
mass_nn_training
script
train_network
.env
.env.test
.env.test
*.csv
/nn_training/*.log
/nn_training/*.nn
/nn_training/old/
Empty file added nn_training/.keep
Empty file.
8 changes: 8 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
version: 2.0
shards:
apatite:
git: https://github.com/watzon/apatite.git
version: 0.1.0+git.commit.36f3aef41e31b75e824b9b60ec30cdc399ced9c9

backtracer:
git: https://github.com/sija/backtracer.cr.git
version: 1.2.2
Expand Down Expand Up @@ -76,6 +80,10 @@ shards:
git: https://github.com/samueleaton/sentry.git
version: 0.3.2+git.commit.e448ce83486f99ef016c311e10ec0cac805cded3

shainet:
git: https://github.com/neuralegion/shainet.git
version: 2.4.0

spec-kemal:
git: https://github.com/kemalcr/spec-kemal.git
version: 1.0.0
Expand Down
3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies:
mosquito:
github: mosquito-cr/mosquito
version: 1.0.0.rc3
shainet:
github: NeuraLegion/shainet
version: ~> 2.4.0

development_dependencies:
sam:
Expand Down
3 changes: 2 additions & 1 deletion spec/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ SUPPORTED_STRATEGIES = [
"blast_random_valid",
"chase_closest_food",
"chase_random_food",
"cautious_carol"
"cautious_carol",
"cc_nn"
]

describe "Crystal Snake Battlesnake endpoints for all supported strategies" do
Expand Down
27 changes: 27 additions & 0 deletions src/battle_snake/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,31 @@ class BattleSnake::Context
board.snakes.delete(snake)
end
end

def to_nn_input
turn_data = Array.new(121, 0.as(Int32 | String))
board.food.each do |food|
offset = food.x + (food.y * 11)
turn_data[offset] = 10
end

snake_counter = 100
you.body.each do |point|
offset = point.x + (point.y * 11)
turn_data[offset] = snake_counter
snake_counter += 1
end

enemies.each_with_index do |snake, i|
snake_counter = 200 + (i * 100)
snake.body.each do |point|
offset = point.x + (point.y * 11)
turn_data[offset] = snake_counter
snake_counter += 1
end
snake.body
end

turn_data.map(&.to_i32)
end
end
1 change: 1 addition & 0 deletions src/cc_nn.nn

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions src/mass_nn_training.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "dotenv"
Dotenv.load if File.exists?(".env")
require "../config/**"

# Shainet
layer_neurons_opts = [35, 60, 75]
learning_rate_opts = [0.005, 0.2, 0.7]
momentum_opts = [0.05, 0.1, 0.3]
error_threshold = 0.0001
option_permutations = [] of Hash(Symbol, Float64)
27.times do |i|
hash = Hash(Symbol, Float64).new
hash[:neurons] = layer_neurons_opts[i % 3]
hash[:learning_rate] = learning_rate_opts[(i / 3).to_i % 3]
hash[:momentum] = momentum_opts[(i / 9).to_i % 3]
option_permutations << hash
end

Log.info { "#{option_permutations.size} options" }
options_channel = Channel(Hash(Symbol, Float64)).new
complete_channel = Channel(Int32).new

5.times do |i|
spawn do
loop do
[200, 1000, 15000].shuffle.each do |epochs|
options = options_channel.receive
log_name = "./nn_training/cc_nn_#{options[:neurons].to_i}n_#{options[:learning_rate]}l_#{options[:momentum]}m_#{epochs}e_#{error_threshold}t.log"
command = "./train_network -n #{options[:neurons].to_i} -l #{options[:learning_rate]} -m #{options[:momentum]} -e #{epochs} -t #{error_threshold} | tee #{log_name}"
system command
end

complete_channel.send(i)
end
end
end

# Delivery permutations
spawn { option_permutations.shuffle.each { |options| options_channel.send(options) } }

# Wait for permutations processing
27.times do |i|
res = complete_channel.receive
Log.info { "[root] -> Finished processing from #{res}" }
end
4 changes: 2 additions & 2 deletions src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ load_dependencies "jennifer"
task "dev" do
sentry = Sentry::ProcessRunner.new(
display_name: "App",
build_command: "crystal build ./src/app.cr",
build_command: "crystal build ./src/app.cr -Dpreview_mt",
run_command: "./app",
run_args: ["-p", "8080"],
files: [ "./src/**/*", "./config/*.cr" ]
Expand All @@ -25,7 +25,7 @@ task "test" do
end

task "script" do
system "crystal run ./src/script.cr"
system "crystal run ./src/script.cr -Dpreview_mt"
end

Sam.help
Loading
Loading