LightVoting
 All Classes Namespaces Files Functions Variables Pages
CMinimaxApproval.java
Go to the documentation of this file.
1 
24 package org.lightvoting.simulation.rule;
25 
26 import cern.colt.bitvector.BitVector;
28 
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Map;
36 
37 
42 public class CMinimaxApproval
43 {
44 
45  /***
46  * compute the winning committee according to Minimax Approval
47  *
48  * @param p_alternatives available alternatives
49  * @param p_votes submitted votes
50  * @param p_comSize size of committee to be elected
51  * @return elected committee
52  */
53 
54  public BitVector applyRuleBV( final List<String> p_alternatives, final List<BitVector> p_votes, final int p_comSize )
55  {
56  /* compute all possible committees, i.e. all {0,1}^m vectors with exactly k ones */
57 
58  final List<BitVector> l_bitCommittees = this.computeBitComittees( p_alternatives.size(), p_comSize );
59  System.out.println( l_bitCommittees );
60 
61  /* Hashmap for storing the maximal hamming distance to any vote for all committees */
62 
63  Map<Integer, Integer> l_maxMap = new HashMap<Integer, Integer>();
64 
65  for ( int i = 0; i < l_bitCommittees.size(); i++ )
66  {
67  /* Key: Committee ID, Value: maximal Hamming distance to any voter */
68  l_maxMap.put( i, this.determineMaxHDBV( p_votes, l_bitCommittees.get( i ), p_alternatives.size() ) );
69  }
70 
71  l_maxMap = this.sortMapASC( l_maxMap );
72 
73  return l_bitCommittees.get( l_maxMap.entrySet().iterator().next().getKey() );
74 
75  }
76 
84  private List<BitVector> computeBitComittees( final int p_altNum, final int p_comSize )
85  {
86  final CCombination l_combination = new CCombination();
87  final int[] l_arr = new int[p_altNum];
88 
89  for ( int i = 0; i < p_altNum; i++ )
90  l_arr[i] = i;
91 
92  l_combination.combinations( l_arr, p_comSize, 0, new int[p_comSize] );
93 
94  final List<int[]> l_resultList = l_combination.getResultList();
95 
96  final List<BitVector> l_bitVectors = new LinkedList<>();
97 
98  for ( int i = 0; i < l_resultList.size(); i++ )
99  {
100  final BitVector l_bitVector = new BitVector( p_altNum );
101 
102  for ( int j = 0; j < p_comSize; j++ )
103  {
104  l_bitVector.put( l_resultList.get( i )[j], true );
105  }
106  l_bitVectors.add( l_bitVector );
107  }
108 
109  return l_bitVectors;
110  }
111 
112  private int determineMaxHDBV( final List<BitVector> p_votes, final BitVector p_comVect, final int p_altNum )
113  {
114  /* compute Hamming distances to all votes and determine the maximum */
115 
116  int l_maxHD = -1;
117 
118  for ( int i = 0; i < p_votes.size(); i++ )
119  {
120  final BitVector l_curBitCom = p_comVect.copy();
121 
122  l_curBitCom.xor( p_votes.get( i ) );
123 
124  final int l_curHD = l_curBitCom.cardinality();
125 
126  if ( l_curHD > l_maxHD )
127  l_maxHD = l_curHD;
128  }
129  System.out.println( " maximal HD for committee " + p_comVect + ": " + l_maxHD );
130  return l_maxHD;
131  }
132 
141  public Map<Integer, Integer> sortMapASC( final Map<Integer, Integer> p_valuesMap )
142 
143  {
144  final List<Map.Entry<Integer, Integer>> l_list = new LinkedList<>( p_valuesMap.entrySet() );
145 
146  // Sorting the list based on values in ascending order
147  Collections.sort( l_list, Comparator.comparing( Map.Entry::getValue ) );
148 
149  /* Maintaining insertion order with the help of LinkedList */
150  final Map<Integer, Integer> l_sortedMap = new LinkedHashMap<Integer, Integer>();
151  for ( final Map.Entry<Integer, Integer> l_entry : l_list )
152  {
153  l_sortedMap.put( l_entry.getKey(), l_entry.getValue() );
154  }
155 
156  return l_sortedMap;
157  }
158 }
159 
160 
BitVector applyRuleBV(final List< String > p_alternatives, final List< BitVector > p_votes, final int p_comSize)
int determineMaxHDBV(final List< BitVector > p_votes, final BitVector p_comVect, final int p_altNum)
Map< Integer, Integer > sortMapASC(final Map< Integer, Integer > p_valuesMap)
sort HashMap according to its values in ascending order
List< BitVector > computeBitComittees(final int p_altNum, final int p_comSize)
compute possible committees