[EM] simulator code for 2 Approval methods

raphfrk at netscape.net raphfrk at netscape.net
Fri Dec 15 12:04:45 PST 2006


 Thanks. I have created 2 methods.
 
 The first one is approval zero info. In this case, the voter averages his utility for all the candidates and places his approval threshold at the average.
 
 The second one is the same as above. However, it runs 2 elections. The first election uses the same as above and is considered a poll (might be worth reducing the number of voters to <numv ?). For the second 'real' election, the threshold is set at the average of the winner and second place in the poll. This approx means that the voter will vote for his favourite of the top 2 and all he prefers more.
 
 Unless anyone objects, I will post the code here (or at least if someone objects, I won't do it again). This has the added advantage that any code errors may be spotted :).
 
 I don't have the ability to compile as I am basing it on the code snippet, so it may not compile, but I did read it through.
 
 void ApprovalNoInfo::runElection( int* winnerR, const VoterArray& they ) {
 int i,j;
 int* talley;
 int winner, second_place;
 int numc = they.numc;
 int numv = they.numv;
 
 // init things
 talley = new int[numc];
 for ( i = 0; i < numc; i++ ) {
 talley[i] = 0;
 }
 
 double* thresh;
 thresh = new double[numv];
 
 for ( i = 0; i < numv; i++ ) {
 thresh[i] = 0;
 for ( j = 0; j < numc; j++) {
 thresh[i] += they[i].getPref( j );
 }
 thresh[i] = thresh[i]/numc;
 }
 
 // count votes for each candidate
 for ( i = 0; i < numv; i++ ) {
 for ( j = 0; j < numc; j++) {
 if ( they[i].getPref[j] >= thresh[i] ) {
 talley[j]++;
 }
 }
 }
 // find winner + second place
 {
 int m = -1;
 int m2 = -2; // second place
 for ( i = 0; i < numc; i++ ) {
 if ( talley[i] > m ) {
 m2 = m;
 m = talley[i];
 second_place=winner;
 winner = i;
 } else if ( talley[i] > m2 ) {
 m2 = talley[i];
 second_place = i;
 }
 }
 }
 
 
 delete [] talley;
 delete [] thresh;
 if ( winnerR ) {
 *winnerR = winner;
 *(winnerR+1) = second_place; // not really needed
 }
 
 }
 
 -----------------------------------------------------
   
 void ApprovalWithPoll::runElection( int* winnerR, const VoterArray& they ) {
 int i,j;
 int* talley;
 int winner, second_place;
 int numc = they.numc;
 int numv = they.numv;
 
 // init things
 talley = new int[numc];
 for ( i = 0; i < numc; i++ ) {
 talley[i] = 0;
 }
 
 double* thresh;
 thresh = new double[numv];
 
 for ( i = 0; i < numv; i++ ) {
 thresh[i] = 0;
 for ( j = 0; j < numc; j++) {
 thresh[i] += they[i].getPref( j );
 }
 thresh[i] = thresh[i]/numc;
 }
 
 // count votes for each candidate
 for ( i = 0; i < numv; i++ ) {
 for ( j = 0; j < numc; j++) {
 if ( they[i].getPref[j] >= thresh[i] ) {
 talley[j]++;
 }
 }
 }
 // find winner + second place
 {
 int m = -1;
 int m2 = -2; // second place
 for ( i = 0; i < numc; i++ ) {
 if ( talley[i] > m ) {
 m2 = m;
 m = talley[i];
 second_place=winner;
 winner = i;
 } else if ( talley[i] > m2 ) {
 m2 = talley[i];
 second_place = i;
 }
 }
 }
 
 // Above assumed to be 'poll'
 
 // init things
 talley = new int[numc];
 for ( i = 0; i < numc; i++ ) {
 talley[i] = 0;
 }
 
 
 // Threshold set to average of top 2 from 'poll'
 for ( i = 0; i < numv; i++ ) {
 thresh[i] = ( they[i].getPref[winner] + they[i].getPref[second_place] ) / 2.0 ;
 }
 
 // count votes for each candidate
 for ( i = 0; i < numv; i++ ) {
 for ( j = 0; j < numc; j++) {
 if ( they[i].getPref[j] >= thresh[i] ) {
 talley[j]++;
 }
 }
 }
 // find winner + second place
 {
 int m = -1;
 int m2 = -2; // second place
 for ( i = 0; i < numc; i++ ) {
 if ( talley[i] > m ) {
 m2 = m;
 m = talley[i];
 second_place=winner;
 winner = i;
 } else if ( talley[i] > m2 ) {
 m2 = talley[i];
 second_place = i;
 }
 }
 }
 
 delete [] talley;
 delete [] thresh;
 if ( winnerR ) {
 *winnerR = winner;
 *(winnerR+1) = second_place;
 }
 
 
 // return pickOneHappiness( they, numv, winner );
 }
 
 
  Raphfrk
 --------------------
 Interesting site
 "what if anyone could modify the laws"
 
 www.wikocracy.com 
 
   
________________________________________________________________________
Check Out the new free AIM(R) Mail -- 2 GB of storage and industry-leading spam and email virus protection.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.electorama.com/pipermail/election-methods-electorama.com/attachments/20061215/662eef8c/attachment-0003.htm>


More information about the Election-Methods mailing list