source: trunk/yat/classifier/ConsensusInputRanker.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 4.2 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010, 2012 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "ConsensusInputRanker.h"
29#include "InputRanker.h"
30#include "IRRetrieve.h"
31#include "MatrixLookup.h"
32#include "MatrixLookupWeighted.h"
33#include "Sampler.h"
34#include "Target.h"
35#include "yat/statistics/Score.h"
36#include "yat/statistics/utility.h"
37#include "yat/statistics/VectorFunction.h"
38#include "yat/utility/stl_utility.h"
39
40#include <cassert>
41#include <functional>
42#include <iostream>
43#include <utility>
44#include <vector>
45#include <cmath>
46
47namespace theplu {
48namespace yat {
49namespace classifier { 
50
51  ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever,
52                                             const statistics::VectorFunction&
53                                             vf)
54    : retriever_(retriever), vec_func_(vf)
55  {
56  }
57
58
59  void ConsensusInputRanker::add(const Sampler& sampler, 
60                                 const MatrixLookup& data, 
61                                 const statistics::Score& score)
62  {
63    assert(sampler.size());
64    assert(id_.empty() || id_.size()==data.rows());
65    input_rankers_.reserve(sampler.size()+input_rankers_.size());
66    id_.resize(data.rows());
67    rank_.resize(data.rows());
68    for (size_t i=0; i<sampler.size(); ++i){
69      input_rankers_.push_back(InputRanker(MatrixLookup(data,sampler.training_index(i), false), 
70                                           sampler.training_target(i), 
71                                           score));       
72    }
73    update();
74  }
75
76  void ConsensusInputRanker::add(const Sampler& sampler, 
77                                 const MatrixLookupWeighted& data, 
78                                 const statistics::Score& score)
79  {
80    assert(id_.empty() || id_.size()==data.rows());
81    id_.resize(data.rows());
82    rank_.resize(data.rows());
83    for (size_t i=0; i<sampler.size(); ++i) {
84      MatrixLookupWeighted mlw(data, utility::Index(data.rows()), 
85                               sampler.training_index(i));
86      input_rankers_.push_back(InputRanker(mlw, 
87                                           sampler.training_target(i), 
88                                           score));       
89    }
90    update();
91  }
92
93  void ConsensusInputRanker::add(const InputRanker& ir)
94  {
95    assert(id_.empty() || id_.size()==ir.id().size());
96    input_rankers_.push_back(ir);
97  }
98
99  size_t ConsensusInputRanker::id(size_t i) const
100  {
101    assert(i<id_.size());
102    return id_[i];
103  }
104
105  const InputRanker& ConsensusInputRanker::input_ranker(size_t i) const
106  {
107    assert(i<input_rankers_.size());
108    return input_rankers_[i];
109  }
110
111  size_t ConsensusInputRanker::rank(size_t i) const
112  {
113    return rank_[i];
114  }
115
116
117  void ConsensusInputRanker::reserve(size_t n)
118  {
119    input_rankers_.reserve(n);
120  }
121
122
123  void ConsensusInputRanker::update(void)
124  {
125    // Sorting with respect to VectorFunction(info) where info is a
126    // vector and each element contains infomation retrieved with
127    // retriever_ from each InputRanker
128    std::vector<std::pair<double,size_t> > cons_rank;
129    cons_rank.reserve(id_.size());
130    for (size_t i=0; i<id_.size(); i++){ 
131      std::vector<double> scores;
132      scores.reserve(input_rankers_.size());
133      for (size_t j=0; j<input_rankers_.size(); j++) {
134        scores.push_back(retriever_(input_rankers_[j],i));
135      }
136      cons_rank.push_back(std::make_pair(vec_func_(scores), i));
137    }
138   
139    //sort cons_rank and assign id_ and rank_
140    sort(cons_rank.begin(), cons_rank.end(),
141         std::greater<std::pair<double, size_t> >()); 
142         
143    for (size_t i=0; i<cons_rank.size(); i++){
144      assert(i<id_.size());
145      id_[i]=cons_rank[i].second;
146      assert(id_[i]<rank_.size());
147      rank_[id_[i]]=i;
148    }
149  }
150
151
152}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.