-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.erl.bak
189 lines (164 loc) · 11 KB
/
node.erl.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
%% Author: nils
%% Created: May 27, 2009
%% Description: TODO: Add description to node
-module(node).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([node_life/5]).%start/0,
%, man_life/4
%%
%% API Functions
%%
%% Param:
%% + POSITION is a list of the positions in each dimension
%% + EDGES is a list of pairs {nodepid, age}
%% + ERROR is a real number
%% + PARENT is a ManagerPID
%% + ENGINE is the PID of the engine
%%
%% receives:
%%
%% + {set_parent, NEW_PARENT_PID} ->
%% - sets the Parent_PID of the node
%%
%% + {position_request, REQUESTING_PID }
%% - Sends REQUESTING_PID ! {my_position, self(), POSITION }
%%
%% + {edges_request, REQUESTING_PID} ->
%% - Sends REQUESTING_PID ! {my_edges, self(), EDGES }
%%
%% + {request_error, REQUESTING_PID} ->
%% - sends REQUESTING_PID ! {my_error, self(), ERROR }
%%
%% + {request_decreased_error, REQUESTING_PID, D} ->
%% - decreases the error by D in the node
%% - returns the decreased error
%% - this is request_error and decrease_error in one
%% - sends REQUESTING_PID ! {my_decreased_error, self(), ERROR*D }
%%
%% + {newEdge, OTHER_NODEPID} -> %has to be added also in the otherNode
%% - creates a new edge to OTHER_NODE
%%
%% + {ageEdges, MAX_AGE} ->
%% - ages the edges and removes the node if no edges left
%% - might send PARENT ! {removeNode, self(), self()},
%%
%% + {removeEdge, OTHER_NODE} ->
%% - has to be evoqued in both nodes
%% - removes the edge to OTHER_NODE
%%
%% + {addToError, DELTA_ERROR} ->
%% - adds DELTA_ERROR to ERROR
%%
%% + {decreaseError, ALPHA} ->
%% - ERROR <- ERROR * ALPHA
%%
%% + {moveKN, INPUT, MOVE_K, MOVE_N} ->
%% - sends {moveN, self(), INPUT, MOVE_N} to all neighbors
%% - moves this node MOVE_K towards INPUT
%%
%% + {moveN, INPUT, MOVE_N} ->
%% - moves this node MOVE_N towards INPUT
%%
%% + {refreshEdge, OTHER_NODE} -> %has to be called for both nodes
%% - sets the age of the edge to OTHER_NODE to 0 or creates this edge
%%
%% + {request_maxNeighbor, REQUESTING_PID} ->
%% - sends REQUESTING_PID ! {my_maxNeighbor, self(), maxNeighbor(EDGES)},
%%
%% + {request_distance, REQUESTING_PID, INPUT} -> %here is REQUESTING_PID needed
%% - sends REQUESTING_PID ! {my_distance, self(), distance(POSITION, INPUT)},
%%
node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) -> receive { ageEdges , MAX_AGE } -> io : format ( "NODE / ageEdges : ~p's old edges ~p ~n" , [ self ( ) , EDGES ] ) , TMP = ageEdges ( EDGES , MAX_AGE ) , ENGINE ! { ageEdges_done , self ( ) } , io : format ( "NODE / ageEdges : ~p's new edges ~p ~n" , [ self ( ) , TMP ] ) , if TMP == [ ] -> PARENT ! { removeNode , self ( ) } , ENGINE ! { nodeRemoved , PARENT } , io : format ( "NODE / ageEdges ~p : I'm dead, because i have no friends :( ~n" , [ self ( ) ] ) , exit ( "no friends" ) ; true -> io : format ( "NODE / ageEdges ~p : I have aged all my Edges ~n" , [ self ( ) ] ) , node_life ( POSITION , TMP , ERROR , PARENT , ENGINE ) end ; { ageMyEdge , OTHER_NODE } -> io : format ( "NODE / ageMyEdge ~p : edge to ~p should be aged~n" , [ self ( ) , OTHER_NODE ] ) , node_life ( POSITION , ageAnEdge ( EDGES , OTHER_NODE ) , ERROR , PARENT , ENGINE ) ; { newEdge , OTHER_NODEPID } -> io : format ( "NODE / newEdge ~p : I have created a new Edge with ~p ~n" , [ self ( ) , OTHER_NODEPID ] ) , io : format ( " my new EDGES ~p ~n" , [ [ { OTHER_NODEPID , 0 } | EDGES ] ] ) , node_life ( POSITION , [ { OTHER_NODEPID , 0 } | EDGES ] , ERROR , PARENT , ENGINE ) ; { request_position , REQUESTING_PID } -> io : format ( "NODE / request_position ~p : I'm sending my position: ~p to ~p ~n" , [ self ( ) , POSITION , REQUESTING_PID ] ) , REQUESTING_PID ! { my_position , self ( ) , POSITION } , node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) ; { request_edges , REQUESTING_PID } -> io : format ( "NODE / request_edges ~p : I'm sending my edges: ~p to ~p ~n" , [ self ( ) , EDGES , REQUESTING_PID ] ) , REQUESTING_PID ! { my_edges , self ( ) , EDGES } , node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) ; { request_error , REQUESTING_PID } -> io : format ( "NODE / request_error ~p : I'm sending my error: ~p to ~p ~n" , [ self ( ) , ERROR , REQUESTING_PID ] ) , REQUESTING_PID ! { my_error , self ( ) , ERROR } , node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) ; { request_decreased_error , REQUESTING_PID , D } -> io : format ( "NODE / request_decreased_error ~p : I'm sending my error: ~p to ~p ~n" , [ self ( ) , ERROR , REQUESTING_PID ] ) , REQUESTING_PID ! { my_decreased_error , self ( ) , ERROR * D } , node_life ( POSITION , EDGES , ERROR * D , PARENT , ENGINE ) ; { request_decreased_error_and_position , REQUESTING_PID , ALPHA } -> REQUESTING_PID ! { my_decreased_error_and_position , self ( ) , { ERROR * ALPHA , POSITION } } , io : format ( "NODE / request_decreased_error_and_position ~p : I returned my error ~p and my position ~p~n" , [ self ( ) , ERROR * ALPHA , POSITION ] ) , { ok , FILE_WRITE } = file : open ( nodes . dat , [ append ] ) , io : format ( FILE_WRITE , "~s~n" , [ posToStr2 ( POSITION ) ] ) , file : close ( FILE_WRITE ) , io : format ( " NODE / request_distance ~p : filewrite was OK~n" , [ self ( ) ] ) , node_life ( POSITION , EDGES , ERROR * ALPHA , PARENT , ENGINE ) ; { removeEdge , OTHER_NODE } -> TMP = removeEdge ( OTHER_NODE , EDGES ) , if TMP == [ ] -> PARENT ! { removeNode , self ( ) } , ENGINE ! { nodeRemoved , PARENT } , io : format ( "NODE / removeEdge ~p : I'm dead, because i have no friends :( ~n" , [ self ( ) ] ) , exit ( "no friends" ) ; true -> io : format ( "NODE / removeEdge ~p : edge to ~p was removed~n" , [ self ( ) , OTHER_NODE ] ) , node_life ( POSITION , TMP , ERROR , PARENT , ENGINE ) end ; { addToError , DELTA_ERROR } -> io : format ( "NODE / addToError ~p : my error is now ~p bigger ~n" , [ self ( ) , DELTA_ERROR ] ) , node_life ( POSITION , EDGES , ERROR + DELTA_ERROR , PARENT , ENGINE ) ; { decreaseError , ALPHA } -> io : format ( "NODE / decreaseError ~p : I decreased my error to a ~p part ~n" , [ self ( ) , ALPHA ] ) , node_life ( POSITION , EDGES , ERROR * ALPHA , PARENT , ENGINE ) ; { moveKN , INPUT , MOVE_K , MOVE_N } -> moveNeighbors ( EDGES , INPUT , MOVE_N ) , io : format ( "NODE / moveKN ~p : I moved ~p and hopefully my neighbors ~n" , [ self ( ) , MOVE_K ] ) , node_life ( move ( POSITION , INPUT , MOVE_K ) , EDGES , ERROR , PARENT , ENGINE ) ; { moveN , INPUT , MOVE_N } -> io : format ( "NODE / moveN ~p : I moved ~p ~n" , [ self ( ) , MOVE_N ] ) , node_life ( move ( POSITION , INPUT , MOVE_N ) , EDGES , ERROR , PARENT , ENGINE ) ; { refreshEdge , OTHER_NODE } -> io : format ( "NODE / refreshEdge ~p : edge to ~p refreshed~n" , [ self ( ) , OTHER_NODE ] ) , node_life ( POSITION , refreshEdge ( OTHER_NODE , EDGES ) , ERROR , PARENT , ENGINE ) ; { request_maxNeighbor , REQUESTING_PID } -> REQUESTING_PID ! { my_maxNeighbor , self ( ) , maxNeighbor ( EDGES ) } , io : format ( "NODE / request_maxNeighbor ~p : neighbor with biggest error was returned to ~p~n" , [ self ( ) , REQUESTING_PID ] ) , node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) ; { request_distance , REQUESTING_PID , INPUT } -> io : format ( "NODE / request_distance ~p : I'm sending my distance, i'm in position: ~p~n" , [ self ( ) , POSITION ] ) , REQUESTING_PID ! { my_distance , self ( ) , distance ( POSITION , INPUT ) } , io : format ( " NODE / request_distance ~p : I'm about printing my position: ~p~n" , [ self ( ) , POSITION ] ) , node_life ( POSITION , EDGES , ERROR , PARENT , ENGINE ) ; { removeEdgeAndTell , OTHER_NODE } -> TMP = removeEdge ( OTHER_NODE , EDGES ) , io : format ( "NODE / removeEdgeAndTell ~p : edge to ~p was removed ~n" , [ self ( ) , OTHER_NODE ] ) , io : format ( " my old edges ~p~n" , [ EDGES ] ) , io : format ( " my new edges ~p~n" , [ TMP ] ) , OTHER_NODE ! { removeEdgeAndTell_done , self ( ) } , if TMP == [ ] -> PARENT ! { removeNode , self ( ) } , ENGINE ! { nodeRemoved , PARENT } , io : format ( "NODE / removeEdgeAndTell ~p : I'm dead, because i have no friends :( ~n" , [ self ( ) ] ) , exit ( "no friends" ) ; true -> node_life ( POSITION , removeEdge ( OTHER_NODE , EDGES ) , ERROR , PARENT , ENGINE ) end ; { set_parent , NEW_PARENT_PID } -> node_life ( POSITION , EDGES , ERROR , NEW_PARENT_PID , ENGINE ) end .
%% io:format(" I'm back to life ~p to ~p ~n", [POSITION,self()]),
% calls ageEdges and removes the node if no edges left
%has to be added also in the otherNode
%TODO has to be REQUESTING_PID_2 ???
%returns the decreased error
%returns the decreased error and the position in a tuple
%% PRINTING POSITION ON THE FILE %%%%%%%%%
%has to be called for both nodes
% %% PRINTING POSITION ON THE FILE %%%%%%%%%
% {ok, FILE_WRITE} = file:open(nodes.dat, [append]),
% io:format(FILE_WRITE,"~s~n",[posToStr2(POSITION)]),
% file:close(FILE_WRITE),
% io:format(" NODE / request_distance ~p : filewrite was OK~n", [self()]),
% posToStr([], FILE)->
% io:format(FILE, "\n",[]),
% [];
% posToStr([HEAD|TALE], FILE)->
% io:format(FILE, "~f\t",[HEAD]),
% posToStr(TALE, FILE)
% .
%% man_life(NODEPID, OTHER_NODE, STAYING_NODE, INPUT) ->
%%
%%
%%
%% NODEPID ! {request_position, self()},
%%
%% NODEPID ! {request_edges, self()},
%%
%% NODEPID ! {request_error, self()},
%%
%% NODEPID ! {newEdge, self(), OTHER_NODE},
%%
%% NODEPID ! {newEdge, self(), STAYING_NODE},
%%
%% NODEPID ! {ageEdges, self(), 5 },%MAX_AGE},
%%
%% NODEPID ! {ageEdges, self(), 1 },%MAX_AGE},
%%
%% NODEPID ! {removeEdge, self(), OTHER_NODE},
%%
%% NODEPID ! {addToError, self(), 1.4 }, %DELTA_ERROR},
%%
%% NODEPID ! {decreaseError, self(), 0.5},%ALPHA},
%%
%% NODEPID ! {moveKN, self(), INPUT, 0.3, 0.1}, %MOVE_K, MOVE_N},
%%
%% NODEPID ! {moveN, self(), INPUT, 0.2}, %MOVE_N},
%%
%% NODEPID ! {refreshEdge, self(), OTHER_NODE},
%%
%% NODEPID ! {request_maxNeighbor, self()},
%%
%% NODEPID ! {request_distance, self(), INPUT}
%%
%% %% ,
%% %% receive
%% %% {my_pos, NPID, POS} ->
%% %% io:format(" Node ~p`s position: ~p ~n", [NPID,POS])
%% %%
%% %%
%% %% end
%%
%% .
%%
%%
%% start()->
%%
%% NEWNODE=spawn(node,node_life, [[1,1],[],0,self()]),
%% OTHER_NODE = spawn(node,node_life, [[2,2],[],0,self()]),
%% STAYING_NODE = spawn(node,node_life, [[5,5],[],0,self()]),
%% NEWMAN=spawn(node,man_life,[NEWNODE, OTHER_NODE, STAYING_NODE, [3,3]])
%% .
%%
%% local Functions
%%
%%%%% ageEdges %%%%%
% ages all the edges and removes too old ones
% returns all edges
%%%%% removeEdge(OTHER_NODE, EDGES) %%%%%
% removes the edge to OTHER_NODE
% returns all edges
%%%%% move %%%%%
% returns a new position, (INPUT -POSITION)*MOVE + POSITION
%%%%% maxNeighbor %%%%%
% returns the neighbor with the highest accumulated error
% TODO better, more parallel function?engine:start(int.dat).
%%%%% distance %%%%%
% returns the square of the Euclidean distance between POSITION and INPUT