LightVoting
 All Classes Namespaces Files Functions Variables Pages
CMinisumApproval.java
Go to the documentation of this file.
1 
24 package org.lightvoting.simulation.rule;
25 
26 
27 //import java.util.Arrays;
28 
29 import cern.colt.bitvector.BitVector;
30 
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 
39 
40 
47 public class CMinisumApproval
48 {
49 
50  /* m_alternatives list */
51  private List<String> m_alternatives;
52  /* committee size */
53  private int m_comSize;
54 
55  private BitVector m_comBV;
56 
57  private List<BitVector> m_bitVotes;
58 
68  public BitVector applyRuleBV( final List<String> p_alternatives, final List<BitVector> p_votes, final int p_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  }
114 
121  public Map<Integer, Integer> sortMapDESC( final Map<Integer, Integer> p_valuesMap )
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  }
138 
139 }
BitVector applyRuleBV(final List< String > p_alternatives, final List< BitVector > p_votes, final int p_comSize)
compute the winning committee according to Minisum Approval
Map< Integer, Integer > sortMapDESC(final Map< Integer, Integer > p_valuesMap)
sort HashMap according to its values in descending order