/******************************************************************** 
 * Reps come from the Candidate that wins an election.  All the internal 
 * parts are initialized to be the same as the candidates.  If this is a 
 * new Rep, the name is changed.  Reps have the methods, tagAdapt and  
 * BillVote.  Since Reps are self-interested, and wish for everyone to  
 * join their party, they alter the tags of the other representatives. 
 * Reps consider the proposed bill versus the statusquo, and vote for the 
 * closest position.  They also keep track of their most recent tag. 
 ********************************************************************/ 
public class Rep extends Individual{ 
    public int age = 0; 
    public int[] newTag; 
    public int VoterID; 
    public int votesReceived = 0; 
    public double majority;
 
    // This constructor takes a Candidate and initializes all the  
    // paramaters of the Rep 
    public Rep(Candidate tempCand) { 
	ideology = tempCand.ideology; 
	strengths = tempCand.strengths; 
	votingRecord = tempCand.votingRecord; 
	partyTag = tempCand.partyTag; 
	if (!tempCand.incumbent) { 
	    name = "Rep is " + tempCand.name; 
	} 
	else 
	    name = tempCand.name; 
	age = tempCand.age + 1; 
	VoterID = tempCand.VoterID; 
	parent = tempCand.parent; 
	newTag = new int[partyTag.length]; 
	System.arraycopy(partyTag, 0, newTag, 0, partyTag.length); 
	votesReceived = tempCand.votesReceived; 
    } 
 
    // Reps need to vote on bills to determine policy for Congress
    public int BillVote(int bill, int issue, int statusQuo,  
			int[] billTag, int voteNumber) { 
 
	// Find the distance between themselves and the bill 
	double billDistance = Math.abs(ideology[issue] - bill); 
	billDistance = billDistance * strengths[issue]; 
 
	// Find the distance between themselves and the statusQuo 
	double quoDistance = Math.abs(ideology[issue] - statusQuo); 
	quoDistance = quoDistance * strengths[issue]; 
 
	// Find the similarity of the tags 
	double tagSimilarity = findSimilarity(billTag); 
	double byTag = Math.random();
	
	// If the tag influence is not strong enough
	if (byTag > Math.abs(tagSimilarity)) {

	    // Vote for the position that is closest 
	    if (quoDistance > billDistance) { 
		votingRecord[voteNumber] = 1; 
		return 1; 
	    } 
	    else { 
		votingRecord[voteNumber] = 0; 
		return 0; 
	    } 
	}
	
	// otherwise vote based on party tag. 
	else {
	    if (tagSimilarity >= 0) { 
		votingRecord[voteNumber] = 1; 
		return 1; 
	    } 
	    else { 
		votingRecord[voteNumber] = 0; 
		return 0; 
	    } 
	}
    } 
 
    // Based on the similarity of the voting record of another Rep, 
    // this method alters the other Rep's tag. 
    public void tagAdapt(Rep otherRep) { 
	int similarity = 0; 
 
	double tagSim = findSimilarity(otherRep.partyTag); 
	if (Math.abs(tagSim) == 1) { 
	    //System.err.println("> 100% similar!"); 
	    return; 
	} 
	if (Math.abs(tagSim) >= .9) { 
	    //System.err.println("> 90% similar!");
	} 
	   
	// When they voted the same, increment the similarity 
	for (int i=0; i<votingRecord.length; i++) 
	    if (votingRecord[i] == otherRep.votingRecord[i]) 
		similarity++; 
 
	// Random chance has them voting together half of the time, 
	// therefore we must find the significance of the similarity. 
	// This is determined by subtracting half the number of votes from 
	// the similarity. 
	similarity = similarity - (votingRecord.length / 2); 
 
	// The similarity determines how many bits to change in the  
	// tag of the other Rep. The more significant the similarity, 
	// the more bits that are changed.	 
	for (int i=0; i<Math.abs(similarity); i++) { 
	    int bitChange = Math.abs(Utilities.rand.nextInt()  
				     % partyTag.length); 
 
	    // If they voted together more than half the time, alter 
	    // the other Rep's tag to match 
	    if (similarity >= 0) 
		if (partyTag[bitChange] != otherRep.partyTag[bitChange]) 
		    otherRep.newTag[bitChange] = partyTag[bitChange]; 
		else i--; 
	    // Otherwise, change the other Rep's tag to be dissimilar 
	    else 
		if (partyTag[bitChange] == otherRep.partyTag[bitChange]) 
		    otherRep.newTag[bitChange] = (otherRep.partyTag[bitChange]+1) % 2; 
		else i--; 
	} 
    } 

    // isMajority determines the majority status of the representative
    // given the previous term in congress.  This returns a double,
    // the percent they voted with the majority.
    public double isMajority() {
	int majorityCount = 0;
	for (int i=0; i<votingRecord.length; i++)
	    if ((votingRecord[i]==1 && parent.Government.didItPass[i]=="Y") ||
		(votingRecord[i]==0 && parent.Government.didItPass[i]=="N"))
		majorityCount++;
	double majorityFactor = 2*((majorityCount / votingRecord.length) -.5);
	return(majorityFactor);
    }
    
    // After the tag adaptation is complete for all members of congress, 
    // we set partyTag equal to the altered tag. 
    public void resetTag() { 
	System.arraycopy(newTag, 0, partyTag, 0, newTag.length); 
	majority = isMajority();
    } 
} 

