Commit 07995d97 authored by Martin Beseda's avatar Martin Beseda

ENH: Added Makefiles and Connection module separated into two file - one with...

ENH: Added Makefiles and Connection module separated into two file - one with module itself and the other one for testing purposes.
parent 6284c756
*.o
*.mod
*.out
before_script:
- apt-get install make
- make -j 4
- make
test_connection:
script: "connection_test.out"
......@@ -51,7 +51,7 @@ PROJECT_BRIEF = "Fortran neural networks library"
# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
# the logo to the output directory.
PROJECT_LOGO = "../img/4neuro_logo_small.png"
PROJECT_LOGO = "img/4neuro_logo_small.png"
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
# into which the generated documentation will be written. If a relative path is
......
.DEFAULT_GOAL:=all
all: build
.PHONY:=build
build:
$(MAKE) -C src
.PHONY:=clean
clean:
$(MAKE) -C src clean
#----------------#--------------------------------------------
# User variables #
#----------------#
# Source codes
SOURCES:=$(wildcard *.f08)
# *.mod files, which are supposed to be build
MODULES:=connection_m.mod
# Module object files, used for linking with binaries
MOD_OBJS:=$(MODULES:.mod=.o)
# *.out files, which are supposed to be build
RUNNABLE:=connection_test.out
# Already built files
#BUILT_FILES:="$(wildcard *.out) $(wildcard *.mod)"
# Fortran compiler
FC:=gfortran
# Fortran standard flag used to build
FORT_STD=-std=f2008
#--------------------#----------------------------------------
# Makefile variables #
#--------------------#
# Default target
.DEFAULT_GOAL:=all
#--------------#----------------------------------------------
# Make targets #
#--------------#
.PHONY=clean
clean:
rm -f *.out *.mod *.o
all: ${MOD_OBJS} ${RUNNABLE}
connection_m.o: connection_m.f08
${FC} ${FORT_STD} -c connection_m.f08
connection_test.out: connection_test.f08 connection_m.o
${FC} ${FORT_STD} -o connection_test.out connection_test.f08 connection_m.o
module Connection_mod
module connection_m
implicit none
public
! TODO smazat
type :: Neuron
type :: neuron_t
real :: state
contains
procedure :: get_state => get_state_impl
procedure :: set_state => set_state_impl
end type Neuron
contains
procedure :: get_state => get_state_impl
procedure :: set_state => set_state_impl
end type neuron_t
!------------------!------------------------------------------------------------------------
! Type definitions !
!------------------!
!> Represents a connection between two neurons.
type, abstract :: Connection
type, abstract :: connection_t
private
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
class(neuron_t), pointer :: input_neuron !< Pointer to an input neuron
class(neuron_t), pointer :: output_neuron !< Pointer to an output neuron
real :: weight !< Weight of the connection
contains
contains
!> 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
!> Initializes the common connection_t class components
!! 'input_neuron', 'output_neuron' and 'weight'.
!! I.e. serves similarly to an abstract constructor.
procedure, private :: init_components => init_components_impl
!> Adds a given value to the current weight of the
!! connection.
!! @param added_value Number (real) to be added to the current weight
procedure :: adjust_weight => adjust_weight_impl
!> Adds a given value to the current weight of the
!! connection.
!! @param added_value Number (real) to be added to the current weight
procedure :: adjust_weight => adjust_weight_impl
!> Getter for the private 'input_neuron' component
!! @return Pointer to the input neuron (type Neuron, pointer)
procedure :: get_input_neuron => get_input_neuron_impl
!> Getter for the private 'input_neuron' component
!! @return Pointer to the input neuron (type neuron_t, pointer)
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
!> Getter for the private 'output_neuron' component
!! @return Pointer to the output neuron (type neuron_t, pointer)
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
!> Getter for the private 'weight' component
!! @return Weight of the connection (type real)
procedure :: get_weight => get_weight_impl
end type Connection
end type connection_t
!> Represents a connection between two neurons.
!! Able to pass a signal from an input neuron to
!! an output one.
type, extends(Connection) :: IntervalConnection
contains
type, extends(connection_t) :: interval_connection_t
contains
!> Passes (assigns) the product
!! input neuron state * weight)
!! to an output neuron.
procedure :: pass_signal => pass_signal_impl
end type IntervalConnection
!> Passes (assigns) the product
!! input neuron state * weight)
!! to an output neuron.
procedure :: pass_signal => pass_signal_impl
end type interval_connection_t
!------------!------------------------------------------------------------------------------
! Interfaces !
!------------!
interface IntervalConnection
!> Constructor of IntervalConnection class
interface interval_connection_t
!> Constructor of interval_connection_t class
module procedure :: new_interval_connection
end interface IntervalConnection
end interface interval_connection_t
contains
!------------------------!------------------------------------------------------------------
! Method implementations !
! -----------------------!
!------------------------!------------------------------------------------------------------
! Method implementations !
! -----------------------!
!--------------!
! class Neuron !
! class neuron_t !
!--------------!
! TODO smazat
function get_state_impl(this) result(state)
class(Neuron), intent(in) :: this
class(neuron_t), intent(in) :: this
real :: state
state = this%state
......@@ -88,19 +88,19 @@
! TODO smazat
subroutine set_state_impl(this, new_state)
class(Neuron), target :: this
class(neuron_t), target :: this
real, intent(in) :: new_state
this%state = new_state
end subroutine set_state_impl
!------------------!
! class Connection !
! class connection_t !
!------------------!
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
class(connection_t), intent(inout) :: this
type(neuron_t), pointer :: input_neuron
type(neuron_t), pointer :: output_neuron
real, intent(in) :: weight
this%input_neuron => input_neuron
......@@ -108,18 +108,21 @@
this%weight = weight
end subroutine init_components_impl
!> Adds a given value to the current weight of the
!! connection.
!! @param added_value Number (real) to be added to the current weight
subroutine adjust_weight_impl(this, added_value)
class(Connection), intent(inout) :: this
class(connection_t), intent(inout) :: this
real, intent(in) :: added_value
this%weight = this%weight + added_value
end subroutine adjust_weight_impl
!--------------------------!
! class IntervalConnection !
! class interval_connection_t !
!--------------------------!
subroutine pass_signal_impl(this)
class(IntervalConnection), intent(in) :: this
class(interval_connection_t), intent(in) :: this
call this%output_neuron%set_state(this%input_neuron%get_state() * this%weight)
end subroutine pass_signal_impl
......@@ -128,10 +131,10 @@
! Constructors !
!--------------!
function new_interval_connection(input_neuron, output_neuron, weight) result(ret_obj)
type(Neuron), pointer :: input_neuron
type(Neuron), pointer :: output_neuron
type(neuron_t), pointer :: input_neuron
type(neuron_t), pointer :: output_neuron
real, intent(in) :: weight
type(IntervalConnection), pointer :: ret_obj
type(interval_connection_t), pointer :: ret_obj
allocate(ret_obj)
......@@ -142,60 +145,26 @@
! Getters & Setters !
!-------------------!
!> asdf
!! aaaa
function get_input_neuron_impl(this) result (input_neuron)
class(Connection), target, intent(in) :: this
class(Neuron), pointer :: input_neuron
class(connection_t), target, intent(in) :: this
class(neuron_t), pointer :: input_neuron
input_neuron => this%input_neuron
end function get_input_neuron_impl
function get_output_neuron_impl(this) result (output_neuron)
class(Connection), target, intent(in) :: this
class(Neuron), pointer :: output_neuron
class(connection_t), target, intent(in) :: this
class(neuron_t), pointer :: output_neuron
output_neuron => this%output_neuron
end function get_output_neuron_impl
function get_weight_impl(this) result (weight)
class(Connection), intent(in) :: this
class(connection_t), intent(in) :: this
real :: weight
weight = this%weight
end function get_weight_impl
end module Connection_mod
program a
use Connection_mod
type(Neuron), target :: n1
type(Neuron), target :: n2
type(Neuron), pointer :: n1_p
type(Neuron), pointer :: n2_p
type(Neuron), pointer :: dummy_p
type(IntervalConnection), pointer :: con
n1 = Neuron(21)
n2 = Neuron(13)
n1_p => n1
n2_p => n2
con => IntervalConnection(n1_p, n2_p, 5.0)
print *, 'weight: ', con%get_weight()
dummy_p => con%get_input_neuron()
print *, dummy_p%get_state()
dummy_p => con%get_output_neuron()
print *, dummy_p%get_state()
call con%pass_signal()
print *, dummy_p%get_state()
end module connection_m
end program a
program a
use connection_m
type(neuron_t), target :: n1
type(neuron_t), target :: n2
type(neuron_t), pointer :: n1_p
type(neuron_t), pointer :: n2_p
type(neuron_t), pointer :: dummy_p
type(interval_connection_t), pointer :: con
n1 = neuron_t(21)
n2 = neuron_t(13)
n1_p => n1
n2_p => n2
con => interval_connection_t(n1_p, n2_p, 5.0)
print *, 'weight: ', con%get_weight()
dummy_p => con%get_input_neuron()
print *, dummy_p%get_state()
dummy_p => con%get_output_neuron()
print *, dummy_p%get_state()
call con%pass_signal()
print *, dummy_p%get_state()
end program a
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment