source: trunk/yat/classifier/ConsensusInputRanker.cc @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 13 years ago

converted files to utf-8. fixes #577

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