docs
[mirrors/Programs.git] / php / neuronics / neural.php
CommitLineData
21c4e167
H
1#!/usr/bin/php
2<?php
3//NMNN - Nervous Man's Neural Network
4//Harvie 2oo7/8
5/*
6 * My first neural network
7 */
8
9///SETTINGS/INIT
10$nmnn_version = '0.1-DEV';
11
12
13///FUNCTIONS///////////////////////////////////////////////////////////////////////////////////////
14
15function nervous_banner($text) { //Print banner with custom text
16 echo("\n[*]-== Nervous Man's Neural Network ==-[*]\n");
17 $text = explode("\n", $text);
18 foreach($text as $line) {
19 $line = trim($line);
20 echo(" =-[+]\r");
21 echo("[+]-= $line\n");
22 }
23 echo("[*]-==================================-[*]\n\n");
24}
25
26function neural_network_create($net) { //Create new network in $GLOBALS['neural_networks'][$net]
27 if(isset($GLOBALS['neural_networks'][$net])) {
28 echo("Net already exists\n");
29 return(0);
30 }
31 $GLOBALS['neural_networks'][$net] = new neural_network($net);
32 return(1);
33}
34
35function neural_network($net) {
36 return($GLOBALS['neural_networks'][$net]);
37}
38
39function global_neuron($net, $id) {
40 return neural_network($net)->neurons[$id];
41}
42
43///CLASSES/////////////////////////////////////////////////////////////////////////////////////////
44
45class neuron { //Single Neurone class
46
47 var $net = 'N/A'; //ID of network containing this neuron
48 var $id = 'N/A'; //ID of this neuron
49 var $treshold = 0; //Treshold is sensitivity of this neuron
50 var $synapses = array(); //List of neurons connected to dendrites (inputs)
51 var $decisions = array(); //List of values received from dendrites
52 var $axon = 'N/A'; //Final decision of this neuron
53 var $sensor = false; //Is this a sensor?
54
55 function neuron($net, $id, $treshold=0) { //Constructor - Set neuron identification
56 $this->net = $net;
57 $this->id = $id;
58 $this->treshold = $treshold;
59 }
60
61 function synapse_add($id, $strength=0) { //Connect neuron to dendrite
62 $this->synapses[$id] = $strength;
63 }
64
65 function synapse_request($id) { //Request synapse add by another neuron
66 if(!is_array($id)) {
67 global_neuron($this->net, $id)->synapse_add($this->id);
68 } else foreach($id as $id_single) {
69 global_neuron($this->net, $id_single)->synapse_add($this->id);
70 }
71 }
72
73 function synapse_break($id) { //Disconnect neuron from dendrite
74 unset($this->synapses[$id]);
75 }
76
77 function debug() { //Print info about this neuron
78 echo("[i] Info about neuron no.$this->id in $this->net network:\n");
79 if($this->sensor) { //if this is sensor print different stuff
80 echo("\tThis cell is a SENSOR!!!\n\tSensor axon value:$this->axon\n\n");
81 return('s');
82 }
83 echo("\tTreshold: $this->treshold\n\tAxon: $this->axon\n");
84 echo("\tSynapses:\n");
85 print_r($this->synapses);
86 echo("\tDecisions:\n");
87 print_r($this->decisions);
88 echo("\n");
89 }
90
91 function dendrites_read() { //Receive axon values from all neurons connected to dendrites
92 if($this->sensor) return('s');
93 $this->decisions = array(); //Remove old decisions
94 foreach($this->synapses as $synapse => $strength) {
95 //print_r (global_neuron($this->net, $synapse)); //debug
96 global_neuron($this->net, $synapse)->dendrites_read();
97 global_neuron($this->net, $synapse)->decide();
98 $this->decisions[$synapse]=global_neuron($this->net, $synapse)->axon_get();
99 }
100 }
101
102 function decide() { //Decide final axon (output) value - read dendrites first!
103 if($this->sensor) return('s');
104 $this->axon = 0; //Reset old
105 foreach($this->decisions as $synapse => $decision) {
106 $this->axon += $decision * $this->synapses[$synapse];
107 }
108 if($this->axon > $this->treshold) {
109 $this->axon = 1;
110 } else {
111 $this->axon = 0;
112 }
113 }
114
115 function axon_get() {
116 return $this->axon;
117 }
118
119 function learn($truth) { //Determine, what is truth
120 if($this->sensor) return('s');
121 foreach($this->decisions as $synapse => $decision) {
122 if($decision == $truth) {
123 $this->synapses[$synapse]++;
124 } else {
125 $this->synapses[$synapse]--;
126 }
127 }
128 }
129
130 function sensor_set($sensor_value, $is_sensor=true) { //Make sensor from this neuron and set axon value
131 $this->sensor = $is_sensor;
132 $this->axon = $sensor_value;
133 }
134
135}
136
137
138
139class neural_network { //Single neural network class
140
141 var $net = 'N/A'; //Name of this network
142 var $neurons = array(); //Array of neuron objects
143
144 function neural_network($net) { //Constructor - Set network name
145 $this->net = $net;
146 }
147
148 function add_neuron($id, $treshold=0) { //Creates new neurone with specified id
149 if(!isset($this->neurons[$id])) {
150 $this->neurons[$id] = new neuron($this->net, $id, $treshold);
151 } else return(0);
152 }
153
154 function debug() { //Print debug for all neurons in network
155 echo("###########\nDebug information about $this->net network:\n###########\n\n");
156 foreach($this->neurons as $neuron) $neuron->debug();
157 }
158
159 function learn($truth) { //Learn all neurons
160 foreach($this->neurons as $neuron) $neuron->learn($truth);
161 }
162
163}
164
165///BANNER
166nervous_banner("\nNeural network simulator\nCoded by Harvie 2oo7/8\nInfo:\n- Version: $nmnn_version\n- NeuroNet library loaded!\n");
167
168/*
169 8x ---------------------------------- CUT HERE ----------------------------------
170 */
171
172///SAMPLE_NETWORK///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
173
174neural_network_create('neuronet'); //Create new network called neuronet
175for($i=1;$i<=6;$i++) neural_network('neuronet')->add_neuron($i,-1); //Create few neurons in neuronet
176
177/*
178 Now wee have six neurons floating somewhere in the network space:
179
180 (1) (2) (3) (4) (5) (6)
181 */
182
183//Connect neurons into synapse infrastructure:
184global_neuron('neuronet', 1)->synapse_request(array(3,4));
185global_neuron('neuronet', 2)->synapse_request(array(4,5));
186global_neuron('neuronet', 3)->synapse_request(6);
187global_neuron('neuronet', 4)->synapse_request(6);
188global_neuron('neuronet', 5)->synapse_request(6);
189
190//Convert neurons 1 and 2 to sensors:
191for($i=1;$i<=2;$i++) global_neuron('neuronet', $i)->sensor_set(0, true);
192
193
194/*
195 After making this synapse infrastructure, we'll get somethinq like this:
196
197 _(3)_
198 /--> ]1)_/ \
199 Input \_(4)__\_(6> -> Output
200 \--> ]2)_/ /
201 \_(5)_/
202
203 ]1) and ]2) are sensors/receptors/...
204 (6> is output
205
206 Network structure inspired by: http://en.wikipedia.org/wiki/Image:Neural_network_example.png
207
208 If you can't understand this, i can try to say it easier: "It's something like your brain (but much more complicated)" ;D
209 Or try to read one of these:
210 - http://en.wikipedia.org/wiki/Neural_networks
211 - http://en.wikipedia.org/wiki/Artificial_neural_network
212
213 */
214
215//Now, i have neural network and i will try to teach it something
216
217/*
218
219 I will try to teach this network to compare two booleans
220 Epected output values:
221 INPUT | OUTPUT
222 0,0 | 1
223 1,1 | 1
224 0,1 | 0
225 1,0 | 0
226
227 So. Let's try to write some function for this:
228
229*/
230
231function same($first, $second) { //This function uses neural network to compare two bools
232
233 global_neuron('neuronet', 1)->sensor_set($first);
234 global_neuron('neuronet', 2)->sensor_set($second);
235
236 global_neuron('neuronet', 6)->dendrites_read();
237 global_neuron('neuronet', 6)->decide();
238 $decision = global_neuron('neuronet', 6)->axon_get();
239
240 echo("$first == $second ?\nnetwork says: $decision (0=NO, 1=YES)\n");
241
242 return($decision);
243}
244
245/*
246
247 We got nice function (in fact it hardly sux, like whole this network...)
248 How to use it?:
249
250*/
251
252///Learning:
253
254same(0,0); //Ask
255neural_network('neuronet')->learn('true'); //Correct answer (teach)
256neural_network('neuronet')->learn('true'); //Correct answer (teach)
257same(1,0);
258neural_network('neuronet')->learn('false');
259neural_network('neuronet')->learn('false');
260same(0,1);
261neural_network('neuronet')->learn('false');
262neural_network('neuronet')->learn('false');
263
264///Using superinteligent lifeform ehm... 3 neurones brain ;P
265
266echo("\nI hope it will work now: 8')\n\n");
267same(0,0); //Ask (without teaching)
268
269//Now, we like to print some stats
270neural_network('neuronet')->debug(); //Print some info about neuronet and it's neurons
271
272
This page took 0.610928 seconds and 5 git commands to generate.