21
21
from sklearn .neighbors import NearestNeighbors
22
22
import networkx as nx
23
23
from pypdevs .infinity import INFINITY
24
+ import math
24
25
25
26
# Import code for DEVS model representation:
26
27
from pypdevs .DEVS import *
@@ -39,7 +40,7 @@ class Parameters:
39
40
TOPOLOGY_FILE = ""
40
41
INIT_RESOURCES = 10
41
42
MAX_AG = 20
42
- RV = 4
43
+ RV = 2
43
44
TRIALS = 10
44
45
MUTATION_RATE = 0.05
45
46
@@ -58,6 +59,7 @@ class Enum(object): pass
58
59
GIVERS_GREATER_THAN = 'Givers' ,
59
60
SORTED_BY_GIVER = 'If no Givers greater than, then second best' ,
60
61
GIVERS_MEAN = 'Givers Mean values' ,
62
+ GIVERS_GENE_MEAN = 'Givers Mean values' ,
61
63
TOL_MEAN = 'Tolerance mean values' ,
62
64
GIVERS_SD = 'Givers SD values' ,
63
65
TOTAL_CRED_MEAN = 'Total credits mean' ,
@@ -98,7 +100,7 @@ def __init__(self):
98
100
self .stats = []
99
101
self .name = 'logAgent'
100
102
self .ta = 1
101
- self .state = "LogAgent "
103
+ self .state = "_LogAgent "
102
104
self .current_time = 0
103
105
self .elapsed = 0
104
106
self .my_input = {}
@@ -108,9 +110,10 @@ def __lt__(self, other):
108
110
109
111
def saveLoginfo (self ):
110
112
givers_mean = self .parent .getContextInformation (ENVProps .GIVERS_MEAN )
113
+ givers_gene_mean = self .parent .getContextInformation (ENVProps .GIVERS_GENE_MEAN )
111
114
tolerance_mean = self .parent .getContextInformation (ENVProps .TOL_MEAN )
112
115
Nagents = self .parent .getContextInformation (ENVProps .NAGENTS )
113
- stats = (self .current_time , givers_mean , tolerance_mean )
116
+ stats = (self .current_time , givers_gene_mean , tolerance_mean )
114
117
self .stats .append (stats )
115
118
116
119
def intTransition (self ):
@@ -173,7 +176,7 @@ def get_neighbor(self):
173
176
ENVProps .SORTED_BY_GIVER ,
174
177
avoid_self = self .state .id ))
175
178
if sorted_neighbors :
176
- neighbor_to_give = sorted_neighbors [ 0 ]
179
+ neighbor_to_give = np . random . choice ( sorted_neighbors )
177
180
outport = self .out_ports_dict .get (neighbor_to_give )
178
181
return outport
179
182
@@ -290,6 +293,9 @@ def create_topology(self):
290
293
self .tolerance = {v .state .id : v .state .tg #Parameters.INIT_RESOURCES \
291
294
for k , v in self .agents .items () if k != - 1 }
292
295
296
+ self .giving_gene = {v .state .id : v .state .gg #Parameters.INIT_RESOURCES \
297
+ for k , v in self .agents .items () if k != - 1 }
298
+
293
299
294
300
def modelTransition (self , state ):
295
301
# Create the new generations, destroy the old one
@@ -304,12 +310,25 @@ def modelTransition(self, state):
304
310
elem [1 ] >= g_mean + 1 * (g_sd ),
305
311
self .total_credits .items ()))
306
312
307
- to_eliminate = dict (filter (lambda elem : \
308
- elem [1 ] < g_mean - 1 * (g_sd ),
309
- self .total_credits .items ()))
313
+ # to_eliminate = dict(filter(lambda elem: \
314
+ # elem[1] <= (g_mean - 1 * (g_sd) + 1),
315
+ # self.total_credits.items()))
316
+
317
+ if not to_duplicate :
318
+ __import__ ('ipdb' ).set_trace ()
319
+
320
+
321
+
322
+ all_agents = [el [0 ] for el in sorted (self .total_credits .items (),
323
+ key = lambda x : x [1 ], reverse = True )]
324
+
325
+ # to_duplicate = all_agents[0: max(1, len(all_agents) / 4)]
326
+ to_eliminate = all_agents [- len (to_duplicate ):]
327
+ if not to_eliminate :
328
+ __import__ ('ipdb' ).set_trace ()
310
329
311
330
# Remove the ones to eliminate
312
- for model_id in to_eliminate . keys () :
331
+ for model_id in to_eliminate :
313
332
self .removeSubModel (self .agents [model_id ])
314
333
del self .agents [model_id ]
315
334
# del self.given_credits[model_id]
@@ -336,6 +355,9 @@ def modelTransition(self, state):
336
355
self .tolerance = {v .state .id : v .state .tg #Parameters.INIT_RESOURCES \
337
356
for k , v in self .agents .items () if k != - 1 }
338
357
358
+ self .giving_gene = {v .state .id : v .state .gg #Parameters.INIT_RESOURCES \
359
+ for k , v in self .agents .items () if k != - 1 }
360
+
339
361
self .given_updates = {ag .state .id : 0 \
340
362
for ag in self .agents .values ()}
341
363
@@ -378,8 +400,11 @@ def getContextInformation(self, property, *args, **kwargs):
378
400
379
401
if property == ENVProps .SORTED_BY_GIVER :
380
402
avoid_self = kwargs ['avoid_self' ]
381
- return [el [0 ] for el in sorted (self .given_credits .items (),
382
- key = lambda x : x [1 ], reverse = True ) if el [0 ] != avoid_self ]
403
+ max_given = max (self .given_credits .values ())
404
+ return [k for k , v in self .given_credits .items () if v == max_given ]
405
+
406
+ # return [el[0] for el in sorted(self.given_credits.items(),
407
+ # key= lambda x: x[1], reverse=True) if el[0] != avoid_self]
383
408
384
409
if property == ENVProps .GIVERS_MEAN :
385
410
given = np .array (list (self .given_credits .values ()))
@@ -391,6 +416,9 @@ def getContextInformation(self, property, *args, **kwargs):
391
416
if property == ENVProps .TOL_MEAN :
392
417
return np .array (list (self .tolerance .values ())).mean ()
393
418
419
+ if property == ENVProps .GIVERS_GENE_MEAN :
420
+ return np .array (list (self .giving_gene .values ())).mean ()
421
+
394
422
if property == ENVProps .GIVERS_SD :
395
423
return np .array (list (self .given_credits .values ())).std ()
396
424
0 commit comments