Skip to content
Snippets Groups Projects
Commit 6284c756 authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH + FIX: Added comment to multiple methods + fixed constructor...

ENH + FIX: Added comment to multiple methods + fixed constructor implementation for the class IntervalConnection.
parent be95744d
No related branches found
No related tags found
No related merge requests found
module Connection_mod module Connection_mod
implicit none implicit none
public public
! TODO smazat ! TODO smazat
!> Dummy class Neuron
!! blabla
type :: Neuron type :: Neuron
real :: state ! TODO v realne tride 'private'! real :: state
contains contains
procedure :: get_state => get_state_impl procedure :: get_state => get_state_impl
...@@ -20,41 +18,57 @@ module Connection_mod ...@@ -20,41 +18,57 @@ module Connection_mod
!> Represents a connection between two neurons. !> Represents a connection between two neurons.
type, abstract :: Connection type, abstract :: Connection
class(Neuron), pointer, private :: input_neuron private
class(Neuron), pointer, private :: output_neuron
real, private :: weight class(Neuron), pointer :: input_neuron !< Pointer to an input neuron
class(Neuron), pointer :: output_neuron !< Pointer to an output neuron
real :: weight !< Weight of the connection
contains contains
!> Adds a gven value to the current weight of the !> Initializes the common Connection class components
!! 'input_neuron', 'output_neuron' and 'weight'.
!! I.e. serves similarly to an abstract constructor.
procedure :: init_components => init_components_impl
!> Adds a given value to the current weight of the
!! connection. !! connection.
!! @param added_value Number (real) to be added to the current weight !! @param added_value Number (real) to be added to the current weight
procedure :: adjust_weight => adjust_weight_impl procedure :: adjust_weight => adjust_weight_impl
! Constructors !> Getter for the private 'input_neuron' component
procedure :: new_connection => new_connection_impl !! @return Pointer to the input neuron (type Neuron, pointer)
! Getters and setters
procedure :: get_input_neuron => get_input_neuron_impl procedure :: get_input_neuron => get_input_neuron_impl
!> Getter for the private 'output_neuron' component
!! @return Pointer to the output neuron (type Neuron, pointer)
procedure :: get_output_neuron => get_output_neuron_impl procedure :: get_output_neuron => get_output_neuron_impl
!> Getter for the private 'weight' component
!! @return Weight of the connection (type real)
procedure :: get_weight => get_weight_impl procedure :: get_weight => get_weight_impl
end type Connection end type Connection
!> Represents a connection between two neurons.
!! Able to pass a signal from an input neuron to
!! an output one.
type, extends(Connection) :: IntervalConnection type, extends(Connection) :: IntervalConnection
contains contains
!> Passes (assigns) the product
!! input neuron state * weight)
!! to an output neuron.
procedure :: pass_signal => pass_signal_impl procedure :: pass_signal => pass_signal_impl
end type IntervalConnection end type IntervalConnection
!------------!------------------------------------------------------------------------------ !------------!------------------------------------------------------------------------------
! Interfaces ! ! Interfaces !
!------------! !------------!
interface IntervalConnection
! For signatures of abstract functions !> Constructor of IntervalConnection class
module procedure :: new_interval_connection
!interface Connection end interface IntervalConnection
!
!end interface Connection
contains contains
!------------------------!------------------------------------------------------------------ !------------------------!------------------------------------------------------------------
...@@ -83,6 +97,17 @@ module Connection_mod ...@@ -83,6 +97,17 @@ module Connection_mod
!------------------! !------------------!
! class Connection ! ! class Connection !
!------------------! !------------------!
subroutine init_components_impl(this, input_neuron, output_neuron, weight)
class(Connection), intent(inout) :: this
type(Neuron), pointer :: input_neuron
type(Neuron), pointer :: output_neuron
real, intent(in) :: weight
this%input_neuron => input_neuron
this%output_neuron => output_neuron
this%weight = weight
end subroutine init_components_impl
subroutine adjust_weight_impl(this, added_value) subroutine adjust_weight_impl(this, added_value)
class(Connection), intent(inout) :: this class(Connection), intent(inout) :: this
real, intent(in) :: added_value real, intent(in) :: added_value
...@@ -96,28 +121,29 @@ module Connection_mod ...@@ -96,28 +121,29 @@ module Connection_mod
subroutine pass_signal_impl(this) subroutine pass_signal_impl(this)
class(IntervalConnection), intent(in) :: this class(IntervalConnection), intent(in) :: this
call this%output_neuron%set_state(5.0) call this%output_neuron%set_state(this%input_neuron%get_state() * this%weight)
end subroutine pass_signal_impl end subroutine pass_signal_impl
!--------------!------------------------------------------------------------------------ !--------------!------------------------------------------------------------------------
! Constructors ! ! Constructors !
!--------------! !--------------!
subroutine new_connection_impl(this, input_neuron, output_neuron, weight) function new_interval_connection(input_neuron, output_neuron, weight) result(ret_obj)
class(Connection), intent(out) :: this type(Neuron), pointer :: input_neuron
class(Neuron), pointer, intent(in) :: input_neuron type(Neuron), pointer :: output_neuron
class(Neuron), pointer, intent(in) :: output_neuron real, intent(in) :: weight
real, intent(in) :: weight type(IntervalConnection), pointer :: ret_obj
this%input_neuron => input_neuron allocate(ret_obj)
this%output_neuron => output_neuron
this%weight = weight
print *, 'Constructor!' call ret_obj%init_components(input_neuron, output_neuron, weight)
end subroutine new_connection_impl end function new_interval_connection
!-------------------!------------------------------------------------------------------- !-------------------!-------------------------------------------------------------------
! Getters & Setters ! ! Getters & Setters !
!-------------------! !-------------------!
!> asdf
!! aaaa
function get_input_neuron_impl(this) result (input_neuron) function get_input_neuron_impl(this) result (input_neuron)
class(Connection), target, intent(in) :: this class(Connection), target, intent(in) :: this
class(Neuron), pointer :: input_neuron class(Neuron), pointer :: input_neuron
...@@ -131,13 +157,14 @@ module Connection_mod ...@@ -131,13 +157,14 @@ module Connection_mod
output_neuron => this%output_neuron output_neuron => this%output_neuron
end function get_output_neuron_impl end function get_output_neuron_impl
function get_weight_impl(this) result (weight) function get_weight_impl(this) result (weight)
class(Connection), intent(in) :: this class(Connection), intent(in) :: this
real :: weight real :: weight
weight = this%weight weight = this%weight
end function get_weight_impl end function get_weight_impl
end module Connection_mod end module Connection_mod
program a program a
...@@ -150,14 +177,15 @@ program a ...@@ -150,14 +177,15 @@ program a
type(Neuron), pointer :: n2_p type(Neuron), pointer :: n2_p
type(Neuron), pointer :: dummy_p type(Neuron), pointer :: dummy_p
type(IntervalConnection) :: con type(IntervalConnection), pointer :: con
n1 = Neuron(21) n1 = Neuron(21)
n2 = Neuron(13) n2 = Neuron(13)
n1_p => n1 n1_p => n1
n2_p => n2 n2_p => n2
call con%new_connection(n1, n2, 5.12)
con => IntervalConnection(n1_p, n2_p, 5.0)
print *, 'weight: ', con%get_weight() print *, 'weight: ', con%get_weight()
...@@ -166,4 +194,8 @@ program a ...@@ -166,4 +194,8 @@ program a
dummy_p => con%get_output_neuron() dummy_p => con%get_output_neuron()
print *, dummy_p%get_state() print *, dummy_p%get_state()
call con%pass_signal()
print *, dummy_p%get_state()
end program a end program a
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment