void AdaptiveSO2CPGSynPlas::updateWeights() { const double& phi = getPhi(); const double& beta = getBeta(); const double& gamma = getGamma(); const double& epsilon = getEpsilon(); const double& mu = getMu(); const double& F = getOutput(2); const double& w01 = getWeight(0,1); const double& x = getOutput(0); const double& y = getOutput(1); const double& P = getPerturbation(); // general approach setPhi ( phi + mu * gamma * F * w01 * y ); setBeta ( beta + betaHebbRate * x * F - betaDecayRate * (beta - betaZero) ); setGamma ( gamma + gammaHebbRate * x * F - gammaDecayRate * (gamma - gammaZero) ); setEpsilon( epsilon + epsilonHebbRate * F * P - epsilonDecayRate * (epsilon - epsilonZero) ); }
int main(int argc, char * argv[]) { int numPointsPerDimension; int verbose = 0; double omega; double epsilon; double * * points; struct timeval startTime; struct timeval endTime; double duration; double breakdown = 0; int numIterations; double maxDiff, tmpMaxDiff; int numProcesses; int workingProcesses; int myRank; MPI_Status status; MPI_Request requestUpSend, requestUpRecv; MPI_Request requestDownSend, requestDownRecv; int partitions; int remainder; int width; int i, k; int buffSize; int startRow; double * upPointsSend, * upPointsRecv; double * downPointsSend, * downPointsRecv; int upperProc, lowerProc; struct timeval startInterval; struct timeval endInterval; if (argc < 2) { fprintf(stderr, "ERROR: Too few arguments!\n"); printUsage(argv[0]); exit(1); } else if (argc > 3) { fprintf(stderr, "ERROR: Too many arguments!\n"); printUsage(argv[0]); exit(1); } else { int argIdx = 1; if (argc == 3) { if (strncmp(argv[argIdx], OPTION_VERBOSE, strlen(OPTION_VERBOSE)) != 0) { fprintf(stderr, "ERROR: Unexpected option '%s'!\n", argv[argIdx]); printUsage(argv[0]); exit(1); } verbose = 1; ++argIdx; } numPointsPerDimension = atoi(argv[argIdx]); if (numPointsPerDimension < 2) { fprintf(stderr, "ERROR: The number of points, '%s', should be " "a numeric value greater than or equal to 2!\n", argv[argIdx]); printUsage(argv[0]); exit(1); } } MPI_Init(&argc, &argv); /* get info about how may processes are running * and what is your rank number */ MPI_Comm_size(MPI_COMM_WORLD, &numProcesses); MPI_Comm_rank(MPI_COMM_WORLD, &myRank); /* calculate nominal size of data per each process */ partitions = numPointsPerDimension / numProcesses; /* calculate number of processes with the additional row of data */ remainder = numPointsPerDimension % numProcesses; /* according to myRank, set the width of the table */ width = (myRank < remainder) ? partitions + 1 : partitions; /* decide how many processes are required to do the calculation */ workingProcesses = (numProcesses > numPointsPerDimension) ? numPointsPerDimension : numProcesses; /* terminate processes that won't be used */ /* start of copied part of code */ MPI_Comm MY_WORLD = MPI_COMM_WORLD; if(workingProcesses < numProcesses) { MPI_Group world_group; MPI_Comm_group(MPI_COMM_WORLD, &world_group); // Remove all unnecessary ranks MPI_Group new_group; int ranges[1][3] = {{workingProcesses, (numProcesses - 1), 1}}; MPI_Group_range_excl(world_group, 1, ranges, &new_group); // Create a new communicator MPI_Comm_create(MPI_COMM_WORLD, new_group, &MY_WORLD); if (MY_WORLD == MPI_COMM_NULL) { // Bye bye cruel world MPI_Finalize(); exit(0); } } /* end of copied part of code */ /* source: http://stackoverflow.com/questions/13774968/mpi-kill-unwanted-processes */ /* set the calculation parameters */ omega = getOmega(numPointsPerDimension); epsilon = getEpsilon(numPointsPerDimension); /* allocate points table for each process */ points = allocatePoints(numPointsPerDimension, width, numProcesses); if (points == NULL) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Malloc failed!\n"); exit(1); } /* size of the table to send per each iteration */ buffSize = numPointsPerDimension / 2 + numPointsPerDimension % 2 ; /* initialize additional buffers for communication */ upPointsSend = initializeBuffer(buffSize); upPointsRecv = initializeBuffer(buffSize); downPointsSend = initializeBuffer(buffSize); downPointsRecv = initializeBuffer(buffSize); /* process #0 sends to others separate parts of the table * others wait for incoming data */ if (myRank == 0) { startRow = numPointsPerDimension; for(k = workingProcesses - 1; k >= 0 ; --k) { width = (k < remainder) ? partitions + 1 : partitions; /* initialize points */ initializePoints(points, startRow - width, width, numPointsPerDimension); /* send table to k-th process */ if(k != 0) { for(i = 0; i < width; ++i) { MPI_Send(points[i], numPointsPerDimension, MPI_DOUBLE, k, 123, MY_WORLD); } } startRow -= width; } } else { if(myRank < workingProcesses) { for(i = 0; i < width; ++i) { MPI_Recv(points[i], numPointsPerDimension, MPI_DOUBLE, 0, 123, MY_WORLD, &status); } } } /* remember with which processes you comunicate */ upperProc = myRank == 0 ? MPI_PROC_NULL : myRank - 1; lowerProc = myRank == workingProcesses - 1 ? MPI_PROC_NULL : myRank + 1; /* here each process has it's own data set for computations */ if(remainder > 0) { startRow = (myRank < remainder) ? myRank * (partitions + 1) : myRank * partitions + remainder; } else { startRow = myRank * partitions; } if(gettimeofday(&startTime, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } /* Start of computations. */ numIterations = 0; do { int i, j, color; maxDiff = 0.0; for (color = 0; color < 2; ++color) { /* fill downPointsSend with the last row of points data */ setDataBuffer(downPointsSend, points, width - 1, 1 + ((startRow + width) % 2 == color ? 1 : 0), numPointsPerDimension); if(gettimeofday(&startInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } MPI_Isend(downPointsSend, buffSize, MPI_DOUBLE, lowerProc, color, MY_WORLD, &requestDownSend); MPI_Irecv(downPointsRecv, buffSize, MPI_DOUBLE, lowerProc, color, MY_WORLD, &requestDownRecv); if(gettimeofday(&endInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } breakdown += ((double)endInterval.tv_sec + ((double)endInterval.tv_usec / 1000000.0)) - ((double)startInterval.tv_sec + ((double)startInterval.tv_usec / 1000000.0)); /* fill upPointsSend with the last row of points data */ setDataBuffer(upPointsSend, points, 0, 1 + ((startRow - 1) % 2 == color ? 1 : 0), numPointsPerDimension); if(gettimeofday(&startInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } MPI_Isend(upPointsSend, buffSize, MPI_DOUBLE, upperProc, color, MY_WORLD, &requestUpSend); MPI_Irecv(upPointsRecv, buffSize, MPI_DOUBLE, upperProc, color, MY_WORLD, &requestUpRecv); if(gettimeofday(&endInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } breakdown += ((double)endInterval.tv_sec + ((double)endInterval.tv_usec / 1000000.0)) - ((double)startInterval.tv_sec + ((double)startInterval.tv_usec / 1000000.0)); /* computations of the first row requires data that has to be recieved from other process */ MPI_Wait(&requestUpRecv, &status); for (i = 0; i < width; ++i) { /* before computing the last row of its data, * process has to be sure that it has required * row from process rank+1 */ if(i == width - 1) { MPI_Wait(&requestDownRecv, &status); } for (j = 1 + ((startRow+i) % 2 == color ? 1 : 0); j < numPointsPerDimension - 1; j += 2) { if( (myRank != 0 || i != 0 ) && (myRank != workingProcesses - 1 || i != width - 1) ) { double tmp, diff; double down, up; int jIdx = (j - 1 - ((startRow + i) % 2 == color ? 1 : 0))/ 2; /* decide if up or down value should be taken from additional buffers */ up = (i == 0) ? upPointsRecv[jIdx] : points[i-1][j]; down = (i == width - 1) ? downPointsRecv[jIdx] : points[i+1][j]; /* calculate final value */ tmp = (up + down + points[i][j - 1] + points[i][j + 1]) / 4.0; diff = points[i][j]; points[i][j] = (1.0 - omega) * points[i][j] + omega * tmp; diff = fabs(diff - points[i][j]); if (diff > maxDiff) { maxDiff = diff; } } } } MPI_Barrier(MY_WORLD); } if(gettimeofday(&startInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } /* find new maxDiff among all processes */ MPI_Allreduce(&maxDiff, &tmpMaxDiff, 1, MPI_DOUBLE, MPI_MAX, MY_WORLD ); maxDiff = tmpMaxDiff; if(gettimeofday(&endInterval, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } breakdown += ((double)endInterval.tv_sec + ((double)endInterval.tv_usec / 1000000.0)) - ((double)startInterval.tv_sec + ((double)startInterval.tv_usec / 1000000.0)); ++numIterations; } while (maxDiff > epsilon); /* End of computations. */ if(gettimeofday(&endTime, NULL)) { freePoints(points, width, myRank); fprintf(stderr, "ERROR: Gettimeofday failed!\n"); exit(1); } /* calculate how long did the computation lasted */ duration = ((double)endTime.tv_sec + ((double)endTime.tv_usec / 1000000.0)) - ((double)startTime.tv_sec + ((double)startTime.tv_usec / 1000000.0)); /* we choose the process whose execution lasted for the longest time */ double maxDuration; MPI_Allreduce(&duration, &maxDuration, 1, MPI_DOUBLE, MPI_MAX, MY_WORLD); if(myRank==0) { fprintf(stderr, "Statistics: duration(s)=%.10f breakdown=%.10f #iters=%d diff=%.10f epsilon=%.10f\n", maxDuration, breakdown, numIterations, maxDiff, epsilon); } if (verbose) { MPI_Barrier(MY_WORLD); /* process #0 is responsible for printing results of computation * others send their data straight to it */ if(myRank != 0 && myRank < workingProcesses) { for(k = 0; k < width ; ++k) { MPI_Send(points[k], numPointsPerDimension, MPI_DOUBLE, 0, 123, MY_WORLD); } } else if(myRank == 0) { printPoints(points, width, numPointsPerDimension); for(i = 1; i < workingProcesses; ++i) { width = (i < remainder) ? partitions + 1 : partitions; for (k = 0 ; k < width ; ++k) { MPI_Recv(points[k], numPointsPerDimension, MPI_DOUBLE, i, 123, MY_WORLD, &status); } printPoints(points, width, numPointsPerDimension); } } } /* free all the memory that was allocated */ freePoints(points, width, myRank); free(downPointsSend); free(upPointsSend); free(downPointsRecv); free(upPointsRecv); MPI_Finalize(); return 0; }
double scoreOfEllipse(Line *line, CvBox2D ellipse) { // printf("%f, %f, %d\n", getEpsilon(line, ellipse), getTau(ellipse), getPhi(line, ellipse)); double score = pow(getEpsilon(line, ellipse), 2) * exp(abs(1-getTau(ellipse))) / pow(getPhi(line, ellipse), 2); // printf("score : %f\n", score); return score; }
void SC::addType(AtomType* atomType){ SuttonChenAdapter sca = SuttonChenAdapter(atomType); SCAtomData scAtomData; scAtomData.c = sca.getC(); scAtomData.m = sca.getM(); scAtomData.n = sca.getN(); scAtomData.alpha = sca.getAlpha(); scAtomData.epsilon = sca.getEpsilon(); scAtomData.rCut = 2.0 * scAtomData.alpha; // add it to the map: int atid = atomType->getIdent(); int sctid = SCtypes.size(); pair<set<int>::iterator,bool> ret; ret = SCtypes.insert( atid ); if (ret.second == false) { sprintf( painCave.errMsg, "SC already had a previous entry with ident %d\n", atid ); painCave.severity = OPENMD_INFO; painCave.isFatal = 0; simError(); } SCtids[atid] = sctid; SCdata[sctid] = scAtomData; MixingMap[sctid].resize(nSC_); // Now, iterate over all known types and add to the mixing map: std::set<int>::iterator it; for( it = SCtypes.begin(); it != SCtypes.end(); ++it) { int sctid2 = SCtids[ (*it) ]; AtomType* atype2 = forceField_->getAtomType( (*it) ); SCInteractionData mixer; mixer.alpha = getAlpha(atomType, atype2); mixer.rCut = 2.0 * mixer.alpha; mixer.epsilon = getEpsilon(atomType, atype2); mixer.m = getM(atomType, atype2); mixer.n = getN(atomType, atype2); RealType dr = mixer.rCut / (np_ - 1); vector<RealType> rvals; vector<RealType> vvals; vector<RealType> phivals; rvals.push_back(0.0); vvals.push_back(0.0); phivals.push_back(0.0); for (int k = 1; k < np_; k++) { RealType r = dr * k; rvals.push_back(r); vvals.push_back( mixer.epsilon * pow(mixer.alpha/r, mixer.n) ); phivals.push_back( pow(mixer.alpha/r, mixer.m) ); } mixer.vCut = mixer.epsilon * pow(mixer.alpha/mixer.rCut, mixer.n); CubicSpline* V = new CubicSpline(); V->addPoints(rvals, vvals); CubicSpline* phi = new CubicSpline(); phi->addPoints(rvals, phivals); mixer.V = V; mixer.phi = phi; mixer.explicitlySet = false; MixingMap[sctid2].resize( nSC_ ); MixingMap[sctid][sctid2] = mixer; if (sctid2 != sctid) { MixingMap[sctid2][sctid] = mixer; } } return; }
bool HMMDataSet::execBaumWelch(vector<int> *observations) { //initialize double oldProb = 0; double delta=10e+70; setObservationsAlpha(observations); // double initProb=actualProb; while (delta > DELTA) { oldProb=actualProb; // vector<vector<double> > newStateTrans(numStates, vector<double>(numStates, 0)); // vector<double> newInitialStateDist(numStates, 0); // vector<float> newObsProbDist(numStates, 0); // for (int i = 0; i < numStates; i++) { // newInitialStateDist[i] = getGamma(1, i, observations); // } // setFwdVars(observations); setBackwVars(observations); vector<vector<double> > gamma= getGamma(observations); double ***epsilon= getEpsilon(observations); //2.reestimate stateTrans double sumGamma, sumEpsilon,denominatorB,numeratorB; for (int i = 0; i < numStates; i++) { int timeLength=observations->size(); //1.reestimate newInitialStateTrans initialStateDist[i] = .001+ 0.999*gamma[0][i]; //getGamma(0, i, observations); //2.numerator sumGamma = 0; for (int time = 0; time < timeLength - 1; time++) { sumGamma += gamma[time][i]; } //3.denominator for (int j = 0; j < numStates; j++) { sumEpsilon = 0; for (int time = 0; time < timeLength - 1; time++) { sumEpsilon += epsilon[time][i][j];//getEpsilon(time, i, j, observations); } stateTrans[i][j] = .001+ 0.999*sumEpsilon / sumGamma; } denominatorB = sumGamma + gamma[timeLength- 1][i]; for (int k = 0; k < numStates; k++) { //vector<int>::iterator it=observations->begin(); numeratorB = 0.0; for (int t = 0; t < timeLength; t++) { if ((*observations)[t] == k) numeratorB += gamma[t][i]; //it++; } obsProbDist[i][k] = .001 + .999 * numeratorB / denominatorB; } }//end reestimate stateTrans // stateTrans=newStateTrans; // initialStateDist=newInitialStateDist; setObservationsAlpha(observations); delta=actualProb-oldProb; // De-Allocate memory to prevent memory leak for (int i = 0; i < observations->size(); ++i) { for (int j = 0; j < numStates; ++j) delete [] epsilon[i][j]; delete [] epsilon[i]; } delete [] epsilon; } // cout<<"BaumWelch finished init and final prob:"<<initProb<<", "<<actualProb<<endl; }