/*
   RealBrain
   realbrain.h
   
	 This is an implementation of the Adaptrode-based neural network
   software for the MAVRIC robot.  This implementation assumes a 
   graded signal as opposed to the action potential signal used in the
   standard Adaptrode model.

     The adaptrode, artificial neuron based on it and the neural network
   processor implemented in this software is protected under 
   U.S. Patent #5,504,839. Permission to use this software freely for
   research purposes is hereby given by the holder of that patent and
   the author of this software.  This software may not be used for any
   commercial purpose or without giving the holder appropriate 
   acknowledgement.

  Author: George Mobus
  Date: 3/31/00
  Version: 1.0
  Revision History:
	Revised: 1/17/01 - Added extinction support
*/

/* Defined constants */
#define NONE -2
#define SELF -1
#define SLOT 0
#define NEURON 1
#define ADAPTRODE 2

#define FIXED 0
#define EXCITE 1
#define INHIBIT 2

#define MAXGREYSCALE 255.0
#define MINSIGNAL 0.01
#define STRONGSIGNAL 0.2
#define LOWERBOUND 0.001
#define EXTINCT_DWN_CNT 4

#define OUTDECAY 0.4
#define THRESHDECAY 0.25
#define NONLINEAR_THRESHOLD 0.02
#define NONLINEAR_DECAY 0.00001

typedef unsigned char byte;

typedef struct AtypeStr {
/* Atype structures contain the constants used by specific adaptrodes, so
   constitute an adaptrode type.  There will need to be a different atype
   for each different kind of adaptrode used in the network.
*/
	int			id;				/* identification = array index */
	double		decay,			/* decay constant for adaptrode response */
				extinction,		/* decay rate for extinction */
				wt_max,			/* baseline or initial w_max */
				wt_min,			/* constant lower bound on w[3] */
				alpha[4],		/* increase rate constants */
				delta[4];		/* decay rate constants */
} Atype;

typedef struct NtypeStr {
/* Similar to Atype, but for neuron types.  Neurons differ (in this
   version) by where the gating value for the w[0] to w[1] potentiation
   comes from.  They also vary based on the dynamics (if any) of the 
   threshold. 
*/
	int			id,				/* identification = array index */
				US_src;			/* NEURON = n->out0, ADAPTRODE = a->response */
	double		t_decay;		/* threshold decay rate */
	double		t_alpha;		/* threshold increase rate */
} Ntype;

typedef struct AdaptrodeStr {
/*
	Adaptrodes are used primarily for synaptic connections among neurons.
*/
	int			id,				/* identification = array index */
				atype_id,		/* index number of the atype for this */
				type,			/* 0 = fixed, 1 = excite, 2 = inhibit */
				learn,			/* state switch which allows w[1] gating */
				extinguish,		/* state counter to time missed expectations */
				src_type;		/* NEURON or SLOT (used in linking) */
	int			src_id;			/* index of relevant source type */
	double		*src,			/* pointer to source double value, e.g.,
								     NEURON -> neurons[src_id].out0 
									 SLOT   -> inslots[src_id].value */
				response,		/* output response of this */
				w_max,			/* weight max variable for w[0] pull up 
								   w[0] = w[0] + alpha[0]* *src->out0 *
								   (W_MAX - w[0]) - delta[0] * ... */
				w[4];			/* weight vector */
	Atype		*atype;			/* pointer to the atype record for this */
	long		age;			/* incremented each processing cycle 
								   to be used in vertebrate brains */
	byte		record;			/* selection for recording */
} Adaptrode;

typedef struct NeuronStr {
/*
	Artificial neuron based on adaptrode dynamic synapses.
*/
	int			id,				/* identification = array index */
				ntype_id;		/* index of ntype */
	Ntype		*ntype;			/* pointer to ntype record */
	double		out0,			/* old out */
				out1,			/* new out */
				thresh,			/* axonal hillock threshold variable.
								   may be dynamic threshold. */
				t_base,			/* threshold base value used for dynamic
								   thresholds  */
				*t_up, *t_dwn,	/* pointers to up/dwn modulator nodes */
				sum,			/* integration variable for result of 
								   synaptic inputs */
				*reward, 
				*punish, 
				*confirm;		/* pointers to memory trace modulators */
	Adaptrode	*synapses;		/* adaptrode array */
	int			phase;			/* life phase; 0=developmental */
	long		age;			/* incremented each processing cycle
								   to be used in vert. brain */

	int			adapt_cnt,		/* number of adaptrodes in the array */

				reward_src,		/* id of reward source node */
				reward_src_type,/* NEURON or SLOT */
				punish_src,		/* id of punish source for avoidance */
				punish_src_type,/* NEURON or SLOT */
				confirm_src,	/* id of confirmation (long-term reward) */
				confirm_src_type,/* NEURON or SLOT */
				t_up_src,		/* id of threshold up modulator node */
				t_up_src_type,	/* NEURON or SLOT */
				t_dwn_src,		/* id of threshold down modulator node */
				t_dwn_src_type;	/* NEURON or SLOT */
	byte		record;			/* selection for recording */
} Neuron;

typedef struct InslotStr {
/*
	Input slots for getting sensory signals into the neural network.
*/
	int			id;				/* identification = array index */
	int			greyscale;		/* sensor input 0 - MAXGREYSCALE */
	double		value;			/* greyscale / MAXGREYSCALE */
	byte		record;			/* selection for recording */
} Inslot;

typedef struct OutslotStr {
/*
	Output slots for getting signals out to effectors.
*/
	int			id;				/* identification = array index */
	int			greyscale;		/* (int) (value * MAXGREYSCALE) */
	double		value;			/* output from a neuron out0 */
	int			neuron_id;		/* index of source neuron */
	double		*src;			/* pointer to neuron out0 */
	byte		record;			/* selection for recording */
} Outslot;

typedef struct BrainStr {
	int			atype_cnt,		/* number of atype objects in brain */
				ntype_cnt,		/* number of ntype objects in brain */
				inslot_cnt,		/* number of inslot objects in brain */
				neuron_cnt,		/* number of neuron objects in brain */
				outslot_cnt;	/* number of outslot objects in brain */
	Atype		*atypes;		/* dynamic array of atypes */
	Ntype		*ntypes;		/* dynamic array of ntypes */
	Inslot		*inslots;		/* dynamic array of inslots */
	Neuron		*neurons;		/* dynamic array of neurons */
	Outslot		*outslots;		/* dynamic array of outslots */
	long		age;			/* incremented every 200 seconds */
} Brain;

/* End of realbrain.h */
