Exemplo n.º 1
0
          int find_root(data_type &root, const f__ &fun, const g__ &fprime, const data_type &f0) const
          {
            data_type x(x0), fx(fun(x0)), fpx(fprime(x0)), eval, eval_abs, eval_abs2, dx(1);
            typename iterative_root_base<data__>::iteration_type count;

            // calculate the function evaluated at the initial location
            eval=fx-f0;
            eval_abs=std::abs(eval);
            if (this->test_converged(0, eval_abs/f0, eval_abs, 0, 0))
            {
              root=x;
              return iterative_root_base<data__>::converged;
            }

            eval_abs2=0;
            count=0;
            while (!this->test_converged(count, eval_abs/f0, eval_abs, eval_abs2/x0, eval_abs2) && (std::abs(dx)>0))
            {
              if (fpx==0)
                return iterative_root_base<data__>::no_root_found;

              dx=this->calculate_delta_factor(x, -eval/fpx);
              x+=dx;
              fx=fun(x);
              fpx=fprime(x);
              eval=fx-f0;
              eval_abs=std::abs(eval);
              eval_abs2=std::abs(dx);

              ++count;
            }

            root=x;
            if (this->max_iteration_reached(count))
              return this->max_iteration; // could not converge
            if (dx==0)
              return this->hit_constraint; // constraints limited convergence

            return this->converged;
          }
Exemplo n.º 2
0
int main() {
   constants_wrapper cfg;
   cfg.show();
   InitRNG RNG;
   RNG.seed(cfg.SEED);
   int a(0);
   if (!a) std::cout << "TRUEEEEE!!!" << std::endl;
   std::cout << std::endl << std::endl;
   std::cout << "X_MIN = " << cfg.X_MIN << std::endl;
   std::cout << "X_MAX = " << cfg.X_MAX << std::endl;
   std::cout << "Y_MIN = " << cfg.Y_MIN << std::endl;
   std::cout << "Y_MAX = " << cfg.Y_MAX << std::endl;

   std::cout << "READ_FOOD_FROM_FILE = " << cfg.READ_FOOD_FROM_FILE << std::endl;
   std::cout << "FOOD_POINT_DISTRIBUTION = " << cfg.FOOD_POINT_DISTRIBUTION << std::endl;


   chromo beauty_d(beauty_default);
   chromo dim_d(dim_default);
   chromo athlet_d(athlet_default);
   chromo karma_d(karma_default);
   chromo attracted_d(attracted_default);
   chromo charm_d(charm_default);

   DNA dna1(charm_d, beauty_d, dim_d, athlet_d, karma_d, attracted_d);
   DNA dna2(attracted_d, beauty_d, dim_d, athlet_d, karma_d, attracted_d);
   //dna2.set_chromo(c1,5);
   if (dna1 == dna2)  {
      std::cout << "TRUE1" << std::endl;
   } else { std::cout << "qualche problema" << std::endl;
     };
   
   being conf_being(dna1, 0, cfg.starting_energy, true, 1.0, 2.0, 0, 0);
   conf_being.configure(cfg);
   being b1(dna1, 0, cfg.starting_energy, true, 1.0, 2.0, 0, 0);
   //b1.show();
   being b2(dna2, 0, 100, true, 2.0, 2.0, 0, 0);

   food_point fp2(4.1, 4.2, cfg.default_nutrival);
   food_point fp3(1.1, 2.2, cfg.default_nutrival);

   point_2d p1(1,1);
   point_2d p2(2,2);

   // create and open a character archive for output
   std::ofstream ofs("./points.txt");
   // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << p1;
        oa << p2;
        oa << beauty_d;
        oa << dna1;
        oa << b1;
    	// archive and stream closed when destructors are called
    }


    // ... some time later restore the class instance to its orginal state
    point_2d p1new;
    point_2d p2new;
    chromo new_beauty;
    DNA dna1new;
    being b1new;

    {
        // create and open an archive for input
        std::ifstream ifs("./points.txt");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> p1new;
        ia >> p2new;
        ia >> new_beauty;
        ia >> dna1new;
        ia >> b1new;
        // archive and stream closed when destructors are called
    }

   std::cout << "P1new = ";
   p1new.show_point();
   std::cout << std::endl;

   std::cout << "P2new = ";
   p2new.show_point();
   std::cout << std::endl;

   std::cout << "new beauty = " << new_beauty << std::endl;
   std::cout << "newdna1 = " << dna1new << std::endl;

   if (dna1 == dna1new) std::cout << "TRUE!" << std::endl;

   std::cout << "B1NEW = " << std::endl << b1new << std::endl;

   world myworld(cfg.N_BEINGS,cfg.N_FOOD_POINT_AT_START);
   myworld.name("MyWorld");
   std::cout << "World name = " << myworld.name() << std::endl;
   
   //myworld.add(b1);
   //myworld.add(b2);
   //myworld.add(fp2);
   //myworld.add(fp3);
   //myworld.stats();
   //myworld.advance_one_generation();
   //myworld.stats();

//   myworld.load("DATA/200.txt");
//   myworld.stats();
//   myworld.evolve(1);
//   myworld.stats();

   std::vector<int> iv;
   iv.reserve(10);
   for (int i=0; i<10; ++i) iv.push_back(i);

   std::vector<int>::iterator iv_end = iv.end();
   for (std::vector<int>::iterator it = iv.begin(); it!=iv_end; ++it) {
      std::cout << *it << std::endl;
      iv.push_back(11);
   }

   if (cfg.BEINGS_START_DISTRIBUTION == "UNIFORM") {
      std::uniform_real_distribution<float> beings_distribution_x(cfg.X_MIN , cfg.X_MAX);
      std::uniform_real_distribution<float> beings_distribution_y(cfg.Y_MIN , cfg.Y_MAX);
      for (int i = 0; i < cfg.N_BEINGS; ++i) {
         b1.set_x(beings_distribution_x(RNG.generator));
         b1.set_y(beings_distribution_y(RNG.generator));
         myworld.add(b1) ;
      };
   };

   std::cout << "READ_FOOD_FROM_FILE = " << cfg.READ_FOOD_FROM_FILE << std::endl;
   if (!cfg.READ_FOOD_FROM_FILE) 
   {
      std::cout << "Creating food point from scratch" << std::endl;
      if (cfg.FOOD_POINT_DISTRIBUTION == "UNIFORM") {
         std::uniform_real_distribution<float> food_distribution_x(cfg.X_MIN , cfg.X_MAX);
         std::uniform_real_distribution<float> food_distribution_y(cfg.Y_MIN , cfg.Y_MAX);
         for (int i = 0; i < cfg.N_FOOD_POINT_AT_START; ++i) {
            food_point fpx( food_distribution_x(RNG.generator) , food_distribution_y(RNG.generator) , cfg.default_nutrival );
            myworld.add(fpx) ;
         }
      }
   }
   else 
   {
      std::cout << "Reading food points from file = " << cfg.food_file << std::endl;
      // create and open an archive for input
      std::ifstream ifs2(cfg.food_file);
      boost::archive::text_iarchive ia(ifs2);
      food_point newfp;
      // read class state from archive
      for (int i=0; i<cfg.N_FOOD_POINT_AT_START; ++i) {
         ia >> newfp;
         myworld.add(newfp);
      }
   };
  

   myworld.stats();

   //return 0;
   std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
   start = std::chrono::high_resolution_clock::now();
   myworld.evolve(cfg.ITER_NUMBER);
   end = std::chrono::high_resolution_clock::now();
   cfg.save("myworld.cfg");
   std::cout << "World evolved in = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms." << std::endl;
/*
   // create and open a character archive for output
   std::ofstream ofs2("./world.txt");
   // save data to archive
    {
        boost::archive::text_oarchive oa(ofs2);
        // write class instance to archive
        oa << myworld;
        // archive and stream closed when destructors are called
    }



    world newworld(1000,1000);


    {
        // create and open an archive for input
        std::ifstream ifs2("./world.txt");
        boost::archive::text_iarchive ia(ifs2);
        // read class state from archive
        ia >> newworld;
        // archive and stream closed when destructors are called
    }


   newworld.stats();
*/
   return 0;
};
Exemplo n.º 3
0
	void FPSumOf3Squares::emulate(TestCase * tc)
	{
		/* Get I/O values */
		mpz_class svX = tc->getInputValue("X");
		mpz_class svY = tc->getInputValue("Y");
		mpz_class svZ = tc->getInputValue("Z");
		mpz_class svR2 = tc->getInputValue("R2");

		/* Compute correct value */
		FPNumber fpx(wE, wF);
		fpx = svX;
		FPNumber fpy(wE, wF);
		fpy = svY;
		FPNumber fpz(wE, wF);
		fpz = svZ;
		FPNumber fpr2(wE, wF);
		fpr2 = svR2;

		mpfr_t x,y,z, r2, ru, rd;
		mpfr_init2(x,  1+wF);
		mpfr_init2(y,  1+wF);
		mpfr_init2(z,  1+wF);
		mpfr_init2(r2, 1+wF);
		mpfr_init2(ru, 1+wF);
		mpfr_init2(rd, 1+wF);


		fpr2.getMPFR(r2); 

		fpx.getMPFR(x); 
		fpy.getMPFR(y); 
		fpz.getMPFR(z);

		mpfr_mul(x,x,x, GMP_RNDU);
		mpfr_mul(y,y,y, GMP_RNDU);
		mpfr_mul(z,z,z, GMP_RNDU);
		mpfr_add(x,x,y, GMP_RNDU);
		mpfr_add(ru,x,z, GMP_RNDU); // x^2+y^2+r^2, all rounded up
 
		fpx.getMPFR(x); 
		fpy.getMPFR(y); 
		fpz.getMPFR(z);

		mpfr_mul(x,x,x, GMP_RNDD);
		mpfr_mul(y,y,y, GMP_RNDD);
		mpfr_mul(z,z,z, GMP_RNDD);
		mpfr_add(x,x,y, GMP_RNDD);
		mpfr_add(rd,x,z, GMP_RNDD); // x^2+y^2+r^2, all rounded down
 
#if 0
		mpfr_out_str (stderr, 10, 30, x, GMP_RNDN); cerr << " ";
		mpfr_out_str (stderr, 10, 30, rd, GMP_RNDN); cerr << " ";
		mpfr_out_str (stderr, 10, 30, ru, GMP_RNDN); cerr << " ";
		cerr << endl;
#endif
		mpz_class predfalse = 0;
		mpz_class predtrue =  1;

		if(mpfr_cmp(r2, rd)<0) { // R2< X^2+Y^2+Z^2
			tc->addExpectedOutput("P", predfalse);
			tc->addExpectedOutput("P", predfalse);
		}
		else if(mpfr_cmp(r2, ru)>0) {
			tc->addExpectedOutput("P", predtrue);
			tc->addExpectedOutput("P", predtrue);
		}
		else{ // grey area
			tc->addExpectedOutput("P", predtrue);
			tc->addExpectedOutput("P", predfalse);
		}

		mpfr_clears(x,y,z,r2, ru, rd, NULL);

	}