// fill DP-table int align (alignentry*** table, char* s1, int len1, char* s2, int len2, int (*costFunc)(char,char)) { int i = 0; int j = 0; int cost = 0; int a,b,c,north,west,northwest; // determine gap-costs int gap = costFunc ('-', '-'); // fill first columns of all rows for (i = 0; i <= len1; i++) { cost = i*gap; // check if costs are bigger than "infinity" => costs = INF if (cost > INF) cost = INF; table[i][0] = alignentry_new(cost,0,1,0); } // fill first row for (j = 0; j <= len2; j++) { cost = j*gap; if (cost > INF) cost = INF; table[0][j] = alignentry_new(cost,1,0,0); } // iterate over matrix and calculate current element for (i = 1; i <= len1; i++) { for (j = 1; j <= len2; j++) { a = table[i][j-1]->value + gap; b = table[(i-1)][j]->value + gap; c = table[(i-1)][j-1]->value + costFunc ((s1[i-1]), (s2[j-1])); cost = MIN(a,b,c); north = 0; west = 0; northwest = 0; if (cost == a) west = 1; if (cost == b) north = 1; if (cost == c) northwest = 1; if (cost > INF) cost = INF; table[i][j] = alignentry_new(cost, west, north, northwest); } } // return costs for last element in table return cost; }
// evaluate the alignment costs // takes pointer to alignment and function pointer to cost-function int alignment_evalcost (alignment* a, int (*costFunc) (char, char)) { int costs = 0; int i,j; // use two indices (one for each sequence) int index1 = 0; int index2 = 0; multiedit* me = NULL; int gap = costFunc ('-', '-'); // gap costs // iterate over all entries in multiedit list for (i = 0; i < a->editlength; i++) { me = a->edits[i]; // iterate over all affected bases from current multiedt operation for (j = 0; j < me->stretch; j++) { // replacement operation if (me->operation == 'R') { // check if current bases in both sequences exist // (and if string is not terminated); // better not to rely on sequence length if (a->s1[index1] == '\0' || a->s2[index2] == '\0') return -1; // determine costs of current base alignment costs += costFunc(a->s1[index1], a->s2[index2]); index1++; index2++; } // insert operation else if (me->operation == 'I') { if (a->s2[index2] == '\0') return -1; costs += gap; index2++; } else { if (a->s1[index1] == '\0') return -1; costs += gap; index1++; } } } return costs; }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- float NavMeshGetPathDistance( Vector &vStart, Vector &vGoal, bool anyz, float maxdist, bool bNoTolerance, CUnitBase *pUnit ) { if( g_pynavmesh_debug.GetInt() > 1 ) DevMsg("NavMeshGetPathDistance: anyz: %d, maxdist: %f, bNoTolerance: %d, unit: %d\n", anyz, maxdist, bNoTolerance, pUnit); CNavArea *startArea, *goalArea; if( bNoTolerance ) { startArea = TheNavMesh->GetNavArea(vStart); goalArea = TheNavMesh->GetNavArea(vGoal); } else { startArea = TheNavMesh->GetNearestNavArea(vStart, anyz, maxdist, false, false); goalArea = TheNavMesh->GetNearestNavArea(vGoal, anyz, maxdist, false, false); } if( !startArea || !goalArea ) { if( g_pynavmesh_debug.GetBool() ) DevMsg("NavMeshGetPathDistance: No start=%d or goal=%d area\n", startArea, goalArea); return -1; } if( g_pynavmesh_debug.GetInt() > 1 ) DevMsg("NavMeshGetPathDistance: startArea: %d, goalArea: %d\n", startArea->GetID(), goalArea->GetID()); if( !pUnit ) { ShortestPathCost costFunc; return NavAreaTravelDistance<ShortestPathCost>(startArea, goalArea, costFunc, maxdist); } else { UnitShortestPathCost costFunc( pUnit, false ); return NavAreaTravelDistance<UnitShortestPathCost>(startArea, goalArea, costFunc, maxdist); } }
void RigidAlignment::optimization(void) { cost_function costFunc(this); nIter = 0; min_newuoa(m_nSubj * 3, m_rot, costFunc, (float)M_PI, 1e-6f, 20000); }