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

Last change on this file since 828 was 828, checked in by Peter, 15 years ago

Generalized ConsenusInputRanker?, Fixes #151

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