Exemplo n.º 1
0
bool DNA::mutations(unsigned int random) {
    for (unsigned int i = 0; i < random % (G_DNA_SIZE / 2) + 1; ++i)
      mutations(random % (G_DNA_SIZE - 1) + 1, random);
//    mutations(random % (G_DNA_SIZE - 1) + 1, random);

    return true;
}
Error FinalComposite::initInternal(const ConfigSet& config)
{
	ANKI_ASSERT("Initializing PPS");

	ANKI_CHECK(loadColorGradingTexture("engine_data/DefaultLut.ankitex"));

	m_fbDescr.m_colorAttachmentCount = 1;
	m_fbDescr.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::DONT_CARE;
	m_fbDescr.bake();

	ANKI_CHECK(getResourceManager().loadResource("engine_data/BlueNoiseLdrRgb64x64.ankitex", m_blueNoise));

	// Progs
	ANKI_CHECK(getResourceManager().loadResource("shaders/FinalComposite.glslp", m_prog));

	ShaderProgramResourceMutationInitList<3> mutations(m_prog);
	mutations.add("BLUE_NOISE", 1).add("BLOOM_ENABLED", 1).add("DBG_ENABLED", 0);

	ShaderProgramResourceConstantValueInitList<3> consts(m_prog);
	consts.add("LUT_SIZE", U32(LUT_SIZE))
		.add("FB_SIZE", UVec2(m_r->getWidth(), m_r->getHeight()))
		.add("MOTION_BLUR_SAMPLES", U32(config.getNumber("r.final.motionBlurSamples")));

	const ShaderProgramResourceVariant* variant;
	m_prog->getOrCreateVariant(mutations.get(), consts.get(), variant);
	m_grProgs[0] = variant->getProgram();

	mutations[2].m_value = 1;
	m_prog->getOrCreateVariant(mutations.get(), consts.get(), variant);
	m_grProgs[1] = variant->getProgram();

	return Error::NONE;
}
Exemplo n.º 3
0
int main()
{
  typedef double                                   scalar_type; 
  typedef numeric::ublas::vector     <scalar_type> vector_type;
  typedef numeric::ublas::unit_vector<scalar_type> unit_vector_type;
  typedef numeric::ublas::matrix     <scalar_type> matrix_type;
  
  typedef boost::function<scalar_type( vector_type const & )> function_type;
  typedef boost::function<vector_type( vector_type const & )> function_grad_type;
  typedef boost::function<matrix_type( vector_type const & )> function_hessian_type;
  typedef boost::function<matrix_type( vector_type const & )> function_inv_hessian_type;
  typedef scalar_type (*scalar_norm_type  )( scalar_type );
  typedef scalar_type (*vector_norm_type  )( numeric::ublas::vector_expression<vector_type> const & );
    
  function_type             const f                  = &function::function<vector_type>;
  function_grad_type        const df                 = &function::functionGrad<vector_type>;
  function_hessian_type     const h                  = &function::hessian<vector_type>;
  scalar_type               const preferredPrecision = function::preferredPrecision;
  scalar_type               const step               = function::step;
  scalar_norm_type          const sNorm              = &xmath::abs<scalar_type>;
  vector_norm_type          const vNorm              = &numeric::ublas::norm_2<vector_type>;
  
  if (0)
  {
    // 
    // Calculating Lipschitz constant.
    //
    
    vector_type x(2);
    x(0) = function::loLipschitzX;
    x(1) = function::loLipschitzY;

    vector_type y(2);
    y(0) = function::hiLipschitzX;
    y(1) = function::hiLipschitzY;
    
    vector_type step(2);
    step(0) = function::lipschitzStepX;
    step(1) = function::lipschitzStepY;

    scalar_type const R = 
        numeric::lipschitz_constant<function_type, scalar_type, scalar_norm_type, vector_type, vector_norm_type>(
            f, sNorm, x, y, vNorm, step);
    
    {
      // Saving Lipschitz constant.
      char const *dataFileName = "../output/lipschitz_const.tex";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      *ofs << boost::format("%1$15.8f") % R;
    }
  }

  {
    //
    // Newton algorithm.
    //
    
    vector_type startPoint(2);
    startPoint(0) = function::startX;
    startPoint(1) = function::startY;
    
    std::vector<vector_type> points;
    
    vector_type const xMin = numeric::newton::find_min
                               <function_type, function_grad_type, function_inv_hessian_type, vector_type>(
                                  f, df, h,
                                  startPoint, 
                                  preferredPrecision, step, 
                                  std::back_inserter(points));
    
    {
      // Saving passed spots.
      char const *dataFileName = "output/nm_points.dat";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      std::for_each(points.begin(), points.end(), 
                    boost::bind<void>(&numeric::output_vector_coordinates<std::ofstream, vector_type>, 
                                      boost::ref(*ofs), _1, " ", "\n", "%1$15.8f"));
    }
    
    if (points.begin() != points.end())
    {
      // Saving passed spots function values (for contours).
      char const *dataFileName = "output/nm_contours.gp";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      *ofs << "set cntrparam levels discrete ";
      
      std::transform(++points.begin(), points.end(), std::ostream_iterator<double>(*ofs, ","), f);
      *ofs << f(*points.begin());
      *ofs << std::endl;
    }
    
    {
      // Generating report data.
      
      char const *dataFileName = "output/nm_result.tex";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      boost::optional<scalar_type> prevFMinVal;
      for (size_t p = 0; p < numeric::array_size(function::precisions); ++p)
      {
        scalar_type const precision = function::precisions[p];
        
        std::vector<vector_type> points;
        vector_type const xMin = numeric::newton::find_min
                               <function_type, function_grad_type, function_inv_hessian_type, vector_type>(
                                  f, df, h,
                                  startPoint, 
                                  precision, step, 
                                  std::back_inserter(points));

        *ofs << boost::format("%1$1.0e") % precision << " & " << points.size() << " & (";
        numeric::output_vector_coordinates(*ofs, xMin, ", ", "");
        *ofs << ") & ";
        
        scalar_type const curFMinVal = f(xMin);
        
        *ofs << boost::format("%1$15.8f") % curFMinVal << " & ";
        
        if (prevFMinVal)
          *ofs << boost::format("%1e") % (curFMinVal - *prevFMinVal);
        
        prevFMinVal = curFMinVal;
        
        vector_type const curGrad = df(xMin);
        *ofs << " & (";
        numeric::output_vector_coordinates(*ofs, curGrad, ", ", "", "%1e");
        *ofs << ")";

        *ofs << " \\\\\n";
      }
    }
  }
  
  if (0)
  {
    //
    // Gradient descent algorithm.
    //
    
    vector_type startPoint(2);
    startPoint(0) = function::startX;
    startPoint(1) = function::startY;
    
    std::vector<vector_type> points;
    
    vector_type xMin;
    numeric::gradient_descent::gradient_descent_result const result =
        numeric::gradient_descent::find_min
                               <function_type, function_grad_type, vector_type>(
                                  f, df, 
                                  startPoint, 
                                  preferredPrecision, step, 
                                  xMin,
                                  numeric::true_predicate(), std::back_inserter(points));
    BOOST_ASSERT(result == result); // TODO: Handle result states.
    
    {
      // Saving passed spots.
      char const *dataFileName = "../output/gd_points.dat";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      std::for_each(points.begin(), points.end(), 
                    boost::bind<void>(&numeric::output_vector_coordinates<std::ofstream, vector_type>, 
                                      boost::ref(*ofs), _1, " ", "\n", "%1$15.8f"));
    }
    
    if (points.begin() != points.end())
    {
      // Saving passed spots function values (for contours).
      char const *dataFileName = "../output/gd_contours.gp";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      *ofs << "set cntrparam levels discrete ";
      
      std::transform(++points.begin(), points.end(), std::ostream_iterator<double>(*ofs, ","), f);
      *ofs << f(*points.begin());
      *ofs << std::endl;
    }
    
    {
      // Generating report data.
      
      char const *dataFileName = "../output/gd_result.tex";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      boost::optional<scalar_type> prevFMinVal;
      for (size_t p = 0; p < numeric::array_size(function::precisions); ++p)
      {
        scalar_type const precision = function::precisions[p];
        
        std::vector<vector_type> points;
        vector_type xMin;
        numeric::gradient_descent::gradient_descent_result const result =
            numeric::gradient_descent::find_min
                                   <function_type, function_grad_type, vector_type>(
                                      f, df, 
                                      startPoint, 
                                      precision, step, 
                                      xMin,
                                      numeric::true_predicate(), std::back_inserter(points));
        BOOST_ASSERT(result == result); // TODO: Handle result states.

        *ofs << boost::format("%1$1.0e") % precision << " & " << points.size() << " & (";
        numeric::output_vector_coordinates(*ofs, xMin, ", ", "");
        *ofs << ") & ";
        
        scalar_type const curFMinVal = f(xMin);
        
        *ofs << boost::format("%1$15.8f") % curFMinVal << " & ";
        
        if (prevFMinVal)
          *ofs << boost::format("%1e") % (curFMinVal - *prevFMinVal);
        
        prevFMinVal = curFMinVal;
        
        vector_type const curGrad = df(xMin);
        *ofs << " & (";
        numeric::output_vector_coordinates(*ofs, curGrad, ", ", "", "%1e");
        *ofs << ")";

        *ofs << " \\\\\n";
      }
    }
  }
  
  if (0)
  {
    //
    // Genetic algorithm.
    //
    
    vector_type loGen(2);
    loGen(0) = function::loGenX;
    loGen(1) = function::loGenY;
    
    vector_type hiGen(2);
    hiGen(0) = function::hiGenX;
    hiGen(1) = function::hiGenY;
    
    vector_type mutations(2);
    mutations(0) = function::mutationX;
    mutations(1) = function::mutationY;
    
    size_t const nIndividuals     = function::nIndividuals;
    size_t const nPrecisionSelect = function::nPrecisionSelect;
    double const liveRate         = function::liveRate;
    
    typedef std::vector<vector_type>     points_vec_type;
    typedef std::vector<points_vec_type> points_vecs_vec_type;
    points_vecs_vec_type selectedPointsVecs, notSelectedPointsVec;
    
    typedef numeric::genetic::ParallelepipedonUniformGenerator<vector_type> generator_type;
    typedef numeric::genetic::LCCrossOver                                   crossover_type;
    typedef numeric::genetic::ParallelepipedonMutation<scalar_type>         mutation_type;
    
    vector_type const xMin = numeric::genetic::vectorSpaceGeneticSearch
      <generator_type, crossover_type, mutation_type, vector_type, function_type, scalar_type>(
        generator_type(loGen, hiGen), crossover_type(), mutation_type(mutations.begin(), mutations.end()), f, 
        nIndividuals, liveRate, preferredPrecision, nPrecisionSelect,
        std::back_inserter(selectedPointsVecs), std::back_inserter(notSelectedPointsVec));
    
    {
      // Saving generations.
      for (size_t i = 0; i < selectedPointsVecs.size(); ++i)
      {
        // Selected.
        {
          std::string const dataFileName = (boost::format("../output/gen_populations/gen_selected_points_%1$03d.dat") % i).str();
          boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName.c_str()));
          if (ofs->fail())
          {
            std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
            return 1;
          }
          
          points_vec_type const &vec = selectedPointsVecs[i];
          
          for (size_t j = 0; j < vec.size(); ++j)
            numeric::output_vector_coordinates(*ofs, vec[j], ",");
        }
        
        // Not selected.
        {
          std::string const dataFileName = (boost::format("../output/gen_populations/gen_not_selected_points_%1$03d.dat") % i).str();
          boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName.c_str()));
          if (ofs->fail())
          {
            std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
            return 1;
          }
          
          points_vec_type const &vec = notSelectedPointsVec[i];
          
          for (size_t j = 0; j < vec.size(); ++j)
            numeric::output_vector_coordinates(*ofs, vec[j], ",");
        }
      }
    }
    
    {
      // Generating report data.
      
      char const *dataFileName = "../output/gen_result.tex";
      boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName));
      if (ofs->fail())
      {
        std::cerr << "Failed to open data file '" << dataFileName << "'.\n";
        return 1;
      }
      
      boost::optional<scalar_type> prevFMinVal;
      for (size_t p = 0; p < numeric::array_size(function::precisions); ++p)
      {
        scalar_type const precision = function::precisions[p];
        
        typedef std::vector<std::vector<vector_type> > points_vecs_vec_type;
        points_vecs_vec_type selectedPointsVecs, notSelectedPointsVec;
        
        vector_type const xMin = numeric::genetic::vectorSpaceGeneticSearch
          <generator_type, crossover_type, mutation_type, vector_type, function_type, scalar_type>(
            generator_type(loGen, hiGen), crossover_type(), mutation_type(mutations.begin(), mutations.end()), f, 
            nIndividuals, liveRate, precision, nPrecisionSelect,
            std::back_inserter(selectedPointsVecs), std::back_inserter(notSelectedPointsVec));
        
        *ofs << boost::format("%1$1.0e") % precision << " & " << selectedPointsVecs.size() << " & (";
        numeric::output_vector_coordinates(*ofs, xMin, ", ", "");
        *ofs << ") & ";

        scalar_type const curFMinVal = f(xMin);
        
        *ofs << boost::format("%1$15.8f") % curFMinVal << " & ";
        
        if (prevFMinVal)
          *ofs << boost::format("%1e") % (curFMinVal - *prevFMinVal);
        prevFMinVal = curFMinVal;
        
        vector_type const curGrad = df(xMin);
        *ofs << " & (";
        numeric::output_vector_coordinates(*ofs, curGrad, ", ", "", "%1e");
        *ofs << ")";


        *ofs << " \\\\\n";
      }
    }
  }
  
  return 0;
}