Changeset 15052
- Timestamp:
- 07/02/08 20:15:15 (6 months ago)
- Location:
- lang/cplusplus/misc/clustercpp
- Files:
-
- 2 added
- 5 modified
-
include/maxdistupdator.h (added)
-
include/ward.h (modified) (2 diffs)
-
include/wardupdator.h (modified) (1 diff)
-
src/Makefile (modified) (2 diffs)
-
src/maxdistupdator.cpp (added)
-
src/ward.cpp (modified) (3 diffs)
-
src/wardupdator.cpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/cplusplus/misc/clustercpp/include/ward.h
r15000 r15052 6 6 #include <boost/shared_ptr.hpp> 7 7 #include <iostream> 8 #include <ctime> 9 #include <cstdlib> 10 8 11 9 12 #include "wardtypes.h" 10 13 #include "wardupdator.h" 14 #include "maxdistupdator.h" 11 15 12 16 … … 19 23 ~Ward(); 20 24 21 template < class VECTORSTYPE >25 template < class UPDATOR, class VECTORSTYPE > 22 26 HISTORIES* cluster(VECTORSTYPE* vectors){ 23 WardUpdator updator; 27 std::clock_t t1,t2; 28 t1 = std::clock(); 29 //WardUpdator updator; 30 //MaxDistUpdator updator; 31 UPDATOR updator; 24 32 History h = updator.create_matrix(vectors); 33 t2 = std::clock(); 34 double T1 = static_cast<double>(t2-t1) / CLOCKS_PER_SEC; 35 std::cout << "Time: building_matrix=" << T1 << std::endl; 36 25 37 INDEX size = updator.size(); 26 38 -
lang/cplusplus/misc/clustercpp/include/wardupdator.h
r15000 r15052 17 17 History create_matrix(SPARSEVECTORS* vectors); 18 18 inline INDEX size(){ return mat->size()+1; } 19 protected: 20 MATRIX* mat; 21 22 virtual double calc_distance(MATRIXROW* objp, INDEX j, INDEX p, INDEX q, INDEX sqp, INDEX sp, INDEX sq, VALUE mqp); 23 inline VALUE elm(INDEX i, INDEX j){ return (*((*mat)[i]))[j]; } 19 24 20 25 private: 21 MATRIX* mat;22 26 std::vector<INDEX> sizes; 23 27 std::set<INDEX> kieta; 28 CACHES* caches; 24 29 25 30 void initcache(); 26 27 virtual double calc_distance(MATRIXROW* objp, INDEX j, INDEX p, INDEX q, INDEX sqp, INDEX sp, INDEX sq, VALUE mqp);28 29 inline VALUE elm(INDEX i, INDEX j){30 return (*((*mat)[i]))[j];}31 CACHES* caches;32 31 inline void savecache(INDEX i, VALUE d, INDEX i1){ 33 32 CacheP p = CacheP(new Cache); -
lang/cplusplus/misc/clustercpp/src/Makefile
r12159 r15052 1 1 CXXFLAGS=-O3 -ffast-math -funroll-loops -Wall -fno-common -march=nocona -DBUILDMAIN 2 ward: ward.o wardupdator.o 3 g++ $(CXXFLAGS) -o ward -I../include ward.o wardupdator.o 2 3 ward: ward.o wardupdator.o maxdistupdator.o 4 g++ $(CXXFLAGS) -o ward -I../include ward.o wardupdator.o maxdistupdator.o 4 5 5 6 wardupdator.o: wardupdator.cpp … … 9 10 g++ $(CXXFLAGS) -c -I../include ward.cpp 10 11 12 maxdistupdator.o: maxdistupdator.cpp 13 g++ $(CXXFLAGS) -c -I../include maxdistupdator.cpp 11 14 12 15 -
lang/cplusplus/misc/clustercpp/src/ward.cpp
r15000 r15052 11 11 #include "ward.h" 12 12 #include "wardupdator.h" 13 #include "maxdistupdator.h" 13 14 14 15 namespace clustercpp{ … … 37 38 ans.push_back(std::string(line, p)); 38 39 return ans.size(); 40 } 41 42 void printmat(MATRIX* mat){ 43 for(MATRIX::iterator i=mat->begin(); i != mat->end(); i++){ 44 for(MATRIXROW::iterator j=i->second->begin(); j != i->second->end(); j++){ 45 std::cout << "(" << i->first << "," << j->first << ")=" << j->second << ","; 46 } 47 std::cout << std::endl; 48 } 49 std::cout << "--------------------" << std::endl; 39 50 } 40 51 … … 108 119 109 120 clustercpp::Ward ward; 110 std::clock_t t1,t2,t3; 111 t1 = std::clock(); 121 std::clock_t t2,t3; 112 122 t2 = std::clock(); 113 123 //clustercpp::printmat(mat); 114 HISTORIES* hist = ward.cluster (vectors.get());124 HISTORIES* hist = ward.cluster<clustercpp::MaxDistUpdator>(vectors.get()); 115 125 t3 = std::clock(); 126 int t = 0; 116 127 for(HISTORIES::iterator it=hist->begin(); it!=hist->end(); it++){ 117 128 idlabel.push_back("("+idlabel[it->index1]+"+"+idlabel[it->index2]+")"); 118 std::cout << "dist "<< it->index1 << "+" << it->index2 << " / " << it->distance << std::endl; // ") " << idlabel[idlabel.size()-1] << std::endl; 129 std::cout << t << " dist "<< it->index1 << "+" << it->index2 << " / " << it->distance << std::endl; // ") " << idlabel[idlabel.size()-1] << std::endl; 130 t++; 119 131 } 120 double T1 = static_cast<double>(t2-t1) / CLOCKS_PER_SEC;121 132 double T2 = static_cast<double>(t3-t2) / CLOCKS_PER_SEC; 122 std::cout << "Time: building_matrix=" << T1 << "clustering=" << T2 << std::endl;133 std::cout << "Time: clustering=" << T2 << std::endl; 123 134 124 135 return 0; -
lang/cplusplus/misc/clustercpp/src/wardupdator.cpp
r15000 r15052 6 6 7 7 namespace clustercpp{ 8 void printmat(MATRIX* mat){9 for(MATRIX::iterator i=mat->begin(); i != mat->end(); i++){10 for(MATRIXROW::iterator j=i->second->begin(); j != i->second->end(); j++){11 std::cout << "(" << i->first << "," << j->first << ")=" << j->second << ",";12 }13 std::cout << std::endl;14 }15 std::cout << "--------------------" << std::endl;16 }17 18 8 WardUpdator::WardUpdator(): sizes(), kieta(){ 19 9 caches = new CACHES; … … 81 71 mat->insert(MATRIX::value_type(i, p)); 82 72 SPARSEVECTOR::iterator e2 = v.end(); 73 double vsum = 0.0; 74 for(SPARSEVECTOR::iterator it0=v.begin();it0!=v.end();it0++){ 75 vsum += (it0->second) * (it0->second); 76 } 77 83 78 for(INDEX j=0; j<i; j++){ 84 79 SPARSEVECTOR& u = (*sparsevectors)[j]; 85 80 //ベクトル間のユークリッド距離を計算 86 double pp = 0.0;81 double pp = vsum; 87 82 SPARSEVECTOR::iterator e = u.end(); 88 83 for(SPARSEVECTOR::iterator it=u.begin();it!=e;it++){ 89 84 SPARSEVECTOR::iterator it2 = v.find(it->first); 85 double x = it->second; 86 pp += x*x; 90 87 if(it2!=e2){ 91 VALUE x = (it2->second)-(it->second); 92 pp += x*x; 88 pp += -2*x*(it2->second); 93 89 } 94 90 } … … 136 132 137 133 double tmpdist = calc_distance(objp, j, p, q, sqp, sp, sq, mqp); 138 /*139 double tmpdist;140 if(j < p){ //TODO ここをobjpの参照先を書き換えるのでなく、リストにして返す or ローカルに保存しっぱなしでどうか。。。141 tmpdist = (*objp)[j] = ((sp+sizes[j])*elm(p,j)+(sq+sizes[j])*elm(q,j)-sizes[j]*mqp)/(sqp+sizes[j]);142 }else if(p<j && j<q){143 tmpdist = (*objp)[j] = ((sp+sizes[j])*elm(j,p)+(sq+sizes[j])*elm(q,j)-sizes[j]*mqp)/(sqp+sizes[j]);144 (*mat)[j]->erase(p); //今後使わなくなる成分の消去145 }else{146 tmpdist = (*objp)[j] =((sp+sizes[j])*elm(j,p)+(sq+sizes[j])*elm(j,q)-sizes[j]*mqp)/(sqp+sizes[j]);147 (*mat)[j]->erase(p); //今後使わなくなる成分の消去148 (*mat)[j]->erase(q); //今後使わなくなる成分の消去149 }150 */151 134 152 135 //今の所最小距離のものを一時記憶 … … 191 174 double WardUpdator::calc_distance(MATRIXROW* objp, INDEX j, INDEX p, INDEX q, INDEX sqp, INDEX sp, INDEX sq, VALUE mqp){ 192 175 double tmpdist; 176 INDEX sizes_j = sizes[j]; 193 177 if(j < p){ //TODO ここをobjpの参照先を書き換えるのでなく、リストにして返す or ローカルに保存しっぱなしでどうか。。。 194 tmpdist = (*objp)[j] = ((sp+sizes[j])*elm(p,j)+(sq+sizes[j])*elm(q,j)-sizes[j]*mqp)/(sqp+sizes[j]);178 return (*objp)[j] = ((sp+sizes_j)*elm(p,j)+(sq+sizes_j)*elm(q,j)-sizes_j*mqp)/(sqp+sizes_j); 195 179 }else if(p<j && j<q){ 196 tmpdist = ( *objp)[j] = ((sp+sizes[j])*elm(j,p)+(sq+sizes[j])*elm(q,j)-sizes[j]*mqp)/(sqp+sizes[j]);180 tmpdist = ((sp+sizes_j)*elm(j,p)+(sq+sizes_j)*elm(q,j)-sizes_j*mqp)/(sqp+sizes_j); 197 181 (*mat)[j]->erase(p); //今後使わなくなる成分の消去 198 182 }else{ 199 tmpdist = ( *objp)[j] =((sp+sizes[j])*elm(j,p)+(sq+sizes[j])*elm(j,q)-sizes[j]*mqp)/(sqp+sizes[j]);183 tmpdist = ((sp+sizes_j)*elm(j,p)+(sq+sizes_j)*elm(j,q)-sizes_j*mqp)/(sqp+sizes_j); 200 184 (*mat)[j]->erase(p); //今後使わなくなる成分の消去 201 185 (*mat)[j]->erase(q); //今後使わなくなる成分の消去 202 186 } 203 return tmpdist;187 return (*objp)[j] = tmpdist; 204 188 } 205 189 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)