Doxygen Generated Documentation of Ben-Jose Trainable SAT Solver Library
brain.h
Go to the documentation of this file.
1 
2 
3 /*************************************************************
4 
5 This file is part of ben-jose.
6 
7 ben-jose is free software: you can redistribute it and/or modify
8 it under the terms of the version 3 of the GNU General Public
9 License as published by the Free Software Foundation.
10 
11 ben-jose is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with ben-jose. If not, see <http://www.gnu.org/licenses/>.
18 
19 ------------------------------------------------------------
20 
21 Copyright (C) 2007-2012, 2014-2016. QUIROGA BELTRAN, Jose Luis.
22 Id (cedula): 79523732 de Bogota - Colombia.
23 See https://github.com/joseluisquiroga/ben-jose
24 
25 ben-jose is free software thanks to The Glory of Our Lord
26  Yashua Melej Hamashiaj.
27 Our Resurrected and Living, both in Body and Spirit,
28  Prince of Peace.
29 ------------------------------------------------------------*/
38 //--------------------------------------------------------------
39 
40 #ifndef BRAIN_H
41 #define BRAIN_H
42 
43 //=================================================================
44 
45 #include "stack_trace.h"
46 #include "tools.h"
47 #include "binder.h"
48 #include "ben_jose.h"
49 #include "instance_info.h"
50 #include "sortor.h"
51 #include "dbg_config.h"
52 #include "dbg_prt.h"
53 
54 #ifdef FULL_DEBUG
55 #include "str_set.h"
56 #endif
57 
58 //=============================================================================
59 // defines
60 
61 #define BRAIN_DBG(prm) DBG(prm)
62 #define BRAIN_CK(prm) DBG_BJ_LIB_CK(prm)
63 #define BRAIN_CK_PRT(prm, comms1) DBG_CK_2(prm, comms1)
64 #define BRAIN_CK_0(prm) DBG_BJ_LIB_CK(prm)
65 
66 #define BRAIN_CK_1(prm)
67 #define BRAIN_CK_2(prm)
68 
69 #define BRAIN_REL_CK(prm) if(! (prm)){ throw brain_exception(0, #prm); }
70 
71 #define BRAIN_REL_CK_PRT(prm, comms1) \
72  if(! (prm)){ \
73  bj_ostream& os = bj_dbg; \
74  comms1; \
75  os << bj_eol; \
76  os.flush(); \
77  throw brain_exception(0, #prm); \
78  } \
79 
80 //--end_of_def
81 
82 //=============================================================================
83 // MAIN CLASSES
84 
85 #define CY_LIB_DIR "draw_cnf_js_lib"
86 #define CY_IC_KIND "_ic_"
87 #define CY_NMP_KIND "_nmp_"
88 #define CY_CIRCLE_LAYOUT "'circle'"
89 #define CY_PRESET_LAYOUT "'preset'"
90 #define MAX_CY_STEPS 1000
91 
92 #define ROOT_LEVEL 0
93 
94 #define MIN_TRAINABLE_NUM_SUB 3
95 
96 enum action_t {
97  ac_invalid = -100,
98  ac_recoil = -1,
99  ac_stop = 0,
100  ac_go_on = 1
101 };
102 
103 enum mem_op_t {
104  mo_invalid = 0,
105  mo_save = 1,
106  mo_find = 2
107 };
108 
109 enum cy_quk_t {
110  cq_invalid = 0,
111  cq_reg = 1,
112  cq_mono = 2,
113  cq_cho = 3,
114  cq_for = 4,
115 };
116 
117 enum mem_find_t {
118  mf_invalid = 0,
119  mf_found = 1,
120  mf_not_found = 2
121 };
122 
123 
124 #define k_invalid_order 0
125 #define k_left_order 1
126 #define k_right_order 2
127 #define k_random_order 3
128 
129 #define INVALID_ROUND -1
130 #define INVALID_RECOIL 0
131 #define INVALID_LEVEL -1
132 #define INVALID_TIER -1
133 #define INVALID_BLOCK -1
134 #define INVALID_COLOR -1
135 #define INVALID_NUM_SUB -1
136 
137 #define INVALID_MINSHA "invalid_minsha"
138 #define INVALID_SHA "invalid_sha"
139 
140 
141 #define MAX_LAYERS 1000
142 
143 //======================================================================
144 // brain_exception
145 
146 class brain_exception : public top_exception {
147 public:
148  brain_exception(long the_id = 0, ch_string assrt = "") : top_exception(the_id, assrt)
149  {}
150 };
151 
152 //=============================================================================
153 // defs
154 
155 #define DBG_DIR "./dbg_ic_output/"
156 #define DBG_PREF "brain"
157 #define DBG_SUFI ".myl"
158 
159 #define DEFINE_GET_DBG_SLV(cls_nm) \
160 solver* \
161 cls_nm::get_dbg_slv(){ \
162  brain* the_brn = get_dbg_brn(); \
163  if(the_brn == NULL_PT){ return NULL_PT; } \
164  solver* the_slv = the_brn->br_pt_slvr; \
165  return the_slv; \
166 } \
167 \
168 
169 // end_of_define
170 
171 //=============================================================================
172 // decl
173 
174 class solver;
175 class skeleton_glb;
176 class instance_info;
177 
178 BRAIN_DBG(class dbg_inst_info;)
179 
180 class ticket;
181 class alert_rel;
182 class quanton;
183 class coloring;
184 class neuron;
185 class prop_signal;
186 class reason;
187 class deduction;
188 class cov_entry;
189 class qulayers;
190 class neuromap;
191 class deducer;
192 class notekeeper;
193 class leveldat;
194 class brain;
195 
196 typedef row<quanton*> row_quanton_t;
197 typedef row<row_quanton_t> row_row_quanton_t;
198 
199 typedef row<neuron*> row_neuron_t;
200 typedef row<row_neuron_t> row_row_neuron_t;
201 
202 typedef row<neuromap*> row_neuromap_t;
203 
204 typedef bj_big_int_t recoil_counter_t;
205 typedef bj_big_int_t round_counter_t;
206 
207 DECLARE_PRINT_FUNCS(ticket)
208 DECLARE_PRINT_FUNCS(alert_rel)
209 DECLARE_PRINT_FUNCS(quanton)
210 DECLARE_PRINT_FUNCS(neuron)
211 DECLARE_PRINT_FUNCS(reason)
212 DECLARE_PRINT_FUNCS(deduction)
213 DECLARE_PRINT_FUNCS(prop_signal)
214 DECLARE_PRINT_FUNCS(coloring)
215 DECLARE_PRINT_FUNCS(qulayers)
216 DECLARE_PRINT_FUNCS(cov_entry)
217 DECLARE_PRINT_FUNCS(neuromap)
218 DECLARE_PRINT_FUNCS(deducer)
219 DECLARE_PRINT_FUNCS(leveldat)
220 
221 //=================================================================
222 // NULL_BRN_PT
223 
224 extern brain* NULL_BRN_PT;
225 
226 //=================================================================
227 // comparison declarations
228 
229 comparison cmp_qlevel(quanton* const & qua1, quanton* const & qua2);
230 
231 //=================================================================
232 // funcs declarations
233 
234 bool dbg_all_consec(row<long>& rr1);
235 void dbg_set_cy_sigs(brain& brn, row<prop_signal>& trace);
236 void dbg_reset_cy_sigs(brain& brn, row<prop_signal>& trace);
237 void dbg_run_diff(ch_string fnm1, ch_string fnm2, ch_string dff_fnm);
238 void dbg_prepare_used_dbg_ccl(row_quanton_t& rr_qua, canon_clause& dbg_ccl);
239 void dbg_print_ccls_neus(bj_ostream& os, row<canon_clause*>& dbg_ccls);
240 bool dbg_run_satex_on(brain& brn, ch_string f_nam, neuromap* dbg_nmp);
241 void dbg_reset_all_na_creat_flags(row_neuromap_t& all_nmp);
242 
243 comparison dbg_cmp_ps(prop_signal const & ps1, prop_signal const & ps2);
244 
245 bool dbg_equal_positons(brain& brn, row_quanton_t& quas1, row_quanton_t& quas2,
246  bool skip_all_n4);
247 void dbg_get_all_positons(row_quanton_t& all_quas, row_quanton_t& all_pos,
248  bool skip_all_n4);
249 
250 bool ck_motives(brain& brn, row_quanton_t& mots);
251 
252 bool is_tk_equal(ticket& x, ticket& y);
253 
254 charge_t negate_trinary(charge_t val);
255 
256 bool has_neu(row_neuron_t& rr_neus, neuron* neu);
257 
258 void get_quas_of(brain& brn, row_neuron_t& all_neus, row_quanton_t& all_quas);
259 
260 void negate_quantons(row_quanton_t& qua_row);
261 void get_ids_of(row_quanton_t& quans, row_long_t& the_ids);
262 long find_max_level(row_quanton_t& tmp_mots);
263 long find_max_tier(row_quanton_t& tmp_mots, long from_idx = 0);
264 
265 void split_tees(brain& brn, sort_glb& srg,
266  row<sortee*>& sorted_tees, row<sortee*>& sub_tees,
267  row<canon_clause*>& ccls_in, row<canon_clause*>& ccls_not_in);
268 
269 
270 void write_all_nmps(row_neuromap_t& to_wrt);
271 
272 void append_missing_opps(brain& brn, row_quanton_t& all_quas);
273 
274 //=============================================================================
275 // ticket
276 
277 class ticket {
278  public:
279  recoil_counter_t tk_recoil; // recoil at update_tk time
280  long tk_level; // level at update_tk time
281 
282  // methods
283 
284  ticket(){
285  init_ticket();
286  }
287 
288  void init_ticket(){
289  tk_recoil = INVALID_RECOIL;
290  tk_level = INVALID_LEVEL;
291  }
292 
293  brain* get_dbg_brn(){
294  return NULL;
295  }
296 
297  solver* get_dbg_slv(){
298  return NULL;
299  }
300 
301  bool is_tk_virgin(){
302  bool c1 = (tk_recoil == INVALID_RECOIL);
303  bool c2 = (tk_level == INVALID_LEVEL);
304 
305  return (c1 && c2);
306  }
307 
308  bool is_valid(){
309  return ((tk_recoil != INVALID_RECOIL) && (tk_level != INVALID_LEVEL));
310  }
311 
312  bool is_older_than(ticket& nmp_tk);
313  bool is_older_than(neuromap* nmp);
314 
315  ch_string get_str();
316 
317  bj_ostream& print_ticket(bj_ostream& os, bool from_pt = false){
318  os << "[";
319  os << "rc:" << tk_recoil;
320  os << " lv:" << tk_level;
321  os << "]";
322  os.flush();
323  return os;
324  }
325 };
326 
327 inline
328 bool
329 is_tk_equal(ticket& x, ticket& y){
330  if(x.tk_recoil != y.tk_recoil){ return false; }
331  if(x.tk_level != y.tk_level){ return false; }
332  return true;
333 }
334 
335 //=============================================================================
336 // alert_rel
337 
338 typedef receptor<alert_rel> rece_ar_t;
339 
340 class alert_rel {
341  public:
342  static
343  char* CL_NAME;
344 
345  virtual
346  char* get_cls_name(){
347  return alert_rel::CL_NAME;
348  }
349 
350  rece_ar_t ar_alert;
351  quanton* ar_qu_alert;
352 
353  rece_ar_t ar_ref;
354  quanton* ar_qu_ref;
355 
356  alert_rel(){
357  init_alert_rel();
358  }
359 
360  void init_alert_rel(quanton* alert = NULL_PT, quanton* ref = NULL_PT);
361 
362  bool is_ar_virgin(){
363  bool c1 = (ar_qu_alert == NULL_PT);
364  bool c2 = (ar_qu_ref == NULL_PT);
365  bool vg = (c1 && c2);
366  return vg;
367  }
368 
369  bj_ostream& print_alert_rel(bj_ostream& os, bool from_pt = false){
370  os << "ar={" << ar_qu_alert << "," << ar_qu_ref << "}";
371  return os;
372  }
373 };
374 
375 //=============================================================================
384 #define DECLARE_NI_FLAG_FUNCS(flag_attr, flag_nam) \
385  bool has_pos_##flag_nam(){ \
386  return is_##flag_nam(); \
387  } \
388  \
389  bool has_neg_##flag_nam(){ \
390  return opposite().is_##flag_nam(); \
391  } \
392  \
393  bool is_##flag_nam(){ \
394  bool resp = get_flag(flag_attr, k_##flag_nam##_flag); \
395  return resp; \
396  } \
397  \
398  bool has_##flag_nam(){ \
399  return (is_##flag_nam() || opposite().is_##flag_nam()); \
400  } \
401  \
402  void reset_its_##flag_nam(brain& brn); \
403  void set_##flag_nam(brain& brn); \
404  void set_bi##flag_nam(brain& brn); \
405  void reset_##flag_nam(brain& brn); \
406  \
407 \
408 
409 // end_of_define
410 
411 #define DEFINE_NI_FLAG_FUNCS(flag_attr, flag_nam, single) \
412  void quanton::reset_its_##flag_nam(brain& brn){ \
413  BRAIN_CK(! single || has_##flag_nam()); \
414  if(has_pos_##flag_nam()){ \
415  reset_##flag_nam(brn); \
416  } \
417  if(has_neg_##flag_nam()){ \
418  opposite().reset_##flag_nam(brn); \
419  } \
420  } \
421  \
422  void quanton::set_##flag_nam(brain& brn){ \
423  BRAIN_CK(single); \
424  BRAIN_CK_PRT((! has_##flag_nam()), os << "________" << this); \
425  if(! is_##flag_nam() && ! opposite().is_##flag_nam()){ \
426  set_flag(flag_attr, k_##flag_nam##_flag); \
427  brn.br_qu_tot_##flag_nam++; \
428  } \
429  } \
430  \
431  void quanton::set_bi##flag_nam(brain& brn){ \
432  BRAIN_CK(! single); \
433  BRAIN_CK(! is_##flag_nam()); \
434  if(! is_##flag_nam()){ \
435  set_flag(flag_attr, k_##flag_nam##_flag); \
436  brn.br_qu_tot_##flag_nam++; \
437  } \
438  } \
439  \
440  void quanton::reset_##flag_nam(brain& brn){ \
441  BRAIN_CK(is_##flag_nam()); \
442  if(is_##flag_nam()){ \
443  BRAIN_CK(! single || ! opposite().is_##flag_nam()); \
444  reset_flag(flag_attr, k_##flag_nam##_flag); \
445  brn.br_qu_tot_##flag_nam--; \
446  } \
447  } \
448  \
449 \
450 
451 // end_of_define
452 
453 #define DECLARE_NI_FLAG_ALL_FUNCS(flag_nam) \
454  long set_all_bi##flag_nam(brain& brn, row_quanton_t& rr_all); \
455  long reset_all_its_##flag_nam(brain& brn, row_quanton_t& rr_all); \
456  long reset_all_##flag_nam(brain& brn, row_quanton_t& rr_all); \
457  long set_all_##flag_nam(brain& brn, row_quanton_t& rr_all); \
458  long append_all_not_##flag_nam(brain& brn, row_quanton_t& rr_src, \
459  row_quanton_t& rr_dst); \
460  void append_all_has_##flag_nam(brain& brn, row_quanton_t& rr_src, \
461  row_quanton_t& rr_dst, bool val_has = true); \
462  bool same_quantons_##flag_nam(brain& brn, row_quanton_t& sup_ss, \
463  row_quanton_t& sub_ss, row_quanton_t* all_bad_qua = NULL_PT); \
464  bool all_qu_have_##flag_nam(brain& brn, row_quanton_t& rr1, bool val = true); \
465 \
466 
467 // end_of_define
468 
469 #define DEFINE_NI_FLAG_ALL_FUNCS(flag_nam) \
470  long set_all_bi##flag_nam(brain& brn, row_quanton_t& rr_all){ \
471  long num_qua_mod = 0; \
472  for(long aa = 0; aa < rr_all.size(); aa++){ \
473  quanton& qua = *(rr_all[aa]); \
474  if(! qua.is_##flag_nam()){ \
475  qua.set_bi##flag_nam(brn); \
476  num_qua_mod++; \
477  } \
478  } \
479  return num_qua_mod; \
480  } \
481  \
482  long reset_all_its_##flag_nam(brain& brn, row_quanton_t& rr_all){ \
483  long num_qua = 0; \
484  for(long aa = 0; aa < rr_all.size(); aa++){ \
485  BRAIN_CK(rr_all[aa] != NULL_PT); \
486  quanton& qua = *(rr_all[aa]); \
487  if(qua.has_##flag_nam()){ \
488  qua.reset_its_##flag_nam(brn); \
489  num_qua++; \
490  } \
491  } \
492  return num_qua; \
493  } \
494  \
495  long reset_all_##flag_nam(brain& brn, row_quanton_t& rr_all){ \
496  long num_qua_mod = 0; \
497  for(long aa = 0; aa < rr_all.size(); aa++){ \
498  BRAIN_CK(rr_all[aa] != NULL_PT); \
499  quanton& qua = *(rr_all[aa]); \
500  if(qua.has_##flag_nam()){ \
501  qua.reset_##flag_nam(brn); \
502  num_qua_mod++; \
503  } \
504  } \
505  return num_qua_mod; \
506  } \
507  \
508  long set_all_##flag_nam(brain& brn, row_quanton_t& rr_all){ \
509  long num_qua_mod = 0; \
510  for(long aa = 0; aa < rr_all.size(); aa++){ \
511  quanton& qua = *(rr_all[aa]); \
512  if(! qua.has_##flag_nam()){ \
513  qua.set_##flag_nam(brn); \
514  num_qua_mod++; \
515  } \
516  } \
517  return num_qua_mod; \
518  } \
519  \
520  long append_all_not_##flag_nam(brain& brn, row_quanton_t& rr_src, \
521  row_quanton_t& rr_dst) \
522  { \
523  long num_qua_app = 0; \
524  for(long aa = 0; aa < rr_src.size(); aa++){ \
525  BRAIN_CK(rr_src[aa] != NULL_PT); \
526  quanton& qua = *(rr_src[aa]); \
527  if(! qua.has_##flag_nam()){ \
528  qua.set_##flag_nam(brn); \
529  rr_dst.push(&qua); \
530  num_qua_app++; \
531  } \
532  } \
533  return num_qua_app; \
534  } \
535  \
536  void append_all_has_##flag_nam(brain& brn, row_quanton_t& rr_src, \
537  row_quanton_t& rr_dst, bool val_has) \
538  { \
539  for(long aa = 0; aa < rr_src.size(); aa++){ \
540  BRAIN_CK(rr_src[aa] != NULL_PT); \
541  quanton& qua = *(rr_src[aa]); \
542  if(qua.has_##flag_nam() == val_has){ \
543  rr_dst.push(&qua); \
544  } \
545  } \
546  } \
547  \
548  bool same_quantons_##flag_nam(brain& brn, row_quanton_t& sup_ss, \
549  row_quanton_t& sub_ss, row_quanton_t* all_bad_qua) \
550  { \
551  BRAIN_CK(brn.br_qu_tot_##flag_nam == 0); \
552  set_all_##flag_nam(brn, sup_ss); \
553  bool sm_quas = true; \
554  for(long aa = 0; aa < sub_ss.size(); aa++){ \
555  quanton& qua = *(sub_ss[aa]); \
556  if(! qua.has_##flag_nam()){ \
557  sm_quas = false; \
558  if(all_bad_qua != NULL_PT){ all_bad_qua->push(&qua); } \
559  } \
560  } \
561  reset_all_##flag_nam(brn, sup_ss); \
562  BRAIN_CK(brn.br_qu_tot_##flag_nam == 0); \
563  return sm_quas; \
564  } \
565  \
566  bool all_qu_have_##flag_nam(brain& brn, row_quanton_t& rr_all, bool val){ \
567  bool all_hv = true; \
568  for(long aa = 0; aa < rr_all.size(); aa++){ \
569  quanton& qua = *(rr_all[aa]); \
570  if(qua.has_##flag_nam() != val){ \
571  all_hv = false; \
572  break; \
573  } \
574  } \
575  return all_hv; \
576  } \
577  \
578 \
579 
580 // end_of_define
581 
582 #define k_note0_flag k_flag0
583 #define k_note1_flag k_flag1
584 #define k_note2_flag k_flag2
585 #define k_note3_flag k_flag3
586 #define k_note4_flag k_flag4
587 #define k_note5_flag k_flag5
588 #define k_note6_flag k_flag6
589 
590 DECLARE_NI_FLAG_ALL_FUNCS(note0);
591 DECLARE_NI_FLAG_ALL_FUNCS(note1);
592 DECLARE_NI_FLAG_ALL_FUNCS(note2);
593 DECLARE_NI_FLAG_ALL_FUNCS(note3);
594 DECLARE_NI_FLAG_ALL_FUNCS(note4);
595 DECLARE_NI_FLAG_ALL_FUNCS(note5);
596 DECLARE_NI_FLAG_ALL_FUNCS(note6);
597 
598 typedef receptor<quanton> qu_rece_t;
599 
600 class quanton {
601  public:
602  static
603  char* CL_NAME;
604 
605  virtual
606  char* get_cls_name(){
607  return quanton::CL_NAME;
608  }
609 
610  BRAIN_DBG(
611  brain* qu_pt_brn;
612  )
613 
614  DBG(long qu_dbg_ic_trail_idx); // idx in trail
615 
616  // id attributes
617  long qu_id; // my long number id
618  long qu_index; // idx in brain's 'positons' or 'negatons'
619  charge_t qu_spin; // positive if positon or negative if negaton
620  quanton* qu_inverse; // my inverse quanton
621 
622  // state attributes
623  // symetric means that it must be set for positons and negatons at the same time
624 
625  t_1byte qu_flags;
626 
627  bool qu_has_been_cho;
628 
629  charge_t qu_charge; // symetric. current charge
630  ticket qu_charge_tk; // symetric.
631 
632  neuron* qu_source; // source of signal when charged
633  long qu_tier; // the tier at which it was charged
634 
635  ticket qu_cicle_tk;
636  row_neuron_t qu_all_cicle_sources; // all src neurons for charged qua
637 
638  // tunneling attributes
639  row_neuron_t qu_tunnels; // tunnelled neurons.
640 
641  // mono attributes
642  row_neuron_t qu_all_neus; // neurons with this qua
643  grip qu_mono_alerts; // quas to update alert_neu
644  grip qu_mono_refs; // alert_neu qua refs.
645  long qu_alert_neu_idx; // not yet charged neu. index in qu_all_neus.
646  long qu_lv_mono;
647 
648  // sorotr data
649 
650  sortee qu_tee;
651  sortrel qu_reltee;
652 
653  long qu_tmp_col;
654 
655  // candidate system
656  ticket qu_candidate_tk;
657  neuromap* qu_candidate_nmp;
658 
659  // min_wrt system
660  ticket qu_upd_to_wrt_tk;
661 
662  //row_neuron_t qu_full_charged;
663  //row_long_t qu_full_chg_min_ti;
664 
665  ticket qu_proof_tk;
666 
667  // dbg attributes
668  DBG(
669  long qu_dbg_choice_idx;
670  long qu_dbg_num_fill_by_qua;
671  long qu_dbg_tee_ti;
672  prop_signal* qu_dbg_cy_sig;
673  neuromap* qu_dbg_cy_nmp;
674  long qu_dbg_phi_grp;
675 
676  long qu_dbg_drw_x_pos;
677  long qu_dbg_drw_y_pos;
678  );
679 
680  // methods
681 
682  quanton(){
683  init_quanton(NULL, cg_neutral, INVALID_IDX, NULL);
684  }
685 
686  ~quanton(){
687  BRAIN_CK_0(qu_source == NULL);
688  BRAIN_CK_0(qu_tunnels.size() == 0);
689 
690  qu_tunnels.clear(false, true);
691  init_quanton(NULL, cg_neutral, 0, NULL);
692  }
693 
694  DECLARE_NI_FLAG_FUNCS(qu_flags, note0); // sngle
695  DECLARE_NI_FLAG_FUNCS(qu_flags, note1); // sngle
696  DECLARE_NI_FLAG_FUNCS(qu_flags, note2); // sngle
697  DECLARE_NI_FLAG_FUNCS(qu_flags, note3); // sngle
698  DECLARE_NI_FLAG_FUNCS(qu_flags, note4); // sngle
699  DECLARE_NI_FLAG_FUNCS(qu_flags, note5); // sngle
700  DECLARE_NI_FLAG_FUNCS(qu_flags, note6); // bina
701 
702  void qua_tunnel_signals(brain& brn);
703 
704  brain* get_dbg_brn(){
705  brain* the_brn = NULL;
706  BRAIN_DBG(the_brn = qu_pt_brn);
707  return the_brn;
708  }
709 
710  solver* get_dbg_slv();
711 
712  void init_quanton(brain* brn, charge_t spn, long ii, quanton* inv){
713  BRAIN_DBG(qu_pt_brn = brn);
714 
715  DBG(qu_dbg_ic_trail_idx = INVALID_IDX);
716 
717  qu_id = (spn == cg_positive)?(ii + 1):(-(ii + 1));
718  if(spn == cg_neutral){ qu_id = 0; }
719 
720  qu_index = ii;
721  qu_spin = spn;
722  qu_inverse = inv;
723 
724  qu_alert_neu_idx = INVALID_IDX;
725  qu_lv_mono = INVALID_LEVEL;
726 
727  //qu_full_charged.clear();
728 
729  qu_flags = 0;
730 
731  qu_has_been_cho = false;
732 
733  qu_charge = cg_neutral;
734  qu_charge_tk.init_ticket();
735 
736  qu_source = NULL;
737  qu_tier = INVALID_TIER;
738 
739  qu_cicle_tk.init_ticket();
740  qu_all_cicle_sources.clear();
741 
742  qu_tee.init_sortee(true);
743  qu_reltee.init_sortrel(true);
744 
745  qu_tee.so_dbg_me_class = 1;
746  qu_tee.so_me = this;
747  qu_tee.so_cmp_val = &qu_tier;
748  qu_tee.so_related = &qu_reltee;
749 
750  qu_tee.so_dbg_extrn_id = qu_id;
751 
752  if(qu_inverse != NULL_PT){
753  sortee& oppt = qu_inverse->qu_tee;
754  qu_reltee.so_opposite = &(oppt);
755  }
756 
757  qu_tmp_col = INVALID_COLOR;
758 
759  qu_candidate_tk.init_ticket();
760  qu_candidate_nmp = NULL_PT;
761 
762  qu_upd_to_wrt_tk.init_ticket();
763 
764  qu_proof_tk.init_ticket();
765 
766  BRAIN_DBG(
767  qu_dbg_choice_idx = INVALID_IDX;
768  qu_dbg_num_fill_by_qua = 0;
769  qu_dbg_tee_ti = INVALID_NATURAL;
770  qu_dbg_cy_sig = NULL_PT;
771  qu_dbg_cy_nmp = NULL_PT;
772  qu_dbg_phi_grp = INVALID_NATURAL;
773 
774  qu_dbg_drw_x_pos = 0;
775  qu_dbg_drw_y_pos = 0;
776  );
777  }
778 
779  void set_candidate(neuromap& nmp);
780  void reset_candidate();
781  bool has_candidate(brain& brn);
782 
783  void update_cicle_srcs(brain& brn, neuron* neu);
784  void reset_cicle_src();
785 
786  void reset_qu_tee();
787 
788  quanton& opposite(){
789  BRAIN_CK(qu_inverse != NULL_PT);
790  return (*qu_inverse);
791  }
792 
793  quanton* get_positon(){
794  if(qu_spin == cg_negative){
795  BRAIN_CK_0(qu_inverse->qu_spin == cg_positive);
796  return qu_inverse;
797  }
798  BRAIN_CK_0(qu_spin == cg_positive);
799  return this;
800  }
801 
802  long abs_id(){
803  long resp = get_positon()->qu_id;
804  BRAIN_CK_0(resp > 0);
805  return resp;
806  }
807 
808  bool ck_all_tunnels();
809  void tunnel_swapop(long idx_pop);
810 
811  long qlevel(){ return qu_charge_tk.tk_level; }
812  leveldat* qlv_dat(brain& brn);
813 
814  bool in_root_qlv(){
815  return (qlevel() == ROOT_LEVEL);
816  }
817 
818 
819  bool is_lv_choice(brain& brn);
820 
821  bool ck_charge(brain& brn);
822 
823  bool is_pos(){ return (get_charge() == cg_positive); }
824  bool is_neg(){ return (get_charge() == cg_negative); }
825  bool is_nil(){ return (get_charge() == cg_neutral); }
826 
827  void set_charge(brain& brn, neuron* src, charge_t cha, long max_tier);
828  charge_t get_charge(){ return qu_charge; }
829  bool has_charge(){ return ! is_nil(); }
830 
831  bool has_tier(){ return (qu_tier != INVALID_TIER); }
832 
833  void set_source(neuron* neu);
834  neuron* get_source();
835 
836  bool has_source(){
837  return (get_source() != NULL_PT);
838  }
839 
840  void add_source(neuron& neu);
841 
842  bool has_learned_source();
843 
844  bool is_choice(){
845  bool cho = (! has_source() && (qlevel() != ROOT_LEVEL));
846  return cho;
847  }
848 
849  bool is_cicle_choice(){
850  bool is_clc_cho = is_choice() && ! is_opp_mono();
851  return is_clc_cho;
852  }
853 
854  bool ck_biqu(brain& brn);
855 
856  bool is_qu_end_of_neuromap(brain& brn);
857 
858  long find_alert_idx(bool is_init, row_quanton_t& all_pos);
859  void update_alert_neu(brain& brn, bool is_init);
860  neuron* get_alert_neu();
861  void set_alert_neu(brain& brn, long the_idx);
862  void append_all_to_alert(brain& brn, row_quanton_t& all_quas);
863  bool is_mono();
864  bool ck_alert_neu();
865 
866  bool is_opp_mono(){
867  return opposite().is_mono();
868  }
869 
870  bool has_mono(){
871  bool h_mn = is_mono() || is_opp_mono();
872  return h_mn;
873  }
874 
875  long get_lv_mono(){
876  if(qu_lv_mono != INVALID_LEVEL){
877  return qu_lv_mono;
878  }
879  long opp_lv_mn = opposite().qu_lv_mono;
880  return opp_lv_mn;
881  }
882 
883  bool has_lv_alert_neu(long lv_nmp);
884 
885  neuromap* get_nmp_to_write(brain& brn);
886  neuromap* get_candidate_to_write(brain& brn);
887  neuromap* get_candidate_to_fill(brain& brn);
888 
889  bool is_qu_dominated(brain& brn);
890  void make_qu_dominated(brain& brn);
891 
892  bool is_smaller_source(neuron& neu, long qti);
893  void update_source(brain& brn, neuron& neu);
894 
895  void update_qu_to_wrt_tk();
896  void update_source_wrt_tk(brain& brn);
897 
898  bool is_qu_to_upd_wrt_tk();
899 
900  bool is_qu_uniron(){
901  if(! in_root_qlv()){
902  return false;
903  }
904  bool is_u = ! qu_proof_tk.is_tk_virgin();
905  return is_u;
906  }
907 
908  cy_quk_t get_cy_kind();
909 
910  bj_ostream& dbg_qu_print_col_cy_edge(bj_ostream& os, long& consec, long neu_idx);
911 
912  bj_ostream& print_quanton_base(bj_ostream& os, bool from_pt,
913  long ps_ti, neuron* ps_src, bool from_tee);
914 
915  bj_ostream& print_quanton(bj_ostream& os, bool from_pt = false);
916 };
917 
918 void
919 set_all_qu_nemap(row_quanton_t& all_quas, neuromap* nmp, long first_idx = 0);
920 
921 //=============================================================================
934 class coloring {
935  public:
936 
937  brain* co_brn;
938 
939  row_quanton_t co_quas;
940  row_long_t co_qua_colors;
941  bool co_all_qua_consec;
942 
943  row_neuron_t co_neus;
944  row_long_t co_neu_colors;
945  bool co_all_neu_consec;
946 
947  coloring(brain* pt_brn = NULL_PT){
948  init_coloring(pt_brn);
949  }
950 
951  ~coloring(){
952  init_coloring();
953  }
954 
955  void init_coloring(brain* pt_brn = NULL){
956  co_brn = pt_brn;
957 
958  co_quas.clear();
959  co_qua_colors.clear();
960  co_all_qua_consec = false;
961 
962  co_neus.clear();
963  co_neu_colors.clear();
964  co_all_neu_consec = false;
965  }
966 
967  brain& get_brn(){
968  BRAIN_CK(co_brn != NULL_PT);
969  return *co_brn;
970  }
971 
972  brain* get_dbg_brn(){
973  brain* the_brn = NULL;
974  BRAIN_DBG(the_brn = co_brn);
975  return the_brn;
976  }
977 
978  solver* get_dbg_slv();
979 
980  bool is_co_virgin(){
981  bool c1 = (co_quas.is_empty());
982  bool c2 = (co_qua_colors.is_empty());
983  bool c3 = (co_all_qua_consec == false);
984 
985  bool c4 = (co_neus.is_empty());
986  bool c5 = (co_neu_colors.is_empty());
987  bool c6 = (co_all_neu_consec == false);
988 
989  bool is_vg = (c1 && c2 && c3 && c4 && c5 && c6);
990 
991  return is_vg;
992  }
993 
994  void dbg_set_brain_coloring();
995 
996  void save_colors_from(sort_glb& neus_srg, sort_glb& quas_srg, tee_id_t consec_kk,
997  bool unique_ccls);
998 
999  void load_colors_into(sort_glb& neus_srg, sort_glb& quas_srg,
1000  dbg_call_id dbg_id, neuromap* nmp = NULL_PT, bool calc_phi_id = false);
1001 
1002  void add_coloring(coloring& clr, neuromap* dbg_nmp = NULL_PT);
1003 
1004  void init_with_trace(brain& brn, row<prop_signal>& trace,
1005  long first_idx = 0, long last_idx = -1);
1006  void join_coloring(coloring& clr); // rebases and skips repeated quas and neus
1007 
1008  void move_co_to(coloring& col2);
1009  void copy_co_to(coloring& col2);
1010  void reset_as_simple_col();
1011 
1012  bool dbg_equal_nmp_to(coloring& col2, bool skip_all_n4);
1013  bool dbg_equal_co_to(coloring& col2, row_quanton_t* skip);
1014 
1015  void dbg_set_tmp_colors(bool skip_all_n4);
1016  void dbg_reset_tmp_colors(bool skip_all_n4);
1017  bool dbg_equal_to_tmp_colors(bool skip_all_n4);
1018 
1019  bool ck_cols(){
1020  BRAIN_CK(co_quas.size() == co_qua_colors.size());
1021  BRAIN_CK(co_neus.size() == co_neu_colors.size());
1022  return true;
1023  }
1024 
1025  bool dbg_ck_consec_col();
1026 
1027  bj_ostream& dbg_print_col_cy_graph(bj_ostream& os, bool is_ic);
1028  void dbg_print_qua_ids(bj_ostream& os);
1029 
1030  bj_ostream& print_coloring(bj_ostream& os, bool from_pt = false);
1031 };
1032 
1033 //=============================================================================
1040 #define DECLARE_NE_FLAG_FUNCS(flag_attr, flag_nam) \
1041  bool has_##flag_nam(){ \
1042  bool resp = get_flag(flag_attr, k_##flag_nam##_flag); \
1043  return resp; \
1044  } \
1045  \
1046  void reset_its_##flag_nam(brain& brn); \
1047  void reset_##flag_nam(brain& brn); \
1048  void set_##flag_nam(brain& brn); \
1049  \
1050 \
1051 
1052 // end_of_define
1053 
1054 #define DEFINE_NE_FLAG_FUNCS(flag_attr, flag_nam) \
1055  void neuron::reset_its_##flag_nam(brain& brn){ \
1056  BRAIN_CK(has_##flag_nam()); \
1057  if(has_##flag_nam()){ \
1058  reset_##flag_nam(brn); \
1059  } \
1060  } \
1061  \
1062  void neuron::reset_##flag_nam(brain& brn){ \
1063  BRAIN_CK(has_##flag_nam()); \
1064  reset_flag(flag_attr, k_##flag_nam##_flag); \
1065  brn.br_ne_tot_##flag_nam--; \
1066  } \
1067  \
1068  void neuron::set_##flag_nam(brain& brn){ \
1069  BRAIN_CK(! has_##flag_nam()); \
1070  set_flag(flag_attr, k_##flag_nam##_flag); \
1071  brn.br_ne_tot_##flag_nam++; \
1072  } \
1073  \
1074 \
1075 
1076 // end_of_define
1077 
1078 #define DECLARE_NE_FLAG_ALL_FUNCS(flag_nam) \
1079  long reset_all_its_##flag_nam(brain& brn, row_neuron_t& rr_all); \
1080  long reset_all_##flag_nam(brain& brn, row_neuron_t& rr_all); \
1081  long set_all_##flag_nam(brain& brn, row_neuron_t& rr_all); \
1082  long append_all_not_##flag_nam(brain& brn, row_neuron_t& rr_src, \
1083  row_neuron_t& rr_dst); \
1084  void append_all_have_##flag_nam(brain& brn, row_neuron_t& rr_src, \
1085  row_neuron_t& rr_dst, bool val_has = true); \
1086  neuron* same_neurons_##flag_nam(brain& brn, row_neuron_t& sup_ss, \
1087  row_neuron_t& sub_ss); \
1088  bool all_neurons_have_##flag_nam(row_neuron_t& rr1, bool val = true); \
1089 \
1090 
1091 // end_of_define
1092 
1093 #define DEFINE_NE_FLAG_ALL_FUNCS(flag_nam) \
1094  long reset_all_its_##flag_nam(brain& brn, row_neuron_t& rr_all){ \
1095  long num_neu = 0; \
1096  for(long aa = 0; aa < rr_all.size(); aa++){ \
1097  BRAIN_CK(rr_all[aa] != NULL_PT); \
1098  neuron& neu = *(rr_all[aa]); \
1099  if(neu.has_##flag_nam()){ \
1100  neu.reset_its_##flag_nam(brn); \
1101  num_neu++; \
1102  } \
1103  } \
1104  return num_neu; \
1105  } \
1106  \
1107  long reset_all_##flag_nam(brain& brn, row_neuron_t& rr_all){ \
1108  long num_neu_mod = 0; \
1109  for(long aa = 0; aa < rr_all.size(); aa++){ \
1110  BRAIN_CK(rr_all[aa] != NULL_PT); \
1111  neuron& neu = *(rr_all[aa]); \
1112  if(neu.has_##flag_nam()){ \
1113  neu.reset_##flag_nam(brn); \
1114  num_neu_mod++; \
1115  } \
1116  } \
1117  return num_neu_mod; \
1118  } \
1119  \
1120  long set_all_##flag_nam(brain& brn, row_neuron_t& rr_all){ \
1121  long num_neu_mod = 0; \
1122  for(long aa = 0; aa < rr_all.size(); aa++){ \
1123  neuron& neu = *(rr_all[aa]); \
1124  if(! neu.has_##flag_nam()){ \
1125  neu.set_##flag_nam(brn); \
1126  num_neu_mod++; \
1127  } \
1128  } \
1129  return num_neu_mod; \
1130  } \
1131  \
1132  long append_all_not_##flag_nam(brain& brn, row_neuron_t& rr_src, \
1133  row_neuron_t& rr_dst) \
1134  { \
1135  long num_neu_app = 0; \
1136  for(long aa = 0; aa < rr_src.size(); aa++){ \
1137  neuron& neu = *(rr_src[aa]); \
1138  if(! neu.has_##flag_nam()){ \
1139  neu.set_##flag_nam(brn); \
1140  rr_dst.push(&neu); \
1141  num_neu_app++; \
1142  } \
1143  } \
1144  return num_neu_app; \
1145  } \
1146  \
1147  void append_all_have_##flag_nam(brain& brn, row_neuron_t& rr_src, \
1148  row_neuron_t& rr_dst, bool val_has) \
1149  { \
1150  for(long aa = 0; aa < rr_src.size(); aa++){ \
1151  BRAIN_CK(rr_src[aa] != NULL_PT); \
1152  neuron& neu = *(rr_src[aa]); \
1153  if(neu.has_##flag_nam() == val_has){ \
1154  rr_dst.push(&neu); \
1155  } \
1156  } \
1157  } \
1158  \
1159  neuron* same_neurons_##flag_nam(brain& brn, row_neuron_t& sup_ss, \
1160  row_neuron_t& sub_ss) \
1161  { \
1162  BRAIN_CK(brn.br_ne_tot_##flag_nam == 0); \
1163  set_all_##flag_nam(brn, sup_ss); \
1164  neuron* bad_neu = NULL_PT; \
1165  for(long aa = 0; aa < sub_ss.size(); aa++){ \
1166  BRAIN_CK(sub_ss[aa] != NULL_PT); \
1167  neuron& neu = *(sub_ss[aa]); \
1168  if(! neu.has_##flag_nam()){ \
1169  bad_neu = &neu; \
1170  break; \
1171  } \
1172  } \
1173  reset_all_##flag_nam(brn, sup_ss); \
1174  BRAIN_CK(brn.br_ne_tot_##flag_nam == 0); \
1175  return bad_neu; \
1176  } \
1177  \
1178  bool all_neurons_have_##flag_nam(row_neuron_t& rr1, bool val) \
1179  { \
1180  bool all_hv = true; \
1181  for(long aa = 0; aa < rr1.size(); aa++){ \
1182  neuron& neu = *(rr1[aa]); \
1183  if(neu.has_##flag_nam() != val){ \
1184  all_hv = false; \
1185  break; \
1186  } \
1187  } \
1188  return all_hv; \
1189  } \
1190  \
1191 \
1192 
1193 // end_of_define
1194 
1195 #define k_tag0_flag k_flag0
1196 #define k_tag1_flag k_flag1
1197 #define k_tag2_flag k_flag2
1198 #define k_tag3_flag k_flag3
1199 #define k_tag4_flag k_flag4
1200 #define k_tag5_flag k_flag5
1201 
1202 DECLARE_NE_FLAG_ALL_FUNCS(tag0);
1203 DECLARE_NE_FLAG_ALL_FUNCS(tag1);
1204 DECLARE_NE_FLAG_ALL_FUNCS(tag2);
1205 DECLARE_NE_FLAG_ALL_FUNCS(tag3);
1206 DECLARE_NE_FLAG_ALL_FUNCS(tag4);
1207 DECLARE_NE_FLAG_ALL_FUNCS(tag5);
1208 
1209 typedef receptor<neuron> ne_rece_t;
1210 
1211 class neuron {
1212  public:
1213  static
1214  char* CL_NAME;
1215 
1216  virtual
1217  char* get_cls_name(){
1218  return neuron::CL_NAME;
1219  }
1220 
1221  BRAIN_DBG(
1222  brain* ne_pt_brn;
1223  )
1224 
1225  long ne_index; //idx in br_neurons
1226  bool ne_original;
1227  t_1byte ne_flags;
1228 
1229  row_quanton_t ne_fibres; // used in forward propagation of negative signls
1230 
1231  long ne_fibre_0_idx; // this == fibres[0]->tunnels[fibre_0_idx]
1232  long ne_fibre_1_idx; // this == fibres[1]->tunnels[fibre_1_idx]
1233 
1234  long ne_edge; // last (with max_level) of negated tunneled quantons
1235  ticket ne_edge_tk;
1236 
1237  bool ne_is_conflict; // to push conflcts only once into conflct queue
1238 
1239  sortee ne_tee;
1240  sortrel ne_reltee;
1241 
1242  long ne_tmp_col;
1243 
1244  ticket ne_candidate_tk;
1245 
1246  // min_wrt system
1247  ticket ne_upd_to_wrt_tk;
1248  ticket ne_to_wrt_tk;
1249 
1250  ticket ne_proof_tk;
1251 
1252  BRAIN_DBG(
1253  canon_clause ne_dbg_ccl;
1254  long ne_dbg_drw_x_pos;
1255  long ne_dbg_drw_y_pos;
1256  recoil_counter_t ne_dbg_wrt_rc;
1257  )
1258 
1259  // methods
1260 
1261  neuron(){
1262  ne_index = INVALID_IDX; // not in init_neuron to be consistent with is_ne_virgin.
1263  init_neuron();
1264  }
1265 
1266  ~neuron(){
1267  DBG(ne_dbg_ccl.cc_in_free = true);
1268  init_neuron();
1269  }
1270 
1271  brain* get_dbg_brn(){
1272  brain* the_brn = NULL;
1273  BRAIN_DBG(the_brn = ne_pt_brn);
1274  return the_brn;
1275  }
1276 
1277  solver* get_dbg_slv();
1278 
1279  void init_neuron(){
1280  BRAIN_DBG(ne_pt_brn = NULL);
1281 
1282  //if(fib_sz() > 0){
1283  if(fib_sz() > 1){
1284  row_quanton_t empty;
1285  BRAIN_CK_0(empty.size() == 0);
1286  quanton* forced_qua = update_fibres(empty, false);
1287  MARK_USED(forced_qua);
1288  BRAIN_CK_0(forced_qua == NULL);
1289  }
1290 
1291  ne_original = false;
1292  ne_flags = 0;
1293 
1294  ne_fibre_0_idx = INVALID_IDX;
1295  ne_fibre_1_idx = INVALID_IDX;
1296 
1297  ne_edge = INVALID_IDX;
1298  ne_edge_tk.init_ticket();
1299 
1300  ne_is_conflict = false;
1301 
1302  ne_tee.init_sortee(true);
1303  ne_reltee.init_sortrel(true);
1304 
1305  ne_tee.so_dbg_me_class = 2;
1306  ne_tee.so_me = this;
1307  ne_tee.so_related = &ne_reltee;
1308  ne_tee.so_ccl.cc_me = this;
1309 
1310  ne_tmp_col = INVALID_COLOR;
1311 
1312  ne_candidate_tk.init_ticket();
1313 
1314  ne_upd_to_wrt_tk.init_ticket();
1315  ne_to_wrt_tk.init_ticket();
1316 
1317  ne_proof_tk.init_ticket();
1318 
1319  BRAIN_DBG(
1320  ne_dbg_ccl.cc_me = this;
1321  ne_dbg_drw_x_pos = 0;
1322  ne_dbg_drw_y_pos = 0;
1323  ne_dbg_wrt_rc = 0;
1324  )
1325  }
1326 
1327 
1328  bool is_ne_virgin(){
1329  bool c1 = (fib_sz() == 0);
1330  bool c2 = (ne_original == false);
1331  bool c3 = (ne_fibre_0_idx == INVALID_IDX);
1332  bool c4 = (ne_fibre_1_idx == INVALID_IDX);
1333  bool c5 = (ne_edge == INVALID_IDX);
1334  bool c6 = (! ne_edge_tk.is_valid());
1335  bool c7 = (ne_is_conflict == false);
1336 
1337  return (c1 && c2 && c3 && c4 && c5 && c6 && c7);
1338  }
1339 
1340  long fib_sz(){ return ne_fibres.size(); }
1341 
1342  bool ck_tunnels(){
1343  BRAIN_CK_0( (ne_fibre_0_idx == INVALID_IDX) ||
1344  ((ne_fibres[0]->qu_tunnels)[ne_fibre_0_idx] == this));
1345  BRAIN_CK_0( (ne_fibre_1_idx == INVALID_IDX) ||
1346  ((ne_fibres[1]->qu_tunnels)[ne_fibre_1_idx] == this));
1347  return true;
1348  }
1349 
1350  quanton& fib0(){
1351  BRAIN_CK(ne_fibres[0] != NULL_PT);
1352  return *(ne_fibres[0]);
1353  }
1354 
1355  quanton& fib1(){
1356  BRAIN_CK(ne_fibres[1] != NULL_PT);
1357  return *(ne_fibres[1]);
1358  }
1359 
1360  long get_min_ti_idx(long fb_idx1, long fb_idx2);
1361  long get_max_lv_idx(long fb_idx1, long fb_idx2);
1362 
1363  DECLARE_NE_FLAG_FUNCS(ne_flags, tag0);
1364  DECLARE_NE_FLAG_FUNCS(ne_flags, tag1);
1365  DECLARE_NE_FLAG_FUNCS(ne_flags, tag2);
1366  DECLARE_NE_FLAG_FUNCS(ne_flags, tag3);
1367  DECLARE_NE_FLAG_FUNCS(ne_flags, tag4);
1368  DECLARE_NE_FLAG_FUNCS(ne_flags, tag5);
1369 
1370  quanton* forced_quanton(){
1371  return ne_fibres[0];
1372  }
1373 
1374  void dbg_old_set_motives(brain& brn, notekeeper& dke, bool is_first);
1375 
1376  void swap_fibres_0_1(){
1377  BRAIN_CK(ck_tunnels());
1378  quanton* fb_0 = ne_fibres[0];
1379  ne_fibres[0] = ne_fibres[1];
1380  ne_fibres[1] = fb_0;
1381 
1382  long fb_0_idx = ne_fibre_0_idx;
1383  ne_fibre_0_idx = ne_fibre_1_idx;
1384  ne_fibre_1_idx = fb_0_idx;
1385  BRAIN_CK(ck_tunnels());
1386  }
1387 
1388  void swap_fibres_1(long idx, long& max_lv_idx);
1389  void swap_fibres_0(long idx, long& max_lv_idx);
1390 
1391  void neu_swap_edge(brain& brn, long ii, long& max_lv_idx);
1392  void neu_tunnel_signals(brain& brn, quanton& r_qua);
1393  void neu_tunnel_signals_2(brain& brn, quanton& r_qua);
1394 
1395  quanton* update_fibres(row_quanton_t& synps, bool orig);
1396 
1397  bool ck_all_neg(long from, bool ck_tunn_ord = false);
1398  bool ck_all_has_charge(long max_lv_idx);
1399  bool ck_no_source_of_any();
1400 
1401  quanton* find_is_pos(){
1402  for(long ii = 0; ii < fib_sz(); ii++){
1403  quanton* qua = ne_fibres[ii];
1404  BRAIN_CK(qua != NULL_PT);
1405  if(qua->is_pos()){
1406  return qua;
1407  }
1408  }
1409  return NULL_PT;
1410  }
1411 
1412  quanton* dbg_find_first_pos();
1413  bool dbg_ck_min_pos_idx(long min_ti_pos_idx);
1414 
1415  bool is_ne_alert(){ // not yet satisf
1416  bool is_alrt = (find_is_pos() == NULL_PT);
1417  return is_alrt;
1418  }
1419 
1420  bool is_ne_inert(){
1421  return ! is_ne_alert();
1422  }
1423 
1424  bool dbg_ne_compute_ck_sat();
1425 
1426  void fill_mutual_sortees(brain& brn);
1427 
1428  bool has_qua(quanton& tg_qua);
1429 
1430  void append_ne_biqu(brain& brn, quanton& cho, quanton& pos_qu,
1431  row_quanton_t& all_biqus);
1432 
1433  sorset* get_sorset(){
1434  return ne_tee.so_vessel;
1435  }
1436 
1437  void update_create_cand(brain& brn, quanton& r_qua,
1438  neuromap& creat_cand, bool dbg_been);
1439 
1440  bool is_ne_dominated(brain& brn);
1441  void make_ne_dominated(brain& brn);
1442  bool is_ne_source();
1443 
1444  void set_cand_tk(ticket& n_tk);
1445 
1446  bool ck_wrt_qu0(bool just_upd);
1447 
1448  void update_ne_to_wrt_tk(brain& brn, ticket& wrt_tk);
1449 
1450  bool is_ne_to_wrt();
1451 
1452  void dbg_get_charges(row_long_t& chgs);
1453 
1454  bj_ostream& dbg_ne_print_col_cy_node(bj_ostream& os);
1455  bj_ostream& dbg_ne_print_col_cy_edge(bj_ostream& os, long& consec);
1456 
1457  bj_ostream& print_tees(bj_ostream& os);
1458  bj_ostream& print_neu_base(bj_ostream& os, bool from_pt, bool from_tee,
1459  bool sort_fib);
1460 
1461  bj_ostream& print_neuron(bj_ostream& os, bool from_pt = false);
1462 };
1463 
1464 //=============================================================================
1473 #define DECLARE_PS_FLAG_ALL_FUNCS(flag_num) \
1474  void reset_ps_all_note##flag_num##_n_tag##flag_num(brain& brn, \
1475  row<prop_signal>& rr_all, bool do_note, bool do_tag); \
1476  void set_ps_all_note##flag_num##_n_tag##flag_num(brain& brn, \
1477  row<prop_signal>& rr_all, bool do_note, bool do_tag); \
1478 \
1479 
1480 // end_of_define
1481 
1482 #define DEFINE_PS_FLAG_ALL_FUNCS(flag_num) \
1483  void reset_ps_all_note##flag_num##_n_tag##flag_num(brain& brn, \
1484  row<prop_signal>& rr_all, bool do_note, bool do_tag) \
1485  { \
1486  for(long ii = 0; ii < rr_all.size(); ii++){ \
1487  prop_signal& q_sig = rr_all[ii]; \
1488  quanton* qua = q_sig.ps_quanton; \
1489  neuron* neu = q_sig.ps_source; \
1490  if(do_note && (qua != NULL_PT) && qua->has_note##flag_num()){ \
1491  qua->reset_note##flag_num(brn); \
1492  } \
1493  if(do_tag && (neu != NULL_PT) && neu->has_tag##flag_num()){ \
1494  neu->reset_tag##flag_num(brn); \
1495  } \
1496  } \
1497  } \
1498  \
1499  void set_ps_all_note##flag_num##_n_tag##flag_num(brain& brn, \
1500  row<prop_signal>& rr_all, bool do_note, bool do_tag) \
1501  { \
1502  for(long ii = 0; ii < rr_all.size(); ii++){ \
1503  prop_signal& q_sig = rr_all[ii]; \
1504  quanton* qua = q_sig.ps_quanton; \
1505  neuron* neu = q_sig.ps_source; \
1506  if(do_note && (qua != NULL_PT) && ! qua->has_note##flag_num()){ \
1507  qua->set_note##flag_num(brn); \
1508  } \
1509  if(do_tag && (neu != NULL_PT) && ! neu->has_tag##flag_num()){ \
1510  neu->set_tag##flag_num(brn); \
1511  } \
1512  } \
1513  } \
1514 \
1515 
1516 // end_of_define
1517 
1518 DECLARE_PS_FLAG_ALL_FUNCS(0);
1519 DECLARE_PS_FLAG_ALL_FUNCS(1);
1520 DECLARE_PS_FLAG_ALL_FUNCS(2);
1521 DECLARE_PS_FLAG_ALL_FUNCS(3);
1522 DECLARE_PS_FLAG_ALL_FUNCS(4);
1523 DECLARE_PS_FLAG_ALL_FUNCS(5);
1524 
1526  public:
1527 
1528  quanton* ps_quanton;
1529  neuron* ps_source;
1530  long ps_tier;
1531 
1532  prop_signal(){
1533  init_prop_signal();
1534  }
1535 
1536  void init_prop_signal(quanton* qua = NULL_PT, neuron* the_src = NULL_PT,
1537  long the_tier = INVALID_TIER)
1538  {
1539  ps_quanton = qua;
1540  ps_source = the_src;
1541  ps_tier = the_tier;
1542  }
1543 
1544  void init_qua_signal(quanton& qua){
1545  init_prop_signal(&qua, qua.qu_source, qua.qu_tier);
1546  }
1547 
1548  ~prop_signal(){
1549  init_prop_signal();
1550  }
1551 
1552  bool is_ps_virgin(){
1553  bool c1 = (ps_quanton == NULL_PT);
1554  bool c2 = (ps_source == NULL_PT);
1555  bool c3 = (ps_tier == INVALID_TIER);
1556 
1557  return (c1 && c2 && c3);
1558  }
1559 
1560  bool is_ps_uniron(){
1561  if(ps_quanton == NULL_PT){
1562  return false;
1563  }
1564  bool is_u = ps_quanton->is_qu_uniron();
1565  BRAIN_CK(! is_u || (ps_source == NULL_PT));
1566  return is_u;
1567  }
1568 
1569  long get_level() const {
1570  long lv = INVALID_LEVEL;
1571  if(ps_quanton != NULL_PT){
1572  lv = ps_quanton->qlevel();
1573  }
1574  return lv;
1575  }
1576 
1577  void get_ps_cand_to_wrt(brain& brn, row_neuromap_t& to_wrt, long trace_idx);
1578 
1579  bool is_ps_of_qua(quanton& qua, neuromap* dbg_nmp = NULL_PT);
1580 
1581  brain* get_dbg_brn(){
1582  brain* the_brn = NULL;
1583  BRAIN_DBG(if(ps_quanton != NULL){ the_brn = ps_quanton->get_dbg_brn(); });
1584  return the_brn;
1585  }
1586 
1587  bool equal_ps_to(prop_signal& ps2){
1588  bool c1 = (ps_quanton == ps2.ps_quanton);
1589  bool c2 = (ps_source == ps2.ps_source);
1590  bool c3 = (ps_tier == ps2.ps_tier);
1591 
1592  bool eq = (c1 && c2 && c3);
1593  return eq;
1594  }
1595 
1596  solver* get_dbg_slv();
1597 
1598  bj_ostream& print_prop_signal(bj_ostream& os, bool from_pt = false);
1599 };
1600 
1601 inline
1602 void
1603 append_all_trace_quas(row<prop_signal>& trace, row_quanton_t& all_quas,
1604  long first_idx = 0, long last_idx = -1)
1605 {
1606  if(last_idx < 0){ last_idx = trace.size(); }
1607 
1608  BRAIN_CK(first_idx <= last_idx);
1609  BRAIN_CK((first_idx == trace.size()) || trace.is_valid_idx(first_idx));
1610  BRAIN_CK((last_idx == trace.size()) || trace.is_valid_idx(last_idx));
1611 
1612  for(long ii = first_idx; ii < last_idx; ii++){
1613  prop_signal& q_sig = trace[ii];
1614  BRAIN_CK(q_sig.ps_quanton != NULL_PT);
1615  all_quas.push(q_sig.ps_quanton);
1616  }
1617 }
1618 
1619 inline
1620 void
1621 append_all_trace_neus(row<prop_signal>& trace, row_neuron_t& all_neus)
1622 {
1623  for(long ii = 0; ii < trace.size(); ii++){
1624  prop_signal& q_sig = trace[ii];
1625  if((q_sig.ps_source != NULL_PT) && (q_sig.ps_source->ne_original)){
1626  all_neus.push(q_sig.ps_source);
1627  }
1628  }
1629 }
1630 
1631 //=============================================================================
1632 // reason
1633 
1634 class reason {
1635  public:
1636 
1637  ticket rs_tk;
1638  row_quanton_t rs_motives;
1639  quanton* rs_forced;
1640  long rs_target_level;
1641 
1642  reason(){
1643  init_reason();
1644  }
1645 
1646  ~reason(){
1647  init_reason();
1648  }
1649 
1650  void init_reason(){
1651  rs_tk.init_ticket();
1652  rs_motives.clear();
1653  rs_forced = NULL_PT;
1654  rs_target_level = INVALID_LEVEL;
1655  }
1656 
1657  brain* get_dbg_brn(){
1658  brain* the_brn = NULL;
1659  BRAIN_DBG(if(rs_forced != NULL){ the_brn = rs_forced->get_dbg_brn(); });
1660  return the_brn;
1661  }
1662 
1663  solver* get_dbg_slv();
1664 
1665  bool is_rs_virgin(){
1666  bool c1 = (rs_motives.is_empty());
1667  bool c2 = (rs_forced == NULL_PT);
1668  bool c3 = (rs_target_level == INVALID_LEVEL);
1669 
1670  bool is_vg = (c1 && c2 && c3);
1671 
1672  return is_vg;
1673  }
1674 
1675  void copy_to_rsn(reason& rsn2){
1676  BRAIN_CK(rsn2.is_rs_virgin());
1677  rs_motives.copy_to(rsn2.rs_motives);
1678 
1679  rsn2.rs_forced = rs_forced;
1680  rsn2.rs_target_level = rs_target_level;
1681  }
1682 
1683  bool equal_to_rsn(reason& rsn2);
1684 
1685  void dbg_set_with(brain& brn, notekeeper& dke, quanton& nxt_qua);
1686 
1687  bool is_dt_singleton(){
1688  return rs_motives.is_empty();
1689  }
1690 
1691  bool is_root_confl();
1692 
1693  long calc_target_tier(brain& brn);
1694 
1695  bj_ostream& print_reason(bj_ostream& os, bool from_pt = false){
1696  MARK_USED(from_pt);
1697  os << "dt={ mots=" << rs_motives;
1698  os << " qu:" << rs_forced;
1699  os << " lv:" << rs_target_level;
1700  os << "}";
1701  os.flush();
1702  return os;
1703  }
1704 };
1705 
1706 //=============================================================================
1715 class deduction {
1716  public:
1717  brain* dt_brn;
1718 
1719  neuron* dt_confl;
1720  neuromap* dt_last_found;
1721 
1722  row_quanton_t dt_first_causes;
1723  row<prop_signal> dt_all_noted;
1724  reason dt_rsn;
1725 
1726  bool dt_found_top;
1727 
1728  row_neuromap_t dt_all_to_wrt;
1729 
1730  // methods
1731 
1732  deduction(){
1733  init_deduction();
1734  }
1735 
1736  void init_deduction(brain* the_brn = NULL_PT){
1737  dt_brn = the_brn;
1738 
1739  dt_confl = NULL_PT;
1740  dt_last_found = NULL_PT;
1741 
1742  dt_first_causes.clear();
1743  dt_all_noted.clear(true, true);
1744  dt_rsn.init_reason();
1745 
1746  dt_found_top = false;
1747 
1748  dt_all_to_wrt.clear();
1749  }
1750 
1751  void reset_deduction(){
1752  init_deduction(dt_brn);
1753  }
1754 
1755  bool can_go_on();
1756 
1757  void update_all_to_wrt_for_proof();
1758 
1759  brain* get_dbg_brn(){
1760  return dt_brn;
1761  }
1762 
1763  solver* get_dbg_slv();
1764 
1765  brain& get_brn(){
1766  BRAIN_CK(dt_brn != NULL);
1767  return (*dt_brn);
1768  }
1769 
1770  bool ck_dbg_srcs(){
1771  bool c1 = (dt_confl == NULL_PT);
1772  bool c2 = (dt_last_found == NULL_PT);
1773  return (c1 != c2);
1774  }
1775 
1776  bool is_dt_virgin(){
1777  bool c1 = (dt_confl == NULL_PT);
1778  bool c2 = (dt_last_found == NULL_PT);
1779 
1780  bool c3 = (dt_first_causes.is_empty());
1781  bool c4 = (dt_all_noted.is_empty());
1782  bool c5 = (dt_rsn.is_rs_virgin());
1783 
1784  bool c6 = (dt_all_to_wrt.is_empty());
1785 
1786  return (c1 && c2 && c3 && c4 && c5 && c6);
1787  }
1788 
1789  bj_ostream& print_deduction(bj_ostream& os, bool from_pt = false){
1790  os << "[deduc:\n";
1791  os << dt_rsn;
1792  os << "\n]";
1793  os.flush();
1794  return os;
1795  }
1796 };
1797 
1798 //=============================================================================
1799 // cov_entry
1800 
1801 class cov_entry {
1802  public:
1803  neuromap* ce_nmp;
1804  neuron* ce_neu;
1805 
1806  // methods
1807 
1808  cov_entry(){
1809  init_cov_entry();
1810  }
1811 
1812  void init_cov_entry(neuromap* nmp = NULL_PT, neuron* neu = NULL_PT){
1813  ce_nmp = nmp;
1814  ce_neu = neu;
1815  }
1816 
1817  brain* get_dbg_brn(){
1818  return NULL;
1819  }
1820 
1821  solver* get_dbg_slv(){
1822  return NULL;
1823  }
1824 
1825  bool is_ce_virgin(){
1826  bool c1 = (ce_nmp == NULL_PT);
1827  bool c2 = (ce_neu == NULL_PT);
1828 
1829  return (c1 && c2);
1830  }
1831 
1832  bj_ostream& print_cov_entry(bj_ostream& os, bool from_pt = false);
1833 };
1834 
1835 //=============================================================================
1836 // qulayers
1837 
1838 class qulayers {
1839  public:
1840 
1841  brain* ql_brain;
1842  row_row_quanton_t ql_quas_by_layer;
1843  long ql_pop_layer;
1844 
1845  qulayers(brain* the_brn = NULL_PT)
1846  {
1847  init_qulayers(the_brn);
1848  }
1849 
1850  ~qulayers(){
1851  init_qulayers();
1852  }
1853 
1854  void init_qulayers(brain* the_brn = NULL_PT){
1855  ql_brain = the_brn;
1856  ql_quas_by_layer.clear(true, true);
1857  ql_pop_layer = INVALID_IDX;
1858  }
1859 
1860  brain& get_ql_brain();
1861 
1862  brain* get_dbg_brn(){
1863  brain* the_brn = NULL;
1864  BRAIN_DBG(the_brn = ql_brain);
1865  return the_brn;
1866  }
1867 
1868  solver* get_dbg_slv();
1869 
1870  quanton* pop_motive(){
1871  row_row_quanton_t& mots_by_ly = ql_quas_by_layer;
1872  BRAIN_CK(ck_pop_layer());
1873  if(! mots_by_ly.is_valid_idx(ql_pop_layer)){
1874  return NULL_PT;
1875  }
1876  row_quanton_t& mots = mots_by_ly[ql_pop_layer];
1877  if(mots.is_empty()){
1878  return NULL_PT;
1879  }
1880  quanton* qua = mots.pop();
1881  if(mots.is_empty()){
1882  update_pop_layer();
1883  }
1884  return qua;
1885  }
1886 
1887  void update_pop_layer(bool with_restart = false){
1888  ql_pop_layer = get_nxt_busy_layer(ql_pop_layer, with_restart);
1889  }
1890 
1891  quanton* last_qua_in_layer(long lyr){
1892  row_row_quanton_t& mots_by_ly = ql_quas_by_layer;
1893  if(! mots_by_ly.is_valid_idx(lyr)){
1894  return NULL_PT;
1895  }
1896  row_quanton_t& mots = mots_by_ly[lyr];
1897  if(mots.is_empty()){
1898  return NULL_PT;
1899  }
1900  quanton* qua = mots.last();
1901  return qua;
1902  }
1903 
1904  quanton* last_quanton(){
1905  BRAIN_CK(ck_pop_layer());
1906  quanton* qua = last_qua_in_layer(ql_pop_layer);
1907  return qua;
1908  }
1909 
1910  bool ck_pop_layer(){
1911  row_row_quanton_t& mots_by_ly = ql_quas_by_layer;
1912  if(mots_by_ly.is_empty()){
1913  BRAIN_CK(ql_pop_layer == INVALID_IDX);
1914  return true;
1915  }
1916  long p_ly = mots_by_ly.last_idx();
1917  while(mots_by_ly.is_valid_idx(p_ly) && (p_ly > ql_pop_layer)){
1918  BRAIN_CK(mots_by_ly[p_ly].is_empty());
1919  p_ly--;
1920  }
1921  BRAIN_CK(p_ly == ql_pop_layer);
1922  if(mots_by_ly.is_valid_idx(ql_pop_layer)){
1923  BRAIN_CK(! mots_by_ly[ql_pop_layer].is_empty());
1924 
1925  }
1926  return true;
1927  }
1928 
1929  bool has_motives(){
1930  BRAIN_CK(ck_pop_layer());
1931  if(ql_pop_layer == INVALID_IDX){
1932  return false;
1933  }
1934  return true;
1935  }
1936 
1937  long last_qlevel(){
1938  quanton* qua = last_quanton();
1939  if(qua == NULL_PT){
1940  return 0;
1941  }
1942  return qua->qlevel();
1943  }
1944 
1945  long last_qtier(){
1946  quanton* qua = last_quanton();
1947  if(qua == NULL_PT){
1948  return 0;
1949  }
1950  long qti = qua->qu_tier;
1951  BRAIN_CK(qti >= 0);
1952  return qti;
1953  }
1954 
1955  void add_motive(quanton& qua, long q_layer){
1956  row_quanton_t& layer_mots = get_qu_layer(q_layer);
1957  layer_mots.push(&qua);
1958  if(q_layer > ql_pop_layer){
1959  ql_pop_layer = q_layer;
1960  }
1961  }
1962 
1963  long get_nxt_busy_layer(long lyr, bool with_restart = false){
1964  row_row_quanton_t& mots_by_ly = ql_quas_by_layer;
1965  if(mots_by_ly.is_empty()){
1966  return INVALID_IDX;
1967  }
1968  long nxt_lyr = lyr;
1969  if(with_restart){
1970  nxt_lyr = mots_by_ly.last_idx();
1971  }
1972  while(mots_by_ly.is_valid_idx(nxt_lyr) && mots_by_ly[nxt_lyr].is_empty()){
1973  nxt_lyr--;
1974  }
1975  BRAIN_CK((nxt_lyr == INVALID_IDX) || mots_by_ly.is_valid_idx(nxt_lyr));
1976  return nxt_lyr;
1977  }
1978 
1979  void get_all_ordered_quantons(row_quanton_t& mots){
1980  mots.clear();
1981  for(long aa = 0; aa < ql_quas_by_layer.size(); aa++){
1982  row_quanton_t& lv_mots = ql_quas_by_layer[aa];
1983  lv_mots.append_to(mots);
1984  }
1985  }
1986 
1987  long get_tot_quantons(){
1988  long nmm = 0;
1989  for(long aa = 0; aa < ql_quas_by_layer.size(); aa++){
1990  row_quanton_t& lv_mots = ql_quas_by_layer[aa];
1991  nmm += lv_mots.size();
1992  }
1993  return nmm;
1994  }
1995 
1996  long get_all_sz(row_long_t& all_sz, long fst_ly){
1997  all_sz.clear();
1998  long nmm = 0;
1999  for(long aa = fst_ly; aa < ql_quas_by_layer.size(); aa++){
2000  row_quanton_t& lv_mots = ql_quas_by_layer[aa];
2001  long mot_sz = lv_mots.size();
2002 
2003  all_sz.push(mot_sz);
2004  nmm += mot_sz;
2005  }
2006  return nmm;
2007  }
2008 
2009  bool is_ql_empty(){
2010  return (get_tot_quantons() == 0);
2011  }
2012 
2013  row_quanton_t& get_qu_layer(long q_layer);
2014 
2015  bool ck_empty_layers(long q_layer){
2016  BRAIN_CK(q_layer >= 0);
2017  BRAIN_CK(! ql_quas_by_layer.is_empty());
2018 
2019  long l_idx = ql_quas_by_layer.last_idx();
2020  BRAIN_CK(q_layer <= l_idx);
2021  for(long ii = l_idx; ii > q_layer; ii--){
2022  BRAIN_CK(get_qu_layer(ii).is_empty());
2023  }
2024  return true;
2025  }
2026 
2027  void dbg_ql_init_all_cy_pos();
2028 
2029  bj_ostream& print_qulayers(bj_ostream& os, bool from_pt = false);
2030 
2031 };
2032 
2033 //=============================================================================
2042 #define DECLARE_NA_FLAG_FUNCS(flag_attr, flag_nam) \
2043  bool has_##flag_nam(){ \
2044  bool resp = get_flag(flag_attr, k_##flag_nam##_flag); \
2045  return resp; \
2046  } \
2047  \
2048  void reset_its_##flag_nam(brain& brn); \
2049  void reset_##flag_nam(brain& brn); \
2050  void set_##flag_nam(brain& brn); \
2051  \
2052 \
2053 
2054 // end_of_define
2055 
2056 #define DEFINE_NA_FLAG_FUNCS(flag_attr, flag_nam) \
2057  void neuromap::reset_its_##flag_nam(brain& brn){ \
2058  BRAIN_CK(has_##flag_nam()); \
2059  if(has_##flag_nam()){ \
2060  reset_##flag_nam(brn); \
2061  } \
2062  } \
2063  \
2064  void neuromap::reset_##flag_nam(brain& brn){ \
2065  BRAIN_CK(has_##flag_nam()); \
2066  reset_flag(flag_attr, k_##flag_nam##_flag); \
2067  brn.br_na_tot_##flag_nam--; \
2068  } \
2069  \
2070  void neuromap::set_##flag_nam(brain& brn){ \
2071  BRAIN_CK(! has_##flag_nam()); \
2072  set_flag(flag_attr, k_##flag_nam##_flag); \
2073  brn.br_na_tot_##flag_nam++; \
2074  } \
2075  \
2076 \
2077 
2078 // end_of_define
2079 
2080 #define DECLARE_NA_FLAG_ALL_FUNCS(flag_nam) \
2081  long reset_all_its_##flag_nam(brain& brn, row_neuromap_t& rr_all); \
2082  long reset_all_##flag_nam(brain& brn, row_neuromap_t& rr_all); \
2083  long set_all_##flag_nam(brain& brn, row_neuromap_t& rr_all); \
2084  long append_all_not_##flag_nam(brain& brn, row_neuromap_t& rr_src, \
2085  row_neuromap_t& rr_dst); \
2086  bool same_neuromaps_##flag_nam(brain& brn, row_neuromap_t& sup_ss, \
2087  row_neuromap_t& sub_ss); \
2088  bool all_neuromaps_have_##flag_nam(row_neuromap_t& rr1, bool val = true); \
2089 \
2090 
2091 // end_of_define
2092 
2093 #define DEFINE_NA_FLAG_ALL_FUNCS(flag_nam) \
2094  long reset_all_its_##flag_nam(brain& brn, row_neuromap_t& rr_all){ \
2095  long num_nmp = 0; \
2096  for(long aa = 0; aa < rr_all.size(); aa++){ \
2097  BRAIN_CK(rr_all[aa] != NULL_PT); \
2098  neuromap& nmp = *(rr_all[aa]); \
2099  if(nmp.has_##flag_nam()){ \
2100  nmp.reset_its_##flag_nam(brn); \
2101  num_nmp++; \
2102  } \
2103  } \
2104  return num_nmp; \
2105  } \
2106  \
2107  long reset_all_##flag_nam(brain& brn, row_neuromap_t& rr_all){ \
2108  long num_nmp_mod = 0; \
2109  for(long aa = 0; aa < rr_all.size(); aa++){ \
2110  BRAIN_CK(rr_all[aa] != NULL_PT); \
2111  neuromap& nmp = *(rr_all[aa]); \
2112  if(nmp.has_##flag_nam()){ \
2113  nmp.reset_##flag_nam(brn); \
2114  num_nmp_mod++; \
2115  } \
2116  } \
2117  return num_nmp_mod; \
2118  } \
2119  \
2120  long set_all_##flag_nam(brain& brn, row_neuromap_t& rr_all){ \
2121  long num_nmp_mod = 0; \
2122  for(long aa = 0; aa < rr_all.size(); aa++){ \
2123  neuromap& nmp = *(rr_all[aa]); \
2124  if(! nmp.has_##flag_nam()){ \
2125  nmp.set_##flag_nam(brn); \
2126  num_nmp_mod++; \
2127  } \
2128  } \
2129  return num_nmp_mod; \
2130  } \
2131  \
2132  long append_all_not_##flag_nam(brain& brn, row_neuromap_t& rr_src, \
2133  row_neuromap_t& rr_dst) \
2134  { \
2135  long num_nmp_app = 0; \
2136  for(long aa = 0; aa < rr_src.size(); aa++){ \
2137  neuromap& nmp = *(rr_src[aa]); \
2138  if(! nmp.has_##flag_nam()){ \
2139  nmp.set_##flag_nam(brn); \
2140  rr_dst.push(&nmp); \
2141  num_nmp_app++; \
2142  } \
2143  } \
2144  return num_nmp_app; \
2145  } \
2146  \
2147  bool same_neuromaps_##flag_nam(brain& brn, row_neuromap_t& sup_ss, \
2148  row_neuromap_t& sub_ss) \
2149  { \
2150  BRAIN_CK(brn.br_na_tot_##flag_nam == 0); \
2151  set_all_##flag_nam(brn, sup_ss); \
2152  bool sm_nmps = true; \
2153  for(long aa = 0; aa < sub_ss.size(); aa++){ \
2154  neuromap& nmp = *(sub_ss[aa]); \
2155  if(! nmp.has_##flag_nam()){ \
2156  sm_nmps = false; \
2157  } \
2158  } \
2159  reset_all_##flag_nam(brn, sup_ss); \
2160  BRAIN_CK(brn.br_na_tot_##flag_nam == 0); \
2161  return sm_nmps; \
2162  } \
2163  \
2164  bool all_neuromaps_have_##flag_nam(row_neuromap_t& rr1, bool val) \
2165  { \
2166  bool all_hv = true; \
2167  for(long aa = 0; aa < rr1.size(); aa++){ \
2168  neuromap& nmp = *(rr1[aa]); \
2169  if(nmp.has_##flag_nam() != val){ \
2170  all_hv = false; \
2171  break; \
2172  } \
2173  } \
2174  return all_hv; \
2175  } \
2176  \
2177 \
2178 
2179 // end_of_define
2180 
2181 #define k_na0_flag k_flag0
2182 #define k_na1_flag k_flag1
2183 #define k_na2_flag k_flag2
2184 #define k_na3_flag k_flag3
2185 #define k_na4_flag k_flag4
2186 #define k_na5_flag k_flag5
2187 
2188 DECLARE_NA_FLAG_ALL_FUNCS(na0);
2189 DECLARE_NA_FLAG_ALL_FUNCS(na1);
2190 
2191 class neuromap {
2192  public:
2193  static
2194  char* CL_NAME;
2195 
2196  virtual
2197  char* get_cls_name(){
2198  return neuromap::CL_NAME;
2199  }
2200 
2201  long na_index; //idx in br_neuromaps
2202 
2203  brain* na_brn;
2204  t_1byte na_flags;
2205  bool na_is_head;
2206  bool na_is_min_simple;
2207 
2208  round_counter_t na_orig_rnd;
2209 
2210  long na_orig_lv;
2211  long na_orig_ti;
2212  quanton* na_orig_cho;
2213  cy_quk_t na_orig_cy_ki;
2214 
2215  neuromap* na_nxt_no_mono;
2216  neuromap* na_submap;
2217  long na_num_submap;
2218 
2219  row<prop_signal> na_propag; // all psigs propagated
2220  row<cov_entry> na_all_centry;
2221  row_neuron_t na_all_cov;
2222 
2223  bool na_upd_to_write;
2224  row_neuron_t na_to_write;
2225  row_neuron_t na_not_to_write;
2226 
2227  //row_neuron_t na_all_found;
2228  coloring na_found_col;
2229 
2230  long na_guide_tot_vars;
2231  coloring na_guide_col;
2232  coloring na_pend_col;
2233 
2234  mem_find_t na_found_in_skl;
2235 
2236  // candidate system
2237  ticket na_candidate_tk;
2238  quanton* na_candidate_qua;
2239 
2240  // proof wrt system
2241  long na_to_wrt_trace_idx;
2242  ch_string na_tauto_pth;
2243  bool na_tauto_oper_ok;
2244  coloring na_tauto_col;
2245 
2246  BRAIN_DBG(
2247  ticket na_dbg_candidate_tk;
2248  bool na_dbg_cand_sys;
2249  bool na_dbg_creating;
2250 
2251  mem_op_t na_dbg_nmp_mem_op;
2252  ch_string na_dbg_tauto_min_sha_str;
2253  ch_string na_dbg_tauto_sha_str;
2254  ch_string na_dbg_guide_sha_str;
2255  ch_string na_dbg_quick_sha_str;
2256  ch_string na_dbg_diff_min_sha_str;
2257 
2258  ch_string na_dbg_is_no_abort_full_wrt_pth;
2259  bool na_dbg_is_no_abort_full_nmp;
2260 
2261  coloring na_dbg_tauto_col;
2262  row_long_t na_dbg_phi_id;
2263 
2264  neuromap* na_dbg_real_cand;
2265  );
2266 
2267  neuromap(brain* pt_brn = NULL) {
2268  na_index = INVALID_IDX;
2269  init_neuromap(pt_brn);
2270  }
2271 
2272  ~neuromap(){
2273  init_neuromap();
2274  }
2275 
2276  void init_neuromap(brain* pt_brn = NULL){
2277  na_brn = pt_brn;
2278  na_flags = 0;
2279  na_is_head = false;
2280  na_is_min_simple = false;
2281 
2282  na_orig_rnd = INVALID_ROUND;
2283 
2284  na_orig_lv = INVALID_LEVEL;
2285  na_orig_ti = INVALID_TIER;
2286  na_orig_cho = NULL_PT;
2287  na_orig_cy_ki = cq_invalid;
2288 
2289  na_nxt_no_mono = this;
2290  na_submap = NULL_PT;
2291 
2292  na_num_submap = INVALID_NUM_SUB;
2293 
2294  na_propag.clear(true, true);
2295  na_all_centry.clear(true, true);
2296  na_all_cov.clear();
2297 
2298  na_upd_to_write = false;
2299  na_to_write.clear();
2300  na_not_to_write.clear();
2301 
2302  //na_all_found.clear();
2303  na_found_col.init_coloring();
2304 
2305  na_guide_tot_vars = 0;
2306  na_guide_col.init_coloring();
2307  na_pend_col.init_coloring();
2308 
2309  na_found_in_skl = mf_invalid;
2310 
2311  na_candidate_tk.init_ticket();
2312  na_candidate_qua = NULL_PT;
2313 
2314  na_to_wrt_trace_idx = INVALID_IDX;
2315  na_tauto_pth = INVALID_PATH;
2316  na_tauto_oper_ok = false;
2317  na_tauto_col.init_coloring();
2318 
2319  DBG(
2320  na_dbg_candidate_tk.init_ticket();
2321  na_dbg_cand_sys = false;
2322  na_dbg_creating = false;
2323 
2324  na_dbg_nmp_mem_op = mo_invalid;
2325  na_dbg_tauto_min_sha_str = INVALID_MINSHA;
2326  na_dbg_tauto_sha_str = INVALID_SHA;
2327  na_dbg_guide_sha_str = INVALID_SHA;
2328  na_dbg_quick_sha_str = INVALID_SHA;
2329 
2330  na_dbg_is_no_abort_full_wrt_pth = INVALID_PATH;
2331  na_dbg_is_no_abort_full_nmp = false;
2332 
2333  na_dbg_tauto_col.init_coloring();
2334  na_dbg_phi_id.clear();
2335 
2336  na_dbg_real_cand = NULL_PT;
2337  );
2338  }
2339 
2340  bool is_na_virgin(){
2341  bool c1 = (na_brn == NULL_PT);
2342  bool c2 = (na_is_head == false);
2343  bool c3 = (na_orig_lv == INVALID_LEVEL);
2344  bool c4 = (na_orig_cho == NULL_PT);
2345  bool c5 = (na_submap == NULL_PT);
2346 
2347  return (c1 && c2 && c3 && c4 && c5);
2348  }
2349 
2350  void full_release();
2351  void release_candidate();
2352 
2353  brain& get_brn(){
2354  BRAIN_CK(na_brn != NULL);
2355  return (*na_brn);
2356  }
2357 
2358  brain* get_dbg_brn(){
2359  brain* the_brn = NULL;
2360  BRAIN_DBG(the_brn = na_brn);
2361  return the_brn;
2362  }
2363 
2364  solver* get_dbg_slv();
2365 
2366  DECLARE_NA_FLAG_FUNCS(na_flags, na0);
2367  DECLARE_NA_FLAG_FUNCS(na_flags, na1);
2368  DECLARE_NA_FLAG_FUNCS(na_flags, na2);
2369  DECLARE_NA_FLAG_FUNCS(na_flags, na3);
2370  DECLARE_NA_FLAG_FUNCS(na_flags, na4);
2371  DECLARE_NA_FLAG_FUNCS(na_flags, na5);
2372 
2373  bool is_na_mono(){
2374  BRAIN_CK(na_nxt_no_mono != NULL_PT);
2375  bool is_mn = (na_nxt_no_mono != this);
2376  return is_mn;
2377  }
2378 
2379  bool ck_rel_idx();
2380 
2381  bool has_stab_guide();
2382 
2383  long get_cand_lv(){
2384  return na_candidate_tk.tk_level;
2385  }
2386 
2387  void map_get_all_propag_ps(row<prop_signal>& all_ps);
2388  void map_rec_get_all_propag_ps(row<prop_signal>& all_ps);
2389  bool map_dbg_ck_ord(row<prop_signal>& all_ps);
2390 
2391  void map_get_all_cov_neus(row_neuron_t& all_neus, bool with_clear, bool skip_tail,
2392  mem_op_t mm);
2393 
2394  void map_get_all_quas(row_quanton_t& all_quas);
2395  void map_get_all_neus(row_neuron_t& all_neus, bool only_found = false,
2396  mem_op_t mm = mo_invalid);
2397 
2398  bool map_ck_all_upper_quas(row_quanton_t& all_upper_quas);
2399  void map_get_all_upper_quas(row_quanton_t& all_upper_quas);
2400 
2401  quanton* map_choose_propag_qua();
2402  quanton* map_choose_quanton();
2403 
2404  bool map_ck_all_quas();
2405  bool dbg_ck_all_neus();
2406 
2407  bool has_submap(){
2408  bool h_s = (na_submap != NULL_PT);
2409  return h_s;
2410  }
2411 
2412  ch_string map_dbg_oper_str(mem_op_t mm){
2413  if(mm == mo_find){ return "FIND"; }
2414  BRAIN_CK(mm == mo_save);
2415  return "SAVE";
2416  }
2417 
2418  bool map_ck_contained_in(coloring& colr, dbg_call_id dbg_id);
2419  void map_dbg_print(bj_ostream& os, mem_op_t mm);
2420 
2421  ch_string map_dbg_get_phi_ids_str();
2422 
2423  bool map_find();
2424  bool map_write(bool force_full = false);
2425 
2426  neuromap& map_to_write();
2427 
2428  bool map_oper(mem_op_t mm);
2429  bool map_prepare_mem_oper(mem_op_t mm);
2430  void map_prepare_wrt_cnfs(mem_op_t mm, ch_string quick_find_ref, row_str_t& dbg_shas);
2431  void map_set_stab_guide();
2432  void map_stab_guide_col();
2433  void map_init_stab_guide();
2434 
2435  //void map_get_ini_guide_col(coloring& clr);
2436 
2437  void map_get_initial_guide_coloring(coloring& clr);
2438  void map_get_initial_tauto_coloring(coloring& prv_clr, coloring& tauto_clr,
2439  mem_op_t mm);
2440 
2441  void map_get_simple_coloring(coloring& clr, mem_op_t mm = mo_invalid);
2442  void map_get_simple_guide_col(coloring& clr);
2443 
2444  bool nmp_is_min_simple();
2445  void nmp_set_min_simple();
2446 
2447  neuromap* nmp_init_with(quanton& qua);
2448  bool nmp_is_cand(bool ck_chg = true, dbg_call_id dbg_id = dbg_call_1);
2449  void nmp_fill_upper_covs();
2450  void nmp_fill_all_upper_covs(long dbg_idx = 0);
2451 
2452  void nmp_set_quas_cand_tk();
2453  void nmp_set_neus_cand_tk();
2454  void nmp_set_all_cand_tk();
2455 
2456  void nmp_set_all_num_sub();
2457 
2458  void nmp_update_to_write(row_neuron_t& upd_from, ticket& nmp_wrt_tk);
2459  void nmp_update_all_to_write(ticket& nmp_wrt_tk);
2460  void nmp_reset_write();
2461 
2462  void nmp_add_to_write(row_neuromap_t& to_wrt, long trace_idx = INVALID_IDX);
2463 
2464  bool dbg_has_simple_coloring_quas(coloring& clr);
2465  void dbg_prt_simple_coloring(bj_ostream& os);
2466 
2467  void map_dbg_set_cy_maps();
2468  void map_dbg_reset_cy_maps();
2469  void map_dbg_get_cy_coloring(coloring& clr);
2470  ch_string map_dbg_html_data_str(ch_string msg);
2471  void map_dbg_update_html_file(ch_string msg);
2472  void map_dbg_all_sub_update_html_file(ch_string msg);
2473 
2474  bool dbg_ck_cand(bool nw_cands);
2475  ch_string dbg_na_id();
2476  bool dbg_is_watched();
2477  long dbg_get_depth_in(neuromap& hd_nmp);
2478 
2479  bj_ostream& print_cand_id(bj_ostream& os);
2480  bj_ostream& print_nmp_hd(bj_ostream& os);
2481 
2482  bj_ostream& print_neuromap(bj_ostream& os, bool from_pt = false);
2483 
2484  bj_ostream& print_all_subnmp(bj_ostream& os, bool only_pts = false);
2485  bj_ostream& print_subnmp(bj_ostream& os, bool only_pts = false);
2486 };
2487 
2488 inline
2489 neuromap&
2490 as_neuromap(binder* bdr){
2491  BRAIN_CK(bdr != NULL_PT);
2492  neuromap& nmp = rcp_as<neuromap>(bdr);
2493  return nmp;
2494 }
2495 
2496 //=============================================================================
2497 // notekeeper
2498 
2499 class notekeeper {
2500  public:
2501  typedef bool (quanton::*has_fn_pt_t)();
2502  typedef void (quanton::*do_fn_pt_t)(brain& brn);
2503  typedef long (*do_row_fn_pt_t)(brain& brn, row_quanton_t& quans);
2504  typedef long (*do_append_fn_pt_t)(brain& brn, row_quanton_t& src,
2505  row_quanton_t& dst);
2506  typedef bool (*do_same_fn_pt_t)(brain& brn, row_quanton_t& rr1,
2507  row_quanton_t& rr2, row_quanton_t* all_bad_qua);
2508 
2509  brain* dk_brain;
2510 
2511  long* dk_note_counter;
2512  has_fn_pt_t dk_has_note_fn;
2513  do_fn_pt_t dk_set_note_fn;
2514  do_fn_pt_t dk_reset_note_fn;
2515  do_row_fn_pt_t dk_set_all_fn;
2516  do_row_fn_pt_t dk_reset_all_fn;
2517  do_append_fn_pt_t dk_append_not_noted_fn;
2518  do_same_fn_pt_t dk_same_quas_noted_fn;
2519 
2520  long dk_note_layer;
2521  long dk_tot_noted;
2522  long dk_num_noted_in_layer;
2523 
2524  qulayers dk_quas_lyrs; // layers are levels
2525 
2526  notekeeper(brain* brn = NULL_PT, long tg_lv = INVALID_LEVEL)
2527  {
2528  init_notekeeper(brn, tg_lv);
2529  }
2530 
2531  ~notekeeper(){
2532  init_notekeeper();
2533  }
2534 
2535  void init_notekeeper(brain* brn = NULL_PT, long tg_lv = INVALID_LEVEL)
2536  {
2537  dk_brain = brn;
2538 
2539  dk_note_counter = NULL_PT;
2540  dk_has_note_fn = NULL_PT;
2541  dk_set_note_fn = NULL_PT;
2542  dk_reset_note_fn = NULL_PT;
2543  dk_set_all_fn = NULL_PT;
2544  dk_reset_all_fn = NULL_PT;
2545  dk_append_not_noted_fn = NULL_PT;
2546  dk_same_quas_noted_fn = NULL_PT;
2547 
2548  init_notes(tg_lv);
2549  }
2550 
2551  long nk_get_counter(){
2552  BRAIN_CK(dk_note_counter != NULL_PT);
2553  return *dk_note_counter;
2554  }
2555 
2556  bool nk_has_note(quanton& qua){
2557  BRAIN_CK(dk_has_note_fn != NULL_PT);
2558  return (qua.*dk_has_note_fn)();
2559  }
2560 
2561  void nk_set_note(quanton& qua){
2562  BRAIN_CK(dk_set_note_fn != NULL_PT);
2563  brain& brn = get_dk_brain();
2564  (qua.*dk_set_note_fn)(brn);
2565  }
2566 
2567  void nk_reset_note(quanton& qua){
2568  BRAIN_CK(dk_reset_note_fn != NULL_PT);
2569  brain& brn = get_dk_brain();
2570  (qua.*dk_reset_note_fn)(brn);
2571  }
2572 
2573  long nk_set_all_notes(row_quanton_t& quans){
2574  BRAIN_CK(dk_set_all_fn != NULL_PT);
2575  brain& brn = get_dk_brain();
2576  return (*dk_set_all_fn)(brn, quans);
2577  }
2578 
2579  long nk_reset_all_notes(row_quanton_t& quans){
2580  BRAIN_CK(dk_reset_all_fn != NULL_PT);
2581  brain& brn = get_dk_brain();
2582  return (*dk_reset_all_fn)(brn, quans);
2583  }
2584 
2585  long nk_append_not_noted(row_quanton_t& src, row_quanton_t& dst){
2586  BRAIN_CK(dk_append_not_noted_fn != NULL_PT);
2587  brain& brn = get_dk_brain();
2588  return (*dk_append_not_noted_fn)(brn, src, dst);
2589  }
2590 
2591  bool nk_same_quas_noted(row_quanton_t& rr1, row_quanton_t& rr2){
2592  BRAIN_CK(dk_same_quas_noted_fn != NULL_PT);
2593  brain& brn = get_dk_brain();
2594  return (*dk_same_quas_noted_fn)(brn, rr1, rr2, NULL_PT);
2595  }
2596 
2597  brain* get_dbg_brn(){
2598  brain* the_brn = NULL;
2599  BRAIN_DBG(the_brn = dk_brain);
2600  return the_brn;
2601  }
2602 
2603  solver* get_dbg_slv();
2604 
2605  void init_funcs(long* cnter, has_fn_pt_t has_note, do_fn_pt_t set_note,
2606  do_fn_pt_t reset_note, do_row_fn_pt_t set_all,
2607  do_row_fn_pt_t reset_all_its, do_append_fn_pt_t append_not_noted,
2608  do_same_fn_pt_t same_quas_noted)
2609  {
2610  dk_note_counter = cnter;
2611  dk_has_note_fn = has_note;
2612  dk_set_note_fn = set_note;
2613  dk_reset_note_fn = reset_note;
2614  dk_set_all_fn = set_all;
2615  dk_reset_all_fn = reset_all_its;
2616  dk_append_not_noted_fn = append_not_noted;
2617  dk_same_quas_noted_fn = same_quas_noted;
2618  }
2619 
2620  void init_notes(long tg_lv){
2621  dk_note_layer = tg_lv;
2622  dk_tot_noted = 0;
2623  dk_num_noted_in_layer = 0;
2624 
2625  dk_quas_lyrs.init_qulayers(dk_brain);
2626  }
2627 
2628  bool ck_funcs(){
2629  bool c0 = (dk_note_counter != NULL_PT);
2630  bool c1 = (dk_has_note_fn != NULL_PT);
2631  bool c2 = (dk_set_note_fn != NULL_PT);
2632  bool c3 = (dk_reset_note_fn != NULL_PT);
2633  bool c4 = (dk_set_all_fn != NULL_PT);
2634  bool c5 = (dk_reset_all_fn != NULL_PT);
2635  return (c0 && c1 && c2 && c3 && c4 && c5);
2636  }
2637 
2638  brain& get_dk_brain();
2639 
2640  long clear_all_quantons(long lim_lv = INVALID_LEVEL, bool reset_notes = true);
2641 
2642  bool ck_notes(row_quanton_t& mots, bool val){
2643 
2644  BRAIN_CK(ck_funcs());
2645  for(long ii = 0; ii < mots.size(); ii++){
2646 
2647  quanton* qua = mots[ii];
2648  MARK_USED(qua);
2649  BRAIN_CK(qua != NULL_PT);
2650 
2651  bool c1 = (qua->*dk_has_note_fn)();
2652  MARK_USED(c1);
2653  BRAIN_CK(c1 == val);
2654  }
2655  return true;
2656  }
2657 
2658  void dec_notes(){
2659  BRAIN_CK(dk_tot_noted > 0);
2660  BRAIN_CK(dk_num_noted_in_layer > 0);
2661 
2662  dk_tot_noted--;
2663  BRAIN_CK(dk_tot_noted >= 0);
2664 
2665  dk_num_noted_in_layer--;
2666  if(dk_num_noted_in_layer == 0){
2667  row_quanton_t& mots = dk_quas_lyrs.get_qu_layer(dk_note_layer);
2668  BRAIN_CK(ck_notes(mots, false));
2669  mots.clear();
2670  }
2671 
2672  BRAIN_CK(dk_num_noted_in_layer >= 0);
2673  }
2674 
2675  void update_notes_layer(long q_layer){
2676  BRAIN_CK(q_layer >= 0);
2677  BRAIN_CK(q_layer <= dk_note_layer);
2678  if(q_layer < dk_note_layer){
2679  row_quanton_t& nxt_mots = dk_quas_lyrs.get_qu_layer(q_layer);
2680 
2681  BRAIN_CK(dk_num_noted_in_layer == 0);
2682  BRAIN_CK(q_layer < dk_note_layer);
2683  BRAIN_CK(dk_quas_lyrs.ck_empty_layers(q_layer));
2684  BRAIN_CK(ck_notes(nxt_mots, true));
2685 
2686  dk_note_layer = q_layer;
2687  dk_num_noted_in_layer = nxt_mots.size();
2688  }
2689  BRAIN_CK(q_layer == dk_note_layer);
2690  }
2691 
2692  void set_motive_notes(row_quanton_t& rr_qua, long from, long until);
2693 
2694 };
2695 
2696 //=============================================================================
2697 // qlayers_ref
2698 
2699 class qlayers_ref {
2700  public:
2701  qulayers* nr_quas_lyrs;
2702  long nr_curr_qua_layer;
2703  long nr_curr_qua_idx;
2704 
2705  qlayers_ref(qulayers* the_kpr = NULL_PT){
2706  init_qlayers_ref(the_kpr);
2707  }
2708 
2709  ~qlayers_ref(){
2710  init_qlayers_ref();
2711  }
2712 
2713  void init_qlayers_ref(qulayers* the_kpr = NULL_PT){
2714  nr_quas_lyrs = the_kpr;
2715  nr_curr_qua_layer = INVALID_IDX;
2716  nr_curr_qua_idx = INVALID_IDX;
2717  }
2718 
2719  bool is_nr_virgin(){
2720  bool c1 = (nr_quas_lyrs == NULL_PT);
2721  bool c2 = (nr_curr_qua_layer == INVALID_IDX);
2722  bool c3 = (nr_curr_qua_idx == INVALID_IDX);
2723 
2724  return (c1 && c2 && c3);
2725  }
2726 
2727  qulayers& get_keeper(){
2728  BRAIN_CK(nr_quas_lyrs != NULL_PT);
2729  return *nr_quas_lyrs;
2730  }
2731 
2732  void update_curr_quanton(bool reset){
2733  if(nr_quas_lyrs == NULL_PT){
2734  return;
2735  }
2736  BRAIN_CK(nr_quas_lyrs != NULL_PT);
2737  nr_curr_qua_layer = nr_quas_lyrs->get_nxt_busy_layer(nr_curr_qua_layer, reset);
2738  nr_curr_qua_idx = INVALID_IDX;
2739 
2740  if(nr_curr_qua_layer != INVALID_IDX){
2741  nr_curr_qua_idx = nr_quas_lyrs->ql_quas_by_layer[nr_curr_qua_layer].last_idx();
2742  }
2743  }
2744 
2745  void reset_curr_quanton(long nxt_lyr = INVALID_IDX){
2746  if(nr_quas_lyrs == NULL_PT){
2747  return;
2748  }
2749  BRAIN_CK(nr_quas_lyrs != NULL_PT);
2750  nr_curr_qua_idx = INVALID_IDX;
2751  nr_curr_qua_layer = nxt_lyr;
2752  bool with_restart = (nr_curr_qua_layer == INVALID_IDX);
2753  update_curr_quanton(with_restart);
2754  }
2755 
2756  quanton* dec_curr_quanton(){
2757  if(nr_quas_lyrs == NULL_PT){
2758  return NULL_PT;
2759  }
2760  BRAIN_CK(nr_quas_lyrs != NULL_PT);
2761  BRAIN_CK(nr_curr_qua_idx >= INVALID_IDX);
2762  //if(nr_curr_qua_idx == INVALID_IDX){
2763  if(nr_curr_qua_idx < 0){
2764  return NULL_PT;
2765  }
2766  nr_curr_qua_idx--;
2767  //if(nr_curr_qua_idx == INVALID_IDX){
2768  if(nr_curr_qua_idx < 0){
2769  BRAIN_CK(nr_curr_qua_layer >= 0);
2770  nr_curr_qua_layer--;
2771  //if(nr_curr_qua_layer == INVALID_IDX){
2772  if(nr_curr_qua_layer < 0){
2773  return NULL_PT;
2774  }
2775  update_curr_quanton(false);
2776  }
2777 
2778  quanton* qua = get_curr_quanton();
2779  return qua;
2780  }
2781 
2782  quanton* get_curr_quanton(){
2783  if(nr_quas_lyrs == NULL_PT){
2784  return NULL_PT;
2785  }
2786  //if(nr_curr_qua_idx == INVALID_IDX){
2787  if(nr_curr_qua_idx < 0){
2788  return NULL_PT;
2789  }
2790  BRAIN_CK(nr_quas_lyrs != NULL_PT);
2791  BRAIN_CK(nr_quas_lyrs->ql_quas_by_layer.is_valid_idx(nr_curr_qua_layer));
2792  row_quanton_t& lv_mots = nr_quas_lyrs->ql_quas_by_layer[nr_curr_qua_layer];
2793 
2794  BRAIN_CK(lv_mots.is_valid_idx(nr_curr_qua_idx));
2795 
2796  quanton* nxt_qua = lv_mots[nr_curr_qua_idx];
2797  BRAIN_CK(nxt_qua != NULL_PT);
2798  return nxt_qua;
2799  }
2800 
2801  long get_curr_qlevel(){
2802  long lv = INVALID_LEVEL;
2803  quanton* qua = get_curr_quanton();
2804  if(qua != NULL_PT){
2805  lv = qua->qlevel();
2806  }
2807  return lv;
2808  }
2809 
2810  bool has_curr_quanton(){
2811  return (get_curr_quanton() != NULL_PT);
2812  }
2813 
2814 };
2815 
2816 //=============================================================================
2823 class deducer {
2824  public:
2825 
2826  brain* de_brain;
2827 
2828  notekeeper de_nkpr;
2829  qlayers_ref de_ref; // layers are tiers (inited with charge_trail)
2830  row<prop_signal> de_all_noted;
2831 
2832  row<prop_signal> de_all_confl;
2833  prop_signal de_next_bk_psig;
2834 
2835  deducer(){
2836  init_deducer();
2837  }
2838 
2839  ~deducer(){
2840  init_deducer();
2841  }
2842 
2843  void init_deducer(brain* brn = NULL_PT);
2844  void reset_deduc();
2845 
2846  static
2847  void init_nk_with_note0(notekeeper& nkpr, brain& brn);
2848 
2849  static
2850  void init_nk_with_note5(notekeeper& nkpr, brain& brn);
2851 
2852  brain* get_dbg_brn(){
2853  brain* the_brn = NULL;
2854  BRAIN_DBG(the_brn = de_brain);
2855  return the_brn;
2856  }
2857 
2858  solver* get_dbg_slv();
2859 
2860  brain& get_de_brain();
2861 
2862  bool is_end_of_rsn(bool in_roo);
2863 
2864  //bool is_de_end_of_neuromap();
2865 
2866  bool ck_end_of_lrn_nmp();
2867 
2868  void fill_rsn(reason& rsn);
2869  void find_all_to_write(row_neuromap_t& to_wrt, reason& dbg_rsn);
2870 
2871  bool ck_deduc_init(long deduc_lv, bool full_ck, row_quanton_t& causes);
2872 
2873  void set_conflicts(row<prop_signal>& all_confl){
2874  de_all_confl.clear(true, true);
2875  all_confl.copy_to(de_all_confl);
2876  BRAIN_CK(! de_all_confl.is_empty());
2877  }
2878 
2879  prop_signal& get_first_conflict(){
2880  BRAIN_CK(! de_all_confl.is_empty());
2881  prop_signal& cfl = de_all_confl.first();
2882  return cfl;
2883  }
2884 
2885  void get_first_causes(deduction& dct);
2886 
2887  long deduce_init(row_quanton_t& causes, long max_lv = INVALID_LEVEL);
2888  void deduce(deduction& dct, long max_lv = INVALID_LEVEL);
2889 
2890  void set_nxt_noted(quanton* nxt_qua);
2891 
2892  bool find_next_source();
2893  bool find_next_noted();
2894  void set_notes_of(row_quanton_t& causes, bool is_first);
2895 
2896  bj_ostream& print_deducer(bj_ostream& os, bool from_pt = false);
2897 
2898 };
2899 
2900 //=============================================================================
2909 class leveldat {
2910  public:
2911 
2912  brain* ld_brn;
2913 
2914  long ld_idx;
2915  row_neuron_t ld_learned;
2916  quanton* ld_chosen;
2917 
2918  long ld_bak_mono_idx;
2919 
2920  leveldat(brain* pt_brn = NULL) {
2921  init_leveldat(pt_brn);
2922  }
2923 
2924  ~leveldat(){
2925  init_leveldat();
2926  }
2927 
2928  void init_leveldat(brain* pt_brn = NULL){
2929  ld_brn = pt_brn;
2930  ld_idx = INVALID_IDX;
2931  ld_learned.clear();
2932  ld_chosen = NULL_PT;
2933 
2934  ld_bak_mono_idx = INVALID_IDX;
2935  }
2936 
2937  brain* get_dbg_brn(){
2938  brain* the_brn = NULL;
2939  BRAIN_DBG(the_brn = ld_brn);
2940  return the_brn;
2941  }
2942 
2943  solver* get_dbg_slv();
2944 
2945  bool ck_maps_active();
2946  void let_maps_go(brain& brn);
2947 
2948  static
2949  leveldat* create_leveldat(brain* pt_brn = NULL){
2950  leveldat* lv = tpl_malloc<leveldat>();
2951  new (lv) leveldat();
2952  lv->ld_brn = pt_brn;
2953  return lv;
2954  }
2955 
2956  static
2957  void release_leveldat(leveldat* lv){
2958  BRAIN_CK(lv != NULL_PT);
2959  lv->~leveldat();
2960  tpl_free<leveldat>(lv);
2961  }
2962 
2963  bool has_learned(){
2964  return ! ld_learned.is_empty();
2965  }
2966 
2967  bool is_ld_mono(){
2968  BRAIN_CK(ld_chosen != NULL_PT);
2969  bool is_mn = (ld_chosen->is_opp_mono());
2970  return is_mn;
2971  }
2972 
2973  long ld_tier(){
2974  BRAIN_CK(ld_chosen != NULL_PT);
2975  BRAIN_CK(ld_chosen->has_charge());
2976  long qti = ld_chosen->qu_tier;
2977  return qti;
2978  }
2979 
2980  long num_learned(){
2981  return ld_learned.size();
2982  }
2983 
2984  void release_learned(brain& brn);
2985 
2986  void reset_monos(brain& brn);
2987 
2988  bj_ostream& print_leveldat(bj_ostream& os, bool from_pt = false);
2989 };
2990 
2991 //=================================================================
2992 // test_info
2993 
2994 class test_info {
2995 public:
2996  bool ti_never_find;
2997  bool ti_never_write;
2998  bool ti_min_trainable;
2999  bool ti_as_release;
3000  bool ti_release;
3001 
3002  test_info() {
3003  init_test_info();
3004  }
3005 
3006  ~test_info(){
3007  init_test_info();
3008  }
3009 
3010  void init_test_info(){
3011  ti_never_find = false;
3012  ti_never_write = false;
3013  ti_min_trainable = false;
3014  ti_as_release = false;
3015  ti_release = false;
3016  }
3017 
3018 };
3019 
3020 
3021 //=================================================================
3022 // dbg_inst_info
3023 
3024 #ifdef FULL_DEBUG
3025 
3026 class dbg_inst_info {
3027 public:
3028  long dbg_before_retract_lv;
3029  long dbg_last_recoil_lv;
3030 
3031  reason dbg_rsn1;
3032  deduction dbg_dct;
3033 
3034  row_neuron_t dbg_simple_neus;
3035  row_neuron_t dbg_used_neus;
3036  row<canon_clause*> dbg_ccls;
3037  canon_cnf dbg_cnf;
3038 
3039  bj_big_int_t dbg_find_id;
3040  bj_big_int_t dbg_save_id;
3041  //bj_big_int_t dbg_canon_find_id;
3042  bj_big_int_t dbg_canon_save_id;
3043 
3044  row_quanton_t dbg_all_chosen;
3045 
3046  bool dbg_clean_code;
3047  bool dbg_old_deduc;
3048 
3049  bool dbg_periodic_prt;
3050 
3051  ch_string dbg_cy_prefix;
3052  long dbg_cy_step;
3053  ch_string dbg_cy_layout;
3054 
3055  long dbg_tot_nmps;
3056 
3057  long dbg_max_lv;
3058  long dbg_max_wrt_num_subnmp;
3059  long dbg_max_fnd_num_subnmp;
3060 
3061  bj_big_int_t dbg_num_glb_cho;
3062  bj_big_int_t dbg_num_loc_cho;
3063 
3064  dbg_inst_info(){
3065  init_dbg_inst_info();
3066  }
3067 
3068  void init_dbg_inst_info();
3069 
3070 };
3071 
3072 #endif
3073 
3074 //=============================================================================
3083 void due_periodic_prt(void* pm, double curr_secs);
3084 
3085 class brain {
3086 private:
3087  brain& operator = (brain& other){
3088  throw instance_exception(inx_bad_eq_op);
3089  }
3090 
3091  brain(brain& other){
3092  throw instance_exception(inx_bad_creat);
3093  }
3094 
3095 public:
3096 
3097  BRAIN_DBG(
3098  dbg_inst_info br_dbg;
3099  brain* br_pt_brn;
3100  bj_ofstream br_dbg_htm_os;
3101  long br_dbg_num_phi_grps;
3102  row_quanton_t br_dbg_phi_id_quas;
3103  str_str_map_t br_dbg_phi_wrt_ids;
3104  bool br_dbg_keeping_learned;
3105  long br_dbg_min_trainable_num_sub;
3106  //row<prop_signal> br_dbg_propag;
3107  row<prop_signal> br_dbg_all_ps_upd_wrt;
3108  row_neuron_t br_dbg_all_neu_upd_wrt;
3109  bj_big_int_t br_dbg_num_find_anal;
3110  neuromap* br_dbg_abort_nmp;
3111  long br_dbg_watched_nmp_idx;
3112  ticket br_dbg_watched_nmp_tk;
3113  bool br_dbg_is_watched_file;
3114  bool br_dbg_skl_bug;
3115  bool br_dbg_found_top;
3116  bool br_dbg_in_analysis;
3117  )
3118 
3119  solver* br_pt_slvr;
3120 
3121  timer br_prt_timer;
3122 
3123  double br_start_load_tm;
3124  double br_start_solve_tm;
3125 
3126  row<leveldat*> br_data_levels;
3127 
3128  bool br_dbg_only_deduc;
3129 
3130  // temporal attributes
3131 
3132  reason br_tmp_find_rsn;
3133 
3134  row_long_t br_tmp_all_sz_side1;
3135  row_long_t br_tmp_all_sz_side2;
3136 
3137  row_quanton_t br_tmp_fixing_quantons;
3138  row_quanton_t br_tmp_load_quantons;
3139  row_quanton_t br_tmp_assig_quantons;
3140 
3141  row_quanton_t br_tmp_prt_quas;
3142  row_quanton_t br_tmp_motives;
3143  row_quanton_t br_tmp_choose;
3144  row_quanton_t br_tmp_nmp_quas_for_upper_qu;
3145  row_quanton_t br_tmp_f_analysis;
3146  row_quanton_t br_tmp_ck_col;
3147  row_quanton_t br_tmp_ck_all_orig;
3148  row_quanton_t br_tmp_uncharged_in_alert_neus;
3149  row_quanton_t br_tmp_mono_all_neg;
3150  row_quanton_t br_tmp_eq_nmp_all_pos1;
3151  row_quanton_t br_tmp_eq_nmp_all_pos2;
3152  row_quanton_t br_tmp_prv_biqus;
3153  row_quanton_t br_tmp_nxt_biqus;
3154  row_quanton_t br_tmp_all_cicles;
3155  row_quanton_t br_tmp_cicles_lv_quas;
3156  row_quanton_t br_tmp_biqus_lv1;
3157  row_quanton_t br_tmp_biqus_lv2;
3158  row_quanton_t br_tmp_biqus_lv3;
3159  row_quanton_t br_tmp_all_impl_cho;
3160  row_quanton_t br_tmp_rever_quas;
3161  row_quanton_t br_tmp_trail; // in time of charging order
3162  row_quanton_t br_tmp_extra_quas;
3163 
3164  row_neuron_t br_tmp_prt_nmp_neus;
3165  row_neuron_t br_tmp_nmp_neus_for_upper_qu;
3166  row_neuron_t br_tmp_upd_wrt_neus;
3167 
3168  row_long_t br_tmp_proof_idxs;
3169 
3170  // config attributes
3171  ch_string br_file_name;
3172  ch_string br_file_name_in_ic;
3173 
3174  // state attributes
3175  recoil_counter_t br_prv_round_last_rc;
3176  ticket br_curr_choice_tk;
3177  round_counter_t br_round;
3178  ticket br_curr_write_tk;
3179 
3180  k_row<quanton> br_positons; // all quantons with positive charge
3181  k_row<quanton> br_negatons; // all quantons with negative charge
3182 
3183  qulayers br_charge_trail; // layers are tiers
3184 
3185  charge_t br_choice_spin;
3186  long br_choice_order;
3187 
3188  bool br_last_retract;
3189 
3190  row_quanton_t br_choices; // to find non charged quantons quickly
3191  row_quanton_t br_chosen; // the in 'root' level + chosen ones
3192 
3193  row_quanton_t br_monos;
3194  long br_last_monocho_idx;
3195 
3196  k_row<neuron> br_neurons; // all neurons
3197  row_neuron_t br_free_neurons;
3198  long br_num_active_neurons;
3199 
3200  row_neuron_t br_unit_neurons;
3201  row_neuron_t br_learned_unit_neurons;
3202 
3203  k_row<neuromap> br_neuromaps; // all maps
3204  row_neuromap_t br_free_neuromaps;
3205  long br_num_active_neuromaps;
3206 
3207  k_row<alert_rel> br_alert_rels; // all alert_rels
3208  row<alert_rel*> br_free_alert_rels;
3209  long br_num_active_alert_rels;
3210 
3211  long br_first_psignal;
3212  long br_last_psignal;
3213  row<prop_signal> br_psignals; // forward propagated signals
3214  row<prop_signal> br_delayed_psignals;
3215  row<prop_signal> br_all_conflicts_found;
3216 
3217  deducer br_dedcer;
3218  qlayers_ref br_wrt_ref;
3219 
3220  deduction br_pulse_deduc;
3221  reason br_retract_rsn;
3222  //reason br_mono_rsn;
3223 
3224  notekeeper br_dbg_retract_nke0;
3225 
3226  row_quanton_t br_all_cy_quas;
3227  row_neuron_t br_all_cy_neus;
3228  coloring br_dbg_full_col;
3229  row_quanton_t br_dbg_quly_cy_quas;
3230 
3231  row<sortee*> br_tmp_wrt_tauto_tees;
3232  row<sortee*> br_tmp_wrt_guide_tees;
3233 
3234  row<canon_clause*> br_tmp_wrt_tauto_ccls;
3235  row<canon_clause*> br_tmp_wrt_diff_ccls;
3236  row<canon_clause*> br_tmp_wrt_guide_ccls;
3237 
3238  canon_cnf br_tmp_wrt_tauto_cnf;
3239  canon_cnf br_tmp_wrt_diff_cnf;
3240  canon_cnf br_tmp_wrt_guide_cnf;
3241 
3242  row_neuromap_t br_candidate_nmp_lvs;
3243  row_neuromap_t br_candidate_nxt_nmp_lvs;
3244  neuromap* br_candidate_next;
3245  recoil_counter_t br_candidate_nxt_rc;
3246  long br_candidate_rsn_lv;
3247 
3248  sort_glb br_forced_srg;
3249  sort_glb br_filled_srg;
3250 
3251  sort_glb br_guide_neus_srg;
3252  sort_glb br_guide_quas_srg;
3253 
3254  //sort_glb br_compl_neus_srg;
3255  //sort_glb br_compl_quas_srg;
3256 
3257  sort_glb br_tauto_neus_srg;
3258  sort_glb br_tauto_quas_srg;
3259 
3260  //sort_glb br_clls_srg;
3261 
3262  //long br_num_memo;
3263 
3264  quanton br_top_block;
3265 
3266  long br_tot_cy_sigs;
3267  long br_tot_cy_nmps;
3268 
3269  row_neuron_t br_tmp_ck_sat_neus;
3270  row_neuron_t br_tmp_prt_neus;
3271  row_neuron_t br_tmp_all_neus;
3272  row_neuron_t br_tmp_all_confl;
3273 
3274  row_neuromap_t br_tmp_wrt_root;
3275 
3276  row<prop_signal> br_tmp_prt_ps;
3277  row<prop_signal> br_tmp_nmp_get_all_ps;
3278 
3279  coloring br_tmp_extra_col;
3280  coloring br_tmp_ini_tauto_col;
3281  coloring br_tmp_tauto_col;
3282 
3283  // these var MUST start with 'br_qu_tot_'
3284  long br_qu_tot_note0;
3285  long br_qu_tot_note1;
3286  long br_qu_tot_note2;
3287  long br_qu_tot_note3;
3288  long br_qu_tot_note4;
3289  long br_qu_tot_note5;
3290  long br_qu_tot_note6;
3291 
3292  // these var MUST start with 'br_ne_tot_'
3293  long br_ne_tot_tag0;
3294  long br_ne_tot_tag1;
3295  long br_ne_tot_tag2;
3296  long br_ne_tot_tag3;
3297  long br_ne_tot_tag4;
3298  long br_ne_tot_tag5;
3299 
3300  // these var MUST start with 'br_na_tot_'
3301  long br_na_tot_na0;
3302  long br_na_tot_na1;
3303  long br_na_tot_na2;
3304  long br_na_tot_na3;
3305  long br_na_tot_na4;
3306  long br_na_tot_na5;
3307 
3308  // final message
3309 
3310  bj_ostr_stream br_final_msg;
3311 
3312  // methods
3313 
3314  brain(solver& ss);
3315  ~brain();
3316 
3317  void init_brain(solver& ss);
3318 
3319  void init_all_dbg_brn();
3320 
3321  brain* get_dbg_brn(){
3322  brain* the_brn = NULL;
3323  BRAIN_DBG(the_brn = br_pt_brn);
3324  return the_brn;
3325  }
3326 
3327  solver* get_dbg_slv();
3328 
3329  void all_mutual_init(){
3330  br_guide_neus_srg.stab_mutual_init();
3331  br_guide_quas_srg.stab_mutual_init();
3332 
3333  //br_compl_neus_srg.stab_mutual_init();
3334  //br_compl_quas_srg.stab_mutual_init();
3335 
3336  br_tauto_neus_srg.stab_mutual_init();
3337  br_tauto_quas_srg.stab_mutual_init();
3338  }
3339 
3340  ch_string& get_f_nam(){
3341  return get_my_inst().get_f_nam();
3342  }
3343 
3344  void release_all_sortors();
3345  void release_brain();
3346 
3347  solver& get_solver();
3348  skeleton_glb& get_skeleton();
3349  instance_info& get_my_inst();
3350  bj_output_t& get_out_info();
3351 
3352  bool dbg_as_release();
3353 
3354  // core methods
3355 
3356  long brn_tunnel_signals(bool only_orig, row_quanton_t& all_impl_cho);
3357  quanton* choose_quanton();
3358 
3359  quanton* get_curr_mono();
3360  bool ck_prev_monos();
3361  quanton* choose_mono();
3362  void fill_mono_rsn(reason& rsn, row_quanton_t& mots, quanton& mono);
3363 
3364  bool dbg_ck_rsns(reason& rsn1, reason& rsn2);
3365 
3366  bool ck_mono_propag();
3367  long propagate_signals();
3368  void pulsate();
3369 
3370  void start_propagation(quanton& qua);
3371  comparison select_propag_side(bool cnfl1, long sz1, row_long_t& all_sz1,
3372  bool cnfl2, long sz2, row_long_t& all_sz2);
3373 
3374  void init_forced_sorter();
3375 
3376  // psignal methods
3377 
3378  void put_psignal(quanton& qua, neuron* src, long max_tier);
3379 
3380  prop_signal& pick_psignal(){
3381  br_first_psignal++;
3382  BRAIN_CK(br_first_psignal <= br_last_psignal);
3383  BRAIN_CK(br_psignals.is_valid_idx(br_first_psignal));
3384  prop_signal& sgnl = br_psignals[br_first_psignal];
3385  return sgnl;
3386  }
3387 
3388  bool has_psignals(){
3389  BRAIN_CK(br_first_psignal <= br_last_psignal);
3390  return (br_first_psignal < br_last_psignal);
3391  }
3392 
3393  long num_psignals(){
3394  BRAIN_CK(br_first_psignal <= br_last_psignal);
3395  return (br_last_psignal - br_first_psignal);
3396  }
3397 
3398  void reset_psignals(){
3399  br_first_psignal = 0;
3400  br_last_psignal = 0;
3401  }
3402 
3403  bool has_reset_psignals(){
3404  return ((br_first_psignal == 0) && (br_last_psignal == 0));
3405  }
3406 
3407  void send_psignal(quanton& qua, neuron* neu, long nxt_ti){
3408  BRAIN_CK((nxt_ti > 0) || (level() == ROOT_LEVEL));
3409  put_psignal(qua, neu, nxt_ti);
3410  }
3411 
3412  void send_row_psignals(row<prop_signal>& to_send){
3413  for(long aa = 0; aa < to_send.size(); aa++){
3414  prop_signal& psig = to_send[aa];
3415  BRAIN_CK(psig.ps_quanton != NULL_PT);
3416 
3417  send_psignal(*psig.ps_quanton, psig.ps_source, psig.ps_tier);
3418  }
3419  }
3420 
3421  quanton* receive_psignal(bool only_orig);
3422 
3423  void send_next_mono();
3424 
3425  void get_bineu_sources(quanton& cho, quanton& qua, row_quanton_t& all_src);
3426  void get_all_bineu_sources(quanton& cho, row_quanton_t& all_src);
3427  void get_all_cicle_cho(row_quanton_t& all_cicl);
3428  quanton* get_cicles_common_cho(quanton*& replace_cho, row_quanton_t& all_impl_cho);
3429  void get_last_lv_charges(row_quanton_t& all_lv_pos);
3430 
3431  bj_ostream& print_psignals(bj_ostream& os, bool just_qua = false){
3432  os << "[";
3433  for(long aa = br_first_psignal + 1; aa <= br_last_psignal; aa++){
3434  if(br_psignals.is_valid_idx(aa)){
3435  if(just_qua){
3436  os << br_psignals[aa].ps_quanton << ", ";
3437  } else {
3438  os << br_psignals[aa] << bj_eol;
3439  }
3440  }
3441  }
3442  os << "]";
3443  os.flush();
3444  return os;
3445  }
3446 
3447  // aux memaps
3448 
3449  void pop_all_canditates();
3450  void write_all_canditates();
3451 
3452  // aux neuromaps
3453 
3454  neuromap& locate_neuromap(){
3455  neuromap* pt_nmp = NULL_PT;
3456  if(! br_free_neuromaps.is_empty()){
3457  pt_nmp = br_free_neuromaps.pop();
3458  long nmp_idx = pt_nmp->na_index;
3459  MARK_USED(nmp_idx);
3460  BRAIN_CK(br_neuromaps.is_valid_idx(nmp_idx));
3461  BRAIN_CK(&(br_neuromaps[nmp_idx]) == pt_nmp);
3462  } else {
3463  pt_nmp = &(br_neuromaps.inc_sz());
3464  pt_nmp->na_index = br_neuromaps.last_idx();
3465  }
3466  BRAIN_CK(pt_nmp != NULL);
3467 
3468  neuromap& nmp = *pt_nmp;
3469  BRAIN_CK(nmp.na_index != INVALID_IDX);
3470  BRAIN_CK(nmp.is_na_virgin());
3471 
3472  nmp.na_brn = this;
3473 
3474  br_num_active_neuromaps++;
3475  return nmp;
3476  }
3477 
3478  void release_neuromap(neuromap& nmp){
3479  BRAIN_CK(! br_neuromaps.is_empty());
3480  br_free_neuromaps.push(&nmp);
3481  nmp.init_neuromap();
3482 
3483  BRAIN_CK(nmp.is_na_virgin());
3484 
3485  br_num_active_neuromaps--;
3486  }
3487 
3488  // aux alert_rels
3489 
3490  alert_rel& locate_alert_rel(){
3491  alert_rel* pt_arl = NULL_PT;
3492  if(! br_free_alert_rels.is_empty()){
3493  pt_arl = br_free_alert_rels.pop();
3494  } else {
3495  pt_arl = &(br_alert_rels.inc_sz());
3496  }
3497  BRAIN_CK(pt_arl != NULL);
3498 
3499  alert_rel& arl = *pt_arl;
3500  BRAIN_CK(arl.is_ar_virgin());
3501 
3502  br_num_active_alert_rels++;
3503  return arl;
3504  }
3505 
3506  void release_alert_rel(alert_rel& arl){
3507  BRAIN_CK(! br_alert_rels.is_empty());
3508  br_free_alert_rels.push(&arl);
3509  arl.init_alert_rel();
3510 
3511  BRAIN_CK(arl.is_ar_virgin());
3512 
3513  br_num_active_alert_rels--;
3514  }
3515 
3516  // aux neuron
3517 
3518  neuron& locate_neuron(){
3519  neuron* pt_neu = NULL_PT;
3520  if(! br_free_neurons.is_empty()){
3521  pt_neu = br_free_neurons.pop();
3522  long neu_idx = pt_neu->ne_index;
3523  MARK_USED(neu_idx);
3524  BRAIN_CK(br_neurons.is_valid_idx(neu_idx));
3525  BRAIN_CK(&(br_neurons[neu_idx]) == pt_neu);
3526  } else {
3527  pt_neu = &(br_neurons.inc_sz());
3528  pt_neu->ne_index = br_neurons.last_idx();
3529  }
3530  BRAIN_CK(pt_neu != NULL);
3531 
3532  neuron& neu = *pt_neu;
3533  BRAIN_CK(neu.ne_index != INVALID_IDX);
3534  BRAIN_CK(neu.is_ne_virgin());
3535 
3536  BRAIN_DBG(neu.ne_pt_brn = this);
3537 
3538  br_num_active_neurons++;
3539  return neu;
3540  }
3541 
3542  void release_neuron(neuron& neu){
3543  BRAIN_CK(! br_neurons.is_empty());
3544  br_free_neurons.push(&neu);
3545  neu.init_neuron();
3546 
3547  BRAIN_CK(neu.is_ne_virgin());
3548 
3549  br_num_active_neurons--;
3550  }
3551 
3552  // aux methods
3553 
3554  void reset_conflict(){
3555  br_all_conflicts_found.clear(true, true);
3556  }
3557 
3558  prop_signal& first_conflict(){
3559  BRAIN_CK(! br_all_conflicts_found.is_empty());
3560  prop_signal& fst_cfl = br_all_conflicts_found[0];
3561  BRAIN_CK(fst_cfl.ps_source != NULL_PT);
3562  BRAIN_CK(fst_cfl.ps_source->ne_original);
3563  BRAIN_CK(fst_cfl.ps_tier != INVALID_TIER);
3564  return fst_cfl;
3565  }
3566 
3567  bool found_conflict(){
3568  bool h_cfl = ! br_all_conflicts_found.is_empty();
3569  BRAIN_CK(! h_cfl || ! first_conflict().is_ps_virgin());
3570  return h_cfl;
3571  }
3572 
3573  bool ck_confl_ti();
3574 
3575  void set_file_name_in_ic(ch_string f_nam = "");
3576  void config_brain(ch_string f_nam = "");
3577  void init_loading(long num_qua, long num_neu);
3578 
3579  void init_alert_neus();
3580  void update_monos();
3581 
3582  neuron& add_neuron(row_quanton_t& quans, quanton*& forced_qua, bool orig);
3583  neuron* learn_mots(reason& rsn);
3584 
3585  quanton* get_quanton(long q_id);
3586 
3587  recoil_counter_t recoil(){
3588  return br_curr_choice_tk.tk_recoil;
3589  }
3590 
3591  void inc_recoil(){
3592  br_curr_choice_tk.tk_recoil++;
3593  }
3594 
3595  long level(){
3596  BRAIN_CK(br_data_levels.size() == (br_curr_choice_tk.tk_level + 1));
3597  return br_curr_choice_tk.tk_level;
3598  }
3599 
3600  bool in_root_lv(){
3601  return (level() == ROOT_LEVEL);
3602  }
3603 
3604  leveldat& get_leveldat(long lv = INVALID_LEVEL){
3605  row<leveldat*>& all_lv = br_data_levels;
3606  if(! all_lv.is_valid_idx(lv)){
3607  BRAIN_CK(! all_lv.is_empty());
3608  leveldat* pt_lv = all_lv.last();
3609  BRAIN_CK(pt_lv != NULL);
3610  return *pt_lv;
3611  }
3612 
3613  BRAIN_CK(all_lv[lv] != NULL_PT);
3614 
3615  leveldat& lv_dat = *(all_lv[lv]);
3616  BRAIN_CK((lv == ROOT_LEVEL) || (lv_dat.ld_chosen != NULL_PT));
3617  BRAIN_CK((lv == ROOT_LEVEL) || (lv_dat.ld_chosen->qlevel() == lv_dat.ld_idx));
3618  return lv_dat;
3619  }
3620 
3621  /*leveldat& data_level(){
3622  BRAIN_CK(! br_data_levels.is_empty());
3623  leveldat* pt_lv = br_data_levels.last();
3624  BRAIN_CK(pt_lv != NULL);
3625  return *pt_lv;
3626  }*/
3627 
3628  leveldat& inc_data_levels(){
3629  leveldat* pt_lv = leveldat::create_leveldat(this);
3630  BRAIN_CK(pt_lv != NULL);
3631  br_data_levels.push(pt_lv);
3632  leveldat& dat_lv = *pt_lv;
3633  dat_lv.ld_idx = br_data_levels.last_idx();
3634  return dat_lv;
3635  }
3636 
3637  void inc_level(quanton& qua){
3638  (br_curr_choice_tk.tk_level)++;
3639 
3640  leveldat& dat_lv = inc_data_levels();
3641  dat_lv.ld_chosen = &qua;
3642  }
3643 
3644  void dec_level();
3645 
3646  long trail_level(){
3647  return br_charge_trail.last_qlevel();
3648  }
3649 
3650  //ticket get_curr_cho_tk();
3651  //ticket get_curr_cand_tk();
3652 
3653  quanton* curr_cho(){
3654  BRAIN_CK(! br_data_levels.is_empty());
3655  leveldat* lv = br_data_levels.last();
3656  BRAIN_CK(lv != NULL);
3657  quanton* cho = lv->ld_chosen;
3658  return cho;
3659  }
3660 
3661  quanton& curr_choice(){
3662  quanton* cho = curr_cho();
3663  BRAIN_CK(cho != NULL);
3664  BRAIN_CK(! cho->has_charge() || (cho->qlevel() == level()));
3665  return *cho;
3666  }
3667 
3668  bool is_curr_cho_mono(){
3669  quanton* cho = curr_cho();
3670  if(cho == NULL_PT){
3671  return false;
3672  }
3673  bool is_mn = cho->is_opp_mono();
3674  return is_mn;
3675  }
3676 
3677  long get_last_lv_all_trail_sz(row_long_t& all_sz){
3678  quanton& cho = curr_choice();
3679  all_sz.clear();
3680  long num_prop = br_charge_trail.get_all_sz(all_sz, cho.qu_tier);
3681  return num_prop;
3682  }
3683 
3684  void update_tk_charge(ticket& nw_tk);
3685  void update_tk_trail(ticket& nw_tk);
3686 
3687  bool lv_has_learned(){
3688  return get_leveldat().has_learned();
3689  }
3690 
3691  long lv_num_learned(){
3692  return get_leveldat().num_learned();
3693  }
3694 
3695  void replace_choice(quanton& cho, quanton& nw_cho, dbg_call_id dbg_id = dbg_call_1);
3696  void retract_to(long tg_lv, bool full_reco);
3697  bool dbg_in_edge_of_level();
3698  bool dbg_in_edge_of_target_lv(reason& rsn);
3699  void dbg_old_reverse_trail();
3700 
3701  bool deduce_and_reverse_trail();
3702  void reverse_with(reason& rsn);
3703 
3704  bool ck_cov_flags();
3705 
3706  neuromap* pop_cand_lv_in(row_neuromap_t& lvs, bool free_mem, bool force_rel);
3707  //void pop_all_cand_by_qua(quanton& qua);
3708  //void pop_all_cand_by_neu(neuron& neu);
3709 
3710  neuromap* pop_cand_lv(bool free_mem);
3711  void pop_cand_lvs_until(quanton& qua);
3712  void pop_all_cand_lvs();
3713  void pop_all_nxt_cand_lvs(long tg_lv = INVALID_LEVEL);
3714 
3715  void use_next_cand(quanton& qua);
3716 
3717  void set_chg_cands_update(quanton& qua);
3718  //void set_chg_cands_update_2(quanton& qua);
3719  void reset_chg_cands_update(quanton& qua);
3720  void reset_cand_next();
3721  void candidates_before_analyse();
3722  void candidates_before_reverse(reason& rsn);
3723  void candidates_after_reverse();
3724  void init_cand_propag(neuromap& nmp, quanton* curr_qua);
3725  //void old_init_cand_propag(neuromap& nmp, quanton* curr_qua);
3726 
3727  bool in_current_round(ticket& the_tk){
3728  bool in_rnd = (the_tk.tk_recoil > br_prv_round_last_rc);
3729  return in_rnd;
3730  }
3731 
3732  long get_lst_cand_lv();
3733  neuromap* get_last_cand(dbg_call_id dbg_id = dbg_call_1);
3734 
3735  void candidate_find_analysis(deducer& dedcer, deduction& dct);
3736 
3737  void add_top_cands(row_neuromap_t& to_wrt);
3738 
3739  void analyse_conflicts(row<prop_signal>& all_confl, deduction& dct);
3740 
3741  void write_analysis(row_quanton_t& causes, long deduc_lv, reason& rsn);
3742  void write_update_all_tk(row_quanton_t& causes);
3743  void write_get_tk(ticket& wrt_tk);
3744  bool ck_write_quas(reason& rsn);
3745 
3746  long get_min_trainable_num_sub();
3747 
3748  bool dbg_ck_candidates(bool nw_cands);
3749 
3750  long tier(){
3751  long tr = br_charge_trail.last_qtier();
3752  return tr;
3753  }
3754 
3755  quanton& trail_last(){
3756  quanton* qua = br_charge_trail.last_quanton();
3757  BRAIN_CK(qua != NULL_PT);
3758  return *qua;
3759  }
3760 
3761  void init_mem_tmps(){
3762  br_tmp_wrt_tauto_ccls.clear();
3763  br_tmp_wrt_guide_ccls.clear();
3764  br_tmp_wrt_diff_ccls.clear();
3765 
3766  br_tmp_wrt_tauto_cnf.clear_cnf();
3767  br_tmp_wrt_diff_cnf.clear_cnf();
3768  br_tmp_wrt_guide_cnf.clear_cnf();
3769  }
3770 
3771  // aux func
3772 
3773  void init_all_tots(){
3774  init_qu_tots();
3775  init_ne_tots();
3776  init_na_tots();
3777  }
3778 
3779  void init_qu_tots(){
3780  br_qu_tot_note0 = 0;
3781  br_qu_tot_note1 = 0;
3782  br_qu_tot_note2 = 0;
3783  br_qu_tot_note3 = 0;
3784  br_qu_tot_note4 = 0;
3785  br_qu_tot_note5 = 0;
3786  br_qu_tot_note6 = 0;
3787  }
3788 
3789  void init_ne_tots(){
3790  br_ne_tot_tag0 = 0;
3791  br_ne_tot_tag1 = 0;
3792  br_ne_tot_tag2 = 0;
3793  br_ne_tot_tag3 = 0;
3794  br_ne_tot_tag4 = 0;
3795  br_ne_tot_tag5 = 0;
3796  }
3797 
3798  void init_na_tots(){
3799  br_na_tot_na0 = 0;
3800  br_na_tot_na1 = 0;
3801  br_na_tot_na2 = 0;
3802  br_na_tot_na3 = 0;
3803  br_na_tot_na4 = 0;
3804  br_na_tot_na5 = 0;
3805  }
3806 
3807  ch_string dbg_prt_margin(bj_ostream& os, bool is_ck = false);
3808  void dbg_prt_cand_info(bj_ostream& os, neuron& neu);
3809 
3810  ch_string& dbg_get_file(){
3811  return get_my_inst().ist_file_path;
3812  }
3813 
3814  bool ck_trail();
3815  void print_trail(bj_ostream& os, bool no_src_only = false);
3816 
3817  bj_ostream& print_all_quantons(bj_ostream& os, long ln_sz, ch_string ln_fd);
3818 
3819  bool dbg_br_compute_binary(row_neuron_t& neus);
3820  bool dbg_br_compute_ck_sat(row_neuron_t& neus);
3821  bool dbg_br_compute_ck_sat_of(row_neuron_t& neus, row_quanton_t& assig);
3822 
3823  void read_cnf(dimacs_loader& ldr);
3824  void parse_cnf(dimacs_loader& ldr, row<long>& all_ccls);
3825  neuron& load_neuron(row_quanton_t& neu_quas);
3826  bool load_brain(long num_neu, long num_var, row_long_t& load_ccls);
3827 
3828  bool load_instance();
3829 
3830  void think();
3831 
3832  bj_satisf_val_t solve_instance(bool load_it);
3833 
3834  void fill_with_origs(row_neuron_t& neus);
3835 
3836  void check_timeout();
3837  void dbg_check_sat_assig();
3838 
3839  void set_result(bj_satisf_val_t re);
3840  bj_satisf_val_t get_result();
3841 
3842  void dbg_add_to_used(neuron& neu);
3843 
3844  void dbg_lv_on(long lv_idx);
3845  void dbg_lv_off(long lv_idx);
3846 
3847  void dbg_prt_all_nmp(bj_ostream& os, row_neuromap_t& all_cand, bool just_ids);
3848  void dbg_prt_all_cands(bj_ostream& os, bool just_ids = false);
3849  void dbg_prt_all_nxt_cands(bj_ostream& os, bool just_ids = false);
3850 
3851  void dbg_prt_br_neuromaps(bj_ostream& os);
3852 
3853  void dbg_prt_lvs_have_learned(bj_ostream& os);
3854  void dbg_prt_lvs_cho(bj_ostream& os);
3855  void dbg_prt_full_stab();
3856 
3857  void dbg_print_cy_nmp_node_plays(bj_ostream& os);
3858  void dbg_print_cy_graph_node_plays(bj_ostream& os);
3859  void dbg_print_cy_graph_node_tiers(bj_ostream& os);
3860 
3861  void dbg_init_html();
3862  void dbg_start_html();
3863  void dbg_finish_html();
3864  void dbg_update_html_cy_graph(ch_string cy_kk, coloring* col, ch_string htm_str);
3865  void dbg_br_init_all_cy_pos();
3866 
3867  void print_active_blocks(bj_ostream& os);
3868 
3869  bj_ostream& dbg_br_print_col_cy_nodes(bj_ostream& os, bool is_ic);
3870  bj_ostream& dbg_print_all_qua_rels(bj_ostream& os);
3871  bj_ostream& dbg_print_htm_all_monos(bj_ostream& os);
3872 
3873  bj_ostream& print_brain(bj_ostream& os);
3874 
3875  bj_ostream& print_all_original(bj_ostream& os);
3876 };
3877 
3878 //=============================================================================
3879 // INLINES dependant on class declarations
3880 
3881 inline
3882 void
3883 quanton::tunnel_swapop(long idx_pop){
3884  long idx_mov = qu_tunnels.size() - 1;
3885  neuron* neu2 = qu_tunnels.last();
3886  BRAIN_CK(neu2->ck_tunnels());
3887  qu_tunnels.swapop(idx_pop);
3888  if(idx_pop != idx_mov){
3889  BRAIN_CK(qu_tunnels[idx_pop] == neu2);
3890  if(neu2->ne_fibres[0] == this){
3891  BRAIN_CK(neu2->ne_fibre_0_idx == idx_mov);
3892  neu2->ne_fibre_0_idx = idx_pop;
3893  } else {
3894  BRAIN_CK(neu2->ne_fibres[1] == this);
3895  BRAIN_CK(neu2->ne_fibre_1_idx == idx_mov);
3896  neu2->ne_fibre_1_idx = idx_pop;
3897  }
3898  BRAIN_CK(neu2->ck_tunnels());
3899  }
3900 }
3901 
3902 inline
3903 neuron*
3904 quanton::get_source(){
3905  if(qu_source == NULL_PT){
3906  //BRAIN_CK(qu_inverse->qu_source == NULL_PT);
3907  return qu_inverse->qu_source;
3908  }
3909 
3910  BRAIN_CK((qu_source == NULL_PT) || ! qu_source->is_ne_virgin());
3911  return qu_source;
3912 }
3913 
3914 inline
3915 bool
3916 quanton::has_learned_source(){
3917  bool h_l_src = has_source() && ! get_source()->ne_original;
3918  return h_l_src;
3919 }
3920 
3921 inline
3922 row_quanton_t&
3923 qulayers::get_qu_layer(long lv){
3924  BRAIN_CK(lv >= 0);
3925  BRAIN_CK(lv < MAX_LAYERS);
3926  while(ql_quas_by_layer.size() <= lv){
3927  ql_quas_by_layer.inc_sz();
3928  }
3929  BRAIN_CK(lv >= 0);
3930  BRAIN_CK(lv < MAX_LAYERS);
3931  BRAIN_CK(ql_quas_by_layer.is_valid_idx(lv));
3932  row_quanton_t& mots = ql_quas_by_layer[lv];
3933  return mots;
3934 }
3935 
3936 inline
3937 brain&
3938 notekeeper::get_dk_brain(){
3939  BRAIN_CK(dk_brain != NULL_PT);
3940  return *dk_brain;
3941 }
3942 
3943 inline
3944 brain&
3945 deducer::get_de_brain(){
3946  BRAIN_CK(de_brain != NULL_PT);
3947  return *de_brain;
3948 }
3949 
3950 inline
3951 brain&
3952 qulayers::get_ql_brain(){
3953  BRAIN_CK(ql_brain != NULL_PT);
3954  return *ql_brain;
3955 }
3956 
3957 inline
3958 comparison cmp_qlevel(quanton* const & qua1, quanton* const & qua2){
3959  BRAIN_CK(qua1 != NULL_PT);
3960  BRAIN_CK(qua2 != NULL_PT);
3961  long qlev1 = qua1->qlevel();
3962  long qlev2 = qua2->qlevel();
3963  bool inv1 = (qlev1 == INVALID_LEVEL);
3964  bool inv2 = (qlev2 == INVALID_LEVEL);
3965  // sort them in inverse order with inv as the max:
3966  if(inv1 && inv2){ return 0; }
3967 
3968  if(inv1 && !inv2){ return -1; }
3969  if(!inv1 && inv2){ return 1; }
3970  if(qlev1 == qlev2){
3971  return cmp_long(qua2->get_charge(), qua1->get_charge());
3972  }
3973  return cmp_long(qlev2, qlev1);
3974 }
3975 
3976 inline
3977 charge_t negate_trinary(charge_t val){
3978  if(val == cg_negative){
3979  return cg_positive;
3980  }
3981  if(val == cg_positive){
3982  return cg_negative;
3983  }
3984  return cg_neutral;
3985 }
3986 
3987 inline
3988 void negate_quantons(row_quanton_t& qua_row){
3989  for(long kk = 0; kk < qua_row.size(); kk++){
3990  qua_row[kk] = qua_row[kk]->qu_inverse;
3991  }
3992 
3993 }
3994 
3995 inline
3996 void get_ids_of(row_quanton_t& quans, row_long_t& the_ids){
3997  the_ids.clear();
3998  for(long kk = 0; kk < quans.size(); kk++){
3999  quanton& qua = *(quans[kk]);
4000  the_ids.push(qua.qu_id);
4001 
4002  }
4003 }
4004 
4005 inline
4006 long find_max_level(row_quanton_t& tmp_mots){
4007  long max_lev = ROOT_LEVEL;
4008 
4009  for(long aa = 0; aa < tmp_mots.size(); aa++){
4010  BRAIN_CK(tmp_mots[aa] != NULL_PT);
4011  quanton& qua = *(tmp_mots[aa]);
4012  BRAIN_CK(qua.has_charge());
4013  max_lev = max_val(max_lev, qua.qlevel());
4014  }
4015  return max_lev;
4016 }
4017 
4018 inline
4019 long find_max_tier(row_quanton_t& tmp_mots, long from_idx){
4020  long max_ti = INVALID_TIER;
4021 
4022  for(long aa = from_idx; aa < tmp_mots.size(); aa++){
4023  BRAIN_CK(tmp_mots[aa] != NULL_PT);
4024  quanton& qua = *(tmp_mots[aa]);
4025  max_ti = max_val(max_ti, qua.qu_tier);
4026  }
4027  return max_ti;
4028 }
4029 
4030 //=============================================================================
4031 // printing funcs
4032 
4033 DEFINE_PRINT_FUNCS(ticket)
4034 DEFINE_PRINT_FUNCS(alert_rel)
4035 DEFINE_PRINT_FUNCS(quanton)
4036 DEFINE_PRINT_FUNCS(neuron)
4037 DEFINE_PRINT_FUNCS(reason)
4038 DEFINE_PRINT_FUNCS(deduction)
4039 DEFINE_PRINT_FUNCS(prop_signal)
4040 DEFINE_PRINT_FUNCS(coloring)
4041 DEFINE_PRINT_FUNCS(qulayers)
4042 DEFINE_PRINT_FUNCS(cov_entry)
4043 DEFINE_PRINT_FUNCS(neuromap)
4044 DEFINE_PRINT_FUNCS(deducer)
4045 DEFINE_PRINT_FUNCS(leveldat)
4046 
4047 #endif // BRAIN_H
4048 
4049 
A canon_cnf is a BCFF. An stabilized sub-formula.
Definition: skeleton.h:386
Class that holds all data used to solve a particular CNF instance.
Definition: brain.h:3085
Class that holds the data used to analyze a conflict.
Definition: brain.h:2823
Class for CNF variables (each variable has a positon and a negaton).
Definition: brain.h:600
Class that holds the result of analyzing (doing resolution) of a conflict.
Definition: brain.h:1715
Class that is an item to be stabilized.
Definition: sortor.h:152
Class that holds an instance data.
Definition: instance_info.h:96
A neuromap is a CNF sub-formula.
Definition: brain.h:2191
ben_jose API declaration.
Class for representing BCP propagation data.
Definition: brain.h:1525
A skeleton_glb is a directory holding a database.
Definition: skeleton.h:761
void write_all_nmps(row_neuromap_t &to_wrt)
Writes all neuromap s (candidates) that need writing.
Definition: macro_ben_jose.cpp:94
bj_satisf_val_t
Posible final results of the solving process.
Definition: ben_jose.h:77
Class for CNF clause behavior. So there is one neuron per clause.
Definition: brain.h:1211
A sorset is a group of sortee s.
Definition: sortor.h:321
Class that holds the data of a level.
Definition: brain.h:2909
The initial and final state for an stabilization is a coloring.
Definition: brain.h:934
Class that holds all global data used to stabilize a group of items.
Definition: sortor.h:579
A sortrel is a relation between two sortee s.
Definition: sortor.h:111