void GeneticAlgorithm::resetParameters(int nPopulation, double scaleFactor, double crossingProbability){
	_scaleFactor = scaleFactor;
	_crossingProbability = crossingProbability;
	_nPopulation = nPopulation;
	delete [] _populationParametersOld;
	delete [] _populationParametersNew;

	_populationParametersOld = (Parameters::fitParameters *) mkl_malloc(sizeof(Parameters::fitParameters)*nPopulation,16);
	_populationParametersNew = (Parameters::fitParameters *) mkl_malloc(sizeof(Parameters::fitParameters)*nPopulation,16);

	for(int i  = 0; i < _nPopulation; i++){
		_populationParametersOld[i].c11 = (randomDouble(0.0,1.0))*pow(10,9);
		_populationParametersOld[i].c22 = _populationParametersOld[i].c11;
		_populationParametersOld[i].c33 = _populationParametersOld[i].c11;
		_populationParametersOld[i].c44 = (randomDouble(0.0,1.0))*pow(10,9);
		_populationParametersOld[i].c55 = _populationParametersOld[i].c44;
		_populationParametersOld[i].c66 = _populationParametersOld[i].c44;
		_populationParametersOld[i].c12 = (randomDouble(0.0,1.0))*pow(10,9);
		_populationParametersOld[i].c13 = _populationParametersOld[i].c12;
		_populationParametersOld[i].c23 = _populationParametersOld[i].c12;
		_populationParametersOld[i].chiSq = 1;
		_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);
	}

	//delete _integerDistribution;
	delete [] ints1;
	delete [] ints2;
	delete [] ints3;

	ints1 = new int[_nPopulation];
    ints2 = new int[_nPopulation];
    ints3 = new int[_nPopulation];
	
}
Esempio n. 2
0
template<typename PointT> int
SmoothNormal<PointT>::applySegmentation(std::list<std::list<int> >& segments){

	std::vector<float> pt_residuals;
	pt_residuals.resize(this->input_->points.size(), -1);

	std::vector<bool> used;
	used.resize(this->input_->points.size(), false);

	int pts_segmented =0;
	int search_idx=0;
	while(pts_segmented < this->input_->points.size()){
		//iterate through the points and either find a
	        // point which is a really low residual or find the lowest
	        // residual left.
		int idx =-1;
		float res = std::numeric_limits<float>::max();

		for(int i=search_idx+1; i<this->input_->points.size(); i++){
			if(used[i]) continue;
			if(pt_residuals[i] < 0){
                            pt_residuals[i] = calculateResidual(i);
			}
			if (pt_residuals[i] <  rthresh_*2){ // if its 2 times smaller than rthresh
                            idx = i;                     // its small enough to use as a seed point
                            break;
			}
			else{
			  if (res > pt_residuals[i] ){
			    idx = i;
			    res = pt_residuals[i];
			  }
			}
		}

		if (idx >=0){
			std::list<int> nseg;
			int s = smoothNormalCluster(idx,nseg,*this->input_, *this->normals_, *this->search_,
						used,cluster_N_nbrs, this->smooth_thresh_,rthresh_);
			std::cout << " Created segment " << idx << " with "  << s << " pts \n";
			if (s>2) segments.push_back(nseg);
			pts_segmented += s;
			search_idx = idx;
		}
		else{
			std::cout << "There was no good seed\n";
			break;
		}
	}
	std::cout << "There were "  << segments.size() << " segments \n";
	return segments.size();
}
void GeneticAlgorithm::residualCalculatingThread(Parameters::arrayBounds * arrayBounds){
	
		
	LARGE_INTEGER time1,time2;


	
	for(int i = arrayBounds->start; i <= arrayBounds->end; i++){
	
			QueryPerformanceCounter(&time1);

		_populationParametersNew[i].chiSq = calculateResidual(&_populationParametersNew[i],arrayBounds->threadID);
	
		QueryPerformanceCounter(&time2);
		arrayBounds->time += 1000*(double)(time2.QuadPart-time1.QuadPart)/(freq.QuadPart);
			if(_populationParametersNew[i].chiSq < _populationParametersOld[i].chiSq){
				_populationParametersOld[i] = _populationParametersNew[i];
			}
	}
	SetEvent(arrayBounds->handle);

	return;
}
Esempio n. 4
0
UINT FitThread::calculateMinT()
{
	{		
			double F,dF1,dF2,phi,A,Td,T,m;	
			T = 4.2;
			m = 1.7;
			contents.myParams.m = m;
			contents.myParams.T = T;			

			for(int i = 0; i <contents.iterations; i++)
		{
			F = randomDouble(450,500);
			dF1 = randomDouble(30,50);
			dF2 = randomDouble(-20,0);
			phi = randomDouble(0,.5);
			A = randomDouble(0,2);
			Td = randomDouble(3,10);
	
			contents.myOscFun->changeParams(F,dF1,dF2,phi);
			contents.myEnvel->changeParams(A,T,Td,m);
				
			double residual = calculateResidual();
	
			if(residual < contents.myParams.chiSq)
			{
				contents.myParams.A = A;
				contents.myParams.chiSq = residual;
				contents.myParams.dF1 = dF1;
				contents.myParams.dF2 = dF2;
				contents.myParams.F = F;
				contents.myParams.phi = phi;
				contents.myParams.Td = Td;
			}	
		}	
	}
	SetEvent(contents.hEvent);
	return 0;}
int main(int argc, char* argv[])
{
	//--This first declaration is included for software engineering reasons: allow main method to control flow of data
	//create function pointer for calculateDependentVariable
	//We want to do this so that calculateJacobian can call calculateDependentVariables as it needs to,
	//but we explicitly give it this authority from the main method
	void (*yourCalculateDependentVariables)(const Teuchos::SerialDenseMatrix<int, std::complex<double> >&, const Teuchos::SerialDenseVector<int, std::complex<double> >&, Teuchos::SerialDenseVector<int, std::complex<double> >&);
	yourCalculateDependentVariables = &calculateDependentVariables;

	//Create an object to allow access to LAPACK using a library found in Trilinos
	//
	Teuchos::LAPACK<int, std::complex<double> > Teuchos_LAPACK_Object;	

	//The problem being solved is to find the intersection of three infinite paraboloids:
	//(x-1)^2 + y^2 + z = 0
	//x^2 + y^2 -(z+1) = 0
	//x^2 + y^2 +(z-1) = 0
	//
	//They should intersect at the point (1, 0, 0)
	Teuchos::SerialDenseMatrix<int, std::complex<double> > offsets(NUMDIMENSIONS, NUMDIMENSIONS); 
	offsets(0,0) = 1.0;
	offsets(1,2) = 1.0;
	offsets(2,2) = 1.0;

	//We need to initialize the target vectors and provide an initial guess
	Teuchos::SerialDenseVector<int, std::complex<double> > targetsDesired(NUMDIMENSIONS);
	Teuchos::SerialDenseVector<int, std::complex<double> > targetsCalculated(NUMDIMENSIONS);
	Teuchos::SerialDenseVector<int, std::complex<double> > unperturbedTargetsCalculated(NUMDIMENSIONS);
	Teuchos::SerialDenseVector<int, std::complex<double> > currentGuess(NUMDIMENSIONS);
	Teuchos::SerialDenseVector<int, std::complex<double> > guessAdjustment(NUMDIMENSIONS);

	currentGuess[0] = 2.0;
	currentGuess[1] = 1.0;
	currentGuess[2] = 1.0;
	//Place to store our tangent-stiffness matrix or Jacobian
	//One element for every combination of dependent variable with independent variable
	Teuchos::SerialDenseMatrix<int, std::complex<double> > jacobian(NUMDIMENSIONS, NUMDIMENSIONS);

	int count = 0;
	double error = 1.0E5;

	std::cout << "Running Forward Difference Example" << std::endl;
	while(count < MAXITERATIONS and error > ERRORTOLLERANCE)
	{


		//Calculate Jacobian tangent to currentGuess point
		//at the same time, an unperturbed targetsCalculated is, well, calculated
		calculateJacobian(offsets,
				  jacobian,
				  targetsCalculated,
				  unperturbedTargetsCalculated,
				  currentGuess,
                                  yourCalculateDependentVariables);


		//Compute a guessChange and immediately set the currentGuess equal to the guessChange
 		updateGuess(currentGuess,
			    targetsCalculated,
			    jacobian,
			    Teuchos_LAPACK_Object);


		//Compute F(x) with the updated, currentGuess
		calculateDependentVariables(offsets,
				            currentGuess,
			       		    targetsCalculated);	


		//Calculate the L2 norm of Ftarget - F(xCurrentGuess)
		calculateResidual(targetsDesired,
			          targetsCalculated,
			  	  error);	  


		count ++;
		//If we have converged, or if we have exceeded our alloted number of iterations, discontinue the loop
		std::cout << "Residual Error: " << error << std::endl;
	}
	
	std::cout << "******************************************" << std::endl;
	std::cout << "Number of iterations: " << count << std::endl;
	std::cout << "Final guess:\n x, y, z\n " << currentGuess[0] << ", " << currentGuess[1] << ", " << currentGuess[2] << std::endl;
	std::cout << "Error tollerance: " << ERRORTOLLERANCE << std::endl;
	std::cout << "Final error: " << error << std::endl;
	std::cout << "--program complete--" << std::endl;

	return 0;
}
int main(int argc, char* argv[])
{
	//--This first declaration is included for software engineering reasons: allow main method to control flow of data
	//create function pointer for calculateDependentVariable
	//We want to do this so that calculateJacobian can call calculateDependentVariables as it needs to,
	//but we explicitly give it this authority from the main method
	void (*yourCalculateDependentVariables)(const arma::Mat<std::complex<double> >&, const arma::Col<std::complex<double> >&, arma::Col<std::complex<double> >&);
	yourCalculateDependentVariables = &calculateDependentVariables;

	//The problem being solved is to find the intersection of three infinite paraboloids:
	//(x-1)^2 + y^2 + z = 0
	//x^2 + y^2 -(z+1) = 0
	//x^2 + y^2 +(z-1) = 0
	//
	//They should intersect at the point (1, 0, 0)
	arma::Mat<std::complex<double> > offsets(NUMDIMENSIONS, NUMDIMENSIONS); 
	offsets.fill(0.0);
	offsets.col(0)[0] = 1.0;
	offsets.col(2)[1] = 1.0;
	offsets.col(2)[2] = 1.0;

	//We need to initialize the target vectors and provide an initial guess
	arma::Col<std::complex<double> > targetsDesired(NUMDIMENSIONS);
	targetsDesired.fill(0.0);

	arma::Col<std::complex<double> > targetsCalculated(NUMDIMENSIONS);
	targetsCalculated.fill(0.0);

	arma::Col<double> realTargetsCalculated(NUMDIMENSIONS);
	realTargetsCalculated.fill(0.0);

	arma::Col<std::complex<double> > currentGuess(NUMDIMENSIONS);
	currentGuess.fill(2.0);

	//Place to store our tangent-stiffness matrix or Jacobian
	//One element for every combination of dependent variable with independent variable
	arma::Mat<double> jacobian(NUMDIMENSIONS, NUMDIMENSIONS);
	jacobian.fill(0.0);

	int count = 0;
	double error = 1.0E5;

	std::cout << "Running complex step example ................" << std::endl;
	while(count < MAXITERATIONS and error > ERRORTOLLERANCE)
	{

		//Calculate Jacobian tangent to currentGuess point
		//at the same time, an unperturbed targetsCalculated is, well, calculated
		calculateJacobian(offsets,
				  jacobian,
				  targetsCalculated,
				  currentGuess,
                                  yourCalculateDependentVariables);

		//Compute a new currentGuess 
		updateGuess(currentGuess,
			    realTargetsCalculated,
			    targetsCalculated,
			    jacobian);

		//Compute F(x) with the updated, currentGuess
		calculateDependentVariables(offsets,
				            currentGuess,
			       		    targetsCalculated);	

		//Calculate the L2 norm of Ftarget - F(xCurrentGuess)
		calculateResidual(targetsDesired,
			          targetsCalculated,
			  	  error);	  

		count ++;
		//If we have converged, or if we have exceeded our alloted number of iterations, discontinue the loop
		std::cout << "Residual Error: " << error << std::endl;
	}
	
	std::cout << "******************************************" << std::endl;
	std::cout << "Number of iterations: " << count << std::endl;
	std::cout << "Final guess:\n x, y, z\n" << arma::real(currentGuess.t());
	std::cout << "Error tollerance: " << ERRORTOLLERANCE << std::endl;
	std::cout << "Final error: " << error << std::endl;
	std::cout << "--program complete--" << std::endl;

	return 0;
}
void GeneticAlgorithm::initializeParameters(double* dataSet, int dataSetLength, int nPopulation, double scaleFactor, double crossingProbability){

	_scaleFactor = scaleFactor;
	_crossingProbability = crossingProbability;
	_dataSetLength = dataSetLength;
	_dataSet = dataSet;

	_residualArray = new double*[nThreads];
	_paramArray = new double*[nThreads];
	for(int i = 0; i < nThreads; i++){
	_residualArray[i] = new double[_dataSetLength];
	_paramArray[i] = new double[nParams];

	}
	
	_populationParametersOld = (Parameters::fitParameters *) mkl_malloc(sizeof(Parameters::fitParameters)*nPopulation,16);
	_populationParametersNew = (Parameters::fitParameters *) mkl_malloc(sizeof(Parameters::fitParameters)*nPopulation,16);
	for(int i  = 0; i < _nPopulation; i++){
		
		_populationParametersOld[i].missFreq = new long[_nMissing];
		_populationParametersNew[i].missFreq = new long[_nMissing];


		
	/*	_populationParametersOld[i].c11 = (randomDouble(196,196.1))*pow(10,9);
		_populationParametersOld[i].c22 = _populationParametersOld[i].c11;
		
		_populationParametersOld[i].c33 = (randomDouble(187,187.1))*pow(10,9);

		_populationParametersOld[i].c44 = (randomDouble(63.5,63.6))*pow(10,9);
		_populationParametersOld[i].c55 = _populationParametersOld[i].c44;
		
		_populationParametersOld[i].c66 = (randomDouble(55.7,55.8))*pow(10,9);

		_populationParametersOld[i].c12 = (randomDouble(62.5,62.6))*pow(10,9);
	
		_populationParametersOld[i].c13 = (randomDouble(69.8,69.9))*pow(10,9);
		_populationParametersOld[i].c23 = _populationParametersOld[i].c13; 

		_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);/// ***Tetragonal PuCoGa5*/
			
	/*	_populationParametersOld[i].c11 = (randomDouble(260,300))*pow(10,9);
		_populationParametersOld[i].c22 = _populationParametersOld[i].c11;
		
		_populationParametersOld[i].c33 = (randomDouble(290,320))*pow(10,9);

		_populationParametersOld[i].c44 = (randomDouble(90,110))*pow(10,9);
		_populationParametersOld[i].c55 = _populationParametersOld[i].c44;
		
		_populationParametersOld[i].c66 = (randomDouble(130,150))*pow(10,9);

		_populationParametersOld[i].c12 = (randomDouble(140,165))*pow(10,9);
	
		_populationParametersOld[i].c13 = (randomDouble(100,130))*pow(10,9);
		_populationParametersOld[i].c23 = _populationParametersOld[i].c13; 

		_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);/// ***Tetragonal URu2Si2
*/

		_populationParametersOld[i].c11 = (randomDouble(220,260))*pow(10,9);
		_populationParametersOld[i].c22 = (randomDouble(210,250))*pow(10,9);
		
		_populationParametersOld[i].c33 = (randomDouble(100,150))*pow(10,9);

		_populationParametersOld[i].c44 = (randomDouble(32,38))*pow(10,9);
		_populationParametersOld[i].c55 = (randomDouble(48,52))*pow(10,9);
		
		_populationParametersOld[i].c66 = (randomDouble(94,98))*pow(10,9);

		_populationParametersOld[i].c12 = (randomDouble(100,150))*pow(10,9);
	
		_populationParametersOld[i].c13 = (randomDouble(25,60))*pow(10,9);
		_populationParametersOld[i].c23 = (randomDouble(20,70))*pow(10,9);

		_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);/// ***Orthorhombic YBCO67

		//_populationParametersOld[i].c11 = (randomDouble(1,400))*pow(10,9);
		//_populationParametersOld[i].c22 = _populationParametersOld[i].c11;
		//_populationParametersOld[i].c33 = _populationParametersOld[i].c11;
		//
		//_populationParametersOld[i].c44 = (randomDouble(1,400))*pow(10,9);
		//_populationParametersOld[i].c55 = _populationParametersOld[i].c44;		
		//_populationParametersOld[i].c66 = _populationParametersOld[i].c44;

		//_populationParametersOld[i].c12 = (randomDouble(1,400))*pow(10,9);	
		//_populationParametersOld[i].c13 = _populationParametersOld[i].c12; 
		//_populationParametersOld[i].c23 = _populationParametersOld[i].c12; 

		//_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);/// ***Cubic Nb

		//_populationParametersOld[i].c11 = (randomDouble(120,200))*pow(10,9);
		//_populationParametersOld[i].c22 = _populationParametersOld[i].c11;
		//
		//_populationParametersOld[i].c33 = (randomDouble(120,200))*pow(10,9);

		//_populationParametersOld[i].c44 = (randomDouble(10,100))*pow(10,9);
		//_populationParametersOld[i].c55 = _populationParametersOld[i].c44;
		//
		//_populationParametersOld[i].c66 = (randomDouble(10,100))*pow(10,9);

		//_populationParametersOld[i].c12 = (randomDouble(10,100))*pow(10,9);
	
		//_populationParametersOld[i].c13 = (randomDouble(10,100))*pow(10,9);
		//_populationParametersOld[i].c23 = _populationParametersOld[i].c13; 

		//_populationParametersOld[i].chiSq = calculateResidual(&_populationParametersOld[i],0);/// ***Tetragonal CeCoIn5

	
	}

		_minimumParameters.c11 = 1;
		_minimumParameters.c22 = 1;
		_minimumParameters.c33 = 1;
		_minimumParameters.c44 = 1;
		_minimumParameters.c55 = 1;
		_minimumParameters.c66 = 1;
		_minimumParameters.c12 = 1;
		_minimumParameters.c13 = 1;
		_minimumParameters.c23 = 1;

		_minimumParameters.chiSq = std::numeric_limits<double>::infinity();

		_minimumParameters.missFreq = new long[_nMissing];
}