bool NeighborClustCount(Neighbor** vNeighbors,vector<int>& vClustIDs,vector<ClusterInfo>& vPrct,int iNNToFind,int iClusts) { int iSz = vClustIDs.size(); if(vPrct.size()!=iClusts+1) return false; int i; vector<int> vCounts(iClusts+1); for(i=1;i<=iClusts;i++) vCounts[i]=count(vClustIDs.begin(),vClustIDs.end(),i); vector< vector<prob_t> > vprct(iClusts+1); for(i=1;i<=iClusts;i++) vprct[i]=vector<prob_t>(vCounts[i],0.0); vector<int> vIndex(iClusts+1,0); for(i=0;i<iSz;i++) { int j, iCount = 0, iClust = vClustIDs[i]; if(!iClust)continue;//skip background vectors int idx = vIndex[iClust]++; for(j=0;j<iNNToFind;j++) { if(vClustIDs[vNeighbors[i][j].m_id]==iClust) vprct[iClust][idx]+=1.0f; } vprct[iClust][idx] /= iNNToFind; } for(i=1;i<=iClusts;i++) vPrct[i].m_fPrctKNNInClust=Avg(vprct[i]); return true; }
double DistanceVdVdtMatrix::compare(const VdVdtMatrix & o) const { const DistanceVdVdtMatrix & other = dynamic_cast<const DistanceVdVdtMatrix &>(o); double errorValue = 0; if (other.getVLength() != vLength) crash("DistanceVdVdtMatrix","V dimensions don't match"); if (other.getdVdtLength() != dVdtLength) crash("DistanceVdVdtMatrix","dVdt dimensions don't match"); double diff; double distance, v1, v2, dVdt1, dVdt2; double maxDistance = pow((double)vLength,2)+pow((double)dVdtLength,2); for (map< const int, double >::const_iterator i = matrix.begin(); i != matrix.end(); i++) { for (map< const int, double >::const_iterator j = (other.matrix).begin(); j != (other.matrix).end(); j++) { diff = i->second-j->second; v1 = vIndex(i->first); v2 = other.vIndex(j->first); dVdt1 = dVdtIndex(i->first); dVdt2 = other.dVdtIndex(j->first); distance = pow(v1-v2,2)+pow(dVdt1-dVdt2,2); errorValue += pow(diff,2)*pow(maxDistance-distance,2); } } return sqrt(errorValue); }
BOOL CmyWord::SetActiveDocument(short i) { COleVariant vIndex(i),vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); m_wdDoc.AttachDispatch(m_wdDocs.Item(vIndex)); m_wdDoc.Activate(); if (!m_wdDoc.m_lpDispatch) { AfxMessageBox(_T("Document获取失败!"), MB_OK|MB_ICONWARNING); return FALSE; } //得到selection变量 m_wdSel = m_wdApp.GetSelection(); if (!m_wdSel.m_lpDispatch) { AfxMessageBox(_T("Select获取失败!"), MB_OK|MB_ICONWARNING); return FALSE; } //得到全部DOC的Range变量 m_wdRange = m_wdDoc.Range(vOptional,vOptional); if(!m_wdRange.m_lpDispatch) { AfxMessageBox(_T("Range获取失败!"), MB_OK|MB_ICONWARNING); return FALSE; } HideApp(); return TRUE; }
//--------------------------------------------------------- // Compute x-coordinates (LP-based approach) (for graphs) //--------------------------------------------------------- void OptimalHierarchyLayout::computeXCoordinates( const Hierarchy& H, GraphCopyAttributes &AGC) { const GraphCopy &GC = H; const int k = H.size(); // // preprocessing: determine nodes that are considered as virtual // NodeArray<bool> isVirtual(GC); int i; for(i = 0; i < k; ++i) { const Level &L = H[i]; int last = -1; for(int j = 0; j < L.size(); ++j) { node v = L[j]; if(H.isLongEdgeDummy(v) == true) { isVirtual[v] = true; node u = v->firstAdj()->theEdge()->target(); if(u == v) u = v->lastAdj()->theEdge()->target(); if(H.isLongEdgeDummy(u) == true) { int down = H.pos(u); if(last != -1 && last > down) { isVirtual[v] = false; } else { last = down; } } } else isVirtual[v] = false; } } // // determine variables of LP // int nSegments = 0; // number of vertical segments int nRealVertices = 0; // number of real vertices int nEdges = 0; // number of edges not in vertical segments int nBalanced = 0; // number of real vertices with deg > 1 for which // balancing constraints may be applied NodeArray<int> vIndex(GC,-1); // for real node: index of x[v] // for dummy: index of corresponding segment NodeArray<int> bIndex(GC,-1); // (relative) index of b[v] EdgeArray<int> eIndex(GC,-1); // for edge not in vertical segment: // its index Array<int> count(GC.numberOfEdges()); // counts the number of dummy vertices // in corresponding segment that are not at // position 0 for(i = 0; i < k; ++i) { const Level &L = H[i]; for(int j = 0; j < L.size(); ++j) { node v = L[j]; if(isVirtual[v] == true) continue; // we've found a real vertex vIndex[v] = nRealVertices++; if(v->degree() > 1) bIndex[v] = nBalanced++; // consider all outgoing edges edge e; forall_adj_edges(e,v) { node w = e->target(); if(w == v) continue; // we've found an edge not belonging to a vetical segment eIndex[e] = nEdges++; if(isVirtual[w] == false) continue; // we've found a vertical segment count[nSegments] = 0; do { vIndex[w] = nSegments; const int high = H[H.rank(w)].high(); if(high > 0) { if (H.pos(w) == 0 || H.pos(w) == high) ++count[nSegments]; else count[nSegments] += 2; } // next edge / dummy in segment e = e->adjTarget()->cyclicSucc()->theEdge(); w = e->target(); } while(isVirtual[w]); // edge following vertical segment eIndex[e] = nEdges++; ++nSegments; } } }
///////////////////////////////////////////////////////////////////////////// // Compute the covariance matrix // This function assumes that ratings are maximum-likelihood ratings ///////////////////////////////////////////////////////////////////////////// void CBradleyTerry::ComputeCovariance() { // // Compute the truncated opposite of the Hessian // CMatrix mTruncatedHessian(crs.GetPlayers() - 1, crs.GetPlayers() - 1); { ConvertEloToGamma(); const double x = std::log(10.0) / 400; const double xx = -x * x; mTruncatedHessian.Zero(); for (int Player = crs.GetPlayers() - 1; --Player >= 0;) { double Diag = 0; double PlayerGamma = pGamma[Player]; for (int j = crs.GetOpponents(Player); --j >= 0;) { const CCondensedResult &cr = crs.GetCondensedResult(Player, j); double OpponentGamma = pGamma[cr.Opponent]; double h = 0; { double d = ThetaW * PlayerGamma + ThetaD * OpponentGamma; h += (cr.w_ij + cr.d_ij) / (d * d); } { double d = ThetaD * ThetaW * PlayerGamma + OpponentGamma; h += (cr.l_ij + cr.d_ij) / (d * d); } { double d = ThetaW * OpponentGamma + ThetaD * PlayerGamma; h += (cr.w_ji + cr.d_ji) / (d * d); } { double d = ThetaD * ThetaW * OpponentGamma + PlayerGamma; h += (cr.l_ji + cr.d_ji) / (d * d); } h *= PlayerGamma * OpponentGamma * ThetaD * ThetaW; Diag -= h; if (cr.Opponent != crs.GetPlayers() - 1) mTruncatedHessian.SetElement(Player, cr.Opponent, h * xx); } mTruncatedHessian.SetElement(Player, Player, Diag * xx); } } // // LU-Decompose it // CLUDecomposition lud(crs.GetPlayers() - 1); std::vector<int> vIndex(crs.GetPlayers() - 1); lud.Decompose(mTruncatedHessian, &vIndex[0]); // // Fill A // CMatrix mA(crs.GetPlayers(), crs.GetPlayers() - 1); { double x = -1.0 / crs.GetPlayers(); for (int i = crs.GetPlayers() * (crs.GetPlayers() - 1); --i >= 0;) mA[i] = x; for (int i = crs.GetPlayers() - 1; --i >= 0;) mA.SetElement(i, i, 1.0 + x); } // // Compute AC // CMatrix mAC(crs.GetPlayers(), crs.GetPlayers() - 1); for (int i = crs.GetPlayers(); --i >= 0;) { int Index = i * (crs.GetPlayers() - 1); lud.Solve(mTruncatedHessian, &vIndex[0], mA + Index, mAC + Index); } // // Compute the covariance // mCovariance.SetProductByTranspose(mAC, mA); }
// This routine set up the IloCplex algorithm to solve the worker LP, and // creates the worker LP (i.e., the dual of flow constraints and // capacity constraints of the flow MILP) // // Modeling variables: // forall k in V0, i in V: // u(k,i) = dual variable associated with flow constraint (k,i) // // forall k in V0, forall (i,j) in A: // v(k,i,j) = dual variable associated with capacity constraint (k,i,j) // // Objective: // minimize sum(k in V0) sum((i,j) in A) x(i,j) * v(k,i,j) // - sum(k in V0) u(k,0) + sum(k in V0) u(k,k) // // Constraints: // forall k in V0, forall (i,j) in A: u(k,i) - u(k,j) <= v(k,i,j) // // Nonnegativity on variables v(k,i,j) // forall k in V0, forall (i,j) in A: v(k,i,j) >= 0 // void createWorkerLP(IloCplex cplex, IloNumVarArray v, IloNumVarArray u, IloObjective obj, IloInt numNodes) { IloInt i, j, k; IloEnv env = cplex.getEnv(); IloModel mod(env, "atsp_worker"); // Set up IloCplex algorithm to solve the worker LP cplex.extract(mod); cplex.setOut(env.getNullStream()); // Turn off the presolve reductions and set the CPLEX optimizer // to solve the worker LP with primal simplex method. cplex.setParam(IloCplex::Reduce, 0); cplex.setParam(IloCplex::RootAlg, IloCplex::Primal); // Create variables v(k,i,j) forall k in V0, (i,j) in A // For simplicity, also dummy variables v(k,i,i) are created. // Those variables are fixed to 0 and do not partecipate to // the constraints. IloInt numArcs = numNodes * numNodes; IloInt vNumVars = (numNodes-1) * numArcs; IloNumVarArray vTemp(env, vNumVars, 0, IloInfinity); for (k = 1; k < numNodes; ++k) { for (i = 0; i < numNodes; ++i) { vTemp[(k-1)*numArcs + i *numNodes + i].setBounds(0, 0); } } v.clear(); v.add(vTemp); vTemp.end(); mod.add(v); // Set names for variables v(k,i,j) for (k = 1; k < numNodes; ++k) { for(i = 0; i < numNodes; ++i) { for(j = 0; j < numNodes; ++j) { char varName[100]; sprintf(varName, "v.%d.%d.%d", (int) k, (int) i, (int) j); v[(k-1)*numArcs + i*numNodes + j].setName(varName); } } } // Associate indices to variables v(k,i,j) IloIntArray vIndex(env, vNumVars); for (j = 0; j < vNumVars; ++j) { vIndex[j] = j; v[j].setObject(&vIndex[j]); } // Create variables u(k,i) forall k in V0, i in V IloInt uNumVars = (numNodes-1) * numNodes; IloNumVarArray uTemp(env, uNumVars, -IloInfinity, IloInfinity); u.clear(); u.add(uTemp); uTemp.end(); mod.add(u); // Set names for variables u(k,i) for (k = 1; k < numNodes; ++k) { for(i = 0; i < numNodes; ++i) { char varName[100]; sprintf(varName, "u.%d.%d", (int) k, (int) i); u[(k-1)*numNodes + i].setName(varName); } } // Associate indices to variables u(k,i) IloIntArray uIndex(env, uNumVars); for (j = 0; j < uNumVars; ++j) { uIndex[j] = vNumVars + j; u[j].setObject(&uIndex[j]); } // Initial objective function is empty obj.setSense(IloObjective::Minimize); mod.add(obj); // Add constraints: // forall k in V0, forall (i,j) in A: u(k,i) - u(k,j) <= v(k,i,j) for (k = 1; k < numNodes; ++k) { for(i = 0; i < numNodes; ++i) { for(j = 0; j < numNodes; ++j) { if ( i != j ) { IloExpr expr(env); expr -= v[(k-1)*numArcs + i*(numNodes) + j]; expr += u[(k-1)*numNodes + i]; expr -= u[(k-1)*numNodes + j]; mod.add(expr <= 0); expr.end(); } } } } }// END createWorkerLP