24 package org.lightvoting.simulation.agent;
26 import cern.colt.bitvector.BitVector;
27 import org.lightjason.agentspeak.action.binding.IAgentAction;
28 import org.lightjason.agentspeak.action.binding.IAgentActionFilter;
29 import org.lightjason.agentspeak.action.binding.IAgentActionName;
30 import org.lightjason.agentspeak.agent.IBaseAgent;
31 import org.lightjason.agentspeak.common.CCommon;
32 import org.lightjason.agentspeak.configuration.IAgentConfiguration;
33 import org.lightjason.agentspeak.generator.IBaseAgentGenerator;
34 import org.lightjason.agentspeak.language.CLiteral;
35 import org.lightjason.agentspeak.language.CRawTerm;
36 import org.lightjason.agentspeak.language.ILiteral;
37 import org.lightjason.agentspeak.language.instantiable.plan.trigger.CTrigger;
38 import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;
39 import org.lightjason.agentspeak.language.score.IAggregation;
44 import java.io.InputStream;
45 import java.text.MessageFormat;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.concurrent.atomic.AtomicIntegerArray;
51 import java.util.concurrent.atomic.AtomicLong;
52 import java.util.concurrent.atomic.AtomicReference;
53 import java.util.stream.Collectors;
54 import java.util.stream.Stream;
63 public final class CChairAgent extends IBaseAgent<CChairAgent>
101 public CChairAgent(
final String p_name,
final IAgentConfiguration<CChairAgent> p_configuration,
final CEnvironment p_environment,
final String p_grouping,
102 final String p_protocol,
103 final String p_fileName
106 super( p_configuration );
109 m_bitVotes = Collections.synchronizedList(
new LinkedList<>() );
110 m_dissList = Collections.synchronizedList(
new LinkedList<>() );
111 m_dissVoters = Collections.synchronizedList(
new LinkedList<>() );
115 m_agents = Collections.synchronizedList(
new LinkedList<>() );
141 @IAgentActionName(
name =
"perceive/group" )
155 @IAgentActionName(
name =
"check/conditions" )
161 final CGroup l_group = this.determineGroup();
165 final ITrigger l_trigger;
173 this.computeResult();
180 l_group.startProgress();
182 l_trigger = CTrigger.from(
183 ITrigger.EType.ADDGOAL,
184 CLiteral.from(
"start/criterion/fulfilled" )
187 this.trigger( l_trigger );
195 final AtomicReference<CGroup> l_groupAtomic =
new AtomicReference<>();
196 final Collection l_groups = this.beliefbase().beliefbase().literal(
"group" );
197 l_groups.stream().forEach( i -> l_groupAtomic.set( ( (ILiteral) i ).values().findFirst().
get().raw() ) );
198 return l_groupAtomic.get();
206 @IAgentActionName(
name =
"start/election" )
210 final CGroup l_group = this.determineGroup();
211 l_group.triggerAgents( this );
220 @IAgentActionName(
name =
"store/vote" )
221 public
void storeVote( final String p_agentName, final BitVector p_vote )
223 final CGroup l_group = this.determineGroup();
225 m_agents.add( l_group.determineAgent( p_agentName ) );
226 m_bitVotes.add( p_vote );
231 final ITrigger l_trigger = CTrigger.from(
232 ITrigger.EType.ADDGOAL,
234 "all/votes/received" )
238 System.out.println(
" CChairAgent.java: all votes received " );
240 this.trigger( l_trigger );
244 private BitVector
toBV(
final AtomicIntegerArray p_vote )
246 final BitVector l_bitVector =
new BitVector( p_vote.length() );
247 for (
int i = 0; i < p_vote.length(); i++ )
248 if ( p_vote.get( i ) == 1 )
249 l_bitVector.put( i,
true );
258 @IAgentActionName(
name =
"compute/result" )
262 final CGroup l_group = this.determineGroup();
266 final List<String> l_alternatives =
new LinkedList<>();
268 for (
char l_char :
"ABCDEF".toCharArray() )
270 l_alternatives.add( String.valueOf( l_char ) );
272 System.out.println(
" Alternatives: " + l_alternatives );
274 System.out.println(
" Votes: " +
m_bitVotes );
276 final BitVector l_comResultBV = l_minisumApproval.applyRuleBV( l_alternatives,
m_bitVotes, 3 );
278 System.out.println(
" Result of election as BV: " + l_comResultBV );
285 this.beliefbase().add( l_group.
updateBasic(
this, l_comResultBV ) );
292 System.out.println(
" reopening group " );
293 m_environment.reopen( l_group );
299 System.out.println(
" Update iterative " );
306 if (
"ITERATIVE".equals(
m_protocol ) && !l_group.finale() )
308 System.out.println(
" Update basic " );
309 this.beliefbase().add( l_group.
updateBasic(
this, l_comResultBV ) );
323 @IAgentActionName(
name =
"store/diss" )
325 public
void storeDiss( final String p_name, final Double p_diss, final Integer p_iteration )
327 final CGroup l_group = this.determineGroup();
329 m_dissList.add( p_diss );
330 final CVotingAgent l_dissAg = l_group.determineAgent( p_name );
331 m_dissVoters.add( l_dissAg );
333 System.out.println(
"Storing diss " + p_diss );
337 final ITrigger l_trigger = CTrigger.from(
338 ITrigger.EType.ADDGOAL,
340 "all/dissValues/received",
341 CRawTerm.from( p_iteration )
346 this.trigger( l_trigger );
348 System.out.println( p_iteration +
" All voters submitted their dissatisfaction value" );
356 @IAgentActionName(
name =
"remove/voter" )
359 final CGroup l_group = this.determineGroup();
361 final int l_maxIndex = this.getMaxIndex(
m_dissList );
362 final double l_max = m_dissList.get( l_maxIndex );
363 System.out.println(
" max diss is " + l_max );
367 System.out.println(
" Determining most dissatisfied voter " );
368 final CVotingAgent l_maxDissAg = m_dissVoters.get( l_maxIndex );
369 System.out.println(
" Most dissatisfied voter is " + l_maxDissAg.name() );
371 m_bitVotes.remove( l_maxDissAg.getBitVote() );
372 m_dissVoters.remove( l_maxDissAg );
373 l_group.remove( l_maxDissAg );
375 System.out.println(
"Removing " + l_maxDissAg.name() );
383 if ( l_group.
size() == 0 )
384 System.out.println(
" Voter list is empty, we are done " );
389 System.out.println(
" No dissatisfied voter left, we are done " );
395 for (
int i = 0; i < p_dissValues.size(); i++ )
397 if ( p_dissValues.get( i ) > p_dissValues.get( l_maxIndex ) )
399 System.out.println(
" changed max index to " + i +
" diss: " + p_dissValues.get( i ) );
436 final String p_fileName
446 CCommon.actionsFromPackage(),
449 CCommon.actionsFromAgentClass( CChairAgent.class ),
456 ).collect( Collectors.toSet() ),
482 l_chairAgent.sleep( Integer.MAX_VALUE );
Created by sophie on 10.01.17.
boolean readyForElection()
int getMaxIndex(final List< Double > p_dissValues)
final CEnvironment m_environment
environment
void startElection()
start election
Created by sophie on 22.02.17.
ILiteral detectGroup(final CChairAgent p_chairAgent)
detect group of chair agent
String m_grouping
grouping algorithm: "RANDOM" or "COORDINATED"
List< CVotingAgent > m_dissVoters
void storeVote(final String p_agentName, final BitVector p_vote)
store vote
Class CChairAgentGenerator.
CChairAgentGenerator(final InputStream p_stream, final CEnvironment p_environment, final String p_grouping, final String p_protocol, final String p_fileName)
constructor of the generator
List< BitVector > m_bitVotes
List< CVotingAgent > m_agents
List< Double > m_dissList
Created by sophie on 21.02.17.
final CChairAgent generatesingle(final Object...p_data)
generator method of the agent
void removeVoter()
remove most dissatisfied voter
final CEnvironment m_environment
environment
ILiteral updateIterative(final CChairAgent p_chairAgent, final BitVector p_result, final int p_iteration)
update group literal for chair agent ( for random grouping )
CChairAgent(final String p_name, final IAgentConfiguration< CChairAgent > p_configuration, final CEnvironment p_environment, final String p_grouping, final String p_protocol, final String p_fileName)
constructor of the agent
final AtomicLong m_agentcounter
Current free agent id, needs to be thread-safe, therefore using AtomicLong.
void storeDiss(final String p_name, final Double p_diss, final Integer p_iteration)
store dissatisfaction value
Created by sophie on 24.04.17.
final String m_name
name of chair
void checkConditions()
check conditions
void computeResult()
compute result of election
BitVector toBV(final AtomicIntegerArray p_vote)
BDI agent with voting capabilities.
ILiteral updateBasic(final CChairAgent p_chairAgent, final BitVector p_result)
update group literal for chair agent ( for random grouping )
void perceiveGroup()
perceive group