void DcmeDualUpdate(int zz, DcmeBookkeeping* b, heap* twh) { int j, k; NumMulMatVec(model->tar, b->hh + zz * N, V, N, b->dd + zz * V); // dd b->ent[zz] = NumSoftMax(b->dd + zz * V, b->hn[zz], V); // ent (sm) // dual reset distribution (hh and hn) of zz -------------------------------- if (V_DUAL_RESET_OPT == 1) { // ------------------- 1) clean reset b->hn[zz] = 0; if (V_DUAL_HI) NumFillZeroVec(b->hi + zz * V, V); else NumFillZeroVec(b->hh + zz * N, N); } else if (V_DUAL_RESET_OPT == 2) { // ------------ 2) half reset b->hn[zz] *= 0.5; if (V_DUAL_HI) NumVecMulC(b->hi + zz * V, 0.5, V); else NumVecMulC(b->hh + zz * N, 0.5, N); } else if (V_DUAL_RESET_OPT == 3) { // ------------ 3) decay/overshoot reset real v = 0.1; b->hn[zz] *= v; if (V_DUAL_HI) NumVecMulC(b->hi + zz * V, v, V); else NumVecMulC(b->hh + zz * N, v, N); } // ---------------------------------------------- 4) no reset // -------------------------------------------------------------------------- if (Q > 0) { // top words HeapEmpty(twh); // tw reset for (j = 0; j < V; j++) HeapPush(twh, j, b->dd[zz * V + j]); // tw add for (j = 0; j < Q; j++) b->tw[zz * Q + j] = twh->d[j].key; // tw load qsort(b->tw + zz * Q, Q, sizeof(int), cmp_int); // tw sort b->twps[zz] = 0; for (j = 0; j < Q; j++) { k = b->tw[zz * Q + j]; b->twps[zz] += b->dd[zz * V + k]; } } if (V_MICRO_ME) { NumFillZeroVec(b->ow + zz * N, N); NumFillZeroVec(b->ww + zz * N, N); for (j = 0, k = 0; j < V; j++) { // ww, ow: two way merge of tw and ow if (j == b->tw[zz * Q + k]) { // tw NumVecAddCVec(b->ww + zz * N, model->tar + j * N, b->dd[zz * V + j], N); k++; } else NumVecAddCVec(b->ow + zz * N, model->tar + j * N, b->dd[zz * V + j], N); } NumVecAddCVec(b->ww + zz * N, b->ow + zz * N, 1, N); // ww: adding ow } else { NumMulVecMat(b->dd + zz * V, model->tar, V, N, b->ww + zz * N); // ww } b->last_updated_zz = zz; return; }
void DcmeDualUpdate(int zz, DcmeBookkeeping* b, heap* twh) { // quick update: dd, ww, ow, twps, int j, k, tw[QUP]; real ent, twps, dd[CUP], ow[NUP], ww[NUP]; NumMulMatVec(weight, b->hh + zz * N, C, N, dd); // dd ent = NumSoftMax(dd, b->hn[zz], C); // ent (sm) NumCopyVec(b->dd + zz * C, dd, C); b->ent[zz] = ent; // dual reset distribution (hh and hn) of zz -------------------------------- if (V_DUAL_RESET_OPT == 1) { // ------------------- 1) clean reset b->hn[zz] = 0; NumFillZeroVec(b->hh + zz * N, N); } else if (V_DUAL_RESET_OPT == 2) { // ------------ 2) half reset b->hn[zz] *= 0.5; NumVecMulC(b->hh + zz * N, 0.5, N); } else if (V_DUAL_RESET_OPT == 3) { // ------------ 3) decay/overshoot reset real v = 0.1; b->hn[zz] *= v; NumVecMulC(b->hh + zz * N, v, N); } // ---------------------------------------------- 4) no reset // -------------------------------------------------------------------------- if (Q > 0) { // top class HeapEmpty(twh); // tw reset for (j = 0; j < C; j++) HeapPush(twh, j, b->dd[zz * C + j]); // tw add for (j = 0; j < Q; j++) tw[j] = twh->d[j].key; // tw load qsort(tw, Q, sizeof(int), cmp_int); // tw sort NumCopyIntVec(b->tw + zz * Q, tw, Q); twps = 0; for (j = 0; j < Q; j++) { k = b->tw[zz * Q + j]; twps += b->dd[zz * C + k]; } b->twps[zz] = twps; } if (V_MICRO_ME) { // Q > 0 required NumFillZeroVec(ow, N); NumFillZeroVec(ww, N); for (j = 0, k = 0; j < C; j++) { // ww, ow: two way merge of tw and ow if (j == b->tw[zz * Q + k]) { // tw NumVecAddCVec(ww, weight + j * N, b->dd[zz * C + j], N); k++; } else NumVecAddCVec(ow, weight + j * N, b->dd[zz * C + j], N); } NumVecAddCVec(b->ww + zz * N, b->ow + zz * N, 1, N); // ww: adding ow NumCopyVec(b->ww + zz * N, ww, N); NumCopyVec(b->ow + zz * N, ow, N); } else { NumMulVecMat(b->dd + zz * C, weight, C, N, ww); // ww NumCopyVec(b->ww + zz * N, ww, N); } b->last_updated_zz = zz; return; }
void Dijkstra(node*list, int destiny, int count_nodes, int**node_distance, int**node_hops) { adj_node*links; int dijkstra_u=0; int dijkstra_identifier; Heap*heap; int i, *heap_place; heap_place=malloc(count_nodes*sizeof(int)); if(heap_place==NULL)exit(-1); for(i=0; i<count_nodes; i++) heap_place[i]=-1; if(count_nodes>0) { heap=NewHeap(count_number_nodes(list, count_nodes)); Initialize_distance_matrix(count_nodes, &(*node_distance), &(*node_hops), list, destiny, heap, &heap_place); while(HeapEmpty(heap)) { dijkstra_identifier= RemoveMax(heap, (*node_distance), &heap_place, (*node_hops)); //printf("removed %d from heap\n", dijkstra_identifier); dijkstra_u = dijkstra_identifier - 1; if((*node_distance)[dijkstra_u]!=-1) { /*for each uv*/ for(links=(list[dijkstra_u]).link; links!=NULL; links=links->next) { if(heap_place[(links->identifier)-1]!=-1) { if(((*node_distance)[(links->identifier)-1] <= min((*node_distance)[dijkstra_u],links->preference))&& ((links->preference)<=(*node_distance)[dijkstra_u])) { /*if it is a consecutive peer route, the route is not usable*/ if(!(((*node_distance)[dijkstra_u]==2)&&((links->preference)==2))) { if(((*node_distance)[(links->identifier)-1])==min((*node_distance)[dijkstra_u],links->preference)) { /*same route type, chooses the one with less hops*/ if(((*node_hops)[(links->identifier)-1])>((*node_hops)[dijkstra_u]+1)) (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1; } else { /*changed route type so just updates the number of hops*/ (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1; //these 2 lines were outside the else (*node_distance)[(links->identifier)-1] = min((*node_distance)[dijkstra_u],links->preference); } FixUp(heap, heap_place[(links->identifier)-1], (*node_distance), &heap_place, (*node_hops)); } } } } } } invert_weights(&(*node_distance), count_nodes); } free(heap_place); return; }
Element<Type>* MaxHeap<Type>::DeleteMax(Element<Type>& x) { if (!n) {HeapEmpty(); return 0;} x = heap[1]; Element<Type> k = heap[n]; n--; for (int i = 1, j = 2; j <= n; ) { if ( j < n) if (heap[j].key < heap[j+1].key) j++; // j points to the larger child if (k.key >= heap[j].key) break; heap[i] = heap[j]; i = j; j *= 2; } heap[i] = k; return &x; }