QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
optimization.h
Go to the documentation of this file.
1 /*
2  * QuickRank - A C++ suite of Learning to Rank algorithms
3  * Webpage: http://quickrank.isti.cnr.it/
4  * Contact: quickrank@isti.cnr.it
5  *
6  * Unless explicitly acquired and licensed from Licensor under another
7  * license, the contents of this file are subject to the Reciprocal Public
8  * License ("RPL") Version 1.5, or subsequent versions as allowed by the RPL,
9  * and You may not copy or use this file in either source code or executable
10  * form, except in compliance with the terms and conditions of the RPL.
11  *
12  * All software distributed under the RPL is provided strictly on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND
14  * LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
15  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16  * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific
17  * language governing rights and limitations under the RPL.
18  *
19  * Contributor:
20  * HPC. Laboratory - ISTI - CNR - http://hpc.isti.cnr.it/
21  */
22 #pragma once
23 
24 #include <memory>
25 
26 #include "data/dataset.h"
27 #include "metric/ir/metric.h"
28 #include "learning/ltr_algorithm.h"
29 
30 namespace quickrank {
31 namespace optimization {
32 
33 class Optimization {
34 
35  public:
36 
37  enum class OptimizationAlgorithm {
38  EPRUNING
39  };
40 
41  Optimization() { };
42 
43  /// Generates a LTR_Algorithm instance from a previously saved XML model.
44  Optimization(const pugi::xml_document &model);
45 
46  virtual ~Optimization() = default;
47 
48  /// Avoid inefficient copy constructor
49  Optimization(const Optimization &other) = delete;
50  /// Avoid inefficient copy assignment
51  Optimization &operator=(const Optimization &) = delete;
52 
53  /// Returns the name of the optimizer.
54  virtual std::string name() const = 0;
55 
56  virtual bool need_partial_score_dataset() const = 0;
57 
58  /// Executes the optimization process.
59  ///
60  /// \param training_dataset The training dataset.
61  /// \param validation_dataset The validation dataset.
62  /// \param metric The metric to be optimized.
63  /// \param partial_save Allows to save a partial model every given number of iterations.
64  /// \param model_filename The file where the model, and the partial models, are saved.
65  virtual void optimize(std::shared_ptr<learning::LTR_Algorithm> algo,
66  std::shared_ptr<data::Dataset> training_dataset,
67  std::shared_ptr<data::Dataset> validation_dataset,
68  std::shared_ptr<metric::ir::Metric> metric,
69  size_t partial_save,
70  const std::string model_filename) = 0;
71 
72  /// Save the current model to the output_file.
73  ///
74  /// \param model_filename The output file name.
75  /// \param suffix The suffix used to identify partial model saves.
76  virtual void save(std::string model_filename, int suffix = -1) const;
77 
78  /// Return the xml model representing the current object
79  virtual pugi::xml_document *get_xml_model() const = 0;
80 
81  virtual bool is_pre_learning() const = 0;
82 
83  /// Load a model from a given XML file.
84  ///
85  /// \param model_filename The input file name.
86  static std::shared_ptr<Optimization> load_model_from_file(
87  std::string model_filename);
88 
89  // Static methods
90  static const std::vector<std::string> optimizationAlgorithmNames;
91 
93  auto i_item = std::find(optimizationAlgorithmNames.cbegin(),
94  optimizationAlgorithmNames.cend(),
95  name);
96  if (i_item != optimizationAlgorithmNames.cend()) {
97 
98  return OptimizationAlgorithm(std::distance(optimizationAlgorithmNames.cbegin(),
99  i_item));
100  }
101 
102  throw std::invalid_argument("pruning method " + name + " is not valid");
103  }
104 
105  static std::string getPruningMethod(OptimizationAlgorithm optAlgo) {
106  return optimizationAlgorithmNames[static_cast<int>(optAlgo)];
107  }
108 
109  protected:
110 
111  /// The output stream operator.
112  friend std::ostream &operator<<(std::ostream &os, const Optimization &a) {
113  return a.put(os);
114  }
115 
116  /// Prints the description of Algorithm, including its parameters
117  virtual std::ostream &put(std::ostream &os) const = 0;
118 
119 };
120 
121 } // namespace optimization
122 } // namespace quickrank
Definition: dataset.cc:28
Definition: optimization.h:33
virtual bool is_pre_learning() const =0
static std::shared_ptr< Optimization > load_model_from_file(std::string model_filename)
Load a model from a given XML file.
Definition: optimization.cc:49
virtual bool need_partial_score_dataset() const =0
virtual void optimize(std::shared_ptr< learning::LTR_Algorithm > algo, std::shared_ptr< data::Dataset > training_dataset, std::shared_ptr< data::Dataset > validation_dataset, std::shared_ptr< metric::ir::Metric > metric, size_t partial_save, const std::string model_filename)=0
Executes the optimization process.
static const std::vector< std::string > optimizationAlgorithmNames
Definition: optimization.h:90
friend std::ostream & operator<<(std::ostream &os, const Optimization &a)
The output stream operator.
Definition: optimization.h:112
virtual pugi::xml_document * get_xml_model() const =0
Return the xml model representing the current object.
OptimizationAlgorithm
Definition: optimization.h:37
virtual std::string name() const =0
Returns the name of the optimizer.
static std::string getPruningMethod(OptimizationAlgorithm optAlgo)
Definition: optimization.h:105
virtual void save(std::string model_filename, int suffix=-1) const
Save the current model to the output_file.
Definition: optimization.cc:36
Optimization & operator=(const Optimization &)=delete
Avoid inefficient copy assignment.
Optimization()
Definition: optimization.h:41
virtual std::ostream & put(std::ostream &os) const =0
Prints the description of Algorithm, including its parameters.
static OptimizationAlgorithm getOptimizationAlgorithm(std::string name)
Definition: optimization.h:92