SHOGUN
v3.2.0
|
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 * Copyright (C) 2012 Sergey Lisitsyn 00008 */ 00009 00010 #include <shogun/transfer/multitask/Task.h> 00011 00012 using namespace shogun; 00013 00014 CTask::CTask() : CSGObject() 00015 { 00016 init(); 00017 00018 m_weight = 0.0; 00019 m_name = "task"; 00020 } 00021 00022 CTask::CTask(index_t min_index, index_t max_index, 00023 float64_t weight, const char* name) : 00024 CSGObject() 00025 { 00026 init(); 00027 00028 REQUIRE(min_index<max_index, "min index should be less than max index") 00029 m_indices = SGVector<index_t>(max_index-min_index); 00030 for (int32_t i=0; i<m_indices.vlen; i++) 00031 m_indices[i] = i+min_index; 00032 m_weight = weight; 00033 m_name = name; 00034 } 00035 00036 CTask::CTask(SGVector<index_t> indices, 00037 float64_t weight, const char* name) : 00038 CSGObject() 00039 { 00040 init(); 00041 00042 m_indices = indices; 00043 } 00044 00045 void CTask::init() 00046 { 00047 m_subtasks = new CList(true); 00048 SG_REF(m_subtasks); 00049 00050 SG_ADD((CSGObject**)&m_subtasks,"subtasks","subtasks of given task", MS_NOT_AVAILABLE); 00051 SG_ADD(&m_indices,"indices","indices of task", MS_NOT_AVAILABLE); 00052 SG_ADD(&m_weight,"weight","weight of task", MS_NOT_AVAILABLE); 00053 } 00054 00055 CTask::~CTask() 00056 { 00057 SG_UNREF(m_subtasks); 00058 } 00059 00060 bool CTask::is_contiguous() 00061 { 00062 REQUIRE(m_indices.vlen>1,"Task indices vector must not be empty or contain only one element") 00063 bool result = true; 00064 for (int32_t i=0; i<m_indices.vlen-1; i++) 00065 { 00066 if (m_indices[i]!=m_indices[i+1]-1) 00067 { 00068 result = false; 00069 break; 00070 } 00071 } 00072 00073 return result; 00074 } 00075 00076 void CTask::add_subtask(CTask* subtask) 00077 { 00078 SGVector<index_t> subtask_indices = subtask->get_indices(); 00079 for (int32_t i=0; i<subtask_indices.vlen; i++) 00080 { 00081 bool found = false; 00082 for (int32_t j=0; j<m_indices.vlen; j++) 00083 { 00084 if (subtask_indices[i] == m_indices[j]) 00085 { 00086 found = true; 00087 break; 00088 } 00089 } 00090 if (!found) 00091 SG_ERROR("Subtask contains indices that are not contained in this task\n") 00092 } 00093 m_subtasks->append_element(subtask); 00094 } 00095 00096 CList* CTask::get_subtasks() 00097 { 00098 SG_REF(m_subtasks); 00099 return m_subtasks; 00100 } 00101 00102 int32_t CTask::get_num_subtasks() 00103 { 00104 return m_subtasks->get_num_elements(); 00105 }