24 package org.lightvoting.simulation.agent;
26 import cern.colt.Arrays;
27 import cern.colt.bitvector.BitVector;
28 import com.google.common.util.concurrent.AtomicDoubleArray;
29 import org.lightjason.agentspeak.action.binding.IAgentAction;
30 import org.lightjason.agentspeak.action.binding.IAgentActionFilter;
31 import org.lightjason.agentspeak.action.binding.IAgentActionName;
32 import org.lightjason.agentspeak.agent.IBaseAgent;
33 import org.lightjason.agentspeak.common.CCommon;
34 import org.lightjason.agentspeak.configuration.IAgentConfiguration;
35 import org.lightjason.agentspeak.generator.IBaseAgentGenerator;
36 import org.lightjason.agentspeak.language.CLiteral;
37 import org.lightjason.agentspeak.language.CRawTerm;
38 import org.lightjason.agentspeak.language.ILiteral;
39 import org.lightjason.agentspeak.language.instantiable.plan.trigger.CTrigger;
40 import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;
41 import org.lightjason.agentspeak.language.score.IAggregation;
47 import java.io.InputStream;
48 import java.text.MessageFormat;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.LinkedHashMap;
52 import java.util.LinkedList;
53 import java.util.List;
55 import java.util.Random;
56 import java.util.concurrent.atomic.AtomicIntegerArray;
57 import java.util.concurrent.atomic.AtomicLong;
58 import java.util.concurrent.atomic.AtomicReference;
59 import java.util.stream.Collectors;
60 import java.util.stream.Stream;
132 public CVotingAgent(
final String p_name,
final IAgentConfiguration<CVotingAgent> p_configuration,
final IBaseAgent<CChairAgent> p_chairagent,
135 final String p_grouping,
136 final String p_fileName
139 super( p_configuration );
144 m_storage.put(
"chair", p_chairagent.raw() );
149 CRawTerm.from( p_chairagent )
154 p_chairagent.sleep( Long.MAX_VALUE );
210 @IAgentActionName(
name =
"perceive/env" )
214 System.out.println( this.name() +
" perceived environment " );
218 @IAgentActionName(
name =
"join/group" )
222 this.joinGroupRandom();
229 @IAgentActionName(
name =
"submit/vote" )
235 p_chairAgent.trigger(
237 ITrigger.EType.ADDGOAL,
240 CRawTerm.from( this.name() ),
241 CRawTerm.from( this.getBitVote() )
251 @IAgentActionName(
name =
"submit/dissatisfaction" )
252 private
void submitDiss( final
CChairAgent p_chairAgent, final Integer p_iteration, final BitVector p_result ) throws InterruptedException
254 p_chairAgent.trigger(
256 ITrigger.EType.ADDGOAL,
259 CRawTerm.from( this.name() ),
260 CRawTerm.from( this.computeDissBV( p_result ) ),
261 CRawTerm.from( p_iteration )
271 for (
int i = 0; i < p_result.size(); i++ )
273 if ( p_result.get( i ) )
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 );
293 return 1 / ( 1 + Math.pow( Math.E, -1 * p_var ) );
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 )
304 System.out.println(
"Vote: " + Arrays.toString( l_voteValues ) );
305 return new AtomicIntegerArray( l_voteValues );
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 );
315 l_voteValues.put( i, false );
316 System.out.println(
"Vote as BitVector: " + l_voteValues );
323 final AtomicReference<List<CGroup>> l_groupList =
new AtomicReference<>();
325 m_beliefbase.beliefbase().literal(
"groups" ).stream().forEach( i ->
326 l_groupList.set( ( (ILiteral) i ).values().findFirst().
get().raw() ) );
328 final List<CGroup> l_activeGroups =
new LinkedList<>();
330 for (
int i = 0; i < l_groupList.get().size(); i++ )
331 if ( l_groupList.get().get( i ).open() )
333 l_activeGroups.add( l_groupList.get().
get( i ) );
336 return l_activeGroups;
344 l_group = m_environment.openNewGroupRandom( this );
347 l_group = m_environment.openNewGroupCoordinated( this );
349 this.beliefbase().add( l_group.literal(
this ) );
350 System.out.println(
"opened new group " + l_group );
356 final List<CGroup> l_activeGroups = this.determineActiveGroups();
358 if ( l_activeGroups.isEmpty() )
364 final Random l_rand =
new Random();
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 ) );
374 System.out.println(
"join group according to coordinated grouping algorithm" );
376 final List<CGroup> l_activeGroups = this.determineActiveGroups();
378 if ( l_activeGroups.isEmpty() )
385 this.determineGroupCoordinated( l_activeGroups );
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++ )
397 final BitVector l_com = p_activeGroups.get( i ).result();
398 System.out.println(
"Committee: " + l_com );
400 System.out.println(
"Vote: " + l_vote );
401 System.out.println(
"Committee: " + l_com );
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 );
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();
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 );
433 for (
int i = 0; i < p_resultValues.length; i++ )
435 if ( p_resultValues[i] == 1 )
436 l_diss = l_diss + ( 1 - m_atomicPrefValues.get( i ) );
445 final List<Map.Entry<
CGroup, Integer>> l_list =
new LinkedList<>( p_valuesMap.entrySet() );
449 Collections.sort( l_list, ( p_first, p_second ) ->
450 p_second.getValue().compareTo( p_first.getValue() ) );
454 final Map<CGroup, Integer> l_sortedMap =
new LinkedHashMap<>();
455 for (
final Map.Entry<CGroup, Integer> l_entry : l_list )
457 l_sortedMap.put( l_entry.getKey(), l_entry.getValue() );
500 final String p_grouping,
501 final String p_fileName
512 CCommon.actionsFromPackage(),
515 CCommon.actionsFromAgentClass( CVotingAgent.class ),
522 ).collect( Collectors.toSet() ),
543 m_send.unregister( p_agent );
560 MessageFormat.format(
"agent {0}",
m_agentcounter.getAndIncrement() ),
572 l_votingAgent.sleep( Integer.MAX_VALUE );
573 m_environment.initialset( l_votingAgent );
574 return m_send.register( l_votingAgent );
boolean m_voted
variable indicating if agent already submitted its vote
final int m_altNum
number of alternatives
Created by sophie on 22.02.17.
Map sortMapDESC(final Map< CGroup, Integer > p_valuesMap)
final AtomicLong m_agentcounter
Current free agent id, needs to be thread-safe, therefore using AtomicLong.
CChairAgent m_chair
associated chair agent;
final CEnvironment m_environment
environment reference
void joinGroupCoordinated()
final CVotingAgent call()
void submitDiss(final CChairAgent p_chairAgent, final Integer p_iteration, final BitVector p_result)
final String name()
Get agent's name.
Double computeDissBV(final BitVector p_result)
ILiteral literal(final CVotingAgent p_votingAgent)
returns literal representation of existing groups
final int m_altNum
number of alternatives
BitVector convertPreferencesToBits(final AtomicDoubleArray p_atomicPrefValues)
void submitVote(final CChairAgent p_chairAgent)
final CVotingAgent generatesingle(final Object...p_data)
AtomicDoubleArray generatePreferences(final int p_altNum)
double sigmoidValue(double p_var)
void determineGroupCoordinated(final List< CGroup > p_activeGroups)
CChairAgent getChair()
get associated chair agent
List< CGroup > determineActiveGroups()
AtomicIntegerArray m_vote
agent's vote
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
Created by sophie on 21.02.17.
final CEnvironment m_environment
environment
Created by sophie on 21.02.17.
final CSend m_send
Store reference to send action to registered agents upon creation.
Action to send messages for communication.
CVotingAgentGenerator(final CSend p_send, final InputStream p_stream, final CEnvironment p_environment, final int p_altNum, final String p_grouping, final String p_fileName)
constructor of the generator
AtomicIntegerArray getVote()
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"
Created by sophie on 24.04.17.
final void unregister(final CVotingAgent p_agent)
Class CVotingAgentGenerator.
double computeDiss(final int[] p_resultValues)
compute dissatisfaction of voter with given committee
BDI agent with voting capabilities.
final String m_name
name of the agent
final BitVector m_bitVote
AtomicIntegerArray convertPreferences(final AtomicDoubleArray p_atomicPrefValues)