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

Created by sophie on 10.01.17. More...

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

Public Member Functions

BitVector applyRuleBV (final List< String > p_alternatives, final List< BitVector > p_votes, final int p_comSize)
 compute the winning committee according to Minisum Approval More...
 
Map< Integer, Integer > sortMapDESC (final Map< Integer, Integer > p_valuesMap)
 sort HashMap according to its values in descending order More...
 

Private Attributes

List< String > m_alternatives
 
List< BitVector > m_bitVotes
 
BitVector m_comBV
 
int m_comSize
 

Detailed Description

Computes result of election according to Minisum Approval voting rule. re-used code from http://stackoverflow.com/questions/8119366/sorting-hashmap-by-values

Definition at line 47 of file CMinisumApproval.java.

Member Function Documentation

BitVector org.lightvoting.simulation.rule.CMinisumApproval.applyRuleBV ( final List< String >  p_alternatives,
final List< BitVector >  p_votes,
final int  p_comSize 
)
Parameters
p_alternativesavailable alternatives
p_votessubmitted votes
p_comSizesize of committee to be elected
Returns
elected committee

Definition at line 68 of file CMinisumApproval.java.

References org.lightvoting.simulation.rule.CMinisumApproval.m_alternatives, org.lightvoting.simulation.rule.CMinisumApproval.m_bitVotes, org.lightvoting.simulation.rule.CMinisumApproval.m_comBV, and org.lightvoting.simulation.rule.CMinisumApproval.m_comSize.

69  {
70  m_alternatives = p_alternatives;
71  m_comSize = p_comSize;
72  m_bitVotes = p_votes;
73  m_comBV = new BitVector( m_alternatives.size() );
74 
75  final int[] l_valuesVect = new int[m_alternatives.size()];
76 
77  Map<Integer, Integer> l_valuesMap = new HashMap<Integer, Integer>();
78  for ( int i = 0; i < m_alternatives.size(); i++ )
79  {
80  for ( int j = 0; j < m_bitVotes.size(); j++ )
81  {
82  if ( ( m_bitVotes.get( j ) ).get( i ) )
83  {
84  l_valuesVect[i]++;
85  }
86  }
87 
88  /* create HashMap with index of alternative as key and score of alternative as value */
89 
90  l_valuesMap.put( i, l_valuesVect[i] );
91  }
92 
93  /* sort the HashMap in descending order according to values */
94 
95  l_valuesMap = this.sortMapDESC( l_valuesMap );
96 
97  /* create committee vector according to sorted HashMap: For the first k entries, put a "1" in the position according to the index, i.e. the key.*/
98 
99  int l_occupied = 0;
100 
101  for ( final Entry<Integer, Integer> l_entry : l_valuesMap.entrySet() )
102  {
103  if ( l_occupied < m_comSize )
104  {
105  m_comBV.put( l_entry.getKey(), true );
106  l_occupied++;
107  }
108  else
109  break;
110  }
111 
112  return m_comBV;
113  }
Map<Integer, Integer> org.lightvoting.simulation.rule.CMinisumApproval.sortMapDESC ( final Map< Integer, Integer >  p_valuesMap)
Parameters
p_valuesMapHashMap with Approval scores
Returns
sorted HashMap

Definition at line 121 of file CMinisumApproval.java.

122  {
123  final List<Entry<Integer, Integer>> l_list = new LinkedList<>( p_valuesMap.entrySet() );
124 
125  /* Sorting the list based on values in descending order */
126  Collections.sort( l_list, ( p_first, p_second ) ->
127  p_second.getValue().compareTo( p_first.getValue() ) );
128 
129  /* Maintaining insertion order with the help of LinkedList */
130  final Map<Integer, Integer> l_sortedMap = new LinkedHashMap<Integer, Integer>();
131  for ( final Entry<Integer, Integer> l_entry : l_list )
132  {
133  l_sortedMap.put( l_entry.getKey(), l_entry.getValue() );
134  }
135 
136  return l_sortedMap;
137  }

Member Data Documentation

List<String> org.lightvoting.simulation.rule.CMinisumApproval.m_alternatives
private
List<BitVector> org.lightvoting.simulation.rule.CMinisumApproval.m_bitVotes
private
BitVector org.lightvoting.simulation.rule.CMinisumApproval.m_comBV
private
int org.lightvoting.simulation.rule.CMinisumApproval.m_comSize
private