objref left[cellnr], right[cellnr], both[cellnr], score

// score the cells
// SMALL score == good performance
// $1: population mean dendritic tree size 0, $2 cell nr 
proc scoreme() { local linearity, equalsize, dsize
		if (left.x[$2] > 0.2 && right.x[$2] > 0.2 && both.x[$2] > 0.2) {
			linearity = abs(left.x[$2] + right.x[$2] - both.x[$2])/both.x[$2]	// judge linear sumation
			equalsize = left.x[$2]/right.x[$2] + right.x[$2]/left.x[$2] -2		// penalty for unequal EPSP sizes
			dsize = cell[$2].nall/$1						// penalty for large cells		
			score.x[$2] = linearity + wtequal*equalsize + wtsize*dsize - wtboth*log(both.x[$2])		
		} else { score.x[$2] = 99 }							// EPSP < .2 mV -> very bad score	
}

// $1: population mean dendritic tree size 0 $2: start 1 $3: start 2 $4: run type
proc linearrun() { local ce
	
	stimulator[0].start = $2
	stimulator[1].start = $3	
	init()
	dt = 0.025
	run()
	 
	if ($4 == 0) {	for ce = 0, cellnr-1 { left.x[ce] = cell[ce].voltage.max() - cell[ce].voltage.x[0] } }
	if ($4 == 1) {	for ce = 0, cellnr-1 { right.x[ce] = cell[ce].voltage.max() - cell[ce].voltage.x[0] } } 
	if ($4 == 2) {	for ce = 0, cellnr-1 { both.x[ce] = cell[ce].voltage.max() - cell[ce].voltage.x[0] }
			for ce = 0, cellnr-1 { scoreme($1, ce) } 		
			}
}

// $1: mean size 
proc lineartest() {
	
	left = new Vector(cellnr)
	right = new Vector(cellnr)
	both = new Vector(cellnr)
		
	// run electrophys. simulations 
	v_init = -70
	tstop = 50
	
	linearrun($1, 5, 9999, 0)
	linearrun($1, 9999, 5, 1)
	linearrun($1, 5, 5, 2)
}