Plan DOptimization::optimizeDiscretePlan() { double maxX = 0; double maxTrace = 0; vector<double> arrayX = (*optimal)[0]; mainOwnershipFunction = new OwnershipFunction(3, 2, 0.1, optimal->remarkCount); mainOwnershipFunction->calcFCMWithoutCenter(arrayX); mainLocalModel->calcFisherMatrix(*mainOwnershipFunction, *optimal); for (double x = beginPoint; x <= endPoint; x += step) { if (find(arrayX.begin(), arrayX.end(), x) == arrayX.end()) { double trace = isOptimal(x); if (maxTrace < trace) { maxTrace = trace; maxX = x; } } } optimal->enlargeDiscrete(maxX); double minX = 0; double minTrace = 0; mainOwnershipFunction = new OwnershipFunction(3, 2, 0.1, optimal->remarkCount); mainOwnershipFunction->calcFCMWithoutCenter((*optimal)[0]); mainLocalModel->calcFisherMatrix(*mainOwnershipFunction, *optimal); for (int i = 0; i < (*optimal)[0].size(); i++) { double trace = isOptimal((*optimal)[0][i]); if (minTrace > trace || minTrace == 0) { minTrace = trace; minX = (*optimal)[0][i]; } } if (minX == maxX) return *optimal; else { optimal->reduce(minX); return optimizeDiscretePlan(); } }
float CEmdWrapper::emd(signature_t *Signature1, signature_t *Signature2, flow_t *Flow, int *FlowSize){ int itr=0; float totalCost; float w; node2_t *XP; flow_t *FlowP; node1_t U[MAX_SIG_SIZE1], V[MAX_SIG_SIZE1]; w = init(Signature1, Signature2); if (_n1 > 1 && _n2 > 1){/* IF _n1 = 1 OR _n2 = 1 THEN WE ARE DONE */ for (itr = 1; itr < MAX_ITERATIONS; itr++){ /* FIND BASIC VARIABLES */ findBasicVariables(U, V); /* CHECK FOR OPTIMALITY */ if (isOptimal(U, V)){ //printf("itr: %d\n",itr); break; } /* IMPROVE SOLUTION */ newSol(); } if (itr == MAX_ITERATIONS) fprintf(stderr, "emd: Maximum number of iterations has been reached (%d)\n", MAX_ITERATIONS); } /* COMPUTE THE TOTAL FLOW */ totalCost = 0; if (Flow != NULL) FlowP = Flow; for(XP=_X; XP < _EndX; XP++){ if (XP == _EnterX) /* _EnterX IS THE EMPTY SLOT */ continue; if (XP->i == Signature1->n || XP->j == Signature2->n) /* DUMMY FEATURE */ continue; if (XP->val == 0) /* ZERO FLOW */ continue; totalCost += XP->val * _C[XP->i][XP->j]; if (Flow != NULL){ FlowP->from = XP->i; FlowP->to = XP->j; FlowP->amount = XP->val; FlowP++; } } if (Flow != NULL) *FlowSize = FlowP-Flow; /* RETURN THE NORMALIZED COST == EMD */ return (float)(totalCost / w); }
Plan DOptimization::optimizeÑontinuousPlan() { double maxX = 0; double maxTrace = 0; mainOwnershipFunction = new OwnershipFunction(3, 2, 0.1, optimal->remarkCount); mainOwnershipFunction->calcFCMWithoutCenter((*optimal)[0]); mainLocalModel->calcFisherMatrix(*mainOwnershipFunction, *optimal); Plan newPlan = *optimal; for (double x = beginPoint; x <= endPoint; x += step) { double trace = isOptimal(x); if (abs(maxTrace - mainOwnershipFunction->elementCount) > abs(trace - mainOwnershipFunction->elementCount)) { maxTrace = trace; maxX = x; } } if (abs(maxTrace - mainOwnershipFunction->elementCount) > 0.0001) { newPlan.enlarge(maxX); newPlan.clean(); double newTrace = isOptimal(maxX, newPlan); if (abs(newTrace - mainOwnershipFunction->elementCount) > 0.0001) { *optimal = newPlan; return optimizeÑontinuousPlan(); } else return *optimal; } else { return newPlan; } }
bool simplex(double **m, int p, int q){ int nbv = (q-1) - (p-1); //no. of non-basic var //stores current basic var int bv[p]; //initial basic var for(int i = 1; i<p; i++) bv[i] = nbv + (i-1); int ev; //index of entering var int ilv; //index of leaving var while(!isOptimal(m, p, q)){ //find entering var ev = findEnteringVar(m, p, q); //find index of leaving var ilv = findLeavingVar(m, p, q, ev); printf("\n ev = %i ilv = %i \n", ev , ilv); //save ev in basic var array saveEnteringVar(bv, p, ev, ilv); //reduce using row reduce(m, p, q, ilv, ev); //print table after each reduction printMatrix(m, p, q); } //values of final basic variables for(int i=1; i<p; i++){ printf("x%i = %lf\n", bv[i], m[i][q-1]); } //Z value printf("Z = %lf\n",m[0][q-1]); }
SimplexData::StepResult SimplexData::simpleSimplexStep(void) { printf("*** BEGIN STEP ***\n"); printTable(); if (isOptimal()) { printf("Optimal, stopping.\n"); return Optimal; } int pivotcol = pickPivotColumn(); if (pivotcol < 0) { printf("Internal error picking pivot column, should never get here!\n"); return InternalError; } printf("Pivot column picked: %d, entering variable x%d\n", pivotcol + 1, collabels[pivotcol]); if (isUnbounded(pivotcol)) { printf("Unbounded, stopping.\n"); return Unbounded; } int pivotrow = pickPivotRow(pivotcol); if (pivotrow < 0) { printf("Internal error picking pivot row, should never get here!\n"); return InternalError; } printf ("Pivot row picked: %d, exit variable x%d\n", pivotrow + 1, rowlabels[pivotrow]); #ifdef ALTERNATE_PIVOT *this = doPivot2(pivotrow, pivotcol); #else *this = doPivot(pivotrow, pivotcol); #endif printTable(); printf("*** END STEP ***\n"); return Continue; }
float emd(signature_t *Signature1, signature_t *Signature2, float (*Dist)(feature_t *, feature_t *), flow_t *Flow, int *FlowSize) { int itr; double totalCost; float w; node2_t *XP; flow_t *FlowP; node1_t U[MAX_SIG_SIZE1], V[MAX_SIG_SIZE1]; w = init(Signature1, Signature2, Dist); #if DEBUG_LEVEL > 1 printf("\nINITIAL SOLUTION:\n"); printSolution(); #endif if (_n1 > 1 && _n2 > 1) /* IF _n1 = 1 OR _n2 = 1 THEN WE ARE DONE */ { for (itr = 1; itr < MAX_ITERATIONS; itr++) { /* FIND BASIC VARIABLES */ findBasicVariables(U, V); /* CHECK FOR OPTIMALITY */ if (isOptimal(U, V)) break; /* IMPROVE SOLUTION */ newSol(); #if DEBUG_LEVEL > 1 printf("\nITERATION # %d \n", itr); printSolution(); #endif } if (itr == MAX_ITERATIONS) fprintf(stderr, "emd: Maximum number of iterations has been reached (%d)\n", MAX_ITERATIONS); } /* COMPUTE THE TOTAL FLOW */ totalCost = 0; if (Flow != NULL) FlowP = Flow; for(XP=_X; XP < _EndX; XP++) { if (XP == _EnterX) /* _EnterX IS THE EMPTY SLOT */ continue; if (XP->i == Signature1->n || XP->j == Signature2->n) /* DUMMY FEATURE */ continue; if (XP->val == 0) /* ZERO FLOW */ continue; totalCost += (double)XP->val * _C[XP->i][XP->j]; if (Flow != NULL) { FlowP->from = XP->i; FlowP->to = XP->j; FlowP->amount = XP->val; FlowP++; } } if (Flow != NULL) *FlowSize = FlowP-Flow; #if DEBUG_LEVEL > 0 printf("\n*** OPTIMAL SOLUTION (%d ITERATIONS): %f ***\n", itr, totalCost); #endif /* RETURN THE NORMALIZED COST == EMD */ return (float)(totalCost / w); }