Reinforcement Learning  1.1
live_model.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "action_flags.h"
10 #include "continuous_action_response.h"
11 #include "decision_response.h"
12 #include "err_constants.h"
13 #include "factory_resolver.h"
14 #include "future_compat.h"
15 #include "multi_slot_response.h"
16 #include "multi_slot_response_detailed.h"
17 #include "multistep.h"
18 #include "ranking_response.h"
19 #include "sender.h"
20 
21 #include <functional>
22 #include <memory>
23 
25 {
27 class live_model_impl; //
28 class ranking_response; //
29 class api_status; //
30  //
31 namespace model_management
32 { //
33 class i_data_transport; //
34 class i_model; //
35 } // namespace model_management
36  //
37 namespace utility
38 { //
39 class configuration; //
40 } // namespace utility
42 
43 // Reinforcement learning client
52 {
53 public:
62  using error_fn = void (*)(const api_status&, void*);
63 
77  explicit live_model(const utility::configuration& config, error_fn fn = nullptr, void* err_context = nullptr,
78  trace_logger_factory_t* trace_factory = &trace_logger_factory,
79  data_transport_factory_t* t_factory = &data_transport_factory, model_factory_t* m_factory = &model_factory,
80  sender_factory_t* s_factory = &sender_factory,
81  time_provider_factory_t* time_prov_factory = &time_provider_factory);
82 
95  explicit live_model(const utility::configuration& config, std::function<void(const api_status&)> error_cb,
96  trace_logger_factory_t* trace_factory = &trace_logger_factory,
97  data_transport_factory_t* t_factory = &data_transport_factory, model_factory_t* m_factory = &model_factory,
98  sender_factory_t* s_factory = &sender_factory,
99  time_provider_factory_t* time_prov_factory = &time_provider_factory);
100 
108  int init(api_status* status = nullptr);
109 
122  int choose_rank(const char* event_id, string_view context_json, ranking_response& resp, api_status* status = nullptr);
123 
137  string_view context_json, ranking_response& resp, api_status* status = nullptr); // event_id is auto-generated
138 
152  int choose_rank(const char* event_id, string_view context_json, unsigned int flags, ranking_response& resp,
153  api_status* status = nullptr);
154 
167  int choose_rank(string_view context_json, unsigned int flags, ranking_response& resp,
168  api_status* status = nullptr); // event_id is auto-generated
169 
183  RL_DEPRECATED("New unified example builder interface is coming")
184  int request_continuous_action(const char* event_id, string_view context_json, unsigned int flags,
185  continuous_action_response& response, api_status* status = nullptr);
186 
199  RL_DEPRECATED("New unified example builder interface is coming")
200  int request_continuous_action(const char* event_id, string_view context_json, continuous_action_response& response,
201  api_status* status = nullptr);
202 
215  RL_DEPRECATED("New unified example builder interface is coming")
217  string_view context_json, unsigned int flags, continuous_action_response& response, api_status* status = nullptr);
218 
230  RL_DEPRECATED("New unified example builder interface is coming")
232  string_view context_json, continuous_action_response& response, api_status* status = nullptr);
233 
248  RL_DEPRECATED("New interface unifying CB with CCB is coming")
250  string_view context_json, unsigned int flags, decision_response& resp, api_status* status = nullptr);
251 
265  RL_DEPRECATED("New interface unifying CB with CCB is coming")
266  int request_decision(string_view context_json, decision_response& resp, api_status* status = nullptr);
267 
283  RL_DEPRECATED("New unified example builder interface is coming")
284  int request_multi_slot_decision(const char* event_id, string_view context_json, unsigned int flags,
285  multi_slot_response& resp, api_status* status = nullptr);
286  RL_DEPRECATED("New unified example builder interface is coming")
288  const char* event_id, string_view context_json, multi_slot_response& resp, api_status* status = nullptr);
289  RL_DEPRECATED("New unified example builder interface is coming")
291  string_view context_json, unsigned int flags, multi_slot_response& resp, api_status* status = nullptr);
292  RL_DEPRECATED("New unified example builder interface is coming")
293  int request_multi_slot_decision(string_view context_json, multi_slot_response& resp, api_status* status = nullptr);
294 
295  RL_DEPRECATED("New unified example builder interface is coming")
296  int request_multi_slot_decision(const char* event_id, string_view context_json, unsigned int flags,
297  multi_slot_response& resp, const int* baseline_actions, size_t baseline_actions_size,
298  api_status* status = nullptr);
299 
300  RL_DEPRECATED("New unified example builder interface is coming")
301  int request_multi_slot_decision(const char* event_id, string_view context_json, unsigned int flags,
302  multi_slot_response_detailed& resp, api_status* status = nullptr);
303  RL_DEPRECATED("New unified example builder interface is coming")
305  const char* event_id, string_view context_json, multi_slot_response_detailed& resp, api_status* status = nullptr);
306  RL_DEPRECATED("New unified example builder interface is coming")
308  string_view context_json, unsigned int flags, multi_slot_response_detailed& resp, api_status* status = nullptr);
309  RL_DEPRECATED("New unified example builder interface is coming")
311  string_view context_json, multi_slot_response_detailed& resp, api_status* status = nullptr);
312 
313  RL_DEPRECATED("New unified example builder interface is coming")
314  int request_multi_slot_decision(const char* event_id, string_view context_json, unsigned int flags,
315  multi_slot_response_detailed& resp, const int* baseline_actions, size_t baseline_actions_size,
316  api_status* status = nullptr);
317 
318  // multistep
319  int request_episodic_decision(const char* event_id, const char* previous_id, string_view context_json,
320  ranking_response& resp, episode_state& episode, api_status* status = nullptr);
321  int request_episodic_decision(const char* event_id, const char* previous_id, string_view context_json,
322  unsigned int flags, ranking_response& resp, episode_state& episode, api_status* status = nullptr);
323 
332  int report_action_taken(const char* event_id, api_status* status = nullptr);
333 
343  int report_action_taken(const char* primary_id, const char* secondary_id, api_status* status = nullptr);
344 
354  int report_outcome(const char* event_id, const char* outcome, api_status* status = nullptr);
355 
365  int report_outcome(const char* event_id, float outcome, api_status* status = nullptr);
366 
379  int report_outcome(const char* primary_id, int secondary_id, float outcome, api_status* status = nullptr);
380 
393  int report_outcome(const char* primary_id, const char* secondary_id, float outcome, api_status* status = nullptr);
394 
407  int report_outcome(const char* primary_id, int secondary_id, const char* outcome, api_status* status = nullptr);
408 
422  const char* primary_id, const char* secondary_id, const char* outcome, api_status* status = nullptr);
423 
424  /*
425  * @brief Refreshes the model if it has background refresh disabled.
426  * @param status Optional field with detailed string description if there is an error
427  * @return int Return error code. This will also be returned in the api_status object
428  */
429  int refresh_model(api_status* status = nullptr);
430 
441  template <typename ErrCntxt>
442  using error_fn_t = void (*)(const api_status&, ErrCntxt*);
443 
458  template <typename ErrCntxt>
459  explicit live_model(const utility::configuration& config, error_fn_t<ErrCntxt> fn, ErrCntxt* err_context = nullptr,
460  trace_logger_factory_t* trace_factory = &trace_logger_factory,
461  data_transport_factory_t* t_factory = &data_transport_factory, model_factory_t* m_factory = &model_factory,
462  sender_factory_t* s_factory = &sender_factory,
463  time_provider_factory_t* time_prov_factory = &time_provider_factory);
464 
468  live_model(live_model&& other) noexcept;
469 
473  live_model& operator=(live_model&& other) noexcept;
474 
475  live_model(
476  const live_model&) = delete;
477  live_model& operator=(
478  live_model&) = delete;
479 
481 
482 private:
483  std::unique_ptr<live_model_impl>
484  _pimpl;
485  bool _initialized = false;
487  const std::vector<int> default_baseline_vector = std::vector<int>();
488  static std::vector<int> c_array_to_vector(
489  const int* c_array, size_t array_size);
490 };
491 
506 template <typename ErrCntxt>
507 live_model::live_model(const utility::configuration& config, error_fn_t<ErrCntxt> fn, ErrCntxt* err_context,
508  trace_logger_factory_t* trace_factory, data_transport_factory_t* t_factory, model_factory_t* m_factory,
509  sender_factory_t* s_factory, time_provider_factory_t* time_prov_factory)
510  : live_model(config, std::bind(fn, std::placeholders::_1, err_context), trace_factory, t_factory, m_factory,
511  s_factory, time_prov_factory)
512 {
513 }
514 } // namespace reinforcement_learning
Report status of all API calls.
Definition: api_status.h:22
Interface class for the Inference API.
Definition: live_model.h:52
void(*)(const api_status &, void *) error_fn
Error callback function. When live_model is constructed, a background error callback and a context (v...
Definition: live_model.h:62
int choose_rank(string_view context_json, unsigned int flags, ranking_response &resp, api_status *status=nullptr)
Choose an action, given a list of actions, action features and context features. The inference librar...
int request_decision(string_view context_json, unsigned int flags, decision_response &resp, api_status *status=nullptr)
(DEPRECATED) Choose an action from the given set for each slot, given a list of actions,...
int choose_rank(string_view context_json, ranking_response &resp, api_status *status=nullptr)
Choose an action, given a list of actions, action features and context features. The inference librar...
live_model(const utility::configuration &config, error_fn fn=nullptr, void *err_context=nullptr, trace_logger_factory_t *trace_factory=&trace_logger_factory, data_transport_factory_t *t_factory=&data_transport_factory, model_factory_t *m_factory=&model_factory, sender_factory_t *s_factory=&sender_factory, time_provider_factory_t *time_prov_factory=&time_provider_factory)
Construct a new live model object.
void(*)(const api_status &, ErrCntxt *) error_fn_t
Error callback function. When live_model is constructed, a background error callback and a context (v...
Definition: live_model.h:442
int choose_rank(const char *event_id, string_view context_json, unsigned int flags, ranking_response &resp, api_status *status=nullptr)
Choose an action, given a list of actions, action features and context features. The inference librar...
int init(api_status *status=nullptr)
Initialize inference library. Initialize the library and start the background threads used for model ...
int choose_rank(const char *event_id, string_view context_json, ranking_response &resp, api_status *status=nullptr)
Choose an action, given a list of actions, action features and context features. The inference librar...
int request_multi_slot_decision(const char *event_id, string_view context_json, unsigned int flags, multi_slot_response &resp, api_status *status=nullptr)
(DEPRECATED) Choose an action from the given set for each slot, given a list of actions,...
int report_action_taken(const char *event_id, api_status *status=nullptr)
Report that action was taken.
int report_outcome(const char *event_id, const char *outcome, api_status *status=nullptr)
Report the outcome for the top action.
live_model(const utility::configuration &config, std::function< void(const api_status &)> error_cb, trace_logger_factory_t *trace_factory=&trace_logger_factory, data_transport_factory_t *t_factory=&data_transport_factory, model_factory_t *m_factory=&model_factory, sender_factory_t *s_factory=&sender_factory, time_provider_factory_t *time_prov_factory=&time_provider_factory)
Construct a new live model object.
int request_continuous_action(const char *event_id, string_view context_json, unsigned int flags, continuous_action_response &response, api_status *status=nullptr)
(DEPRECATED) Choose an action from a continuous range, given a list of context features The inference...
choose_rank() returns the action choice using ranking_response. ranking_response contains all the act...
Definition: ranking_response.h:29
Configuration class to initialize the API Represents a collection of (name,value) pairs used to confi...
Definition: configuration.h:31
Definition of all API error return codes and descriptions.
[Error Generator]
Definition: live_model.h:25
ranking_response definition. ranking_response is returned from choose_rank call. It contains the chos...