QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
coordinate_ascent.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  * Contributors:
20  * - Andrea Battistini (andreabattistini@hotmail.com)
21  * - Chiara Pierucci (chiarapierucci14@gmail.com)
22  * - Claudio Lucchese (claudio.lucchese@isti.cnr.it)
23  */
24 #pragma once
25 
26 #include <memory>
27 
28 #include "data/dataset.h"
29 #include "metric/ir/metric.h"
30 #include "learning/ltr_algorithm.h"
31 
32 namespace quickrank {
33 namespace learning {
34 namespace linear {
35 
36 /// This implements the Coordinate Ascent algorithm.
37 ///
38 /// Metzler, D., Croft, W.B.: Linear feature-based models for information retrieval.
39 /// Information Retrieval 10(3), 257–274 (2007)
41 
42  public:
43  CoordinateAscent(unsigned int num_points, double window_size,
44  double reduction_factor, unsigned int max_iterations,
45  unsigned int max_failed_vali);
46 
47  CoordinateAscent(const pugi::xml_document &model);
48 
49  virtual ~CoordinateAscent();
50 
51  /// Returns the name of the ranker.
52  virtual std::string name() const {
53  return NAME_;
54  }
55 
56  static const std::string NAME_;
57 
58  /// Executes the learning process.
59  ///
60  /// \param training_dataset The training dataset.
61  /// \param validation_dataset The validation training 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 learn(std::shared_ptr<data::Dataset> training_dataset,
66  std::shared_ptr<data::Dataset> validation_dataset,
67  std::shared_ptr<metric::ir::Metric> metric,
68  size_t partial_save,
69  const std::string model_filename);
70 
71  /// Returns the score of a given document.
72  virtual Score score_document(const Feature *d) const;
73 
74  /// Return the xml model representing the current object
75  virtual pugi::xml_document *get_xml_model() const;
76 
77  /// Returns the learned weights
78  virtual std::shared_ptr<std::vector<double>> get_weights() const {
79 
80  return std::shared_ptr<std::vector<double>>(
81  new std::vector<double>(best_weights_));
82  }
83 
84  virtual bool update_weights(std::shared_ptr<std::vector<double>> weights);
85 
86  private:
87  std::vector<double> best_weights_;
88 
89  unsigned int num_samples_;
90  double window_size_;
92  unsigned int max_iterations_;
93  unsigned int max_failed_vali_;
94 
95  /// The output stream operator.
96  friend std::ostream &operator<<(std::ostream &os, const CoordinateAscent &a) {
97  return a.put(os);
98  }
99 
100  /// Prints the description of Algorithm, including its parameters
101  virtual std::ostream &put(std::ostream &os) const;
102 };
103 
104 } // namespace linear
105 } // namespace learning
106 } // namespace quickrank
Definition: dataset.cc:28
CoordinateAscent(unsigned int num_points, double window_size, double reduction_factor, unsigned int max_iterations, unsigned int max_failed_vali)
Definition: coordinate_ascent.cc:56
virtual Score score_document(const Feature *d) const
Returns the score of a given document.
Definition: coordinate_ascent.cc:250
virtual bool update_weights(std::shared_ptr< std::vector< double >> weights)
Update the weights for the ensemble models (only).
Definition: coordinate_ascent.cc:258
double reduction_factor_
Definition: coordinate_ascent.h:91
friend std::ostream & operator<<(std::ostream &os, const CoordinateAscent &a)
The output stream operator.
Definition: coordinate_ascent.h:96
Definition: ltr_algorithm.h:33
unsigned int max_iterations_
Definition: coordinate_ascent.h:92
float Feature
data type for instance predicted label
Definition: types.h:31
virtual std::shared_ptr< std::vector< double > > get_weights() const
Returns the learned weights.
Definition: coordinate_ascent.h:78
std::vector< double > best_weights_
Definition: coordinate_ascent.h:87
static const std::string NAME_
Definition: coordinate_ascent.h:56
unsigned int max_failed_vali_
Definition: coordinate_ascent.h:93
double Score
data type for instance truth label
Definition: types.h:30
virtual void learn(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)
Executes the learning process.
Definition: coordinate_ascent.cc:116
virtual pugi::xml_document * get_xml_model() const
Return the xml model representing the current object.
Definition: coordinate_ascent.cc:271
virtual ~CoordinateAscent()
Definition: coordinate_ascent.cc:102
unsigned int num_samples_
Definition: coordinate_ascent.h:89
double window_size_
Definition: coordinate_ascent.h:90
virtual std::ostream & put(std::ostream &os) const
Prints the description of Algorithm, including its parameters.
Definition: coordinate_ascent.cc:105
virtual std::string name() const
Returns the name of the ranker.
Definition: coordinate_ascent.h:52
This implements the Coordinate Ascent algorithm.
Definition: coordinate_ascent.h:40