# Coding Brain Neurons by using Hodgkin-Huxley model Understanding how the human brain works is a topic of active research and several scientists from various fields publish numerous of papers every year. Why is it important? Because knowing how our brain works will enable us to understand how we operate/think and perhaps enable us build truly intelligent machines in the future.

The first step of understanding how our brain works is modeling the neuron cells and the way they send electrical signals. Thus in this blog post we will briefly present the Hodgkin –Huxley model, we show how these models can be used in order to create intelligent agents/robots and finally we will provide the PHP code that implements the Hodgkin-Huxley model for a single neuron.

## The Hodgkin-Huxley model

In 1952 Alan Lloyd Hodgkin and Andrew Huxley published the academic paper “A quantitative description of membrane current and its application to conduction and excitation in nerve” in which they proposed a mathematical model which described the initiation and propagation of electrical signals in neurons (known as action potentials). This model focused on the ionic mechanisms in squid’s giant axon and its authors received the Nobel Prize in 1963 for their research.

The model consists of nonlinear differential equations which describe the membrane potential in the neuron and thus the electric signals that it sends to other neurons. For the detailed explanation of the model equations check out Dr Wulfram Gerstner’s notes.

## Using Neurons to build Intelligent Agents/Robots

The Braitenberg vehicle is an agent capable of moving autonomously around by using primitive sensors and wheels. One can use populations of spiking neurons in order to simulate such an intelligent agent. Below you can find 2 such examples developed by Zafeirios Fountas and Paul Smaldino:

Understanding the Neurodynamics can help us also build intelligent robots operated by real Neurons. Below you will find an interesting research of Professor Steve M. Potter:

## Coding Hodgkin-Huxley Neural model in PHP

Below I provide the Hodgkin-Huxley Neural model written in PHP. You can download the complete code on Github. The particular model receives a constant current I for a given time in period (0,Tmax) and predicts the spiking activity of the neuron. Note that by combining appropriately several neurons into populations you can build applications similar to what we presented earlier. If you are interested in experimenting with Braitenberg vehicles, check out Dr Murray Shanahan’s Robot demos written Matlab.

Here is the main class of the neural model:

```<?php
/**
* Hodgkin-Huxley Neuron Model
* https://www.datumbox.com/
*
* Licensed under MIT or GPLv3
*/

/**
* This class simulates the Hodgkin-Huxley neuron model by using the Runge-Kutta numerical optimization method.
* Moreover it assumes a resting potential of 0mV as described on the original paper "A quantitative description of membrane current and its application to conduction and excitation in nerve":
* https://www.sfn.org/~/media/SfN/Documents/ClassicPapers/ActionPotentials/hodgkin5.ashx
*/
class HodgkinHuxleyNeuron {

const gNa = 120; //maximum conductance Na
const ENa=115; //voltage measured in mV

const gK=36; //maximum conductance gK
const EK=-12; //voltage measured in mV

const gL=0.3; //voltage-independent conductance
const EL=10.6; //voltage measured in mV

const C=1; //Capacity of neuron

/**
* Runs the Hodgkin Huxley Neuron Model by sending a constant current T for a time period of Tmax
*
* @param float \$I
* @param float \$Tmax
*/
public function run(\$I = 10,\$Tmax = 100) {
\$m=0;
\$n=0;
\$h=0;

\$dt=0.05;

\$v = array('0'=>-10);

for(\$t=\$dt; \$t<=\$Tmax; \$t=\$t+\$dt) {

/*
//Uses Euler's method. (this method requires very small step size and thus is inefficient)
\$v_previous=end(\$v);
\$dm_dt=((2.5-0.1*\$v_previous)/(exp(2.5-0.1*\$v_previous)-1))    *(1-\$m)  -   +4*exp(-\$v_previous/18) *\$m;
\$m=\$m+\$dt*\$dm_dt;

\$dn_dt=((0.1-0.01*\$v_previous)/(exp(1-0.1*\$v_previous)-1))    *(1-\$n)  -   0.125*exp(-\$v_previous/80) *\$n;
\$n=\$n+\$dt*\$dn_dt;

\$dh_dt=(0.07*exp(-\$v_previous/20))    *(1-\$h)  -   1/(exp(3-0.1*\$v_previous)+1) *\$h;
\$h=\$h+\$dt*\$dh_dt;

\$SumI=self::gNa*pow(\$m,3)*\$h*(\$v_previous-self::ENa)+self::gK*pow(\$n,4)*(\$v_previous-self::EK)+self::gL*(\$v_previous-self::EL);
\$dv_dt=(-\$SumI+ \$I)/self::C;
\$v[(string)round(\$t,2)]=\$v_previous+\$dt*\$dv_dt;
*/

//uses Runge-Kutta Method to calculate the parameters of the model.
\$v_previous=end(\$v); //previous value at v(t)

\$dm_dt=((2.5-0.1*\$v_previous)/(exp(2.5-0.1*\$v_previous)-1))    *(1-\$m)  -   +4*exp(-\$v_previous/18) *\$m; //derivative dm/dt
//Runge-Kutta Method to calculate the parameter
\$k1 = \$dm_dt;
\$k2 = \$dm_dt + 1/2*\$dt*\$k1;
\$k3 = \$dm_dt + 1/2*\$dt*\$k2;
\$k4 = \$dm_dt + 1/2*\$dt*\$k3;
\$m=\$m+ 1/6*\$dt*(\$k1+2*\$k2+2*\$k3+\$k4); //estimating the new value of m

\$dn_dt=((0.1-0.01*\$v_previous)/(exp(1-0.1*\$v_previous)-1))    *(1-\$n)  -   0.125*exp(-\$v_previous/80) *\$n; //derivative dn/dt
//Runge-Kutta Method to calculate the parameter
\$k1 = \$dn_dt;
\$k2 = \$dn_dt + 1/2*\$dt*\$k1;
\$k3 = \$dn_dt + 1/2*\$dt*\$k2;
\$k4 = \$dn_dt + 1/2*\$dt*\$k3;
\$n=\$n+ 1/6*\$dt*(\$k1+2*\$k2+2*\$k3+\$k4); //estimating the new value of n

\$dh_dt=(0.07*exp(-\$v_previous/20))    *(1-\$h)  -   1/(exp(3-0.1*\$v_previous)+1) *\$h; //derivative dh/dt
//Runge-Kutta Method to calculate the parameter
\$k1 = \$dh_dt;
\$k2 = \$dh_dt + 1/2*\$dt*\$k1;
\$k3 = \$dh_dt + 1/2*\$dt*\$k2;
\$k4 = \$dh_dt + 1/2*\$dt*\$k3;
\$h=\$h+ 1/6*\$dt*(\$k1+2*\$k2+2*\$k3+\$k4); //estimating the new value of h

\$SumI=self::gNa*pow(\$m,3)*\$h*(\$v_previous-self::ENa)+self::gK*pow(\$n,4)*(\$v_previous-self::EK)+self::gL*(\$v_previous-self::EL); //sum all the currents

\$dv_dt=(-\$SumI+ \$I)/self::C; //derivative dv/dt
//Runge-Kutta Method to calculate the parameter
\$k1 = \$dv_dt;
\$k2 = \$dv_dt + 1/2*\$dt*\$k1;
\$k3 = \$dv_dt + 1/2*\$dt*\$k2;
\$k4 = \$dv_dt + 1/2*\$dt*\$k3;
\$v[(string)round(\$t,2)]=\$v_previous+1/6*\$dt*(\$k1+2*\$k2+2*\$k3+\$k4); //estimating the new value of v

}

return \$v;
}
}
?>
```

Here is the example code which shows its electrical signals by using Google Charts:

```<?php
require_once('./HodgkinHuxleyNeuron.php');

\$HodgkinHuxleyNeuron = new HodgkinHuxleyNeuron();

\$I=10;
\$Tmax=100;
\$data=\$HodgkinHuxleyNeuron->run(\$I,\$Tmax);

?>

<html>
<script type="text/javascript">
// <![CDATA[
function drawChart() {
['T', 'mV'],
<?php
\$str='';
foreach(\$data as \$k=>\$v) {
\$str.='['.\$k.','.\$v.'],';
}
echo trim(\$str,',');
?>
]);

var options = {
title: 'Hodgkin-Huxley Neuron',
legend: 'none',
hAxis: {title: 'Time (ms)'},
vAxis: {title: 'Membrane potential (mV)'}
};

chart.draw(data, options);
}
// ]]>
</script>
<body>
<div id="chart_div" style="width:100%;min-width: 900px; height: 500px;"></div>
</body>
</html>
``` 