24 package org.lightvoting.simulation.rule;
26 import cern.colt.bitvector.BitVector;
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;
54 public BitVector
applyRuleBV(
final List<String> p_alternatives,
final List<BitVector> p_votes,
final int p_comSize )
58 final List<BitVector> l_bitCommittees = this.computeBitComittees( p_alternatives.size(), p_comSize );
59 System.out.println( l_bitCommittees );
63 Map<Integer, Integer> l_maxMap =
new HashMap<Integer, Integer>();
65 for (
int i = 0; i < l_bitCommittees.size(); i++ )
68 l_maxMap.put( i, this.determineMaxHDBV( p_votes, l_bitCommittees.get( i ), p_alternatives.size() ) );
71 l_maxMap = this.sortMapASC( l_maxMap );
73 return l_bitCommittees.get( l_maxMap.entrySet().iterator().next().getKey() );
87 final int[] l_arr =
new int[p_altNum];
89 for (
int i = 0; i < p_altNum; i++ )
92 l_combination.combinations( l_arr, p_comSize, 0,
new int[p_comSize] );
94 final List<int[]> l_resultList = l_combination.getResultList();
96 final List<BitVector> l_bitVectors =
new LinkedList<>();
98 for (
int i = 0; i < l_resultList.size(); i++ )
100 final BitVector l_bitVector =
new BitVector( p_altNum );
102 for (
int j = 0; j < p_comSize; j++ )
104 l_bitVector.put( l_resultList.get( i )[j],
true );
106 l_bitVectors.add( l_bitVector );
112 private int determineMaxHDBV(
final List<BitVector> p_votes,
final BitVector p_comVect,
final int p_altNum )
118 for (
int i = 0; i < p_votes.size(); i++ )
120 final BitVector l_curBitCom = p_comVect.copy();
122 l_curBitCom.xor( p_votes.get( i ) );
124 final int l_curHD = l_curBitCom.cardinality();
126 if ( l_curHD > l_maxHD )
129 System.out.println(
" maximal HD for committee " + p_comVect +
": " + l_maxHD );
141 public Map<Integer, Integer>
sortMapASC(
final Map<Integer, Integer> p_valuesMap )
144 final List<Map.Entry<Integer, Integer>> l_list =
new LinkedList<>( p_valuesMap.entrySet() );
147 Collections.sort( l_list, Comparator.comparing( Map.Entry::getValue ) );
150 final Map<Integer, Integer> l_sortedMap =
new LinkedHashMap<Integer, Integer>();
151 for (
final Map.Entry<Integer, Integer> l_entry : l_list )
153 l_sortedMap.put( l_entry.getKey(), l_entry.getValue() );
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)
Created by sophie on 10.01.17.
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
Created by sophie on 08.02.17.