import java.util.*;
public class AllocationElection {
public static void main(String[] args) {
Vector<AllocationBallot> _ballots = new Vector<AllocationBallot>();
int candiates = 5;
int seats = 2;
_ballots.add(new AllocationBallot(10,new double[]{1,2,3,4,5}));
_ballots.add(new AllocationBallot(10,new double[]{6,2,3,4,0}));
_ballots.add(new AllocationBallot(10,new double[]{0,3,3,4,5}));
_ballots.add(new AllocationBallot(10,new double[]{0,7,4,4,0}));
int[] winners = getWinners( _ballots, candiates, seats);
System.out.print(" winners:");
for( int i = 0; i < winners.length; i++) {
System.out.print(""+winners[i]+", ");
}
System.out.println();
}
public static int[] getWinners(Vector<AllocationBallot> _ballots, int num_candidates, int seats) {
int[] winners = new int[seats];
Vector<AllocationBallot> ballots = new Vector<AllocationBallot>();
for( int i = 0; i < _ballots.size(); i++) {
ballots.add(_ballots.get(i).clone());
}
for( int i = 0 ; i < winners.length; i++) {
for( int j = 0; j < ballots.size(); j++) {
System.out.print("[");
for( int k = 0; k < ballots.get(j).allocs.length; k++) {
System.out.print(ballots.get(j).allocs[k]+", ");
}
System.out.println("]");
}
int[] ignore_order = getIgnoreOrder(ballots,num_candidates);
Vector<Integer> ignores = new Vector<Integer>();
Triplet<Integer,Double,Double> winner = getWinner(ballots,ignores,num_candidates,seats-i);
int ignore = 0;
while( winner.a < 0 && ignore < ignore_order.length) {
ignores.add(ignore_order[ignore++]);
System.out.print(" ignoring: ");
for( int j = 0; j < ignores.size(); j++) {
System.out.print(""+ignores.get(j)+", ");
}
System.out.println();
winner = getWinner(ballots,ignores,num_candidates,seats-i);
}
System.out.println(" elected: "+winner.a+" "+winner.b+" "+winner.c);
System.out.println();
winners[i] = winner.a;
if( winner.a > -1) {
for( int j = 0; j < ballots.size(); j++) {
ballots.get(j).elected(winner.a, winner.b, winner.c, ignores); //is this right? (the surplus vote count / reallocation)
}
}
}
return winners;
}
public static Triplet<Integer,Double,Double> getWinner(Vector<AllocationBallot> ballots, Vector<Integer> ignores, int num_candidates, int seats_left) {
double[] ws = getTotals( ballots, ignores, num_candidates);
System.out.print(" totals: ");
for( int j = 0; j < ws.length; j++) {
System.out.print(""+ws[j]+", ");
}
System.out.println();
double quota = 0;
for( int j = 0; j < ws.length; j++) {
quota += ws[j];
}
quota /= (double)seats_left;
System.out.println(" quota: "+quota);
int max_index = 0;
double max = ws[0];
for( int j = 0; j < ws.length; j++) {
if( ws[j] > max) {
max = ws[j];
max_index = j;
}
}
if( max >= quota) {
return new Triplet<Integer,Double,Double>(max_index,max,quota);
} else {
return new Triplet<Integer,Double,Double>(-1,0.0,0.0);
}
}
public static double[] getTotals(Vector<AllocationBallot> ballots, Vector<Integer> ignores, int num_candidates) {
double[] sum = new double[num_candidates];
for( int i = 0; i < ballots.size(); i++) {
double[] dd = ballots.get(i).getWeightsIgnored(ignores);
for( int j = 0; j < dd.length; j++) {
sum[j] += dd[j];
}
}
return sum;
}
public static int[] getIgnoreOrder(Vector<AllocationBallot> ballots, int num_candidates) {
double[] tots = getTotals(ballots,new Vector<Integer>(),num_candidates);
Vector<Pair<Double,Integer>> s = new Vector<Pair<Double,Integer>>();
for( int i = 0; i < tots.length; i++) {
s.add(new Pair<Double,Integer>(tots[i],i));
}
Collections.sort(s);
int[] order = new int[num_candidates];
for( int i = 0; i < s.size(); i++) {
order[i] = s.get(i).b;
}
return order;
}
}