xFS-bots 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. xFS_bots/graph_models/__init__.py +0 -0
  2. xFS_bots/graph_models/_mg_sample.py +331 -0
  3. xFS_bots/graph_models/action_table.py +395 -0
  4. xFS_bots/graph_models/agent_database.py +77 -0
  5. xFS_bots/graph_models/analog_graph.py +130 -0
  6. xFS_bots/graph_models/analog_schemes.py +320 -0
  7. xFS_bots/graph_models/analog_schemes_aux.py +294 -0
  8. xFS_bots/graph_models/base_node.py +189 -0
  9. xFS_bots/graph_models/bfs.py +69 -0
  10. xFS_bots/graph_models/community.py +389 -0
  11. xFS_bots/graph_models/controverter.py +274 -0
  12. xFS_bots/graph_models/cyclical_navigator.py +241 -0
  13. xFS_bots/graph_models/dependency_seq.py +61 -0
  14. xFS_bots/graph_models/dfs.py +78 -0
  15. xFS_bots/graph_models/dir_imp_path.py +188 -0
  16. xFS_bots/graph_models/eb_graph_navigator.py +326 -0
  17. xFS_bots/graph_models/expr_tree.py +907 -0
  18. xFS_bots/graph_models/game_table.py +129 -0
  19. xFS_bots/graph_models/gcs.py +323 -0
  20. xFS_bots/graph_models/graph_gen.py +462 -0
  21. xFS_bots/graph_models/hg_obj_path.py +421 -0
  22. xFS_bots/graph_models/hg_obj_path_op.py +512 -0
  23. xFS_bots/graph_models/hypergraph.py +221 -0
  24. xFS_bots/graph_models/jamming_graph.py +399 -0
  25. xFS_bots/graph_models/lattice_graph_gen.py +402 -0
  26. xFS_bots/graph_models/micrograph.py +414 -0
  27. xFS_bots/graph_models/modular_graph.py +452 -0
  28. xFS_bots/graph_models/node_navigator_handler.py +31 -0
  29. xFS_bots/graph_models/node_path.py +345 -0
  30. xFS_bots/graph_models/node_priority_function.py +97 -0
  31. xFS_bots/graph_models/node_to_cycle.py +85 -0
  32. xFS_bots/graph_models/path_induction.py +260 -0
  33. xFS_bots/graph_models/pswap_graph.py +440 -0
  34. xFS_bots/graph_models/radial_subgraph.py +106 -0
  35. xFS_bots/graph_models/reactive_simple_graph_rules.py +210 -0
  36. xFS_bots/graph_models/shortest_paths.py +429 -0
  37. xFS_bots/graph_models/shortest_paths_approx.py +461 -0
  38. xFS_bots/graph_models/shortest_paths_type_st.py +119 -0
  39. xFS_bots/graph_models/spanning_tree.py +146 -0
  40. xFS_bots/graph_models/sparse_graph_gen.py +66 -0
  41. xFS_bots/graph_models/tree_gen.py +150 -0
  42. xFS_bots/graph_problems/__init__.py +0 -0
  43. xFS_bots/graph_problems/bkb.py +43 -0
  44. xFS_bots/graph_problems/bkb_ext.py +55 -0
  45. xFS_bots/graph_problems/cb.py +73 -0
  46. xFS_bots/graph_problems/cb_ext.py +61 -0
  47. xFS_bots/graph_problems/drb.py +67 -0
  48. xFS_bots/graph_problems/drb_ext.py +27 -0
  49. xFS_bots/graph_problems/easy_gen.py +429 -0
  50. xFS_bots/graph_problems/easy_gen_default_vars.py +104 -0
  51. xFS_bots/graph_problems/efb.py +145 -0
  52. xFS_bots/graph_problems/efb_ext.py +92 -0
  53. xFS_bots/graph_problems/hfb.py +106 -0
  54. xFS_bots/graph_problems/hfb_ext.py +31 -0
  55. xFS_bots/graph_problems/hte.py +319 -0
  56. xFS_bots/graph_problems/hte_ext.py +165 -0
  57. xFS_bots/graph_problems/ib.py +86 -0
  58. xFS_bots/graph_problems/ib_ext.py +65 -0
  59. xFS_bots/graph_problems/mkb.py +76 -0
  60. xFS_bots/graph_problems/mkb_ext.py +45 -0
  61. xFS_bots/graph_problems/mmb.py +126 -0
  62. xFS_bots/graph_problems/mmb_ext.py +34 -0
  63. xFS_bots/graph_problems/pdib.py +41 -0
  64. xFS_bots/graph_problems/pdib_ext.py +30 -0
  65. xFS_bots/graph_problems/pib.py +138 -0
  66. xFS_bots/graph_problems/pib_ext.py +35 -0
  67. xFS_bots/graph_problems/ptb.py +137 -0
  68. xFS_bots/graph_problems/ptb_ext.py +42 -0
  69. xFS_bots/graph_problems/rnb.py +228 -0
  70. xFS_bots/graph_problems/rnb_ext.py +90 -0
  71. xFS_bots/graph_problems/sb.py +79 -0
  72. xFS_bots/graph_problems/sb_ext.py +67 -0
  73. xFS_bots/graph_problems/sim_solution_search.py +92 -0
  74. xFS_bots/graph_problems/snb.py +115 -0
  75. xFS_bots/graph_problems/snb_ext.py +76 -0
  76. xFS_bots/graph_problems/tftmb.py +54 -0
  77. xFS_bots/graph_problems/tftmb_ext.py +56 -0
  78. xFS_bots/graph_problems/tsb.py +42 -0
  79. xFS_bots/graph_problems/tsb_ext.py +36 -0
  80. xFS_bots/graph_problems/vtb.py +13 -0
  81. xFS_bots/graph_problems/vtb_ext.py +44 -0
  82. xFS_bots/quant/__init__.py +0 -0
  83. xFS_bots/quant/agent_23.py +750 -0
  84. xFS_bots/quant/bool_rules.py +50 -0
  85. xFS_bots/quant/bull_graph.py +434 -0
  86. xFS_bots/quant/ce_agent.py +294 -0
  87. xFS_bots/quant/ce_net.py +228 -0
  88. xFS_bots/quant/cee_map.py +247 -0
  89. xFS_bots/quant/cnf_mask.py +182 -0
  90. xFS_bots/quant/cng.py +243 -0
  91. xFS_bots/quant/controverter_graph.py +247 -0
  92. xFS_bots/quant/default_graph_introproc.py +160 -0
  93. xFS_bots/quant/dual_obj.py +337 -0
  94. xFS_bots/quant/gl_expr_table.py +34 -0
  95. xFS_bots/quant/graph_introspector.py +125 -0
  96. xFS_bots/quant/gt_agent.py +94 -0
  97. xFS_bots/quant/hmm_agent.py +382 -0
  98. xFS_bots/quant/hscript_graph.py +501 -0
  99. xFS_bots/quant/hte_analog_induction.py +81 -0
  100. xFS_bots/quant/hte_aux.py +321 -0
  101. xFS_bots/quant/hte_navigator.py +226 -0
  102. xFS_bots/quant/levenschtein.py +89 -0
  103. xFS_bots/quant/middle_agent.py +221 -0
  104. xFS_bots/quant/middleman_graph.py +346 -0
  105. xFS_bots/quant/mob_graph.py +334 -0
  106. xFS_bots/quant/mutable_ce_net.py +46 -0
  107. xFS_bots/quant/mvec_agent.py +462 -0
  108. xFS_bots/quant/pd_graph.py +318 -0
  109. xFS_bots/quant/pd_inadvertency.py +266 -0
  110. xFS_bots/quant/poison_snt.py +277 -0
  111. xFS_bots/quant/pt_aux.py +346 -0
  112. xFS_bots/quant/qstruct.py +671 -0
  113. xFS_bots/quant/qstruct_aux.py +195 -0
  114. xFS_bots/quant/reactive_graph_introspector.py +103 -0
  115. xFS_bots/quant/rstruct.py +256 -0
  116. xFS_bots/quant/selection_rules.py +227 -0
  117. xFS_bots/quant/simple_hmm_env.py +141 -0
  118. xFS_bots/quant/square_nt.py +94 -0
  119. xFS_bots/quant/static_graph_introspector.py +129 -0
  120. xFS_bots/quant/strangle_form.py +567 -0
  121. xFS_bots/quant/strangle_graph.py +160 -0
  122. xFS_bots/quant/ul_vec_classifier.py +58 -0
  123. xFS_bots/quant/usg_controller.py +84 -0
  124. xFS_bots/quant/vt_graph.py +96 -0
  125. xfs_bots-0.0.1.dist-info/METADATA +208 -0
  126. xfs_bots-0.0.1.dist-info/RECORD +129 -0
  127. xfs_bots-0.0.1.dist-info/WHEEL +5 -0
  128. xfs_bots-0.0.1.dist-info/licenses/LICENSE.txt +19 -0
  129. xfs_bots-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,318 @@
1
+ from ..graph_models.micrograph import *
2
+ from ..graph_models.tree_gen import *
3
+ from ..graph_models.analog_schemes_aux import connect_subgraphs__prior_to_current
4
+ from ..graph_models.shortest_paths import *
5
+ from .pt_aux import *
6
+ from morebs2.graph_basics import *
7
+
8
+ """
9
+ for a forest generated by a <TreeGen> instance, connects
10
+ all the forest components. Uses measure
11
+ (ratio of prior subgraph nodes, ratio of possible edges to current subgraph)
12
+ """
13
+ def connect_TreeGen_forest(T:TreeGen,prg,sg2sg_conn_ratios=(0.15,0.1)):
14
+ gd = GraphComponentDecomposition(T.d)
15
+ gd.decompose()
16
+
17
+ M = MicroGraph(T.d)
18
+
19
+ if len(gd.components) < 2:
20
+ return M.dg
21
+
22
+ c0,c1 = gd.components[0],gd.components[1]
23
+ M0,M1 = M.subgraph_by_nodeset_(c0),M.subgraph_by_nodeset_(c1)
24
+
25
+ MX = []
26
+ for i in range(len(gd.components) - 1):
27
+ c0,c1 = gd.components[i],gd.components[i+1]
28
+ M0,M1 = M.subgraph_by_nodeset_(c0),M.subgraph_by_nodeset_(c1)
29
+
30
+ G = connect_subgraphs__prior_to_current(M0.dg,M1.dg,\
31
+ is_dsg=False,sg2sg_conn_ratios=sg2sg_conn_ratios,prg=prg)
32
+ MX.append(MicroGraph(G))
33
+
34
+ mg = MX.pop(0)
35
+ while len(MX) > 0:
36
+ mg = mg + MX.pop(0)
37
+ return mg.dg
38
+
39
+ def fetch_source2target_paths(G,sources,targets,prg):
40
+
41
+ st_path_map = dict()
42
+ for source in sources:
43
+ bdfs2 = BDFSCache(source,G,is_bfs=False,prg=prg,\
44
+ edge_cost_function=lambda u,v:1,num_paths_per_node=1,max_search_radius=float('inf'),\
45
+ verbose=False)
46
+
47
+ while bdfs2.move_one(): continue
48
+
49
+ st_path_map[source] = dict()
50
+ for t in targets:
51
+ q = bdfs2.min_paths[t][0]
52
+ st_path_map[source][t] = q
53
+ return st_path_map
54
+
55
+ def generate_m_square_integer_matrixes(m,n,prg,ranged_modulo):
56
+ prg_ = prg__single_to_nvec(prg,n)
57
+
58
+ def one_square_matrix():
59
+ x = np.zeros((n,n))
60
+ for i in range(n):
61
+ x[i] = modulo_in_range(prg_(),ranged_modulo)
62
+ return x
63
+
64
+ matrixes = []
65
+ for _ in range(m):
66
+ matrixes.append(one_square_matrix())
67
+ return matrixes
68
+
69
+ def max_number_of_relays_per_target(number_of_sources,relays_per_source=2):
70
+ s = ceil(number_of_sources)
71
+ return s * relays_per_source
72
+
73
+ """
74
+ data structure used for simulation Poison Trace Bot.
75
+ """
76
+ class PoisonDeliveryNetwork:
77
+
78
+ def __init__(self,G:defaultdict,source_map,target_map,verbose:bool=False):
79
+ assert type(verbose) == bool
80
+ self.G = G
81
+ self.source_map = source_map
82
+ self.target_map = target_map
83
+ self.verbose = verbose
84
+ self.relay_map = defaultdict(list)
85
+ self.pending_poisons = dict()
86
+ self.set_verbosity()
87
+ return
88
+
89
+ def set_verbosity(self):
90
+ for t in self.target_map.values():
91
+ t.verbose = self.verbose
92
+
93
+ def set_target_prngs(self,prng_map):
94
+ for k,v in prng_map.items():
95
+ assert k in self.target_map
96
+ self.target_map[k].set_prng(v)
97
+ return
98
+
99
+ def target_performances(self,display:bool=True):
100
+ d = {}
101
+ x = sorted(self.target_map.keys())
102
+ for x_ in x:
103
+ q = self.target_map[x_]
104
+
105
+ x = self.target_perf(x_)
106
+
107
+ if display:
108
+ print("idn: ",x_)
109
+ print("number of terminations: ",x[0])
110
+ print("number of guesses: ",x[1])
111
+ print("number of poisonings: ",x[2])
112
+ print()
113
+ d[x_] = x
114
+ return d
115
+
116
+ def target_perf(self,t_idn):
117
+ q = self.target_map[t_idn]
118
+ return (q.target_idn,q.guess_count,q.poison_count)
119
+
120
+ def __next__(self):
121
+ self.move_sources()
122
+ self.move_targets()
123
+
124
+ def move_sources(self):
125
+ self.pending_poisons.clear()
126
+ sources = sorted(self.source_map.keys())
127
+ for s in sources:
128
+ self.move_source(s)
129
+ return
130
+
131
+ def move_source(self,source_idn):
132
+ q = self.source_map[source_idn]
133
+
134
+ if type(q.active_poison_action) == type(None):
135
+ q.send_poison(self.occupied_targets())
136
+
137
+ x,mode = next(q)
138
+
139
+ # case: poison has finished, terminate target
140
+ if q.active_poison_action.fin_stat:
141
+ t_ = q.active_poison_action.path_target()
142
+ t = self.target_map[t_]
143
+ self.target_map[t_].terminated = True
144
+
145
+ if type(mode) == type(None): return
146
+
147
+ self.process_source_move(source_idn,x,mode)
148
+ return
149
+
150
+ def process_source_move(self,source_idn,x,mode):
151
+ pp = self.source_map[source_idn].active_poison_action
152
+
153
+ if self.verbose:
154
+ print("source: ",source_idn)
155
+ print("mode: ",mode)
156
+ print("info:\n",x)
157
+ print(pp)
158
+ print("----------------")
159
+
160
+ if mode == "send":
161
+ possible_candidates = set(self.source_map.keys())
162
+ for t in self.target_map.values():
163
+ t.relay_register(pp,possible_candidates)
164
+ return
165
+ # case: pending receive for target
166
+ t = pp.path_target()
167
+ assert t not in self.pending_poisons
168
+ self.pending_poisons[t] = (x,pp.poison_type)
169
+ return
170
+
171
+ def move_targets(self):
172
+ targets = sorted(self.target_map.keys())
173
+
174
+ for t in targets:
175
+ self.move_target(t)
176
+ return
177
+
178
+ def move_target(self,target_idn):
179
+ t = self.target_map[target_idn]
180
+ pstat = t.is_poisoned()
181
+
182
+ if self.verbose and pstat:
183
+ print("target {}, terminated? {}".format(target_idn,t.terminated))
184
+ print("target DB size: {}".format(len(t.poison_db)))
185
+ finished_target = next(t)
186
+
187
+ if self.verbose and pstat:
188
+ print("is poisoned: {}".format(pstat))
189
+
190
+ if finished_target:
191
+ if self.verbose:
192
+ print("reproducing terminated agent {}".format(target_idn))
193
+
194
+ s = self.poison_source_of_target(target_idn)
195
+ s.clear_poison_action()
196
+ self.target_map[target_idn] = t.reproduce()
197
+ if self.verbose: print("--------------------------------------")
198
+ return
199
+
200
+ # case: target has been poisoned
201
+ if pstat and target_idn in self.pending_poisons:
202
+ pred = t.predict_poison()
203
+ x = self.pending_poisons[target_idn][0]
204
+
205
+ if self.verbose:
206
+ print("-- reaction {}".format(len(t.poison_reaction_log)))
207
+ print("actual: ",x)
208
+ print("predicted: ",pred)
209
+
210
+ # compare prediction with actual
211
+ # subcase: correct prediction, halt poison
212
+ if type(pred) != type(None):
213
+ if equal_iterables(x,pred,5):
214
+ if self.verbose: print("-- halting poison.")
215
+ self.halt_poison(target_idn)
216
+ # subcase: start backtracking if no guesses
217
+ else:
218
+ if type(t.backtracker) == type(None):
219
+ if self.verbose: print("-- starting backtracker")
220
+ s = self.poison_source_of_target(target_idn)
221
+ assert type(s) != type(None)
222
+ pp = s.active_poison_action
223
+ source_info = deepcopy(s.poison_prng_map[pp.poison_idn()])
224
+ t.start_backtrack(pp,source_info)
225
+
226
+
227
+ if target_idn in self.pending_poisons:
228
+ x = self.pending_poisons[target_idn]
229
+ if len(t.poison_reaction_log) == 0:
230
+ x2 = x[0]
231
+ else:
232
+ x2 = None if x[1] == "inexpressive" else x[0]
233
+
234
+ t.register_poison_reaction(x2)
235
+ del self.pending_poisons[target_idn]
236
+
237
+ if self.verbose and pstat: print("--------------------------------------")
238
+
239
+ ##
240
+ return
241
+
242
+ def occupied_targets(self):
243
+ targets = set()
244
+ for s in self.source_map.values():
245
+ if type(s.active_poison_action) == type(None): continue
246
+ targets |= {s.active_poison_action.path_target()}
247
+ return targets
248
+
249
+ def poison_source_of_target(self,target_idn):
250
+ for s in self.source_map.values():
251
+ if type(s.active_poison_action) == type(None):
252
+ continue
253
+
254
+ if s.active_poison_action.path_target() == target_idn:
255
+ return s
256
+ return None
257
+
258
+ def halt_poison(self,t_idn):
259
+ t = self.target_map[t_idn]
260
+ t.reset()
261
+
262
+ s = self.poison_source_of_target(t_idn)
263
+ s.clear_poison_action()
264
+
265
+ if t_idn in self.pending_poisons:
266
+ del self.pending_poisons[t_idn]
267
+ return
268
+
269
+ @staticmethod
270
+ def generate_instance(num_source_nodes,num_targets,num_poisons,poison2source_ratio_range,poison_matrix_square_dim,\
271
+ expressive_mode,prg,relays_per_source=2,relay_accuracy_range=[0.75,0.9],verbose:bool=False):
272
+
273
+ assert num_source_nodes <= num_targets
274
+ # generate the base graph
275
+ source_nodes = set([i for i in range(num_source_nodes)])
276
+ T = TreeGen.generate_tree__mroot_n_leaves(source_nodes,num_targets,prg,is_dsg=False,\
277
+ growth_type="ordered",branching_range=[1,2])
278
+ G = connect_TreeGen_forest(T,prg)
279
+ L = T.leaves
280
+
281
+ # generate the poisons
282
+ poison_matrices = generate_m_square_integer_matrixes(num_poisons,poison_matrix_square_dim,prg,DEFAULT_POISON_MODEL_SNT_MINMAX)
283
+ poison_matrix_map = {i:m for (i,m) in enumerate(poison_matrices)}
284
+
285
+ # calculate the paths of every source to targets
286
+ source2target_path_map = fetch_source2target_paths(G,source_nodes,L,prg)
287
+
288
+ # generate 1 PRNG per source
289
+ prgs = prg_to_prg__LCG_sequence(prg,num_source_nodes,moduli_scale=2.7799)
290
+
291
+ # generate the sources
292
+ psources = dict()
293
+ for i in range(num_source_nodes):
294
+ target_path_map = source2target_path_map[i]
295
+ prgx = prgs[i]
296
+
297
+ if poison2source_ratio_range[0] == poison2source_ratio_range[1]:
298
+ r = poison2source_ratio_range[0]
299
+ else:
300
+ r = modulo_in_range(prg(),poison2source_ratio_range)
301
+
302
+ m_indices = sorted(poison_matrix_map)
303
+ psize = ceil(num_poisons * r)
304
+ m_indices = prg_choose_n(m_indices,psize,prg__single_to_int(prg),is_unique_picker=True)
305
+
306
+ poison_matrix_map_ = {j:poison_matrix_map[j] for j in m_indices}
307
+ psource = PoisonSource(i,target_path_map,poison_matrix_map_,expressive_mode,prgx)
308
+ psources[i] = psource
309
+
310
+ # generate the targets
311
+ prgs = prg_to_prg__LCG_sequence(prg,num_targets,moduli_scale=3.1122)
312
+ targets = dict()
313
+ max_relays = max_number_of_relays_per_target(num_source_nodes,relays_per_source)
314
+ for (i,l) in enumerate(L):
315
+ targets[l] = PoisonTarget(l,0,prgs[i],max_relays,num_source_nodes,\
316
+ relay_accuracy_range)
317
+
318
+ return PoisonDeliveryNetwork(G,psources,targets,verbose)
@@ -0,0 +1,266 @@
1
+ from ..graph_models.hg_obj_path_op import *
2
+ from morebs2.numerical_generator import prg_to_prg__LCG_sequence
3
+
4
+ """
5
+ A Proaction Inadvertent Effect consists of one pro-action, a Directed Implication Path of
6
+ three nodes (see file<graph_models.dir_imp_path> for a formal mathematical description),
7
+ and I inadvertent actions, s.t. I = i0 + i1 + i2, i0 the number of inadvertencies associated
8
+ with the head, i1 with the center, and i2 with the tail.
9
+
10
+ NOTE:
11
+ See file<graph_models.hg_obj_path_op> for the programmatic details to the data structures
12
+ representing pro-actions and inadvertent actions.
13
+ See class<graph_models.hg_obj_path_op.InadvertentDIPathNavigator> for more information on
14
+ how the inadvertencies work, which is a subclass of class<DIPathNavigator>.
15
+
16
+ When the navigator selects a support value v to cross one of the nodes n of the 3-node pro-action,
17
+ one of two events occur:
18
+ - the navigator is able to pass with support value v => process applies the extra |v - M(n)| to every
19
+ one of that node's inadvertencies.
20
+ - the navigator does not pass with support value v => process applies v to every one of that node's
21
+ inadvertencies.
22
+
23
+ Each of those <InadvertentDIPathNavigator>s attempt to fire completely (travel their respective
24
+ three-node Directed Implication Paths). For every one of these navigators that fires completely,
25
+ the inadvertency score is +1.
26
+ """
27
+ class PRNGProactionInadvertentEffect:
28
+
29
+ def __init__(self,proaction_path_navigator,proaction_node_to_inadvertent_path_navigators):
30
+ assert type(proaction_path_navigator) == DIPathNavigatorHandler
31
+
32
+ nodeset = proaction_path_navigator.path_nodeset()
33
+ assert type(proaction_node_to_inadvertent_path_navigators) == dict
34
+ assert nodeset == set(proaction_node_to_inadvertent_path_navigators.keys())
35
+
36
+ for v in proaction_node_to_inadvertent_path_navigators.values():
37
+ for v_ in v:
38
+ assert type(v_.dipn) == InadvertentDIPathNavigator
39
+
40
+ self.ppn = proaction_path_navigator
41
+ self.n2ipn_map = proaction_node_to_inadvertent_path_navigators
42
+ self.fin_stat = False
43
+ # (node,inadvertent path index) -> # of times inadvertent path fired completely
44
+ self.imap = defaultdict(int)
45
+ return
46
+
47
+ def iscore(self):
48
+ D = defaultdict(int)
49
+ for k,v in self.imap.items():
50
+ D[k[0]] += v
51
+ return D
52
+
53
+ def set_prg(self,prg):
54
+ assert type(prg) in {FunctionType,MethodType}
55
+
56
+ self.ppn.set_prg(prg)
57
+
58
+ for v in self.n2ipn_map.values():
59
+ for v_ in v:
60
+ v_.set_prg(prg)
61
+ return
62
+
63
+ def __next__(self):
64
+ if self.ppn.dipn.fin_stat:
65
+ self.fin_stat = True
66
+
67
+ if self.fin_stat: return None,0.
68
+
69
+ # proaction
70
+ next(self.ppn)
71
+
72
+ # inadvertency
73
+ c,v = self.ppn.current_extra_value()
74
+ if type(c) == type(None):
75
+ return c,0.
76
+
77
+ self.inadvertency_on_proaction_node(c,abs(v))
78
+ return c,v
79
+
80
+ def inadvertency_on_proaction_node(self,n,v):
81
+ q = self.n2ipn_map[n]
82
+ for (i,q_) in enumerate(q):
83
+ q_.add_support(abs(v))
84
+ while not q_.dipn.fin_stat:
85
+ next(q_)
86
+
87
+ stat = q_.dipn.at_tail()
88
+ self.imap[(n,i)] += stat
89
+
90
+ # case: inadvertent path fired. Reset support to 0
91
+ if stat:
92
+ q_.reset_support()
93
+
94
+ @staticmethod
95
+ def generate_instance(start_node_idn,inadvertency_ratio_range,node_value_range,\
96
+ inadvertency_size_range,info_mode,prg):
97
+
98
+ # generate proaction
99
+ dipnh = PRNGProactionInadvertentEffect.generate_one_DIPathNavigatorHandler(\
100
+ start_node_idn,node_value_range,info_mode,is_inadvertent=False,prg=prg)
101
+
102
+ start_node_idn += 3
103
+
104
+ # generate inadvertent effects
105
+ nodeseq = sorted(dipnh.path_nodeset())
106
+ D = dict()
107
+ for n in nodeseq:
108
+ inadvertencies = modulo_in_range(int(prg()),inadvertency_size_range)
109
+ D[n] = []
110
+ for i in range(inadvertencies):
111
+ prg0 = prg_to_prg__LCG_sequence(prg,1,modulo_in_range(prg(),[1,5.]))[0]
112
+ d0,d1 = prg_decimal(prg,inadvertency_ratio_range),prg_decimal(prg,inadvertency_ratio_range)
113
+ d2 = sorted([d0,d1])
114
+ m = modulo_in_range(prg(),node_value_range)
115
+ d3 = [d2[0] * m,d2[1] * m]
116
+ ieffect = PRNGProactionInadvertentEffect.generate_one_DIPathNavigatorHandler(\
117
+ start_node_idn,d3,info_mode,is_inadvertent=True,prg=prg0)
118
+ D[n].append(ieffect)
119
+ start_node_idn += 3
120
+ return PRNGProactionInadvertentEffect(dipnh,D),start_node_idn
121
+
122
+ @staticmethod
123
+ def generate_one_DIPathNavigatorHandler(start_node_idn,node_value_range,\
124
+ info_mode,is_inadvertent:bool,prg):
125
+
126
+ # generate pro-action path
127
+ extra_edge_ratio = prg_decimal(prg,[0.,1.])
128
+ ratio_indirect_activation = prg_decimal(prg,[0.,1.])
129
+ prior_dependency_ratio = prg_decimal(prg,[0.,1.])
130
+ activation_type = "linexp" if prg_decimal(prg,[0.,1.]) >= 0.5 else "single"
131
+
132
+ G = generate_directed_implication_path(3,extra_edge_ratio,prg,start_node_idn=start_node_idn)
133
+ nv_map = generate_nv_map_from_nv_range([i for i in range(start_node_idn,start_node_idn+3)],\
134
+ node_value_range,prg)
135
+
136
+ optdi = ObjectivePathTypeDI.generate_instance(G,nv_map,\
137
+ ratio_indirect_activation,prior_dependency_ratio,activation_type,prg)
138
+
139
+ if not is_inadvertent:
140
+ dipn = DIPathNavigator.from_PathTypeDI(optdi,prg)
141
+ else:
142
+ dipn = InadvertentDIPathNavigator.from_PathTypeDI(optdi,prg)
143
+
144
+ return DIPathNavigatorHandler(optdi,dipn,info_mode=info_mode,verbose=False)
145
+
146
+ """
147
+ A generator that uses its `chain_prg` PRNG to generate one <PRNGProactionInadvertentEffect>
148
+ every time the `current_pie` is null. The `solver_prg` is to solve the active `current_pie`
149
+ at every timestamp.
150
+ """
151
+ class PRNGProactionInadvertentEffectChain:
152
+
153
+ def __init__(self,prior_connectivity_pr,inadvertency_ratio_range,node_value_range,\
154
+ inadvertency_size_range,info_mode,chain_prg,solver_prg):
155
+
156
+ assert 0. <= prior_connectivity_pr <= 1.
157
+ assert is_valid_range(inadvertency_ratio_range,False,False)
158
+ assert 0. <= inadvertency_ratio_range[0] < inadvertency_ratio_range[1] <= 1.
159
+ assert is_valid_range(node_value_range,False,False) or is_valid_range(node_value_range,True,False)
160
+ assert node_value_range[0] > 0.
161
+
162
+ assert info_mode in {0,1}
163
+ assert type(chain_prg) in {MethodType,FunctionType}
164
+ assert type(solver_prg) in {MethodType,FunctionType}
165
+
166
+ self.pc_pr = prior_connectivity_pr
167
+ self.ir_range = inadvertency_ratio_range
168
+ self.nv_range = node_value_range
169
+ self.is_range = inadvertency_size_range
170
+ self.info_mode = info_mode
171
+ self.chain_prg = chain_prg
172
+ self.solver_prg = solver_prg
173
+
174
+ self.start_node_idn = 0
175
+
176
+ self.current_pie = None
177
+ # current PIE node -> prior PIE index -> prior PIE nodeseq
178
+ self.current_to_prior_links = None
179
+ self.previous_pie = []
180
+
181
+ self.c = 0
182
+
183
+ def set_prg(self,prg,set_for_solver:bool):
184
+ assert type(prg) in {FunctionType,MethodType}
185
+ assert type(set_for_solver) == bool
186
+
187
+ if set_for_solver:
188
+ self.solver_prg = prg
189
+ else:
190
+ self.chain_prg = prg
191
+
192
+ def iscore_full(self):
193
+ S = 0
194
+ if type(self.current_pie) != type(None):
195
+ S = sum(list(self.current_pie.iscore().values()))
196
+ return S + self.iscore_prev()
197
+
198
+ def iscore_prev(self):
199
+ S = 0
200
+ for p in self.previous_pie:
201
+ S += sum(list(p.iscore().values()))
202
+ return S
203
+
204
+ def __len__(self):
205
+ return len(self.previous_pie) + (type(self.current_pie) != type(None))
206
+
207
+ def __next__(self):
208
+ if type(self.current_pie) == type(None):
209
+ self.next_PIE()
210
+
211
+ c,v = next(self.current_pie)
212
+ if type(c) != type(None):
213
+ self.activate_links(c,v)
214
+
215
+ if self.current_pie.fin_stat:
216
+ self.previous_pie.append(self.current_pie)
217
+ self.current_pie = None
218
+ self.current_to_prior_links = None
219
+
220
+ self.c += 1
221
+
222
+ def next_PIE(self):
223
+ assert type(self.current_pie) == type(None) == type(self.current_to_prior_links)
224
+
225
+ self.current_pie, self.start_node_idn = PRNGProactionInadvertentEffect.generate_instance(\
226
+ self.start_node_idn,self.ir_range,self.nv_range,self.is_range,\
227
+ self.info_mode,self.chain_prg)
228
+ self.current_pie.set_prg(self.solver_prg)
229
+ self.link_current_to_prior_PIE()
230
+ return
231
+
232
+ def activate_links(self,c,v):
233
+ if c not in self.current_to_prior_links: return
234
+
235
+ V = self.current_to_prior_links[c]
236
+ for prior_index,prior_nodeseq in V.items():
237
+ P = self.previous_pie[prior_index]
238
+ for pn in prior_nodeseq:
239
+ P.inadvertency_on_proaction_node(pn,v)
240
+
241
+ def link_current_to_prior_PIE(self):
242
+ self.current_to_prior_links = dict()
243
+ nodeseq = sorted(self.current_pie.ppn.path_nodeset())
244
+ for n in nodeseq:
245
+ self.select_linking_nodes_for_PIE_(n)
246
+
247
+ def select_linking_nodes_for_PIE_(self,current_pie_node):
248
+ for i in range(len(self.previous_pie)):
249
+ self.select_linking_nodes_for_PIE__(current_pie_node,i)
250
+
251
+ def select_linking_nodes_for_PIE__(self,current_pie_node,i):
252
+ assert len(self.previous_pie) > i >= 0
253
+
254
+ P = self.previous_pie[i]
255
+ K = sorted(P.n2ipn_map.keys())
256
+ S = []
257
+ for k in K:
258
+ d = prg_decimal(self.chain_prg,[0.,1.])
259
+ if d <= self.pc_pr:
260
+ S.append(k)
261
+
262
+ if current_pie_node not in self.current_to_prior_links:
263
+ self.current_to_prior_links[current_pie_node] = dict()
264
+ self.current_to_prior_links[current_pie_node][i] = S
265
+ return
266
+