AERA
auto_focus.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef auto_focus_h
86 #define auto_focus_h
87 
88 #include "../r_code/time_buffer.h"
89 
90 #include "overlay.h"
91 #include "group.h"
92 #include "pattern_extractor.h"
93 #include "mem.h"
94 
95 
96 namespace r_exec {
97 
98 class r_exec_dll AutoFocusController :
99  public Controller {
100 private:
101  // icpp_pgm parameters.
102  bool pass_through_;
103  bool ctpx_on_;
104  bool gtpx_on_;
105  bool ptpx_on_;
106  bool trace_injections_;
107  bool decompile_models_;
108  std::vector<Group *> output_groups_; // 1st is the primary, 2nd the secondary, followed by other groups if any.
109 
110  class Rating {
111  public:
112  uint32 evidences_;
113  uint32 positive_evidences_;
114  float32 success_rate_;
115  float32 delta_success_rate_;
116 
117  static bool DeltaSuccessRate(float32 delta_success_rate) {
118 
119  return delta_success_rate > 0 && delta_success_rate < _Mem::Get()->get_tpx_dsr_thr();
120  }
121 
122  Rating() : evidences_(0), positive_evidences_(0), success_rate_(0), delta_success_rate_(1) {}
123 
124  void add_evidence(bool success) {
125 
126  ++evidences_;
127  if (success)
128  ++positive_evidences_;
129  delta_success_rate_ = success_rate_;
130  success_rate_ = positive_evidences_ / evidences_;
131  delta_success_rate_ = success_rate_ - delta_success_rate_;
132  }
133  };
134 
135  typedef std::unordered_map<P<_Fact>, P<TPX>, r_code::PHash<_Fact> > TPXMap;
136 
137  TPXMap goals_; // f->g->f->target.
138  TPXMap predictions_; // f->p->f->target.
139 
140  typedef std::unordered_map<P<_Fact>, Rating, r_code::PHash<_Fact> > RatingMap;
141 
142  // entries are patterns, i.e. abstract targets.
143  RatingMap goal_ratings_;
144  RatingMap prediction_ratings_;
145 
146  static const uint32 CacheInitialSize = 128;
147  static const uint32 CrossBufferInitialSize = 1024;
148 
149  r_code::time_buffer<CInput, CInput::IsInvalidated> cache_; // contains all inputs we don't know yet if they are relevant or not; thz==sampling period.
150  r_code::time_buffer<Input, Input::IsInvalidated> cross_buffer_; // contains all relevant inputs.
151 
152  void notify(_Fact *target, View *input, TPXMap &map);
153  void dispatch_pred_success(Success* success, TPXMap &map);
154  void dispatch(View *input, _Fact *abstract_input, BindingMap *bm, bool &injected, TPXMap &map);
155  void dispatch_no_inject(View *input, _Fact *abstract_input, BindingMap *bm, TPXMap &map);
156  template<class T> TPX *build_tpx(_Fact *target, _Fact *pattern, BindingMap *bm, RatingMap &map, Fact *f_imdl, bool wr_enabled) {
157 
158  if (!gtpx_on_ && !ptpx_on_)
159  return new TPX(this, target, pattern, bm);
160 
161  if (wr_enabled)
162  return new TPX(this, target, pattern, bm);
163 
164  RatingMap::const_iterator r = map.find(pattern);
165  if (r != map.end()) {
166 
167  if (Rating::DeltaSuccessRate(r->second.delta_success_rate_)) // target for which we don't see much improvement over time.
168  return new TPX(this, target, pattern, bm);
169  else
170  return new T(this, target, pattern, bm, f_imdl);
171  } else
172  return new T(this, target, pattern, bm, f_imdl);
173  }
174 public:
177 
178  r_code::Code *get_core_object() const override;
179 
180  void take_input(r_exec::View *input) override;
181  void reduce(r_exec::View *input);
182 
183  View *inject_input(View *input); // inject a filtered input into the output groups starting from 0; return the view injected in the primary group.
184  void inject_input(View *input, uint32 start); // inject an unfiltered input into the output groups starting from start.
185  void inject_input(View *input, _Fact *abstract_input, BindingMap *bm); // inject a filtered input into the output groups.
186  void inject_hlps(const std::vector<P<r_code::Code> > &hlps) const; // called by TPX; hlp is a mdl or a cst.
187 
188  bool decompile_models() const { return decompile_models_; }
189  bool gtpx_on() const { return gtpx_on_; }
190  bool ptpx_on() const { return ptpx_on_; }
191  Group *get_primary_group() const { return output_groups_[0]; }
192 
193  void copy_cross_buffer(r_code::list<Input> &destination);
194  r_code::time_buffer<CInput, CInput::IsInvalidated> &get_cache() { return cache_; }
195 };
196 }
197 
198 
199 #endif
r_code::_View
Definition: r_code/object.h:164
r_exec::BindingMap
Definition: binding_map.h:248
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::TPX
Definition: pattern_extractor.h:148
r_exec::Success
Definition: factory.h:607
r_exec::AutoFocusController
Definition: auto_focus.h:99
r_exec::View
Definition: view.h:102
r_exec::Group
Definition: group.h:108
r_exec::Controller
Definition: controller.h:104
r_code::time_buffer
Definition: time_buffer.h:99
r_code::list
Definition: list.h:99
r_exec::Fact
Definition: factory.h:360
r_code::PHash
Definition: r_code/utils.h:96