Beispiel #1
0
void orange_thresh(float* fbuf, unsigned char* mask) {
  /* Find orange ball mask from HSV float image buffer. */
  /* Thresholds from 'ppm_read_orange.m' */
  float hue, sat, val;
  int i,x,y;
  
  for (x=0; x<WIDTH; x++) {
    for (y=0; y<HEIGHT; y++) {
      hue = fbuf[ind3(x,y,0)];
      sat = fbuf[ind3(x,y,1)];
      val = fbuf[ind3(x,y,2)];
      i = ind1(x, y);
      mask[i] = 0;
      // highlights values
      if (val >= .99 && (hue < 30.0/360.0 || hue > 355.0/360.0) && sat > .1) {
	mask[i] = 1;
      }
      // mid values
      if (val >= .2 && (hue > 5.0/360.0 && hue < 15.0/360.0) && sat > .55) {
	mask[i] = 1;
      }
      if ((hue < 25.0/360.0 || hue > 358.0/360.0) && sat > .6) {
	mask[i] = 1;
      }
      // shadows
      if (val < .6 && val > .3 && (hue < 20.0/360.0 || hue > 355.0/360.0) && sat > .65) {
	mask[i] = 1;
      }
    }
  }
}
Beispiel #2
0
void GA::TestMethod(void)
{
  // initialize individual
  Individual ind1(15, 20), ind2(15, 20);
  ind1.initGenotype(15, 20);
  ind2.initGenotype(15, 20);

  std::cout << "========== Initial Individual ==========" << std::endl;
  std::cout << "Individual 1: ";
  ind1.print();
  std::cout << "individual 2: ";
  ind2.print();

  // swap chromosome
  GeneticOperator::swap(&ind1, &ind2);

  std::cout << "========== After Swap ==========" << std::endl;
  std::cout << "Individual 1: ";
  ind1.print();
  std::cout << "individual 2: ";
  ind2.print();

  // mutation
  ind1.mutate();
  ind2.mutate();

  std::cout << "========== After Mutation ==========" << std::endl;
  std::cout << "Individual 1: ";
  ind1.print();
  std::cout << "individual 2: ";
  ind2.print();

  // crossover 
  Individual child1, child2;
  GeneticOperator::uniform_crossover(ind1, ind2, &child1, &child2);

  std::cout << "========== After Uniform CrossOver ==========" << std::endl;
  std::cout << "Individual 1: ";
  ind1.print();
  std::cout << "individual 2: ";
  ind2.print();
  std::cout << "child      1: ";
  child1.print();
  std::cout << "child      2: ";
  child2.print();
}
int main(int argc, char * argv[])
{
    ProblemDefinitionsSPtr dummy_defs(new ProblemDefinitions(2,2,0,0));
    PopulationSPtr myPop(new Population);
    
    IndividualSPtr ind1(new Individual(dummy_defs));
    std::vector<double> dvs1 {1.0, 0.0};
    ind1->setRealDVs(dvs1);
    ind1->setObjectives(std::vector<double>{1.0, 0.0});
    myPop->push_back(ind1);
    
    IndividualSPtr ind2(new Individual(dummy_defs));
    ind2->setRealDVs(std::vector<double>{0.7, 0.15});
    ind2->setObjectives(std::vector<double>{0.49, 0.0225});
    myPop->push_back(ind2);
    
    IndividualSPtr ind3(new Individual(dummy_defs));
    ind3->setRealDVs(std::vector<double>{0.5, 0.26});
    ind3->setObjectives(std::vector<double>{0.25, 0.0676});
    myPop->push_back(ind3);
    
    IndividualSPtr ind4(new Individual(dummy_defs));
    ind4->setRealDVs(std::vector<double>{0.44, 0.3});
    ind4->setObjectives(std::vector<double>{0.1936, 0.09});
    myPop->push_back(ind4);
    
    IndividualSPtr ind5(new Individual(dummy_defs));
    ind5->setRealDVs(std::vector<double>{0.4, 0.37});
    ind5->setObjectives(std::vector<double>{0.16, 0.1369});
    myPop->push_back(ind5);
    
    IndividualSPtr ind6(new Individual(dummy_defs));
    ind6->setRealDVs(std::vector<double>{0.2, 0.7});
    ind6->setObjectives(std::vector<double>{0.04, 0.49});
    myPop->push_back(ind6);

    IndividualSPtr ind7(new Individual(dummy_defs));
    ind7->setRealDVs(std::vector<double>{0.0, 1.0});
    ind7->setObjectives(std::vector<double>{0, 1});
    myPop->push_back(ind7);

    IndividualSPtr ind1_(new Individual(dummy_defs));
    ind1_->setRealDVs(std::vector<double> {1.0, 0.0});
    ind1_->setObjectives(std::vector<double>{1.2, 0.2});
    myPop->push_back(ind1_);

    IndividualSPtr ind2_(new Individual(dummy_defs));
    ind2_->setRealDVs(std::vector<double>{0.7, 0.15});
    ind2_->setObjectives(std::vector<double>{0.69, 0.2225});
    myPop->push_back(ind2_);

    IndividualSPtr ind3_(new Individual(dummy_defs));
    ind3_->setRealDVs(std::vector<double>{0.5, 0.26});
    ind3_->setObjectives(std::vector<double>{0.45, 0.2676});
    myPop->push_back(ind3_);

    IndividualSPtr ind4_(new Individual(dummy_defs));
    ind4_->setRealDVs(std::vector<double>{0.44, 0.3});
    ind4_->setObjectives(std::vector<double>{0.3936, 0.29});
    myPop->push_back(ind4_);

    IndividualSPtr ind5_(new Individual(dummy_defs));
    ind5_->setRealDVs(std::vector<double>{0.4, 0.37});
    ind5_->setObjectives(std::vector<double>{0.36, 0.3369});
    myPop->push_back(ind5_);

    IndividualSPtr ind6_(new Individual(dummy_defs));
    ind6_->setRealDVs(std::vector<double>{0.2, 0.7});
    ind6_->setObjectives(std::vector<double>{0.24, 0.69});
    myPop->push_back(ind6_);

    IndividualSPtr ind7_(new Individual(dummy_defs));
    ind7_->setRealDVs(std::vector<double>{0.0, 1.0});
    ind7_->setObjectives(std::vector<double>{0.2, 1.2});
    myPop->push_back(ind7_);
    
    std::vector< std::vector<std::pair<IndividualSPtr, double > > > values1 = DebsCrowdingDistance::calculate(myPop);
    FrontsSPtr values2 = myPop->getFronts();
//    typedef std::pair<IndividualSPtr, double> IndDistPair;
//    FrontsSPtr values2(new Fronts(values1.size()));
//    boost::shared_ptr<std::vector<std::vector<IndividualSPtr> > > values2(new std::vector<std::vector<IndividualSPtr> >(values1.size()));
//    for (int i = 0; i < values2->size(); ++i)
//    {
//        BOOST_FOREACH(IndDistPair ind, values1[i])
//        {
//            (*values2)[i].push_back(ind.first);
//        }
//    }
    
#ifdef WITH_VTK
    PlotFrontVTK plot;
    plot(myPop);
#endif

}
int main(int argc, char* argv[])
{
    typedef double DecisionVariable_t;
    typedef std::mt19937 RNG_t;
    unsigned int seed = 1;
    double eta = 10;
    double mutation_probability = 1.0;
    RNG_t rng(seed);
    
    DebsPolynomialMutation<RNG_t> mutation_tester(rng, eta, mutation_probability);
    
    int number_dvs = 1; //number of decision variables
    double min_value = -1.0;
    double max_value = 8.0;
    std::vector<DecisionVariable_t> lower_bounds(number_dvs, min_value);
    std::vector<DecisionVariable_t> upper_bounds(number_dvs, max_value);
    std::vector<int> lower_bounds_i;
    std::vector<int> upper_bounds_i;
    std::vector<MinOrMaxType> min_or_max(1, MINIMISATION);
    ProblemDefinitionsSPtr defs(new ProblemDefinitions(lower_bounds, upper_bounds,lower_bounds_i, upper_bounds_i, min_or_max, 0));
    //    std::vector<DecisionVariable_t> parent1_dv_values {2};
    //    std::vector<DecisionVariable_t> parent2_dv_values {5};
    Individual ind1(defs);

    
    std::vector<DecisionVariable_t> results;
    
    int num_samples = 1000000;
    for (int i = 0; i < num_samples; ++i)
    {
        Individual muted1(ind1);
        mutation_tester.operator()(muted1);
        results.push_back(muted1.getRealDV(0));
        //        std::cout << child1[0] << std::endl;
        //        std::cout << child2[0] << std::endl;
    }
    
#ifdef WITH_VTK
    // plot results.
    // Set up a 2D scene, add an XY chart to it
    VTK_CREATE(vtkContextView, view);
    view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
    view->GetRenderWindow()->SetSize(400, 300);
    VTK_CREATE(vtkChartXY, chart);
    view->GetScene()->AddItem(chart);
    
    // Create a table with some points in it...
    VTK_CREATE(vtkTable, table);
    
    
    VTK_CREATE(vtkIntArray, arrIBin);
    arrIBin->SetName("bin value");
    table->AddColumn(arrIBin);
    
    VTK_CREATE(vtkDoubleArray, arrDBin);
    arrDBin->SetName("decision variable value");
    table->AddColumn(arrDBin);
    
    VTK_CREATE(vtkIntArray, arrFrequency);
    arrFrequency->SetName("Frequency");
    table->AddColumn(arrFrequency);
    
    int num_bins = 50;
    table->SetNumberOfRows(num_bins);
    
    std::vector<int> frequency_count(num_bins);
    std::vector<int> bin_i_names(num_bins);
    std::vector<double> bin_d_names(num_bins);
//    std::vector<std::string> bin_names(num_bins);
    
    for (int i = 0; i < num_samples; ++i)
    {
        frequency_count[int((results[i] - min_value) / (max_value - min_value) * num_bins)]++;
    }
    
    for (int i = 0; i < num_bins; ++i)
    {
        //        bin_names[i] = std::to_string((double(i) / double(num_bins)) * (max_value-min_value) + min_value);
        bin_d_names[i] = ((double(i) / double(num_bins)) * (max_value-min_value) + min_value);
        bin_i_names[i] = i;
    }
    
    for (int i = 0; i < num_bins; i++)
    {
        table->SetValue(i,0,bin_i_names[i]);
        table->SetValue(i,1, bin_d_names[i]);
        table->SetValue(i,2,frequency_count[i]);
    }
    
    // Add multiple line plots, setting the colors etc
    vtkPlot *line = 0;
    
    line = chart->AddPlot(vtkChart::BAR);
#if VTK_MAJOR_VERSION <= 5
    line->SetInput(table, 1, 2);
#else
    line->SetInputData(table, 1, 2);
#endif
    line->SetColor(0, 255, 0, 255);
    
    //Finally render the scene and compare the image to a reference image
    view->GetRenderWindow()->SetMultiSamples(0);
    view->GetInteractor()->Initialize();
    view->GetInteractor()->Start();
#endif
    
}
Beispiel #5
0
Gradient::Gradient(Grid * g){
	//cout << "Trying to create gradient with h = " << g->getH() << endl;
	this->g = g;
	double h = g->getH();
	constantTerm.resize(g->faces.size());
	constantTerm = 0;
	for(int i = g->iMin; i <= g->iMax; i++){
		for(int j = g->jMin; j <= g->jMax; j++){
			if(g->cells(i,j)->isUncovered()){
				//cout << " is uncovered." << endl;
				CellIndex cellIndex(i,j);
				TinyVector<Face*,5> faces = g->cells(i,j)->faces;
				if(g->isFaceUncovered(i,j,B)){
					FaceIndex faceIndex = faces(B)->getIndex();
					Coord centroid = faces(B)->getCentroid();
					TinyVector<double,2> n = faces(B)->getNormal();
					double x, y, r;
					x = centroid(0);
					y = centroid(1);
					r = sqrt(pow2(x) + pow2(y));
					TinyVector<double,2> gradU;
					double pi = acos(-1);
					//gradU(0) = 3*pow(x,2)*pow(y-2,2)+sin(2*(x-pow(y,2)));
					//gradU(1) = 2*pow(x,3)*(y-2) - 2*y*sin(2*(x-pow(y,2)));
					gradU(0) = 0;//-2*pi*sin(2*pi*r)*x/r;
					gradU(1) = 0;//-2*pi*sin(2*pi*r)*y/r;
					constantTerm(faceIndex) += gradU(0)*n(0) + gradU(1)*n(1);
					//cout << "\tAdding boundary condition to constant term." << endl;
				}
				if(faces(N) != 0){
					FaceIndex faceIndex = faces(N)->getIndex();
					if(faces(N)->isRegular()){
						coefficients[faceIndex][cellIndex] = -1/h;
					}
					else if(faces(N)->isIrregular()){
						if(coefficients[faceIndex].count(cellIndex) == 0){
							double alpha = g->cells(i,j)->getFace(N)->getArea();
							if(g->isFaceUncovered(i+1,j,N)){
								CellIndex ind1(i,j);
								CellIndex ind2(i,j+1);
								CellIndex ind3(i+1,j);
								CellIndex ind4(i+1,j+1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
							else if(g->isFaceUncovered(i-1,j,N)){
								CellIndex ind1(i,j);
								CellIndex ind2(i,j+1);
								CellIndex ind3(i-1,j);
								CellIndex ind4(i-1,j+1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
						}
					}
					//cout << "\tDid north face" << endl;
				}
				if(faces(S) != 0){
					FaceIndex faceIndex = faces(S)->getIndex();
					if(faces(S)->isRegular()){
						coefficients[faceIndex][cellIndex] = 1/h;
					}
					else if(faces(S)->isIrregular()){
						if(coefficients[faceIndex].count(cellIndex) == 0){
							double alpha = g->cells(i,j)->getFace(S)->getArea();
							if(g->isFaceUncovered(i+1,j,S)){
								CellIndex ind1(i,j-1);
								CellIndex ind2(i,j);
								CellIndex ind3(i+1,j-1);
								CellIndex ind4(i+1,j);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
							else if(g->isFaceUncovered(i-1,j,S)){
								CellIndex ind1(i,j-1);
								CellIndex ind2(i,j);
								CellIndex ind3(i-1,j-1);
								CellIndex ind4(i-1,j);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
						}
					}
					//cout << "\tDid south face" << endl;
				}
				if(faces(E) != 0){
					FaceIndex faceIndex = faces(E)->getIndex();
					CellIndex cellIndex(i,j);
					if(faces(E)->isRegular()){
						coefficients[faceIndex][cellIndex] = -1/h;
					}
					else if(faces(E)->isIrregular()){
						if(coefficients[faceIndex].count(cellIndex) == 0){
							double alpha = g->cells(i,j)->getFace(E)->getArea();
							if(g->isFaceUncovered(i,j+1,E)){
								CellIndex ind1(i,j);
								CellIndex ind2(i+1,j);
								CellIndex ind3(i,j+1);
								CellIndex ind4(i+1,j+1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
							else if(g->isFaceUncovered(i,j-1,E)){
								CellIndex ind1(i,j);
								CellIndex ind2(i+1,j);
								CellIndex ind3(i,j-1);
								CellIndex ind4(i+1,j-1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
						}
					}
					//cout << "\tDid east face" << endl;
				}
				if(faces(W) != 0){
					FaceIndex faceIndex = faces(W)->getIndex();
					CellIndex cellIndex(i,j);
					if(faces(W)->isRegular()){
						coefficients[faceIndex][cellIndex] = 1/h;
					}
					else if(faces(W)->isIrregular()){
						if(coefficients[faceIndex].count(cellIndex) == 0){
							double alpha = g->cells(i,j)->getFace(W)->getArea();
							if(g->isFaceUncovered(i,j+1,W)){
								CellIndex ind1(i+1,j);
								CellIndex ind2(i,j);
								CellIndex ind3(i+1,j+1);
								CellIndex ind4(i,j+1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
							else if(g->isFaceUncovered(i,j-1,W)){
								CellIndex ind1(i+1,j);
								CellIndex ind2(i,j);
								CellIndex ind3(i+1,j-1);
								CellIndex ind4(i,j-1);
								interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex);
							}
						}
					}
					//cout << "\tDid west face" << endl;
				}
			}
		}
	}

	//cout << "Resized" << endl;
/*
	for(int i = g->iMin; i <= g->iMax; i++){
		for(int j = g->jMin; j <= g->jMax; j++){
			cout << "i = " << i << " j = " << j << endl;
			if(g->cells(i,j)->isUncovered()){
				cout << "Uncovered" << endl;
				double c = 1;
				CellIndex index(i,j);
				TinyVector<Face*,5> faces = g->cells(i,j)->faces;
				cout << "Got faces" << endl;
				for(int k = 0; k < 5; k++){
					cout << "Testing face " << k << endl;
					if(faces(k) != 0 && faces(k)->isUncovered()){
						cout << "Face " << k << " is uncovered ";
						cout << "and has index " << faces(k)->getIndex() << endl;
						cout << coefficients(faces(k)->getIndex()).empty() << endl;
						coefficients(faces(k)->getIndex())[index] = c;
					}
				}
			}
		}
	}*/
}