LightVoting
 All Classes Namespaces Files Functions Variables Pages
org.lightvoting.simulation.rule.CMinimaxApproval Class Reference

Created by sophie on 10.01.17. More...

+ Collaboration diagram for org.lightvoting.simulation.rule.CMinimaxApproval:

Public Member Functions

BitVector applyRuleBV (final List< String > p_alternatives, final List< BitVector > p_votes, final int p_comSize)
 
Map< Integer, Integer > sortMapASC (final Map< Integer, Integer > p_valuesMap)
 sort HashMap according to its values in ascending order More...
 

Private Member Functions

List< BitVector > computeBitComittees (final int p_altNum, final int p_comSize)
 compute possible committees More...
 
int determineMaxHDBV (final List< BitVector > p_votes, final BitVector p_comVect, final int p_altNum)
 

Detailed Description

Member Function Documentation

BitVector org.lightvoting.simulation.rule.CMinimaxApproval.applyRuleBV ( final List< String >  p_alternatives,
final List< BitVector >  p_votes,
final int  p_comSize 
)

Definition at line 54 of file CMinimaxApproval.java.

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  }
List<BitVector> org.lightvoting.simulation.rule.CMinimaxApproval.computeBitComittees ( final int  p_altNum,
final int  p_comSize 
)
private
Parameters
p_altNumnumber of alternatives
p_comSizecommittee size
Returns
possible committees

Definition at line 84 of file CMinimaxApproval.java.

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  }
int org.lightvoting.simulation.rule.CMinimaxApproval.determineMaxHDBV ( final List< BitVector >  p_votes,
final BitVector  p_comVect,
final int  p_altNum 
)
private

Definition at line 112 of file CMinimaxApproval.java.

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  }
Map<Integer, Integer> org.lightvoting.simulation.rule.CMinimaxApproval.sortMapASC ( final Map< Integer, Integer >  p_valuesMap)
Parameters
p_valuesMapHashMap with Approval scores
Returns
sorted HashMap

Definition at line 141 of file CMinimaxApproval.java.

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  }