-
Notifications
You must be signed in to change notification settings - Fork 1
/
AgentBoard.java
433 lines (362 loc) · 12.8 KB
/
AgentBoard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package agents;
import behaviours.AssignCompanies;
import behaviours.AssignInvestors;
import behaviours.CreateRound;
import behaviours.EndNegotiation;
import behaviours.FindAgents;
import behaviours.OfferCompanies;
import behaviours.Print;
import behaviours.SendMessage;
import behaviours.StateMachine;
import helper.*;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import market.Company;
import market.CompanyFactory;
import market.InvestmentType;
import market.profits.Profits;
import market.profits.ProfitsFactory;
public class AgentBoard extends OurAgent {
private static final int COMPANY_NUMBER = 15;
private static final int NR_ROUNDS = 3;
// The catalogue of books for sale (maps the title of a book to its price)
private Map<InvestmentType, List<Company>> catalogue;
private Map<InvestmentType, Profits> profitsBoard;
private ConcurrentMap<AID, Integer> investors;
private ConcurrentMap<AID, Integer> managers;
private ConcurrentMap<AID, Map<InvestmentType,Integer>> investments;
private Round round;
private Integer currentShift;
private Integer currentRound;
private Random rand;
public Map<AID, Integer> getInvestors() {
return investors;
}
public List<AID> getListInvestors(){
return new ArrayList<>(this.investors.keySet());
}
public Map<AID, Integer> getManagers() {
return managers;
}
public List<AID> getListManagers(){
return new ArrayList<>(this.managers.keySet());
}
public Round getRound() {
return round;
}
public Shift getCurrentShift() {
return this.round.getShift(this.currentShift);
}
public void incCurrentShift() {
this.setCurrentShift(this.currentShift + 1);
}
public void incCurrentRound() {
this.setCurrentRound(this.currentRound + 1);
}
private void setCurrentShift(Integer currentShift) {
this.currentShift = currentShift;
}
private void setCurrentRound(Integer currentRound) {
this.currentRound = currentRound;
}
public Integer getCurrentRound() {
return currentRound;
}
public void resetCurrentShift() {
this.currentShift = 0;
}
public boolean isEndRound() {
return this.currentShift >= (this.round.getShifts().size() - 1);
}
public boolean isEndGame() {
return this.currentRound >= (NR_ROUNDS - 1);
}
private void initializeProfitsBoard(){
this.profitsBoard = ProfitsFactory.createAllProfits();
}
private Map<InvestmentType, Profits> getProfitsBoard() {
return profitsBoard;
}
public void setRound(Round round) {
this.round = round;
this.currentShift = 0;
}
// Put agent initializations here
protected void setup() {
// Create the catalogue
catalogue = generateCatalogue();
investors = new ConcurrentHashMap<>();
managers = new ConcurrentHashMap<>();
this.investments = new ConcurrentHashMap<>();
rand = new Random();
this.resetCurrentShift();
this.currentRound = 0;
this.initializeProfitsBoard();
// Register the book-selling service in the yellow pages
registerDFS();
stateMachine();
}
private void stateMachine() {
Behaviour findManagers = new FindAgents(AgentType.MANAGER, this);
Behaviour findInvestors = new FindAgents(AgentType.INVESTOR, this);
Behaviour createRound = new CreateRound(this);
Behaviour assignCompanies = new AssignCompanies(this);
Behaviour assignInvestors = new AssignInvestors(this);
Behaviour printEnd = new Print("MSG Received");
Behaviour endNegotiation = new EndNegotiation(this);
Behaviour sendRoundEnd = new SendMessage(this, State.ROUND_END);
Behaviour sendShiftEnd = new SendMessage(this, State.SHIFT_END);
Behaviour sendGameEnd = new SendMessage(this, State.GAME_END);
Behaviour offerCompanies = new OfferCompanies(this);
Transition t1 = new Transition(findManagers, findInvestors);
Transition t2 = new Transition(findInvestors, assignCompanies);
Transition t3_1 = new Transition(assignCompanies, createRound);
Transition t3_2 = new Transition(createRound, assignInvestors);
Transition t4 = new Transition(assignInvestors, endNegotiation);
Transition t5_1 = new Transition(endNegotiation, sendShiftEnd, 0);
Transition t5_2 = new Transition(endNegotiation, sendRoundEnd, 1);
Transition t5_3 = new Transition(endNegotiation, sendGameEnd, 2);
Transition t6_1 = new Transition(sendShiftEnd, assignInvestors);
Transition t6_11 = new Transition(sendRoundEnd, offerCompanies);
Transition t6_2 = new Transition(offerCompanies, createRound);
Transition t6_3 = new Transition(sendGameEnd, printEnd);
StateMachine sm = new StateMachine(this, findManagers, printEnd, t1, t2, t3_1, t3_2, t4, t5_1,
t5_2, t5_3, t6_1, t6_2, t6_3, t6_11);
addBehaviour(sm);
}
private void registerDFS() {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
sd.setType(String.valueOf(AgentType.BOARD));
sd.setName("JADE-Panic-Wall-Street");
dfd.addServices(sd);
try {
DFService.register(this, dfd);
} catch (FIPAException fe) {
fe.printStackTrace();
}
}
// Put agent clean-up operations here
protected void takeDown() {
// Deregister from the yellow pages
try {
DFService.deregister(this);
} catch (FIPAException fe) {
fe.printStackTrace();
}
// Close the GUI
// Printout a dismissal message
Logger.print(this.getLocalName(), "Seller-agent " + getAID().getName() + " terminating.");
}
@Override
public void registerAgent(AID[] agents, AgentType type) {
switch (type) {
case INVESTOR:
for(AID agent: agents){
this.investors.put(agent, 120);
initializeInvestment(agent);
}
break;
case MANAGER:
for(AID agent: agents){
this.managers.put(agent, 0);
}
break;
default:
Logger.print(this.getLocalName(), "Invalid agent type");
break;
}
}
private void initializeInvestment(AID agent) {
Map<InvestmentType,Integer> initialMap = new HashMap<>();
initialMap.put(InvestmentType.YELLOW, 0);
initialMap.put(InvestmentType.RED, 0);
initialMap.put(InvestmentType.BLUE, 0);
initialMap.put(InvestmentType.GREEN, 0);
this.investments.put(agent, initialMap);
}
@Override
public void sendMessage(State type) {
switch (type) {
case ROUND_END:
case SHIFT_END:
case GAME_END:
sendEndStateMsg(type);
break;
default:
break;
}
}
private void sendEndStateMsg(State state) {
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.setSender(getAID());
for (Map.Entry<AID, Integer> entry : this.managers.entrySet()) {
AID manager = entry.getKey();
msg.addReceiver(manager);
}
for (Map.Entry<AID, Integer> entry : this.investors.entrySet()) {
AID investor = entry.getKey();
msg.addReceiver(investor);
}
if(state.equals(State.ROUND_END)){
this.rollDices();
createRoundEndMsg(msg);
} else
if(state.equals(State.GAME_END)){
this.rollDices();
createGameEndMsg(msg);
} else
msg.setContent(state.toString());
msg.setConversationId(state.toString());
send(msg);
Logger.print(this.getLocalName(),
"send message: " + state.toString() + " -> " + msg.getContent());
}
private Map<InvestmentType, Integer> getInvestmentResults() {
Map<InvestmentType, Integer> results = new HashMap<>();
for (Map.Entry<InvestmentType, Profits> entry : this.getProfitsBoard().entrySet()) {
Profits profits = entry.getValue();
InvestmentType type = entry.getKey();
results.put(type, profits.getActualProfit());
}
return results;
}
private void createRoundEndMsg(ACLMessage msg) {
Map<InvestmentType, Integer> results = getInvestmentResults();
StateEndMsg stateEndMsg = new StateEndMsg(results,this.investors, this.managers);
try {
msg.setContentObject(stateEndMsg);
} catch (IOException e) {
e.printStackTrace();
}
}
private void createGameEndMsg(ACLMessage msg) {
Map<InvestmentType, Integer> results = getInvestmentResults();
Map<AID, Integer> winnerManagers = this.findWinnerManagers();
Map<AID, Integer> winnerInvestors = this.findWinnerInvestors();
StateEndMsg content = new StateEndMsg(results, this.investors, this.managers);
content.setWinnerInvestors(winnerInvestors);
content.setWinnerManagers(winnerManagers);
try {
msg.setContentObject(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public Map<InvestmentType, List<Company>> getCatalogue() {
return catalogue;
}
public void setCatalogue(Map<InvestmentType, List<Company>> catalogue) {
this.catalogue = catalogue;
}
public Company drawCompany(InvestmentType type) {
List<Company> existingCompanies = catalogue.get(type);
int index = rand.nextInt(existingCompanies.size());
Company company = existingCompanies.remove(index);
catalogue.put(type, existingCompanies);
return company;
}
private Map<InvestmentType, List<Company>> generateCatalogue() {
final int companyNumber = COMPANY_NUMBER * 4;
HashMap<InvestmentType, List<Company>> catalogue = new HashMap<>();
for (InvestmentType type : InvestmentType.values()) {
List<Company> list = new LinkedList<>();
for (int i = 0; i < companyNumber; i++) {
list.add(CompanyFactory.createCompany(type));
}
catalogue.put(type, list);
}
return catalogue;
}
private void rollDices(){
for (Map.Entry<InvestmentType, Profits> entry : this.profitsBoard.entrySet()) {
InvestmentType type = entry.getKey();
Profits profits = entry.getValue();
profits.roll_dice();
for(Map.Entry<AID, Map<InvestmentType, Integer>> e : this.investments.entrySet()){
AID agent = e.getKey();
Map<InvestmentType, Integer> value = e.getValue();
int nrCompanies = value.get(type);
Integer investorCapital = this.investors.get(agent) + profits.getActualProfit()*nrCompanies;
this.investors.put(agent, investorCapital);
}
}
Logger.print(this.getLocalName(), "ROLL DICES: " + this.profitsBoard);
}
public void handleEndNegotiationMsg(ACLMessage msg) {
AID manager = msg.getSender();
Integer managerCapital = this.managers.get(manager);
try {
Map<InvestmentType, List<Company>> wallet = (HashMap<InvestmentType, List<Company>>) msg.getContentObject();
for (Map.Entry<InvestmentType, List<Company>> entry : wallet.entrySet()) {
List<Company> companies = entry.getValue();
InvestmentType type = entry.getKey();
for(Company company: companies){
Integer price = company.getPrice();
AID owner = company.getCurrentOwner();
Boolean isDouble = company.isDoubleValue();
if(owner.compareTo(manager) != 0) {
Integer investorCapital = this.investors.get(owner) - price;
this.investors.put(owner, investorCapital);
this.incInvestment(owner,type, isDouble);
if(isDouble)
managerCapital+=price;
managerCapital += price;
}
}
}
this.managers.put(manager, managerCapital);
} catch (UnreadableException e) {
e.printStackTrace();
}
}
private void incInvestment(AID agent, InvestmentType type, Boolean isDouble) {
Map<InvestmentType, Integer> inv = this.investments.get(agent);
Integer nr = inv.get(type);
if(isDouble)
nr+=1;
inv.put(type, nr + 1);
this.investments.put(agent, inv);
}
private Map<AID,Integer> findWinnerAgents(Map<AID, Integer> agents){
Map<AID,Integer> result = new HashMap<>();
Map.Entry<AID, Integer> maxEntry = null;
for (Map.Entry<AID, Integer> entry : agents.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
maxEntry = entry;
}
for (Map.Entry<AID, Integer> entry : agents.entrySet())
{
if (entry.getValue().compareTo(maxEntry.getValue()) == 0)
result.put(entry.getKey(), entry.getValue());
}
return result;
}
private Map<AID,Integer> findWinnerInvestors(){
return this.findWinnerAgents(this.investors);
}
private Map<AID,Integer> findWinnerManagers(){
return this.findWinnerAgents(this.managers);
}
@Override
public int onEnd(State state) {
return 0;
}
@Override
public int handleMessage(ACLMessage msg) {
//TODO
return -1;
}
}