PlifArray.cpp

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) 1999-2008 Gunnar Raetsch
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/config.h"
00012 
00013 #include <stdio.h>
00014 #include <string.h>
00015 
00016 #include "lib/io.h"
00017 
00018 #include "structure/PlifArray.h"
00019 #include "structure/Plif.h"
00020 
00021 //#define PLIFARRAY_DEBUG
00022 
00023 CPlifArray::CPlifArray()
00024 : CPlifBase()
00025 {
00026     min_value=-1e6;
00027     max_value=1e6;
00028 }
00029 
00030 CPlifArray::~CPlifArray()
00031 {
00032 }
00033 
00034 void CPlifArray::add_plif(CPlifBase* new_plif)
00035 {
00036     ASSERT(new_plif);
00037     m_array.append_element(new_plif) ;
00038     
00039     min_value = -1e6 ;
00040     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00041     {
00042         ASSERT(m_array[i]);
00043         if (!m_array[i]->uses_svm_values())
00044             min_value = CMath::max(min_value, m_array[i]->get_min_value()) ;
00045     }
00046     
00047     max_value = 1e6 ;
00048     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00049         if (!m_array[i]->uses_svm_values())
00050             max_value = CMath::min(max_value, m_array[i]->get_max_value()) ;
00051 }
00052 
00053 void CPlifArray::clear() 
00054 {
00055     m_array.clear_array();
00056     min_value = -1e6 ;
00057     max_value = 1e6 ;
00058 }
00059 
00060 float64_t CPlifArray::lookup_penalty(
00061     float64_t p_value, float64_t* svm_values) const
00062 {
00063     //min_value = -1e6 ;
00064     //max_value = 1e6 ;
00065     if (p_value<min_value || p_value>max_value)
00066     {
00067         //SG_WARNING("lookup_penalty: p_value: %i min_value: %f, max_value: %f\n",p_value, min_value, max_value);
00068         return -CMath::INFTY ;
00069     }
00070     float64_t ret = 0.0 ;
00071     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00072         ret += m_array[i]->lookup_penalty(p_value, svm_values) ;
00073     return ret ;
00074 } 
00075 
00076 float64_t CPlifArray::lookup_penalty(
00077     int32_t p_value, float64_t* svm_values) const
00078 {
00079     //min_value = -1e6 ;
00080     //max_value = 1e6 ;
00081     if (p_value<min_value || p_value>max_value)
00082     {
00083         //SG_WARNING("lookup_penalty: p_value: %i min_value: %f, max_value: %f\n",p_value, min_value, max_value);
00084         return -CMath::INFTY ;
00085     }
00086     float64_t ret = 0.0 ;
00087     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00088     {
00089         float64_t val = m_array[i]->lookup_penalty(p_value, svm_values) ;
00090         ret += val ;
00091 #ifdef PLIFARRAY_DEBUG
00092         CPlif * plif = (CPlif*)m_array[i] ;
00093         if (plif->get_use_svm())
00094             SG_PRINT("penalty[%i]=%1.5f (use_svm=%i -> %1.5f)\n", i, val, plif->get_use_svm(), svm_values[plif->get_use_svm()-1]) ;
00095         else
00096             SG_PRINT("penalty[%i]=%1.5f\n", i, val) ;
00097 #endif
00098     }
00099     return ret ;
00100 } 
00101 
00102 void CPlifArray::penalty_clear_derivative() 
00103 {
00104     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00105         m_array[i]->penalty_clear_derivative() ;
00106 }
00107 
00108 void CPlifArray::penalty_add_derivative(
00109     float64_t p_value, float64_t* svm_values)
00110 {
00111     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00112         m_array[i]->penalty_add_derivative(p_value, svm_values) ;
00113 }
00114 
00115 bool CPlifArray::uses_svm_values() const
00116 {
00117     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00118         if (m_array[i]->uses_svm_values())
00119             return true ;
00120     return false ;
00121 }
00122 
00123 int32_t CPlifArray::get_max_id() const
00124 {
00125     int32_t max_id = 0 ;
00126     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00127         max_id = CMath::max(max_id, m_array[i]->get_max_id()) ;
00128     return max_id ;
00129 }
00130 
00131 void CPlifArray::get_used_svms(int32_t* num_svms, int32_t* svm_ids)
00132 {
00133     SG_PRINT("get_used_svms: num: %i \n",m_array.get_num_elements());
00134     for (int32_t i=0; i<m_array.get_num_elements(); i++)
00135     {
00136         m_array[i]->get_used_svms(num_svms, svm_ids);
00137     }
00138     SG_PRINT("\n");
00139 }

SHOGUN Machine Learning Toolbox - Documentation