Exemplo n.º 1
0
static VALUE rb_gsl_fminimizer_x(VALUE obj)
{
  gsl_multimin_fminimizer *gmf = NULL;
  gsl_vector *x = NULL;
  Data_Get_Struct(obj, gsl_multimin_fminimizer, gmf);
  x = gsl_multimin_fminimizer_x(gmf);
  return Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, x);
}
Exemplo n.º 2
0
CAMLprim value ml_gsl_multimin_fminimizer_minimum(value ox, value T)
{
  gsl_multimin_fminimizer *t=GSLMULTIMINFMINIMIZER_VAL(T);
  if(Is_block(ox)) {
      value x=Unoption(ox);
      _DECLARE_VECTOR(x);
      _CONVERT_VECTOR(x);
      gsl_vector_memcpy(&v_x, gsl_multimin_fminimizer_x(t));
  }
  return copy_double(gsl_multimin_fminimizer_minimum(t));
}
Exemplo n.º 3
0
double GSLOptimizer::optimize(unsigned int iter,
                              const gsl_multimin_fminimizer_type*t,
                              double ms, double mxs) {
  fis_= get_optimized_attributes();
  best_score_=std::numeric_limits<double>::max();
  unsigned int n= get_dimension();
  if (n ==0) {
    IMP_LOG(TERSE, "Nothing to optimize" << std::endl);
    return get_scoring_function()->evaluate(false);
  }
  gsl_multimin_fminimizer *s=gsl_multimin_fminimizer_alloc (t, n);

  gsl_vector *x= gsl_vector_alloc(get_dimension());
  update_state(x);
  gsl_vector *ss= gsl_vector_alloc(get_dimension());
  gsl_vector_set_all(ss, mxs);

  gsl_multimin_function f= internal::create_f_function_data(this);
  gsl_multimin_fminimizer_set (s, &f, x, ss);
  try {
    int status;
    do {
      --iter;
      //update_state(x);
      status = gsl_multimin_fminimizer_iterate(s);
      if (status) {
        IMP_LOG(TERSE, "Ending optimization because of state " << s
                << std::endl);
        break;
      }
      double sz= gsl_multimin_fminimizer_size(s);
      status= gsl_multimin_test_size(sz, ms);
      update_states();
      if (status == GSL_SUCCESS) {
        IMP_LOG(TERSE, "Ending optimization because of small size " << sz
                << std::endl);
        break;
      }
    } while (status == GSL_CONTINUE && iter >0);
  } catch (AllDone){
  }
  gsl_vector *ret=gsl_multimin_fminimizer_x (s);
  best_score_=gsl_multimin_fminimizer_minimum (s);
  write_state(ret);
  gsl_multimin_fminimizer_free (s);
  gsl_vector_free (x);
  return best_score_;
}
Exemplo n.º 4
0
static PyObject* multimin_multimin_x(PyObject *self,
				     PyObject *args
				     ) {
  gsl_vector* result;
  PyObject* py_result;
  size_t i;
  if ( ((multimin_multiminObject*)self)->min==NULL || ((multimin_multiminObject*)self)->func==NULL) {
    PyErr_SetString(PyExc_RuntimeError,"no function specified!");
    return NULL;
  }
  result=gsl_multimin_fminimizer_x(((multimin_multiminObject*)self)->min);
  if (result==NULL) {
    return NULL;
  }
  py_result=PyTuple_New(result->size);
  if (py_result==NULL) {
    return NULL;
  }
  for (i=0; i<result->size; i++)
    PyTuple_SetItem(py_result,i,PyFloat_FromDouble(gsl_vector_get(result,i)));
  return py_result;
}
Exemplo n.º 5
0
int FC_FUNC_(oct_minimize_direct, OCT_MINIMIZE_DIRECT)
     (const int *method, const int *dim, double *point, const double *step, 
      const double *toldr, const int *maxiter, funcn f, 
      const print_f_fn_ptr write_info, double *minimum)
{
  int iter = 0, status, i;
  double size;

  const gsl_multimin_fminimizer_type *T = NULL;
  gsl_multimin_fminimizer *s = NULL;
  gsl_vector *x, *ss;
  gsl_multimin_function my_func;

  param_fn_t p;
  p.func = f;

  my_func.f = &fn;
  my_func.n = *dim;
  my_func.params = (void *) &p;

  /* Set the initial vertex size vector */
  ss = gsl_vector_alloc (*dim);
  gsl_vector_set_all (ss, *step);

  /* Starting point */
  x = gsl_vector_alloc (*dim);
  for(i=0; i<*dim; i++) gsl_vector_set (x, i, point[i]);

  switch(*method){
  case 6:
    T = gsl_multimin_fminimizer_nmsimplex;
    break;
  }

  s = gsl_multimin_fminimizer_alloc (T, *dim);
  gsl_multimin_fminimizer_set (s, &my_func, x, ss);

  do
    {
      iter++;
      status = gsl_multimin_fminimizer_iterate(s);

      if(status) break;

      *minimum = gsl_multimin_fminimizer_minimum(s);
      for(i=0; i<*dim; i++) point[i] = gsl_vector_get(gsl_multimin_fminimizer_x(s), i);

      size = gsl_multimin_fminimizer_size (s);
      status = gsl_multimin_test_size (size, *toldr);

      write_info(&iter, dim, minimum, &size, point);

    }
  while (status == GSL_CONTINUE && iter < *maxiter);

  if(status == GSL_CONTINUE) status = 1025;

  gsl_vector_free(x); 
  gsl_vector_free(ss);
  gsl_multimin_fminimizer_free(s);
  return status;
}
Exemplo n.º 6
0
void minimd(density_t * ndft){
  int  status;
  int i;
  double stepmin, minimum, g_initial;
  char * output_string;
  gsl_vector *ss;
  const gsl_multimin_fminimizer_type *T;
  gsl_multimin_fminimizer *s;
  gsl_multimin_function my_func;
  size_t iter;
  params_gsl_multimin_function_t params;

  switch(gradient_free_mode){

  case SIMPLEX :

    output_string = (char *) malloc(25*sizeof(char));

    params.nDFT = density_get_val(ndft);
    my_func.n = ipf.npar;
    my_func.f = my_f;
    my_func.params = (void *) (&params);

    /* Initial step sizes */
    ss = gsl_vector_alloc (ipf.npar);
    gsl_vector_set_all(ss, 0.1);

    /* We use the Simplex algorithm from thee GNU Scientific Library (GSL)
       in its optimized version nmsimplex2 */
    T = gsl_multimin_fminimizer_nmsimplex2;

    messages_basic("\n\n\nStarting the optimization.\n\n\n");


    g_initial = my_f(ipf.gamma, &params);
    if(g_initial < epsilon_gvalue){
      if(myid == 0) printf("The value of G for the starting gamma is %.12lg,\n", g_initial);
      if(myid == 0) printf("which is already below the requested threshold of %.12lg\n", epsilon_gvalue);
      parallel_end(EXIT_SUCCESS);
    }
    if(myid == 0) printf("  Starting from gamma =  ");
    if(myid == 0) {for(i = 0; i < ipf.npar; i++) printf ("%.5f ", gsl_vector_get (ipf.gamma, i));}
    if(myid == 0) printf("\n  G(gamma) = %.12lg\n", g_initial);

    /* Initialization of the minimizer s for the function
       my_func starting at the x point */
    messages_basic("\n\nInitialization of the minimizer.\n\n\n");
    s = gsl_multimin_fminimizer_alloc (T, ipf.npar);
    gsl_multimin_fminimizer_set (s, &my_func, ipf.gamma, ss);


    minimum = g_initial;
    iter = 0;
    do
      {
      iter++;
      if(myid == 0) printf("  Iter = %d\n", (int)iter);
      if(myid == 0) printf("    gamma =  ");
      if(myid == 0) {for(i = 0; i < ipf.npar; i++) printf ("%.5f ", gsl_vector_get (gsl_multimin_fminimizer_x(s), i));}
      if(myid == 0) printf("\n    starting G(gamma) = %15.10lg\n", minimum);
      /* We make an iteration of the minimizer s */
      status = gsl_multimin_fminimizer_iterate (s);
      minimum = gsl_multimin_fminimizer_minimum(s);

      if (status){
        if(myid == 0) printf("  Breaking. Reason: %s\n", gsl_strerror(status));
        break;
      }

      if(myid == 0) printf("    G(gamma) = %15.10f\n", minimum);

      stepmin = gsl_multimin_fminimizer_size (s);
      status = gsl_multimin_test_size (stepmin, 1e-2);

    }
    while (status == GSL_CONTINUE && iter < 100);

    if(myid == 0) printf("\n\nFinished optimization. status = %d (%s)\n", status, gsl_strerror(status));
    if(myid == 0) printf("  Final gamma =  ");
    if(myid == 0) {for(i = 0; i < ipf.npar; i++) printf ("%.5f ", gsl_vector_get (gsl_multimin_fminimizer_x(s), i));}
    if(myid == 0) printf("\n  With value: G(gamma) = %.12lg\n\n", gsl_multimin_fminimizer_minimum(s));

    gsl_vector_memcpy(ipf.gamma, gsl_multimin_fminimizer_x(s));
    sprintf(output_string, "pot");
    ipf_write(ipf, ipf_ref, output_string);
    gsl_multimin_fminimizer_free (s);
    fflush(stdout);

    gsl_vector_free(ss);

    break;

  case GENETIC_ALGORITHM :

    output_string = (char *) malloc(75*sizeof(char));

    sprintf(output_string, "python ga.py %f %f %f %f %f %f %d %d %d",
            grid.l, grid.step, extpot.alpha, extpot.Lmin, extpot.Lmax,
            extpot.delta, extpot.npotex, ipf.npar, ipf.poten_selector);

    system(output_string);
    free(output_string);

    break;
  }
}
Exemplo n.º 7
0
void GslOptimizer::minimize_no_gradient( unsigned int dim, OptimizerMonitor* monitor )
{
  // Set initial point
  gsl_vector* x = gsl_vector_alloc(dim);
  for (unsigned int i = 0; i < dim; i++) {
    gsl_vector_set(x, i, (*m_initialPoint)[i]);
  }

  // Tell GSL which solver we're using
  const gsl_multimin_fminimizer_type* type = NULL;

  switch(m_solver_type)
    {
    case(NELDER_MEAD):
      type = gsl_multimin_fminimizer_nmsimplex;
      break;
    case(NELDER_MEAD2):
      type = gsl_multimin_fminimizer_nmsimplex2;
      break;
    case(NELDER_MEAD2_RAND):
      type = gsl_multimin_fminimizer_nmsimplex2rand;
      break;
    case(FLETCHER_REEVES_CG):
    case(POLAK_RIBIERE_CG):
    case(BFGS):
    case(BFGS2):
    case(STEEPEST_DESCENT):
    default:
      // Wat?!
      queso_error();
    }

  // Init solver
  gsl_multimin_fminimizer* solver =
    gsl_multimin_fminimizer_alloc(type, dim);

  // Point GSL at the right functions
  gsl_multimin_function minusLogPosterior;
  minusLogPosterior.n = dim;
  minusLogPosterior.f = &c_evaluate;
  minusLogPosterior.params = (void *)(this);

  // Needed for these gradient free algorithms.
  gsl_vector* step_size = gsl_vector_alloc(dim);

  for(unsigned int i = 0; i < dim; i++) {
    gsl_vector_set(step_size, i, m_fstep_size[i]);
  }

  gsl_multimin_fminimizer_set(solver, &minusLogPosterior, x, step_size);

  int status;
  size_t iter = 0;
  double size = 0.0;

  do
    {
      iter++;
      status = gsl_multimin_fminimizer_iterate(solver);

      if (status) {
        if( m_objectiveFunction.domainSet().env().fullRank() == 0 )
          {
            std::cerr << "Error while GSL does optimisation. "
                      << "See below for GSL error type." << std::endl;
            std::cerr << "Gsl error: " << gsl_strerror(status) << std::endl;
          }
        break;
      }

      size = gsl_multimin_fminimizer_size(solver);

      status = gsl_multimin_test_size (size, this->getTolerance());

      if(monitor)
      {
        gsl_vector* x = gsl_multimin_fminimizer_x(solver);
        std::vector<double> x_min(dim);
        for( unsigned int i = 0; i < dim; i++)
          x_min[i] = gsl_vector_get(x,i);

        double f = gsl_multimin_fminimizer_minimum(solver);

        monitor->append( x_min, f, size );
      }

    }

  while ((status == GSL_CONTINUE) && (iter < this->getMaxIterations()));

  for (unsigned int i = 0; i < dim; i++) {
    (*m_minimizer)[i] = gsl_vector_get(solver->x, i);
  }

  // We're being good human beings and cleaning up the memory we allocated
  gsl_vector_free(step_size);
  gsl_multimin_fminimizer_free(solver);
  gsl_vector_free(x);

  return;
}
void simplex(struct s_best *p_best, struct s_data *p_data, void *p_params_simplex, double (*f_simplex)(const gsl_vector *, void *), double CONVERGENCE_STOP_SIMPLEX, int M, enum plom_print print_opt)
{
  /* simplex algo using GSL. Straightforward adaptation of the GSL doc
     example */

  char str[255];

  FILE *p_file_trace = NULL;
  if(print_opt & PLOM_PRINT_BEST) {
      p_file_trace = plom_fopen(SFR_PATH, GENERAL_ID, "trace", "w", header_trace, p_data);
  }

  double log_like = 0.0;

  const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex2;
  gsl_multimin_fminimizer *simp = NULL;
  gsl_multimin_function minex_func;

  int iter = 0;
  int status;
  double size;

  gsl_vector *x = gsl_vector_alloc(p_best->n_to_be_estimated);
  gsl_vector *jump_sizes = gsl_vector_alloc(p_best->n_to_be_estimated);

  int k;
  for (k=0; k<p_best->n_to_be_estimated; k++) {
      gsl_vector_set(x, k, gsl_vector_get(p_best->mean, p_best->to_be_estimated[k]));
      gsl_vector_set(jump_sizes, k, sqrt(gsl_matrix_get(p_best->var, p_best->to_be_estimated[k], p_best->to_be_estimated[k]))); //note the sqrt !!
  }

  /* Initialize method and iterate */
  minex_func.n = p_best->n_to_be_estimated;
  minex_func.f = f_simplex;
  minex_func.params = p_params_simplex;

  simp = gsl_multimin_fminimizer_alloc(T, p_best->n_to_be_estimated );

  gsl_multimin_fminimizer_set(simp, &minex_func, x, jump_sizes);

  do
    {
#if FLAG_JSON //for the webApp, we block at every iterations to prevent the client to be saturated with msg
        block();
#endif

      iter++;
      status = gsl_multimin_fminimizer_iterate(simp);
      if (status) break;
      size = gsl_multimin_fminimizer_size(simp);
      status = gsl_multimin_test_size(size, CONVERGENCE_STOP_SIMPLEX);

      log_like = - gsl_multimin_fminimizer_minimum(simp);

      if (!(print_opt & PLOM_QUIET)) {
	  if (status == GSL_SUCCESS) {
	      print_log ("converged to maximum !");
	  }
	  sprintf(str, "%5d logLike = %12.5f size = %.14f", iter, log_like, size);
	  print_log(str);
      }

      transfer_estimated(p_best, gsl_multimin_fminimizer_x(simp), p_data);

      if(print_opt & PLOM_PRINT_BEST){
          print_trace(p_file_trace, iter-1, p_best, p_data, log_like);
      }

    } while (status == GSL_CONTINUE && iter < M);

  if(!(print_opt & PLOM_PRINT_BEST)){
      p_file_trace = plom_fopen(SFR_PATH, GENERAL_ID, "trace", "w", header_trace, p_data);
      print_trace(p_file_trace, iter-1, p_best, p_data, log_like);
  }

  plom_fclose(p_file_trace);

  gsl_multimin_fminimizer_free(simp);
  gsl_vector_free(x);
  gsl_vector_free(jump_sizes);
}
Exemplo n.º 9
0
int
main (int argc, char **argv)
{
	struct experiment_params params;
	double dx, dy;
	double speed, angle;
	gsl_vector *solution;
	int c;
	double compute_start, delta;

	while ((c = getopt (argc, argv, "v")) != EOF) {
		switch (c) {
		case 'v':
			vflag = 1;
			break;
		default:
			usage ();
		}
	}


	memset (&params, 0, sizeof params);
	params.observed_hit[0] = 0;
	params.observed_hit[1] = 0;
	params.observed_hit[2] = 1;
	params.observed_bounce[0] = 25;
	params.observed_bounce[1] = 0;
	params.observed_bounce[2] = 0;
	params.observed_secs = 1.359;

	dx = params.observed_bounce[0] - params.observed_hit[0];
	dy = params.observed_bounce[1] - params.observed_hit[1];
	params.observed_dist = hypot (dy, dx);

	params.simulator_dimen = 4;

	params.odesys.function = sim_func;
	params.odesys.dimension = params.simulator_dimen;
	params.odesys.params = &params;

	params.stepper = gsl_odeiv_step_alloc (gsl_odeiv_step_rk8pd,
					       params.simulator_dimen);
	params.controller = gsl_odeiv_control_y_new (1e-6, 0.0);
	params.evolver = gsl_odeiv_evolve_alloc (params.simulator_dimen);
	
	params.minimizer_dimen = 2;
	params.starting_point = gsl_vector_alloc (params.minimizer_dimen);

	params.minimizer_step_sizes = gsl_vector_alloc (params.minimizer_dimen);
	params.minimizer = gsl_multimin_fminimizer_alloc
		(gsl_multimin_fminimizer_nmsimplex2, params.minimizer_dimen);

       gsl_vector_set_all (params.starting_point, 0);
       gsl_vector_set_all (params.minimizer_step_sizes, 1.0);
     
	if (0) {
		graph_error_func (&params);
	}

	compute_start = get_secs ();
	solve_by_simulation (&params);
	delta = get_secs () - compute_start;

	solution = gsl_multimin_fminimizer_x (params.minimizer);

	speed = gsl_vector_get (solution, 0);
	angle = gsl_vector_get (solution, 1);

	printf ("speed = %8.3f angle = %8.3f; compute time %.3fms\n",
		speed * METERS_PER_SEC_TO_MPH,
		RTOD (angle),
		delta * 1000);

	return (0);
}