# Antibody.py
# Author: Juliette Zerick (jnzerick@math.ucdavis.edu)
# EAD 221 + PHY 250 (Spring 2009)
# Purpose: The Antibody class encapsulates an Antibody object, part of an
# 	Immune System Algorithm.Note that the functionality of the class
#	is independent of the specific problem; distance functions and
# 	such are passed in as function handles. Also, the Trajectory
#	object keeps a record or "history" of the Antibody as it mutates,
#	from one generation to the next, in the state space.

from UtilsNConstants import *

# a class that represents a single Antibody in an idiotypic network
class Antibody:
	# get the antigen-antibody affinity
	def AgAb_aff(self,Ag):
		return self.fh_AbAg_aff(self,Ag)
	
	# calculate the affinity between two antibodies
	def Ab_aff(self,Ab):
		return self.fh_Ab_aff(self,Ab)
	
	# print the Antibody
	def printAntibody(self):
		self.fh_printfull(self)

	# Antibody constructor--does DEEP copy of the chromosome and trajectory
	# passed in
	def __init__(self,fh_mut,fh_AbAg_aff,fh_Ab_aff,fh_printfull,fh_tostr,\
		C, G, T, ID):
		self.fh_mutate = fh_mut
		self.fh_AbAg_aff = fh_AbAg_aff
		self.fh_Ab_aff = fh_Ab_aff
		self.fh_printfull = fh_printfull
		self.fh_tostr = fh_tostr

		self.chromosome = copylist(C)
		self.gen = G
		self.history = T.copy()
		self.ID = ID

	# returns a DEEP copy of this Antibody
	def copy(self):
		return Antibody(self.fh_mutate, 
			self.fh_AbAg_aff, self.fh_Ab_aff, self.fh_printfull,
			self.fh_tostr,self.chromosome, self.gen,self.history,
			self.ID)

	# mutate this Antibody
	def mutate(self,prob_mut):
		self.fh_mutate(self,prob_mut)

	# "destructor"--free up some memory
	def __del__(self):
		del self.chromosome

	# get a (deep) copy of the chromosome
	def get_chromcopy(self):
		return copylist(self.chromosome)

	# overloaded == operator--NOT consistent with the other
	# overloaded operators! used in the list.remove() method
	def __eq__(self,other):
		for i in xrange(len(self.chromosome)):
			if self.chromosome[i] != other.chromosome[i]:
				return False

		return True

	# overloaded str() typecast operator; also used in print
	def __str__(self):
		return self.fh_tostr(self)
