Skip to content

Commit

Permalink
- some minor stuff:
Browse files Browse the repository at this point in the history
- Math.sign/1
- Sync @Tolerance (commented)
  • Loading branch information
zampino committed Oct 18, 2015
1 parent b027724 commit 303c3e1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/exnn/actuator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule EXNN.Actuator do
defimpl EXNN.Connection do
def signal(actuator, message, metadata) do
state = unquote(caller).act(actuator, message, metadata)
# TODO: pass actuator state to fitness as well
unquote(caller).notify_fitness(message, metadata)
state
end
Expand Down
2 changes: 1 addition & 1 deletion lib/exnn/events/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule EXNN.Events.Manager do
end

# SERVER CALLBACKS

def handle_event({:fitness, {message, metadata}}, messages) do
:ok = EXNN.Fitness.eval message, metadata
{:ok, messages}
Expand Down
2 changes: 0 additions & 2 deletions lib/exnn/fitness.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ defmodule EXNN.Fitness do
end

# internal api

def eval(_message, _meta, _state) do
raise "NotImplementedError"
end
Expand All @@ -44,7 +43,6 @@ defmodule EXNN.Fitness do
end

# server callbacks

def handle_cast {:eval, message, meta}, state do
{:noreply, eval(message, meta, state)}
end
Expand Down
6 changes: 3 additions & 3 deletions lib/exnn/genome.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ defmodule EXNN.Genome do
ids |> Enum.map &build(type, &1)
end

def random_bias, do: (Math.pi * Random.uniform)

def build(:neuron, id) do
%{type: :neuron, id: id, bias: random_bias, activation: &Math.sin(&1)}
%{type: :neuron, id: id, activation: &Math.sin(&1), bias: random_bias}
end

def random_bias, do: Math.pi * Random.uniform

def build(type, id) do
%{type: type, id: id}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/exnn/trainer/mutations/set.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule EXNN.Trainer.Mutations.Set do
# :delete_link,
# :add_node,
# :remove_node
:alter_bias
# :alter_bias
# :alter_activation
# :swap_activation
]
Expand Down
17 changes: 13 additions & 4 deletions lib/exnn/trainer/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule EXNN.Trainer.Sync do
import EXNN.Utils.Logger
alias EXNN.Trainer.Mutations

@tolerance 0.001

@train_interval 100

def start_link do
Expand Down Expand Up @@ -49,15 +51,22 @@ defmodule EXNN.Trainer.Sync do
{:ok, ref}
end

# def handle_call({:sync, %{fitness: value}}, _from, state)
# when value > 1 - @tolerance do
# log "PASS", ""
# schedule_training_task state.sensors
# {:reply, :ok, %{state | counter: state.counter + 1}}
# end

def handle_call {:sync, %{fitness: value}}, _from, state do
new_state = cond do
state.counter > state.max_attempts -> exit(:normal)
state.counter > state.max_attempts -> exit(:normal)
state.reverts_count > state.max_reverts -> reset(state)
value <= state.fitness -> less_fit(state)
true -> fitter(state, value)
value <= state.fitness -> less_fit(state)
true -> fitter(state, value)
end
Mutations.step
{:ok, _ref} = schedule_training_task state.sensors
schedule_training_task state.sensors
log "STATS:", state, :info
{:reply, :ok, %{new_state | counter: new_state.counter + 1}}
end
Expand Down
6 changes: 6 additions & 0 deletions lib/exnn/utils/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ defmodule EXNN.Utils.Math do

def id(x), do: x

def sign(0), do: 0
def sign(0.0), do: 0
def sign(x) do
:erlang.trunc(abs(x)/x)
end

def tanh(x), do: :math.tanh(x)

def sqrt(x), do: :math.sqrt(x)
Expand Down

0 comments on commit 303c3e1

Please sign in to comment.