QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
driver.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 "metric/ir/metric.h"
27 #include "learning/ltr_algorithm.h"
29 
30 #include "io/generate_vpred.h"
32 #include "io/generate_oblivious.h"
33 
34 #include "paramsmap/paramsmap.h"
35 
36 namespace quickrank {
37 namespace driver {
38 
39 /**
40  * This class implements the main logic of the quickrank application.
41  */
42 class Driver {
43  public:
44  Driver();
45  virtual ~Driver();
46 
47  /// Implements the main logic of the quickrank application, detecting
48  /// the metrics to adopt and the phases to execute (train/validation/test).
49  /// Returns the exit code of the application
50  ///
51  /// \param, vm The Variable mapping of CLI options (boost object)
52  static int run(ParamsMap &pmap);
53 
54  private:
55  /// Runs train/validation of \a algo by optimizing \a train_metric
56  /// and then measures \a test_metric on the test data.
57  ///
58  /// \param algo The L-T-R algorithm to be tested.
59  /// \param train_metric The metric optimized during training.
60  /// \param training_filename The training dataset.
61  /// \param validation_filename The validation dataset.
62  /// If empty, validation is not used.
63  /// \param output_filename Model output file.
64  /// If empty, no output file is written.
65  /// \param npartialsave Allows to save a partial model every given number of iterations.
66  static void training_phase(
67  std::shared_ptr<learning::LTR_Algorithm> algo,
68  std::shared_ptr<metric::ir::Metric> train_metric,
69  std::shared_ptr<quickrank::data::Dataset> training_dataset,
70  std::shared_ptr<quickrank::data::Dataset> validation_dataset,
71  const std::string output_filename,
72  const size_t npartialsave);
73 
74  /// Runs train/validation of \a algo by optimizing \a train_metric
75  /// and then measures \a test_metric on the test data.
76  ///
77  /// \param algo The L-T-R algorithm to be tested.
78  /// \param train_metric The metric optimized during training.
79  /// \param training_filename The training dataset.
80  /// \param validation_filename The validation dataset.
81  /// If empty, validation is not used.
82  /// \param output_filename Model output file.
83  /// If empty, no output file is written.
84  /// \param npartialsave Allows to save a partial model every given number of iterations.
85  static void optimization_phase(
86  std::shared_ptr<quickrank::optimization::Optimization> opt_algorithm,
87  std::shared_ptr<learning::LTR_Algorithm> ranking_algo,
88  std::shared_ptr<metric::ir::Metric> train_metric,
89  std::shared_ptr<quickrank::data::Dataset> training_dataset,
90  std::shared_ptr<quickrank::data::Dataset> validation_dataset,
91  std::string training_partial_filename,
92  std::string validation_partial_filename,
93  const std::string output_filename,
94  const std::string opt_algo_model_filename,
95  const size_t npartialsave);
96 
97  /// Runs the learned or loaded model on the test data
98  /// and then measures \a test_metric on the test data.
99  ///
100  /// \param algo The L-T-R algorithm to be tested.
101  /// \param test_metric The metric measured on the test data.
102  /// \param test_filename The test dataset.
103  /// If empty, no performance is measured on the test set.
104  /// \param scores_filename The output scores file.
105  /// If set save the scores computed for the test set.
106  /// \param verbose If True saves an SVML-like file with the score of each ranker in the ensemble.
107  /// NB. Works only for ensembles.
108  static void testing_phase(
109  std::shared_ptr<learning::LTR_Algorithm> algo,
110  std::shared_ptr<metric::ir::Metric> test_metric,
111  std::shared_ptr<quickrank::data::Dataset> test_dataset,
112  const std::string scores_filename,
113  const bool detailed_testing);
114 
115  static std::shared_ptr<quickrank::data::Dataset> load_dataset(
116  const std::string dataset_filename,
117  const std::string dataset_label);
118 
119  static std::shared_ptr<data::Dataset> extract_partial_scores(
120  std::shared_ptr<learning::LTR_Algorithm> algo,
121  std::shared_ptr<data::Dataset> input_dataset);
122 };
123 
124 } // namespace driver
125 } // namespace quickrank
126 
This class implements the main logic of the quickrank application.
Definition: driver.h:42
Definition: dataset.cc:28
virtual ~Driver()
Definition: driver.cc:41
static int run(ParamsMap &pmap)
Implements the main logic of the quickrank application, detecting the metrics to adopt and the phases...
Definition: driver.cc:44
static std::shared_ptr< data::Dataset > extract_partial_scores(std::shared_ptr< learning::LTR_Algorithm > algo, std::shared_ptr< data::Dataset > input_dataset)
Definition: driver.cc:384
static void testing_phase(std::shared_ptr< learning::LTR_Algorithm > algo, std::shared_ptr< metric::ir::Metric > test_metric, std::shared_ptr< quickrank::data::Dataset > test_dataset, const std::string scores_filename, const bool detailed_testing)
Runs the learned or loaded model on the test data and then measures test_metric on the test data...
Definition: driver.cc:312
Driver()
Definition: driver.cc:38
static std::shared_ptr< quickrank::data::Dataset > load_dataset(const std::string dataset_filename, const std::string dataset_label)
Definition: driver.cc:360
static void training_phase(std::shared_ptr< learning::LTR_Algorithm > algo, std::shared_ptr< metric::ir::Metric > train_metric, std::shared_ptr< quickrank::data::Dataset > training_dataset, std::shared_ptr< quickrank::data::Dataset > validation_dataset, const std::string output_filename, const size_t npartialsave)
Runs train/validation of algo by optimizing train_metric and then measures test_metric on the test da...
Definition: driver.cc:218
static void optimization_phase(std::shared_ptr< quickrank::optimization::Optimization > opt_algorithm, std::shared_ptr< learning::LTR_Algorithm > ranking_algo, std::shared_ptr< metric::ir::Metric > train_metric, std::shared_ptr< quickrank::data::Dataset > training_dataset, std::shared_ptr< quickrank::data::Dataset > validation_dataset, std::string training_partial_filename, std::string validation_partial_filename, const std::string output_filename, const std::string opt_algo_model_filename, const size_t npartialsave)
Runs train/validation of algo by optimizing train_metric and then measures test_metric on the test da...
Definition: driver.cc:237