LightVoting
 All Classes Namespaces Files Functions Variables Pages
org.lightvoting.simulation.agent.CVotingAgent Class Reference

BDI agent with voting capabilities. More...

Inherits IBaseAgent< CVotingAgent >.

+ Collaboration diagram for org.lightvoting.simulation.agent.CVotingAgent:

Classes

class  CVotingAgentGenerator
 Class CVotingAgentGenerator. More...
 

Public Member Functions

 CVotingAgent (final String p_name, final IAgentConfiguration< CVotingAgent > p_configuration, final IBaseAgent< CChairAgent > p_chairagent, final CEnvironment p_environment, final int p_altNum, final String p_grouping, final String p_fileName)
 constructor of the agent More...
 
final CVotingAgent call () throws Exception
 
BitVector getBitVote ()
 
CChairAgent getChair ()
 get associated chair agent More...
 
AtomicIntegerArray getVote ()
 
final String name ()
 Get agent's name. More...
 

Private Member Functions

double computeDiss (final int[] p_resultValues)
 compute dissatisfaction of voter with given committee More...
 
Double computeDissBV (final BitVector p_result)
 
AtomicIntegerArray convertPreferences (final AtomicDoubleArray p_atomicPrefValues)
 
BitVector convertPreferencesToBits (final AtomicDoubleArray p_atomicPrefValues)
 
List< CGroupdetermineActiveGroups ()
 
void determineGroupCoordinated (final List< CGroup > p_activeGroups)
 
AtomicDoubleArray generatePreferences (final int p_altNum)
 
void joinGroup ()
 
void joinGroupCoordinated ()
 
void joinGroupRandom ()
 
void openNewGroup ()
 
void perceiveEnv ()
 
double sigmoidValue (double p_var)
 
Map sortMapDESC (final Map< CGroup, Integer > p_valuesMap)
 
void submitDiss (final CChairAgent p_chairAgent, final Integer p_iteration, final BitVector p_result) throws InterruptedException
 
void submitVote (final CChairAgent p_chairAgent)
 

Private Attributes

final int m_altNum
 number of alternatives More...
 
final AtomicDoubleArray m_atomicPrefValues
 agent's preferences More...
 
final BitVector m_bitVote
 
CChairAgent m_chair
 associated chair agent; More...
 
final CEnvironment m_environment
 environment More...
 
final String m_fileName
 
String m_grouping
 grouping algorithm: "RANDOM" or "COORDINATED" More...
 
Integer m_joinThreshold
 threshold for joining a group in the case of coordinated grouping More...
 
final String m_name
 name of the agent More...
 
AtomicIntegerArray m_vote
 agent's vote More...
 
boolean m_voted
 variable indicating if agent already submitted its vote More...
 

Detailed Description

Constructor & Destructor Documentation

org.lightvoting.simulation.agent.CVotingAgent.CVotingAgent ( final String  p_name,
final IAgentConfiguration< CVotingAgent p_configuration,
final IBaseAgent< CChairAgent p_chairagent,
final CEnvironment  p_environment,
final int  p_altNum,
final String  p_grouping,
final String  p_fileName 
)
Parameters
p_namename of the agent
p_configurationagent configuration of the agent generator
p_chairagentcorresponding chair agent
p_environmentenvironment reference
p_altNumnumber of alternatives
p_groupinggrouping algorithm
p_fileNameh5 file

Definition at line 132 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_altNum, org.lightvoting.simulation.agent.CVotingAgent.m_atomicPrefValues, org.lightvoting.simulation.agent.CVotingAgent.m_bitVote, org.lightvoting.simulation.agent.CVotingAgent.m_chair, org.lightvoting.simulation.agent.CVotingAgent.m_environment, org.lightvoting.simulation.agent.CVotingAgent.m_fileName, org.lightvoting.simulation.agent.CVotingAgent.m_grouping, org.lightvoting.simulation.agent.CVotingAgent.m_joinThreshold, org.lightvoting.simulation.agent.CVotingAgent.m_name, org.lightvoting.simulation.agent.CVotingAgent.m_vote, and org.lightvoting.simulation.agent.CVotingAgent.m_voted.

Referenced by org.lightvoting.simulation.agent.CVotingAgent.CVotingAgentGenerator.generatesingle().

138  {
139  super( p_configuration );
140  m_name = p_name;
141  m_environment = p_environment;
142 
143  m_chair = (CChairAgent) p_chairagent;
144  m_storage.put( "chair", p_chairagent.raw() );
145 
146  m_beliefbase.add(
147  CLiteral.from(
148  "chair",
149  CRawTerm.from( p_chairagent )
150  )
151  );
152 
153  // sleep chair, Long.MAX_VALUE -> inf
154  p_chairagent.sleep( Long.MAX_VALUE );
155 
156  m_altNum = p_altNum;
157 
158  m_atomicPrefValues = this.generatePreferences( m_altNum );
159  m_vote = this.convertPreferences( m_atomicPrefValues );
160  m_bitVote = this.convertPreferencesToBits( m_atomicPrefValues );
161  m_voted = false;
162  m_joinThreshold = 5;
163  m_grouping = p_grouping;
164  m_fileName = p_fileName;
165  }
boolean m_voted
variable indicating if agent already submitted its vote
CChairAgent m_chair
associated chair agent;
final int m_altNum
number of alternatives
AtomicIntegerArray m_vote
agent's vote
final CEnvironment m_environment
environment
final AtomicDoubleArray m_atomicPrefValues
agent's preferences
Integer m_joinThreshold
threshold for joining a group in the case of coordinated grouping
String m_grouping
grouping algorithm: "RANDOM" or "COORDINATED"
final String m_name
name of the agent

+ Here is the caller graph for this function:

Member Function Documentation

final CVotingAgent org.lightvoting.simulation.agent.CVotingAgent.call ( ) throws Exception

Definition at line 169 of file CVotingAgent.java.

170  {
171  // run default cycle
172  return super.call();
173  }
double org.lightvoting.simulation.agent.CVotingAgent.computeDiss ( final int[]  p_resultValues)
private
Parameters
p_resultValuescommittee
Returns
dissatisfaction with committee

Definition at line 429 of file CVotingAgent.java.

430  {
431  double l_diss = 0;
432 
433  for ( int i = 0; i < p_resultValues.length; i++ )
434  {
435  if ( p_resultValues[i] == 1 )
436  l_diss = l_diss + ( 1 - m_atomicPrefValues.get( i ) );
437  }
438  return l_diss;
439  }
Double org.lightvoting.simulation.agent.CVotingAgent.computeDissBV ( final BitVector  p_result)
private

Definition at line 267 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_atomicPrefValues.

268  {
269  double l_diss = 0;
270 
271  for ( int i = 0; i < p_result.size(); i++ )
272  {
273  if ( p_result.get( i ) )
274  l_diss = l_diss + ( 1 - m_atomicPrefValues.get( i ) );
275  }
276  return l_diss;
277  }
final AtomicDoubleArray m_atomicPrefValues
agent's preferences
AtomicIntegerArray org.lightvoting.simulation.agent.CVotingAgent.convertPreferences ( final AtomicDoubleArray  p_atomicPrefValues)
private

Definition at line 296 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_altNum.

297  {
298  final int[] l_voteValues = new int[m_altNum];
299  for ( int i = 0; i < m_altNum; i++ )
300  if ( p_atomicPrefValues.get( i ) > 0.5 )
301  l_voteValues[i] = 1;
302  else
303  l_voteValues[i] = 0;
304  System.out.println( "Vote: " + Arrays.toString( l_voteValues ) );
305  return new AtomicIntegerArray( l_voteValues );
306  }
final int m_altNum
number of alternatives
BitVector org.lightvoting.simulation.agent.CVotingAgent.convertPreferencesToBits ( final AtomicDoubleArray  p_atomicPrefValues)
private

Definition at line 308 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_altNum.

309  {
310  final BitVector l_voteValues = new BitVector( m_altNum );
311  for ( int i = 0; i < m_altNum; i++ )
312  if ( p_atomicPrefValues.get( i ) > 0.5 )
313  l_voteValues.put( i, true );
314  else
315  l_voteValues.put( i, false );
316  System.out.println( "Vote as BitVector: " + l_voteValues );
317  return l_voteValues;
318  }
final int m_altNum
number of alternatives
List<CGroup> org.lightvoting.simulation.agent.CVotingAgent.determineActiveGroups ( )
private

Definition at line 321 of file CVotingAgent.java.

322  {
323  final AtomicReference<List<CGroup>> l_groupList = new AtomicReference<>();
324 
325  m_beliefbase.beliefbase().literal( "groups" ).stream().forEach( i ->
326  l_groupList.set( ( (ILiteral) i ).values().findFirst().get().raw() ) );
327 
328  final List<CGroup> l_activeGroups = new LinkedList<>();
329 
330  for ( int i = 0; i < l_groupList.get().size(); i++ )
331  if ( l_groupList.get().get( i ).open() )
332  {
333  l_activeGroups.add( l_groupList.get().get( i ) );
334  }
335 
336  return l_activeGroups;
337  }
void org.lightvoting.simulation.agent.CVotingAgent.determineGroupCoordinated ( final List< CGroup p_activeGroups)
private

Definition at line 388 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_joinThreshold.

389  {
390  final CGroup l_group;
391  // choose group to join
392  final Map<CGroup, Integer> l_groupDistances = new HashMap<>();
393  final BitVector l_vote = this.getBitVote();
394  System.out.println( "Vote: " + l_vote );
395  for ( int i = 0; i < p_activeGroups.size(); i++ )
396  {
397  final BitVector l_com = p_activeGroups.get( i ).result();
398  System.out.println( "Committee: " + l_com );
399 
400  System.out.println( "Vote: " + l_vote );
401  System.out.println( "Committee: " + l_com );
402  l_com.xor( l_vote );
403  final int l_HD = l_com.cardinality();
404  System.out.println( "Hamming distance: " + l_HD );
405  l_groupDistances.put( p_activeGroups.get( i ), l_HD );
406  }
407  final Map l_sortedDistances = this.sortMapDESC( l_groupDistances );
408  final Map.Entry<CGroup, Integer> l_entry = (Map.Entry<CGroup, Integer>) l_sortedDistances.entrySet().iterator().next();
409  l_group = l_entry.getKey();
410 
411  // if Hamming distance is above the threshold, do not join the chair but create a new group
412  if ( l_entry.getValue() > m_joinThreshold )
413  {
414  this.openNewGroup();
415  return;
416  }
417  m_environment.addAgentCoordinated( l_group, this );
418  this.beliefbase().add( l_group.literal( this ) );
419  System.out.println( this.name() + " joins group " + l_group );
420  }
Integer m_joinThreshold
threshold for joining a group in the case of coordinated grouping
AtomicDoubleArray org.lightvoting.simulation.agent.CVotingAgent.generatePreferences ( final int  p_altNum)
private

Definition at line 281 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_altNum, and org.lightvoting.simulation.agent.CVotingAgent.sigmoidValue().

282  {
283  final Random l_random = new Random();
284  final double[] l_prefValues = new double[m_altNum];
285  for ( int i = 0; i < m_altNum; i++ )
286  l_prefValues[i] = this.sigmoidValue( l_random.nextDouble() - 0.5 );
287  System.out.println( "Preference Values: " + Arrays.toString( l_prefValues ) );
288  return new AtomicDoubleArray( l_prefValues );
289  }
final int m_altNum
number of alternatives

+ Here is the call graph for this function:

BitVector org.lightvoting.simulation.agent.CVotingAgent.getBitVote ( )

Definition at line 202 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_bitVote.

203  {
204  return m_bitVote;
205  }
CChairAgent org.lightvoting.simulation.agent.CVotingAgent.getChair ( )
Returns
chair agent

Definition at line 192 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_chair.

193  {
194  return m_chair;
195  }
CChairAgent m_chair
associated chair agent;
AtomicIntegerArray org.lightvoting.simulation.agent.CVotingAgent.getVote ( )

Definition at line 197 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_vote.

198  {
199  return m_vote;
200  }
AtomicIntegerArray m_vote
agent's vote
void org.lightvoting.simulation.agent.CVotingAgent.joinGroup ( )
private

Definition at line 219 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.joinGroupCoordinated(), and org.lightvoting.simulation.agent.CVotingAgent.m_grouping.

220  {
221  if ( "RANDOM".equals( m_grouping ) )
222  this.joinGroupRandom();
223 
224  if ( "COORDINATED".equals( m_grouping ) )
225  this.joinGroupCoordinated();
226  }
String m_grouping
grouping algorithm: "RANDOM" or "COORDINATED"

+ Here is the call graph for this function:

void org.lightvoting.simulation.agent.CVotingAgent.joinGroupCoordinated ( )
private

Definition at line 372 of file CVotingAgent.java.

Referenced by org.lightvoting.simulation.agent.CVotingAgent.joinGroup().

373  {
374  System.out.println( "join group according to coordinated grouping algorithm" );
375 
376  final List<CGroup> l_activeGroups = this.determineActiveGroups();
377 
378  if ( l_activeGroups.isEmpty() )
379  {
380  this.openNewGroup();
381  return;
382  }
383 
384  else
385  this.determineGroupCoordinated( l_activeGroups );
386  }

+ Here is the caller graph for this function:

void org.lightvoting.simulation.agent.CVotingAgent.joinGroupRandom ( )
private

Definition at line 353 of file CVotingAgent.java.

354  {
355 
356  final List<CGroup> l_activeGroups = this.determineActiveGroups();
357 
358  if ( l_activeGroups.isEmpty() )
359  {
360  this.openNewGroup();
361  return;
362  }
363 
364  final Random l_rand = new Random();
365 
366  final CGroup l_randomGroup = l_activeGroups.get( l_rand.nextInt( l_activeGroups.size() ) );
367  m_environment.addAgentRandom( l_randomGroup, this );
368  this.beliefbase().add( l_randomGroup.literal( this ) );
369 
370  }
final String org.lightvoting.simulation.agent.CVotingAgent.name ( )
Returns
name of agent

Definition at line 182 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_name.

183  {
184  return m_name;
185  }
final String m_name
name of the agent
void org.lightvoting.simulation.agent.CVotingAgent.openNewGroup ( )
private

Definition at line 339 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_grouping.

340  {
341  final CGroup l_group;
342 
343  if ( "RANDOM".equals( m_grouping ) )
344  l_group = m_environment.openNewGroupRandom( this );
345 
346  else
347  l_group = m_environment.openNewGroupCoordinated( this );
348 
349  this.beliefbase().add( l_group.literal( this ) );
350  System.out.println( "opened new group " + l_group );
351  }
String m_grouping
grouping algorithm: "RANDOM" or "COORDINATED"
void org.lightvoting.simulation.agent.CVotingAgent.perceiveEnv ( )
private

Definition at line 211 of file CVotingAgent.java.

References org.lightvoting.simulation.environment.CEnvironment.literal(), and org.lightvoting.simulation.agent.CVotingAgent.m_environment.

212  {
213  this.beliefbase().add( m_environment.literal( this ) );
214  System.out.println( this.name() + " perceived environment " );
215  }
ILiteral literal(final CVotingAgent p_votingAgent)
returns literal representation of existing groups
final CEnvironment m_environment
environment

+ Here is the call graph for this function:

double org.lightvoting.simulation.agent.CVotingAgent.sigmoidValue ( double  p_var)
private

Definition at line 291 of file CVotingAgent.java.

Referenced by org.lightvoting.simulation.agent.CVotingAgent.generatePreferences().

292  {
293  return 1 / ( 1 + Math.pow( Math.E, -1 * p_var ) );
294  }

+ Here is the caller graph for this function:

Map org.lightvoting.simulation.agent.CVotingAgent.sortMapDESC ( final Map< CGroup, Integer >  p_valuesMap)
private

Definition at line 443 of file CVotingAgent.java.

444  {
445  final List<Map.Entry<CGroup, Integer>> l_list = new LinkedList<>( p_valuesMap.entrySet() );
446 
447  /* Sorting the list based on values in descending order */
448 
449  Collections.sort( l_list, ( p_first, p_second ) ->
450  p_second.getValue().compareTo( p_first.getValue() ) );
451 
452  /* Maintaining insertion order with the help of LinkedList */
453 
454  final Map<CGroup, Integer> l_sortedMap = new LinkedHashMap<>();
455  for ( final Map.Entry<CGroup, Integer> l_entry : l_list )
456  {
457  l_sortedMap.put( l_entry.getKey(), l_entry.getValue() );
458  }
459 
460  return l_sortedMap;
461  }
void org.lightvoting.simulation.agent.CVotingAgent.submitDiss ( final CChairAgent  p_chairAgent,
final Integer  p_iteration,
final BitVector  p_result 
) throws InterruptedException
private

Definition at line 252 of file CVotingAgent.java.

253  {
254  p_chairAgent.trigger(
255  CTrigger.from(
256  ITrigger.EType.ADDGOAL,
257  CLiteral.from(
258  "diss/received",
259  CRawTerm.from( this.name() ),
260  CRawTerm.from( this.computeDissBV( p_result ) ),
261  CRawTerm.from( p_iteration )
262  )
263  )
264  );
265  }
void org.lightvoting.simulation.agent.CVotingAgent.submitVote ( final CChairAgent  p_chairAgent)
private

Definition at line 230 of file CVotingAgent.java.

References org.lightvoting.simulation.agent.CVotingAgent.m_voted.

231  {
232  if ( m_voted )
233  return;
234 
235  p_chairAgent.trigger(
236  CTrigger.from(
237  ITrigger.EType.ADDGOAL,
238  CLiteral.from(
239  "vote/received",
240  CRawTerm.from( this.name() ),
241  CRawTerm.from( this.getBitVote() )
242  )
243  )
244  );
245 
246  m_voted = true;
247 
248  }
boolean m_voted
variable indicating if agent already submitted its vote

Member Data Documentation

final AtomicDoubleArray org.lightvoting.simulation.agent.CVotingAgent.m_atomicPrefValues
private
final BitVector org.lightvoting.simulation.agent.CVotingAgent.m_bitVote
private
CChairAgent org.lightvoting.simulation.agent.CVotingAgent.m_chair
private
final CEnvironment org.lightvoting.simulation.agent.CVotingAgent.m_environment
private
final String org.lightvoting.simulation.agent.CVotingAgent.m_fileName
private
Integer org.lightvoting.simulation.agent.CVotingAgent.m_joinThreshold
private
final String org.lightvoting.simulation.agent.CVotingAgent.m_name
private
AtomicIntegerArray org.lightvoting.simulation.agent.CVotingAgent.m_vote
private
boolean org.lightvoting.simulation.agent.CVotingAgent.m_voted
private