replicode
auto_focus.h
Go to the documentation of this file.
1 // auto_focus.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef auto_focus_h
32 #define auto_focus_h
33 
34 #include "../r_code/time_buffer.h"
35 
36 #include "overlay.h"
37 #include "group.h"
38 #include "pattern_extractor.h"
39 #include "mem.h"
40 
41 
42 namespace r_exec {
43 
45  public Controller {
46 private:
47 // icpp_pgm parameters.
49  bool _ctpx_on;
50  bool _gtpx_on;
51  bool _ptpx_on;
54  std::vector<Group *> output_groups; // 1st is the primary, 2nd the secondary, followed by other groups if any.
55 
56  class Rating {
57  public:
60  double SR;
61  double dSR;
62 
63  static bool DSR(double dSR) {
64 
65  return dSR > 0 && dSR < _Mem::Get()->get_tpx_dsr_thr();
66  }
67 
68  Rating(): evidences(0), positive_evidences(0), SR(0), dSR(1) {}
69 
70  void add_evidence(bool success) {
71 
72  ++evidences;
73  if (success)
74  ++positive_evidences;
75  dSR = SR;
76  SR = positive_evidences / evidences;
77  dSR = SR - dSR;
78  }
79  };
80 
81  typedef std::unordered_map<P<_Fact>, P<TPX>, PHash<_Fact> > TPXMap;
82 
83  TPXMap goals; // f->g->f->target.
84  TPXMap predictions; // f->p->f->target.
85 
86  typedef std::unordered_map<P<_Fact>, Rating, PHash<_Fact> > RatingMap;
87 
88 // entries are patterns, i.e. abstract targets.
89  RatingMap goal_ratings;
90  RatingMap prediction_ratings;
91 
92  static const size_t CacheInitialSize = 128;
93  static const size_t CrossBufferInitialSize = 1024;
94 
95  time_buffer<CInput, CInput::IsInvalidated> cache; // contains all inputs we don't no yet if they are relevant or not; thz==sampling period.
96  time_buffer<Input, Input::IsInvalidated> cross_buffer; // contains all relevant inputs.
97 
98  void notify(_Fact *target, View *input, TPXMap &map);
99  void dispatch_pred_success(_Fact *predicted_f, TPXMap &map);
100  void dispatch(View *input, _Fact *abstract_input, BindingMap *bm, bool &injected, TPXMap &map);
101  void dispatch_no_inject(View *input, _Fact *abstract_input, BindingMap *bm, TPXMap &map);
102  template<class T> TPX *build_tpx(_Fact *target, _Fact *pattern, BindingMap *bm, RatingMap &map, Fact *f_imdl, bool wr_enabled) {
103 
104  if (!_gtpx_on && !_ptpx_on)
105  return new TPX(this, target, pattern, bm);
106 
107  if (wr_enabled)
108  return new TPX(this, target, pattern, bm);
109 
110  RatingMap::const_iterator r = map.find(pattern);
111  if (r != map.end()) {
112 
113  if (Rating::DSR(r->second.dSR)) // target for which we don't see much improvement over time.
114  return new TPX(this, target, pattern, bm);
115  else
116  return new T(this, target, pattern, bm, f_imdl);
117  } else
118  return new T(this, target, pattern, bm, f_imdl);
119  }
120  void rate(_Fact *target, bool success, TPXMap &map, RatingMap &ratings);
121 public:
124 
125  Code *get_core_object() const;
126 
127  void take_input(r_exec::View *input);
128  void reduce(r_exec::View *input);
129 
130  View *inject_input(View *input); // inject a filtered input into the output groups starting from 0; return the view injected in the primary group.
131  void inject_input(View *input, uint64_t start); // inject an unfiltered input into the output groups starting from start.
132  void inject_input(View *input, _Fact *abstract_input, BindingMap *bm); // inject a filtered input into the output groups.
133 
135  void inject_hlps(const std::vector<P<Code> > &hlps) const; // called by TPX; hlp is a mdl or a cst.
136 
137  bool decompile_models() const {
138  return _decompile_models;
139  }
140  bool gtpx_on() const {
141  return _gtpx_on;
142  }
143  bool ptpx_on() const {
144  return _ptpx_on;
145  }
147  return output_groups[0];
148  }
149 
151  void copy_cross_buffer(r_code::list<Input> &destination);
152 
154  return cache;
155  }
156 };
157 }
158 
159 
160 #endif
std::unordered_map< P< _Fact >, Rating, PHash< _Fact > > RatingMap
Definition: auto_focus.h:86
void add_evidence(bool success)
Definition: auto_focus.h:70
time_buffer< Input, Input::IsInvalidated > cross_buffer
Definition: auto_focus.h:96
bool _decompile_models
Definition: auto_focus.h:53
Definition: factory.h:98
Definition: auto_focus.h:44
double dSR
Definition: auto_focus.h:61
Definition: binding_map.h:177
time_buffer< CInput, CInput::IsInvalidated > cache
Definition: auto_focus.h:95
bool _ptpx_on
Definition: auto_focus.h:51
bool _trace_injections
Definition: auto_focus.h:52
#define dll_export
Definition: dll.h:44
Definition: pattern_extractor.h:94
Definition: _context.cpp:34
std::unordered_map< P< _Fact >, P< TPX >, PHash< _Fact > > TPXMap
Definition: auto_focus.h:81
bool _ctpx_on
Definition: auto_focus.h:49
static _Mem * Get()
Definition: mem.h:187
Definition: base.h:47
Definition: time_buffer.h:44
Definition: group.h:49
std::vector< Group * > output_groups
Definition: auto_focus.h:54
Definition: auto_focus.h:56
static bool DSR(double dSR)
Definition: auto_focus.h:63
RatingMap prediction_ratings
Definition: auto_focus.h:90
Definition: utils.h:43
Definition: view.h:47
Group * get_primary_group() const
Definition: auto_focus.h:146
bool ptpx_on() const
Definition: auto_focus.h:143
bool _pass_through
Definition: auto_focus.h:48
TPXMap goals
Definition: auto_focus.h:83
time_buffer< CInput, CInput::IsInvalidated > & get_cache()
Definition: auto_focus.h:153
Rating()
Definition: auto_focus.h:68
double SR
Definition: auto_focus.h:60
bool decompile_models() const
Definition: auto_focus.h:137
bool _gtpx_on
Definition: auto_focus.h:50
Definition: object.h:100
Definition: list.h:42
TPXMap predictions
Definition: auto_focus.h:84
Definition: object.h:172
Definition: overlay.h:50
void reduce(const IPGMContext &context, const IPGMContext &input_set, const IPGMContext &section, std::vector< uint16_t > &input_indices, std::vector< uint16_t > &production_indices)
Definition: context.cpp:523
double get_tpx_dsr_thr() const
Definition: mem.h:228
uint64_t positive_evidences
Definition: auto_focus.h:59
Definition: factory.h:174
uint64_t evidences
Definition: auto_focus.h:58
RatingMap goal_ratings
Definition: auto_focus.h:89
TPX * build_tpx(_Fact *target, _Fact *pattern, BindingMap *bm, RatingMap &map, Fact *f_imdl, bool wr_enabled)
Definition: auto_focus.h:102
bool gtpx_on() const
Definition: auto_focus.h:140