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

Last change on this file since 1274 was 1000, checked in by Jari Häkkinen, 14 years ago

trac moved to new location.

  • 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, Markus Ringnér, Peter Johansson
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "ConsensusInputRanker.h"
28#include "InputRanker.h"
29#include "IRRetrieve.h"
30#include "MatrixLookup.h"
31#include "MatrixLookupWeighted.h"
32#include "Sampler.h"
33#include "Target.h"
34#include "yat/statistics/Score.h"
35#include "yat/statistics/utility.h"
36#include "yat/statistics/VectorFunction.h"
37#include "yat/utility/stl_utility.h"
38
39#include <cassert>
40#include <functional>
41#include <iostream>
42#include <utility>
43#include <vector>
44#include <cmath>
45
46namespace theplu {
47namespace yat {
48namespace classifier { 
49
50  ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever,
51                                             const statistics::VectorFunction&
52                                             vf)
53    : retriever_(retriever), vec_func_(vf)
54  {
55  }
56
57
58  void ConsensusInputRanker::add(const Sampler& sampler, 
59                                 const MatrixLookup& data, 
60                                 const statistics::Score& score)
61  {
62    assert(sampler.size());
63    assert(id_.empty() || id_.size()==data.rows());
64    input_rankers_.reserve(sampler.size()+input_rankers_.size());
65    id_.resize(data.rows());
66    rank_.resize(data.rows());
67    for (size_t i=0; i<sampler.size(); ++i){
68      input_rankers_.push_back(InputRanker(MatrixLookup(data,sampler.training_index(i), false), 
69                                           sampler.training_target(i), 
70                                           score));       
71    }
72    update();
73  }
74
75  void ConsensusInputRanker::add(const Sampler& sampler, 
76                                 const MatrixLookupWeighted& data, 
77                                 const statistics::Score& score)
78  {
79    assert(id_.empty() || id_.size()==data.rows());
80    id_.resize(data.rows());
81    rank_.resize(data.rows());
82    for (size_t i=0; i<sampler.size(); ++i){
83      input_rankers_.push_back(InputRanker(MatrixLookupWeighted(data,sampler.training_index(i), false), 
84                                           sampler.training_target(i), 
85                                           score));       
86    }
87    update();
88  }
89
90  void ConsensusInputRanker::add(const InputRanker& ir)
91  {
92    assert(id_.empty() || id_.size()==ir.id().size());
93    input_rankers_.push_back(ir);
94  }
95
96  size_t ConsensusInputRanker::id(size_t i) const
97  {
98    assert(i<id_.size());
99    return id_[i];
100  }
101
102  const InputRanker& ConsensusInputRanker::input_ranker(size_t i) const
103  {
104    assert(i<input_rankers_.size());
105    return input_rankers_[i];
106  }
107
108  size_t ConsensusInputRanker::rank(size_t i) const
109  {
110    return rank_[i];
111  }
112
113
114  void ConsensusInputRanker::reserve(size_t n)
115  {
116    input_rankers_.reserve(n);
117  }
118
119
120  void ConsensusInputRanker::update(void)
121  {
122    // Sorting with respect to VectorFunction(info) where info is a
123    // vector and each element contains infomation retrieved with
124    // retriever_ from each InputRanker
125    std::vector<std::pair<double,size_t> > cons_rank;
126    cons_rank.reserve(id_.size());
127    for (size_t i=0; i<id_.size(); i++){ 
128      std::vector<double> scores;
129      scores.reserve(input_rankers_.size());
130      for (size_t j=0; j<input_rankers_.size(); j++) {
131        scores.push_back(retriever_(input_rankers_[j],i));
132      }
133      cons_rank.push_back(std::make_pair(vec_func_(scores), i));
134    }
135   
136    //sort cons_rank and assign id_ and rank_
137    sort(cons_rank.begin(), cons_rank.end(),
138         std::greater<std::pair<double, size_t> >()); 
139         
140    for (size_t i=0; i<cons_rank.size(); i++){
141      assert(i<id_.size());
142      id_[i]=cons_rank[i].second;
143      assert(id_[i]<rank_.size());
144      rank_[id_[i]]=i;
145    }
146  }
147
148
149}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.