MultitaskKernelNormalizer.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2009 Christian Widmer
00008  * Copyright (C) 2009 Max-Planck-Society
00009  */
00010 
00011 
00012 #ifndef _MULTITASKKERNELNORMALIZER_H___
00013 #define _MULTITASKKERNELNORMALIZER_H___
00014 
00015 #include "kernel/KernelNormalizer.h"
00016 #include "kernel/Kernel.h"
00017 
00018 namespace shogun
00019 {
00029 class CMultitaskKernelNormalizer : public CKernelNormalizer
00030 {
00031 
00032     public:
00033 
00036         CMultitaskKernelNormalizer()
00037         {
00038         }
00039 
00045         CMultitaskKernelNormalizer(std::vector<int32_t> task_lhs, std::vector<int32_t> task_rhs)
00046         {
00047 
00048             set_task_vector_lhs(task_lhs);
00049             set_task_vector_rhs(task_rhs);
00050 
00051             //run sanity checks
00052 
00053             //invoke copy contructor
00054             std::vector<int32_t> unique_tasks_lhs = std::vector<int32_t>(task_vector_lhs.begin(), task_vector_lhs.end());
00055             std::vector<int32_t> unique_tasks_rhs = std::vector<int32_t>(task_vector_rhs.begin(), task_vector_rhs.end());
00056 
00057 
00058             //reorder tasks with unique prefix
00059             std::vector<int32_t>::iterator endLocation_lhs = std::unique(unique_tasks_lhs.begin(), unique_tasks_lhs.end());
00060             std::vector<int32_t>::iterator endLocation_rhs = std::unique(unique_tasks_rhs.begin(), unique_tasks_rhs.end());
00061 
00062 
00063             //count unique tasks
00064             int32_t num_unique_tasks_lhs = std::distance(unique_tasks_lhs.begin(), endLocation_lhs);
00065             int32_t num_unique_tasks_rhs = std::distance(unique_tasks_rhs.begin(), endLocation_rhs);
00066 
00067 
00068             //initialize members (lhs has always more or equally many tasks than rhs)
00069             num_tasks = num_unique_tasks_lhs;
00070             dependency_matrix = std::vector<float64_t>(num_tasks * num_tasks);
00071 
00072         }
00073 
00074 
00076         virtual ~CMultitaskKernelNormalizer()
00077         {
00078         }
00079 
00082         virtual bool init(CKernel* k)
00083         {
00084             ASSERT(k);
00085             int32_t num_lhs=k->get_num_vec_lhs();
00086             int32_t num_rhs=k->get_num_vec_rhs();
00087             ASSERT(num_lhs>0);
00088             ASSERT(num_rhs>0);
00089 
00090             return true;
00091         }
00092 
00098         inline virtual float64_t normalize(
00099             float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00100         {
00101 
00102             //lookup tasks
00103             int32_t task_idx_lhs = task_vector_lhs[idx_lhs];
00104             int32_t task_idx_rhs = task_vector_rhs[idx_rhs];
00105 
00106             //lookup similarity
00107             float64_t task_similarity = get_task_similarity(task_idx_lhs, task_idx_rhs);
00108 
00109             std::cout << "lhs: " << task_idx_lhs << ", rhs: " << task_idx_rhs << ", sim: " << task_similarity << std::endl;
00110 
00111             //take task similarity into account
00112             float64_t similarity = value * task_similarity;
00113 
00114             return similarity;
00115 
00116         }
00117 
00122         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00123         {
00124             SG_ERROR("normalize_lhs not implemented");
00125             return 0;
00126         }
00127 
00132         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00133         {
00134             SG_ERROR("normalize_rhs not implemented");
00135             return 0;
00136         }
00137 
00138     public:
00139 
00141         std::vector<int32_t> get_task_vector_lhs() const {
00142             return task_vector_lhs;
00143         }
00144 
00146         void set_task_vector_lhs(std::vector<int32_t> vec) {
00147             task_vector_lhs = vec;
00148         }
00149 
00151         std::vector<int32_t> get_task_vector_rhs() const {
00152             return task_vector_rhs;
00153         }
00154 
00156         void set_task_vector_rhs(std::vector<int32_t> vec) {
00157             task_vector_rhs = vec;
00158         }
00159 
00161         void set_task_vector(std::vector<int32_t> vec) {
00162             task_vector_lhs = vec;
00163             task_vector_rhs = vec;
00164         }
00165 
00171         float64_t get_task_similarity(int32_t task_lhs, int32_t task_rhs) {
00172 
00173             ASSERT(task_lhs < num_tasks && task_lhs >= 0);
00174             ASSERT(task_rhs < num_tasks && task_rhs >= 0);
00175 
00176             return dependency_matrix[task_lhs * num_tasks + task_rhs];
00177 
00178         }
00179 
00185         void set_task_similarity(int32_t task_lhs, int32_t task_rhs, float64_t similarity) {
00186 
00187             ASSERT(task_lhs < num_tasks && task_lhs >= 0);
00188             ASSERT(task_rhs < num_tasks && task_rhs >= 0);
00189 
00190             dependency_matrix[task_lhs * num_tasks + task_rhs] = similarity;
00191 
00192         }
00193 
00195         inline virtual const char* get_name() const { return "MultitaskKernelNormalizer"; }
00196 
00197 
00198     protected:
00199 
00201         std::vector<float64_t> dependency_matrix;
00202 
00204         int32_t num_tasks;
00205 
00207         std::vector<int32_t> task_vector_lhs;
00208 
00210         std::vector<int32_t> task_vector_rhs;
00211 
00212 };
00213 }
00214 #endif

SHOGUN Machine Learning Toolbox - Documentation