コード例 #1
0
ファイル: CParam.cpp プロジェクト: QuanliWang/EditImputeCont
CParam::CParam()  {
  min_Prob_A = 0.01;
  accept_rate = RowVector(2) ; accept_rate = 0 ;
  is_accept = RowVector(2) ; is_accept = 0;
  Prob_A = 0;
  K = -1; //to indicate no number of components has been set
  msg_level = 0; //0 errors only; 1: errors and warnings; 2: errors, warnings and information
}
コード例 #2
0
bool DESolver::Solve(int maxGenerations)
{
	int generation;
	int candidate;
	bool bAtSolution;

	bestEnergy = 1.0E20;
	bAtSolution = false;

	for (generation=0;(generation < maxGenerations) && !bAtSolution;generation++)
		for (candidate=0; candidate < nPop; candidate++)
		{
			(this->*calcTrialSolution)(candidate);
			trialEnergy = EnergyFunction(trialSolution,bAtSolution);

			if (trialEnergy < popEnergy[candidate])
			{
				// New low for this candidate
				popEnergy[candidate] = trialEnergy;
				CopyVector(RowVector(population,candidate),trialSolution);

				// Check if all-time low
				if (trialEnergy < bestEnergy)
				{
					bestEnergy = trialEnergy;
					CopyVector(bestSolution,trialSolution);
				}
			}
		}

	generations = generation;
	return(bAtSolution);
}
コード例 #3
0
ファイル: sbvar.cpp プロジェクト: parb220/dsmh_package
bool SBVAR_symmetric_linear::SetParameters(const TDenseMatrix &A_0, const TDenseMatrix &A_plus)
{
  if ((n_vars != A_0.rows) || (n_vars != A_0.cols) || (n_vars != A_plus.rows) || (n_predetermined != A_plus.cols)) 
    throw dw_exception("VARToParameters() - invalid matrix dimensions");

  parameters.UniqueMemory();
  for (int i=n_vars-1; i >= 0; i--)
    {
      parameters.Insert(begin_b[i],Transpose(U[i])*RowVector(A_0,i));
      parameters.Insert(begin_g[i],Transpose(V[i])*RowVector(A_plus,i));
    }

  A0.UniqueMemory(n_vars,n_vars,false);
  Aplus.UniqueMemory(n_vars,n_predetermined,false);
  for (int i=n_vars-1; i >= 0; i--)
    {
      A0.InsertRowMatrix(i,0,U[i]*parameters.SubVector(begin_b[i],begin_b[i]+dim_b[i]-1));
      Aplus.InsertRowMatrix(i,0,V[i]*parameters.SubVector(begin_g[i],begin_g[i]+dim_g[i]-1));
    }

  return true;
}
コード例 #4
0
ファイル: DESolver.cpp プロジェクト: perwin/imfit
void DESolver::RandToBest1Exp( int candidate )
{
  int r1, r2;
  int n;

  SelectSamples(candidate, &r1, &r2);
  n = (int)RandomUniform(0.0, (double)nDim);

  CopyVector(trialSolution, RowVector(population, candidate));
  for (int i = 0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++) {
    trialSolution[n] += scale * (bestSolution[n] - trialSolution[n])
               + scale * (Element(population,r1,n)
               - Element(population,r2,n));
    n = (n + 1) % nDim;
  }

  return;
}
コード例 #5
0
ファイル: DESolver.cpp プロジェクト: perwin/imfit
void DESolver::Rand2Bin( int candidate )
{
  int r1, r2, r3, r4, r5;
  int n;

  SelectSamples(candidate, &r1, &r2, &r3, &r4, &r5);
  n = (int)RandomUniform(0.0, (double)nDim);

  CopyVector(trialSolution, RowVector(population, candidate));
  for (int i = 0; i < nDim; i++) {
    if ((RandomUniform(0.0,1.0) < probability) || (i  == (nDim - 1)))
      trialSolution[n] = Element(population,r1,n)
                + scale * (Element(population,r2,n)
                      + Element(population,r3,n)
                      - Element(population,r4,n)
                      - Element(population,r5,n));
    n = (n + 1) % nDim;
  }
}
コード例 #6
0
ファイル: DESolver.cpp プロジェクト: perwin/imfit
void DESolver::Best1Bin( int candidate )
{
  int r1, r2;
  int n;

  SelectSamples(candidate, &r1, &r2);
  n = (int)RandomUniform(0.0, (double)nDim);

  CopyVector(trialSolution, RowVector(population, candidate));
  for (int i = 0; i < nDim; i++) {
    if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
      trialSolution[n] = bestSolution[n]
                + scale * (Element(population,r1,n)
                      - Element(population,r2,n));
    n = (n + 1) % nDim;
  }

  return;
}
コード例 #7
0
ファイル: DESolver.cpp プロジェクト: perwin/imfit
int DESolver::Solve( int maxGenerations, int verbose )
{
  int generation;
  int candidate;
  bool bAtSolution;
  double  relativeDeltas[3] = {100.0, 100.0, 100.0};
  double  lastBestEnergy;

  bestEnergy = 1.0E20;
  lastBestEnergy = bestEnergy;

  bAtSolution = false;

  for (generation = 0; (generation < maxGenerations) && !bAtSolution; generation++) {
    for (candidate = 0; candidate < nPop; candidate++) {
      // modified by PE
      //(this->*calcTrialSolution)(candidate);
      CalcTrialSolution(candidate);
      // trialSolution now contains a newly generated parameter vector
      // check for out-of-bounds values and generate random values w/in the bounds
      CopyVector(oldValues, RowVector(population, candidate));
      // oldValues is guaranteed to lie between minBounds and maxBounds
      for (int j = 0; j < nDim; j++) {
        if (trialSolution[j] < minBounds[j])
          trialSolution[j] = minBounds[j] + RandomUniform(0.0,1.0)*(oldValues[j] - minBounds[j]);
        if (trialSolution[j] > maxBounds[j])
          trialSolution[j] = maxBounds[j] - RandomUniform(0.0,1.0)*(maxBounds[j] - oldValues[j]);
      }
      
      // Test our newly mutated/bred trial parameter vector
      trialEnergy = EnergyFunction(trialSolution, bAtSolution);

      if (trialEnergy < popEnergy[candidate]) {
        // New low for this candidate
        popEnergy[candidate] = trialEnergy;
        CopyVector(RowVector(population,candidate), trialSolution);

        // Check if all-time low
        if (trialEnergy < bestEnergy) {
          bestEnergy = trialEnergy;
          CopyVector(bestSolution, trialSolution);
        }
      }
    }
    
    // Debugging printout code added by PE -- print an update every 10 generations
    double  relativeDeltaEnergy = 0.0;
    if ((generation % 10) == 0) {
      if (verbose > 0)
        printf("\nGeneration %4d: bestEnergy = %12.10f", generation, bestEnergy);
      if (generation == 20) {
        relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy);
        relativeDeltas[0] = relativeDeltaEnergy;
        if (verbose > 0)
          printf("   (relative change = %e)", relativeDeltaEnergy);
      }
      else if (generation == 30) {
        relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy);
        relativeDeltas[1] = relativeDeltas[0];
        relativeDeltas[0] = relativeDeltaEnergy;
        if (verbose > 0)
          printf("   (relative change = %e)", relativeDeltaEnergy);
      }
      else if (generation >= 40) {
        relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy);
        relativeDeltas[2] = relativeDeltas[1];
        relativeDeltas[1] = relativeDeltas[0];
        relativeDeltas[0] = relativeDeltaEnergy;
        if (verbose > 0)
          printf("   (relative change = %e)", relativeDeltaEnergy);
        if (TestConverged(relativeDeltas, tolerance)) {
          generations = generation;
          bAtSolution = true;
          return 1;
        }
      }
      lastBestEnergy = bestEnergy;
    }

    if (isnan(bestEnergy)) {
//        fprintf(stderr, "\n*** NaN-valued fit statistic detected (DE optimization)!\n");
      printf("\n\tcandidate %d, bestEnergy = %f\n", candidate, bestEnergy);
    }

  }
  
  generations = generation;
  return 5;
}