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-2008 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 (INT 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 (INT 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 DREAL CPlifArray::lookup_penalty(DREAL p_value, DREAL* svm_values) const 
00061 {   
00062     //min_value = -1e6 ;
00063     //max_value = 1e6 ;
00064     if (p_value<min_value || p_value>max_value)
00065     {
00066         //SG_WARNING("lookup_penalty: p_value: %i min_value: %f, max_value: %f\n",p_value, min_value, max_value);
00067         return -CMath::INFTY ;
00068     }
00069     DREAL ret = 0.0 ;
00070     for (INT i=0; i<m_array.get_num_elements(); i++)
00071         ret += m_array[i]->lookup_penalty(p_value, svm_values) ;
00072     return ret ;
00073 } 
00074 
00075 DREAL CPlifArray::lookup_penalty(INT p_value, DREAL* svm_values) const 
00076 {   
00077     //min_value = -1e6 ;
00078     //max_value = 1e6 ;
00079     if (p_value<min_value || p_value>max_value)
00080     {
00081         //SG_WARNING("lookup_penalty: p_value: %i min_value: %f, max_value: %f\n",p_value, min_value, max_value);
00082         return -CMath::INFTY ;
00083     }
00084     DREAL ret = 0.0 ;
00085     for (INT i=0; i<m_array.get_num_elements(); i++)
00086     {
00087         DREAL val = m_array[i]->lookup_penalty(p_value, svm_values) ;
00088         ret += val ;
00089 #ifdef PLIFARRAY_DEBUG
00090         CPlif * plif = (CPlif*)m_array[i] ;
00091         if (plif->get_use_svm())
00092             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]) ;
00093         else
00094             SG_PRINT("penalty[%i]=%1.5f\n", i, val) ;
00095 #endif
00096     }
00097     return ret ;
00098 } 
00099 
00100 void CPlifArray::penalty_clear_derivative() 
00101 {
00102     for (INT i=0; i<m_array.get_num_elements(); i++)
00103         m_array[i]->penalty_clear_derivative() ;
00104 } 
00105 
00106 void CPlifArray::penalty_add_derivative(DREAL p_value, DREAL* svm_values)
00107 {
00108     for (INT i=0; i<m_array.get_num_elements(); i++)
00109         m_array[i]->penalty_add_derivative(p_value, svm_values) ;
00110 }
00111 
00112 bool CPlifArray::uses_svm_values() const 
00113 {
00114     for (INT i=0; i<m_array.get_num_elements(); i++)
00115         if (m_array[i]->uses_svm_values())
00116             return true ;
00117     return false ;
00118 }
00119 
00120 INT CPlifArray::get_max_id() const 
00121 {
00122     INT max_id = 0 ;
00123     for (INT i=0; i<m_array.get_num_elements(); i++)
00124         max_id = CMath::max(max_id, m_array[i]->get_max_id()) ;
00125     return max_id ;
00126 }
00127 
00128 void CPlifArray::get_used_svms(INT* num_svms, INT* svm_ids)
00129 {
00130     SG_PRINT("get_used_svms: num: %i \n",m_array.get_num_elements());
00131     for (INT i=0; i<m_array.get_num_elements(); i++)
00132     {
00133         m_array[i]->get_used_svms(num_svms, svm_ids);
00134     }
00135     SG_PRINT("\n");
00136 }
00137 
00138 

SHOGUN Machine Learning Toolbox - Documentation