-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoman.cxx
949 lines (826 loc) · 29.6 KB
/
Roman.cxx
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#include <Roman.hxx>
#include <Statistics.hxx>
#include <World.hxx>
#include <Logger.hxx>
#include <ProvinceConfig.hxx>
#include <ControllerFactory.hxx>
namespace Epnet
{
Roman::Roman( const std::string & id, std::string controllerType ) : Agent(id), _resources(5), _maxActions(20), _nbTrades(0)
{
_controller = ControllerFactory::get().makeController(controllerType);
_controller->setAgent(this);
_score=0.0;
}
Roman::Roman( const std::string & id, std::string controllerType,double mutationRate,std::string selectionProcess, std::string innovationProcess) : Agent(id), _resources(5), _maxActions(20), _nbTrades(0)
{
_controller = ControllerFactory::get().makeController(controllerType,mutationRate,selectionProcess,innovationProcess);
_controller->setAgent(this);
_score=0.0;
_mutationRate=mutationRate;
}
Roman::~Roman()
{
}
void Roman::registerAttributes()
{
registerFloatAttribute("scores");
registerFloatAttribute("mu");
for( std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = listGoods.begin(); it != listGoods.end() ; it++)
{
// std::ostringstream oss;
// oss <<std::get<0>(*it) << "_q";
// std::string name=oss.str();
// registerFloatAttribute(name);
std::ostringstream ossb;
ossb <<std::get<0>(*it) << "_p";
std::string name=ossb.str();
registerFloatAttribute(name);
std::ostringstream ossc;
ossc <<std::get<0>(*it) << "_n";
name=ossc.str();
registerFloatAttribute(name);
/*std::ostringstream ossb;
oss <<std::get<0>(*it) << "_q";
registerFloatAttribute(ossb.str());
std::ostringstream ossc;
oss <<std::get<0>(*it) << "_n";
registerFloatAttribute(ossc.str());
*/
}
// registerIntAttribute("nbConnectionsRcv");
// registerIntAttribute("nbConnectionsSend");
// registerIntAttribute("nbAchievedTrades");
}
void Roman::serialize()
{
serializeAttribute("scores", (float)_score);
serializeAttribute("mu", (float)_mutationRate);
for( std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = listGoods.begin(); it != listGoods.end() ; it++)
{
/* std::ostringstream oss;
oss <<std::get<0>(*it) << "_q";
std::string name=oss.str();
float value =(float)getQuantity(std::get<0>(*it));
serializeAttribute(name,value); */
std::ostringstream ossb;
ossb <<std::get<0>(*it) << "_p";
std::string name=ossb.str();
float value =(float)getPrice(std::get<0>(*it));
serializeAttribute(name,value );
std::ostringstream ossc;
ossc <<std::get<0>(*it) << "_n";
name=ossc.str();
value =(float)getNeed(std::get<0>(*it));
serializeAttribute(name,value);
/* std::ostringstream ossb;
oss <<std::get<0>(*it) << "_q";
serializeAttribute(ossb.str(), (float)getQuantity(std::get<0>(*it)));
std::ostringstream ossc;
oss <<std::get<0>(*it) << "_n";
serializeAttribute(ossc.str(), (float)getNeed(std::get<0>(*it)));
*/
}
//serializeAttribute("nbConnectionsRcv", (int) validRcvConnections.size());
//serializeAttribute("nbConnectionsSend", (int) validSendConnections.size());
//serializeAttribute("nbAchievedTrades", _nbTrades);
}
void Roman::updateKnowledge()
{
}
void Roman::selectActions()
{
_actions = _controller->selectActions();
}
void Roman::updateState()
{
//the controller is responsible of the handling of connections and trades
//and the consumption of non-essential resources
_controller->updateState();
//Roman Agent is responsible of the consumption of essential resources
//and potenetial death
//As we don't have "essential" food yet there is no consumption of them nor death.
//consumeEssentialResources();
//checkDeath();
}
void Roman::consumeEssentialResources()
{
for( std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = listGoods.begin(); it != listGoods.end() ; it++)
{
//search for a good with need equal to 1.0
if (std::get<4>(*it) == 1.0)
{
//if found, consume one unit of it
removeGood(std::get<0>(*it),1);
}
}
}
void Roman::checkDeath()
{
bool death = false;
for( std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = listGoods.begin(); it != listGoods.end() ; it++)
{
//search for a good with need equal to 1.0
if (std::get<4>(*it) == 1.0)
{
//if the quantity is less or equal to 0, triger the death
if (std::get<1>(*it) <= 0.0)
{
death = true;
break;
}
}
}
//if death detected, perform all the death actions
if( death == true )
{
while(validSendConnections.size() > 0)
{
std::vector<std::string>::iterator it = validSendConnections.begin();
killTradesTo(*it);
killConnectionTo(*it);
}
//clean the agents in unfinished connections
while (receivedConnections.size() > 0)
{
std::vector<std::string>::iterator it = receivedConnections.begin();
killConnectionFrom(*it);
}
while (proposedConnections.size() > 0)
{
std::vector<std::string>::iterator it = proposedConnections.begin();
killConnectionTo(*it);
}
//clean the agents and possible trades in built connections
while(validRcvConnections.size() > 0)
{
std::vector<std::string>::iterator it = validRcvConnections.begin() ;
killConnectionFrom(*it);
}
remove();
}
}
void Roman::setResources( int resources )
{
_resources = resources;
}
int Roman::getResources() const
{
return _resources;
}
void Roman::resetNbTrades()
{
_nbTrades = 0;
}
int Roman::getMaxActions()
{
return _maxActions;
}
void Roman::increaseNbTrades(int value)
{
_nbTrades += value;
}
void Roman::proposeConnectionTo(std::string target)
{
//if do not ask to connect with self
if( target != _id )
{
//if the connection is not already among the valid one, ask for it
if( std::find(validSendConnections.begin(), validSendConnections.end(), target) == validSendConnections.end() )
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
//std::cout << "function proposeConnectionTo. unable to get requested agent. pass" << std::endl;
return;
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function proposeConnectionTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
targetPtr->requestConnectionFrom(_id);
//if the connection is not already in the proposed one, add it
if( std::find(proposedConnections.begin(), proposedConnections.end(), target) == proposedConnections.end() )
{
proposedConnections.push_back(target);
}
}
}
}
void Roman::killConnectionTo(std::string target)
{
bool wasPresent = false;
//remove traces from the proposedConnections
if( std::find(proposedConnections.begin(), proposedConnections.end(), target) != proposedConnections.end() )
{
proposedConnections.erase(std::remove(proposedConnections.begin(), proposedConnections.end(), target), proposedConnections.end());
wasPresent = true;
}
//remove trances from valid connections
if( std::find(validSendConnections.begin(), validSendConnections.end(), target) != validSendConnections.end() )
{
validSendConnections.erase(std::remove(validSendConnections.begin(), validSendConnections.end(), target), validSendConnections.end());
wasPresent = true;
}
//if the target was present there is a chance that we are in its lists
if (wasPresent == true)
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function killConnectionTo" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function killConnectionTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
targetPtr->killConnectionFrom(_id);
}
}
void Roman::killConnectionFrom(std::string source)
{
bool wasPresent = false;
//remove traces from the receivedConnections
if( std::find(receivedConnections.begin(), receivedConnections.end(), source) != receivedConnections.end() )
{
receivedConnections.erase(std::remove(receivedConnections.begin(), receivedConnections.end(), source), receivedConnections.end());
wasPresent = true;
}
//remove trances from valid connections
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) != validRcvConnections.end() )
{
validRcvConnections.erase(std::remove(validRcvConnections.begin(), validRcvConnections.end(), source), validRcvConnections.end());
wasPresent = true;
}
//if the source was present there is a chance that we are in its lists
if (wasPresent == true)
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function killConnectionFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function killConnectionFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
sourcePtr->killConnectionTo(_id);
}
}
void Roman::killConnections(std::string target)
{
killConnectionTo(target);
killConnectionFrom(target);
}
void Roman::requestConnectionFrom(std::string source)
{
//if the connection has not been received, put it among the received ones
if( std::find(receivedConnections.begin(), receivedConnections.end(), source) == receivedConnections.end() )
{
receivedConnections.push_back(source);
}
}
void Roman::acceptConnectionFrom(std::string source)
{
//if the source has not been already validated
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) == validRcvConnections.end() )
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function acceptConnectionFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function acceptConnectionFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
//if the source is effectively in the received connection
if( std::find(receivedConnections.begin(), receivedConnections.end(), source) != receivedConnections.end() )
{
receivedConnections.erase(std::remove(receivedConnections.begin(), receivedConnections.end(), source), receivedConnections.end());
//confirm the connection to source, and verify that source is aware of it
if (sourcePtr->ackConnectionFrom(_id))
{
validRcvConnections.push_back(source);
}
}
//if the source is not in the lit of received, something went wrong, kill things
else
{
sourcePtr->killConnectionTo(_id);
}
}
}
void Roman::refuseConnectionFrom(std::string source)
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function refuseConnectionFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function refuseConnectionFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
//refuse connection from source
sourcePtr->nackConnectionFrom(_id);
//remove source
receivedConnections.erase(std::remove(receivedConnections.begin(), receivedConnections.end(), source), receivedConnections.end());
}
void Roman::proposeConnectionBetween(std::string source, std::string target)
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function proposeConnectionBetween" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function proposeConnectionBetween" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
sourcePtr->proposeConnectionTo(target);
}
void Roman::killConnectionBetween(std::string source, std::string target)
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function killConnectionBetween" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function killConnectionBetween" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
sourcePtr->killConnectionTo(target);
}
int Roman::ackConnectionFrom(std::string target)
{
//if we had sent a connection we acknowledge it, otherwise we just return an error
if( std::find(proposedConnections.begin(), proposedConnections.end(), target) != proposedConnections.end() )
{
proposedConnections.erase(std::remove(proposedConnections.begin(), proposedConnections.end(), target), proposedConnections.end());
validSendConnections.push_back(target);
return 1;
}
else
{
return 0;
}
}
int Roman::nackConnectionFrom(std::string target)
{
//if we had sent a connection we kill it, otherwise we just return an error
if( std::find(proposedConnections.begin(), proposedConnections.end(), target) != proposedConnections.end() )
{
proposedConnections.erase(std::remove(proposedConnections.begin(), proposedConnections.end(), target), proposedConnections.end());
return 1;
}
else
{
return 0;
}
}
void Roman::receiveMessageFrom(std::string source, std::string msg)
{
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) != validRcvConnections.end() )
{
receivedMessages.push_back(std::make_tuple(source,msg));
}
}
void Roman::sendMessageTo(std::string target, std::string msg)
{
// if the connection with the target has been valideted
if( std::find(validSendConnections.begin(), validSendConnections.end(), target) != validSendConnections.end() )
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function sendMessageTo" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function sendMessageTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
targetPtr->receiveMessageFrom(_id, msg);
}
}
void Roman::addGoodType(std::string type,double max,double price,double need,double productionRate)
{
//check if a good of that type is already in that list
if ( std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;}) == listGoods.end() )
{
//if not, add it
listGoods.push_back(std::make_tuple(type,0,max,price,need,productionRate));
}
}
void Roman::removeGoodType(std::string type)
{
//check if a good of that type exist in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
while ( it != listGoods.end() )
{
//if not, add it
listGoods.erase(it);
//update the presence of good of that type in the list
it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
}
}
std::tuple<double,double,double,double,double> Roman::getGood(std::string type)
{
//check if a good of that type exist in the list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
if ( it != listGoods.end() )
{
//return quantity, maxquantity,price, need and productionRate good
return std::make_tuple(std::get<1>(*it),std::get<2>(*it),std::get<3>(*it),std::get<4>(*it),std::get<5>(*it));
}
//return something impossible as an error
return std::make_tuple(-1.0,-1.0,-1.0,-1.0,-1.0);
}
//TODO: actually this function return the good that is produced. But should return a vector of goods if there are more than one good produced
std::tuple<std::string,double,double,double,double,double> Roman::getProducedGood()
{
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<5>(good) > 0 ;});
return *it;
}
void Roman::addGood(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes add the value to it within the bound
if ( it != listGoods.end() )
{
double toSet = std::min(std::get<1>(*it) + value, std::get<2>(*it));
std::get<1>(*it) = toSet;
}
}
void Roman::setPrice(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes add the value to it within the bound
if ( it != listGoods.end() )
{
std::get<3>(*it) = value;
}
}
void Roman::setQuantity(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes add the value to it within the bound
if ( it != listGoods.end() )
{
std::get<1>(*it) = value;
}
}
void Roman::setNeed(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes add the value to it within the bound
if ( it != listGoods.end() )
{
std::get<4>(*it) = value;
}
}
void Roman::setProductionRate(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes add the value to it within the bound
if ( it != listGoods.end() )
{
std::get<5>(*it) = value;
}
}
void Roman::removeGood(std::string type,double value)
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes remove the value to it as long as it's superior to 0
if ( it != listGoods.end() )
{
double toSet = std::max(std::get<1>(*it) - value, 0.0);
std::get<1>(*it) = toSet;
}
}
std::vector<std::tuple<std::string,double,double,double,double,double> > Roman::getListGoodsFrom(std::string target)
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function getListGoodsFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function getListGoodsFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
return targetPtr->getListGoods();
}
void Roman::printInventory()
{
std::cout<<"-------------------------------"<<std::endl;
std::cout<<"Inventory of :"<<_id<<" with score "<<_score<<std::endl;
for(std::vector< std::tuple< std::string, double, double, double, double, double > >::iterator it = this->listGoods.begin() ; it != this->listGoods.end() ; it++)
{
std::cout<<std::get<0>(*it)<<"\t"<<std::get<1>(*it)<<"\t"<<std::get<2>(*it)<<"\t"<<std::get<3>(*it)<<"\t"<<std::get<4>(*it)<<"\t"<<std::get<5>(*it)<<std::endl;
}
std::cout<<"-------------------------------"<<std::endl;
}
void Roman::sendGoodTo(std::string target, std::string type, double value)
{
// if the connection with the target has been validated
if( std::find(validSendConnections.begin(), validSendConnections.end(), target) != validSendConnections.end() )
{
//check if a good of that type exists in that list
std::vector<std::tuple<std::string,double,double,double,double,double> >::iterator it = std::find_if(listGoods.begin(), listGoods.end(), [=](const std::tuple<std::string,double,double,double,double,double>& good) {return std::get<0>(good) == type;});
//if yes remove the value to it as long as it's superior to 0
if ( it != listGoods.end() )
{
if ( std::get<1>(*it) >= value)
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function sendGoodTo" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function sendGoodTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
if(targetPtr->receiveGoodFrom(_id, type, value))
{
removeGood(type,value);
}
}
}
}
}
int Roman::receiveGoodFrom(std::string source, std::string type, double value)
{
//if in the list of validated senders receive the good
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) != validRcvConnections.end() )
{
//TODO modify add good to add a return value. return 0 if was not able to add the good (good type does not exist)
addGood(type,value);
return 1;
}
//if arrive to that point, the agent was not a valid sender and we return an error
return 0;
}
int Roman::receiveTradeFrom(std::string source, std::string type, double value, double currency)
{
auto it = listGoods.begin();
while( it != listGoods.end() )
{
if (std::get<0>(*it) == type) break;
it++;
}
//if arrived at the end of the list of goods without finding the specific good, quit the function now
if (it == listGoods.end()) return 0;
//if in the list of validated senders receive the trade offer
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) != validRcvConnections.end() )
{
listReceivedTrades.push_back(make_tuple(source,type,value,currency));
return 1;
}
//if arrive to that point, the agent was not a valid sender and we return an error
return 0;
}
void Roman::proposeTradeTo(std::string target, std::string type, double valueGood, double valueCurrency)
{
auto it = listGoods.begin();
while( it != listGoods.end() )
{
if (std::get<0>(*it) == type) break;
it++;
}
//if arrived at the end of the list of goods without finding the specific good, quit the function now
if (it == listGoods.end()) return;
// if the connection with the target has been validated
if( std::find(validSendConnections.begin(), validSendConnections.end(), target) != validSendConnections.end() )
{
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function proposeTradeTo" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function proposeTradeTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
//if the offer is effectively received, add it
if (targetPtr->receiveTradeFrom(_id,type,valueGood,valueCurrency))
{
listProposedTrades.push_back(make_tuple(target,type,valueGood,valueCurrency));
}
}
}
void Roman::acceptTradeFrom(std::string source, std::string type, double valueGood, double valueCurrency)
{
if( std::find(validRcvConnections.begin(), validRcvConnections.end(), source) != validRcvConnections.end() )
{
for (auto it = listReceivedTrades.begin() ; it != listReceivedTrades.end() ; it ++)
{
if ((*it) == std::make_tuple(source,type,valueGood,valueCurrency))
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function acceptTradeFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function acceptTradeFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
if( (std::get<0>(getGood("currency")) >= valueCurrency) && (std::get<0>(sourcePtr->getGood(type)) >= valueGood) )
{
sourcePtr->sendGoodTo(_id,type,valueGood);
sourcePtr->addGood("currency", valueCurrency);
removeGood("currency",valueCurrency);
}
sourcePtr->removeProposedTrade(_id,type,valueGood,valueCurrency);
removeReceivedTrade(source,type,valueGood,valueCurrency);
break;
}
}
}
}
void Roman::refuseTradeFrom(std::string source, std::string type, double valueGood, double valueCurrency)
{
Agent* uncastedPtr = _world->getAgent(source);
if (uncastedPtr == NULL)
{
std::cout << "function refuseTradeFrom" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* sourcePtr = dynamic_cast<Roman*> (uncastedPtr);
if (sourcePtr == NULL)
{
std::cout << "function refuseTradeFrom" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
sourcePtr->removeProposedTrade(_id,type,valueGood,valueCurrency);
removeReceivedTrade(source,type,valueGood,valueCurrency);
}
std::vector<std::tuple<std::string,double,double> > Roman::getReceivedTradesFrom(std::string source)
{
std::vector<std::tuple<std::string,double,double> > tmp;
for (auto it = listReceivedTrades.begin() ; it != listReceivedTrades.end() ; it ++)
{
if (std::get<0>(*it) == source)
{
tmp.push_back(std::make_tuple(std::get<1>(*it),std::get<2>(*it),std::get<3>(*it)));
}
}
return tmp;
}
std::vector<std::tuple<std::string,double,double> > Roman::getProposedTradesTo(std::string target)
{
std::vector<std::tuple<std::string,double,double> > tmp;
for (auto it = listProposedTrades.begin() ; it != listProposedTrades.end() ; it ++)
{
if (std::get<0>(*it) == target)
{
tmp.push_back(std::make_tuple(std::get<1>(*it),std::get<2>(*it),std::get<3>(*it)));
}
}
return tmp;
}
void Roman::removeReceivedTrade(std::string source, std::string type, double value, double currency)
{
for (auto it = listReceivedTrades.begin() ; it != listReceivedTrades.end() ;)
{
if ((*it) == std::make_tuple(source,type,value,currency))
{
listReceivedTrades.erase(it);
break;
}
else
{
++it;
}
}
}
void Roman::removeProposedTrade(std::string source, std::string type, double value, double currency)
{
for (auto it = listProposedTrades.begin() ; it != listReceivedTrades.end() ;)
{
if ((*it) == std::make_tuple(source,type,value,currency))
{
listProposedTrades.erase(it);
break;
}
else
{
++it;
}
}
}
void Roman::killTradesFrom(std::string source)
{
//remove traces from the receivedTrades
for(auto it = listReceivedTrades.begin(); it != listReceivedTrades.end();)
{
if(std::get<0>(*it) == source)
{
it = listReceivedTrades.erase(it);
}
else
{
++it;
}
}
}
void Roman::killTradesTo(std::string target)
{
//remove traces from the proposedTrades
for(auto it = listProposedTrades.begin(); it != listProposedTrades.end();)
{
if(std::get<0>(*it) == target)
{
it = listProposedTrades.erase(it);
}
else
{
++it;
}
}
Agent* uncastedPtr = _world->getAgent(target);
if (uncastedPtr == NULL)
{
std::cout << "function killTradesTo" << std::endl;
std::cout << "unable to get requested agent. clean the connection" << std::endl;
exit(1);
}
Roman* targetPtr = dynamic_cast<Roman*> (uncastedPtr);
if (targetPtr == NULL)
{
std::cout << "function killTradesTo" << std::endl;
std::cout << "dynamic_cast from Agent* to Roman* fail" << std::endl;
exit(1);
}
targetPtr->killTradesFrom(_id);
}
} // namespace Roman