Esempio n. 1
0
int main(void)
{
  double *vect1,*vect2,*vect3;

  wn_gpmake("general_free");

  wn_make_vect(&vect1,LEN);
  wn_make_vect(&vect2,LEN);
  wn_make_vect(&vect3,LEN);

  wn_random_vect(vect1,LEN);
  wn_print_vect(vect1,LEN);
  wn_copy_vect(vect2,vect1,LEN);
  wn_print_vect(vect1,LEN);
  wn_random_vect(vect2,LEN);

  printf("v1.v2 = %lf\n",wn_dot_vects(vect1,vect2,LEN));
  printf("v1.v1 = %lf\n",wn_dot_vects(vect1,vect1,LEN));
  printf("norm2(v1) = %lf\n",wn_norm2_vect(vect1,LEN));
  printf("norm(v1) = %lf\n",wn_norm_vect(vect1,LEN));
  printf("corr(v1,v2)) = %lf\n",
	 wn_dot_vects(vect1,vect2,LEN)/
	 (wn_norm_vect(vect1,LEN)*wn_norm_vect(vect2,LEN)));

  wn_free_vect(vect1,LEN);

  wn_gpfree();

  return(0);
}
Esempio n. 2
0
local void recompute_num_line_samples(wn_cdn_context_type c)
{
# define TARGET_RATIO_DF1_NOISE  (3.0)
  double average_ratio_df1_noise;
  double adjustment; 
  double fract_too_small;

  printf("old num_line_samples = %d\n",c->num_line_samples);

#if 0
  compute_average_ratio_df1_noise(c,&average_ratio_df1_noise);

  printf("average_ratio_df1_noise = %lg\n",average_ratio_df1_noise);

  clamp_number(&average_ratio_df1_noise,0.0,20.0);

  adjustment = pow(average_ratio_df1_noise/TARGET_RATIO_DF1_NOISE,-3.0);

  clamp_number(&adjustment,1.0/1.3,1.3);
#endif

  compute_fract_too_small_ratio_df1_noise(c,&fract_too_small,
					  TARGET_RATIO_DF1_NOISE);
  printf("fract_too_small = %lg\n",fract_too_small);

  if(fract_too_small > 0.3)
  {
    adjustment = 1.3;
  }
  else
  {
    adjustment = 1.0/1.3;
  }

  c->num_line_samples = (int)(adjustment*c->num_line_samples);
  if(c->num_line_samples < c->min_num_line_samples)
  {
    c->num_line_samples = c->min_num_line_samples;
  }
  if(c->num_line_samples > c->num_line_samples_mem)
  {
    wn_gppush(c->current_group);
    c->num_line_samples_mem = c->num_line_samples;
    wn_free(c->xa);
    wn_free(c->fa);
    wn_make_vect(&(c->xa),c->num_line_samples_mem);
    wn_make_vect(&(c->fa),c->num_line_samples_mem);
    wn_gppop();
  }

  printf("new num_line_samples = %d\n",c->num_line_samples);
}
Esempio n. 3
0
local void make_search_direction
(
  wn_cdn_context_type c,
  wn_cdn_srchdir_type *psearch_direction,
  bool is_coord_direction,
  int coord
)
{
  *psearch_direction = (wn_cdn_srchdir_type)wn_zalloc(
                           sizeof(struct wn_cdn_srchdir_type_struct));

  (*psearch_direction)->is_coord_direction = is_coord_direction;
  if(is_coord_direction)
  {
    (*psearch_direction)->coord = coord;
    (*psearch_direction)->dir_vect = NULL;
  }
  else
  {
    (*psearch_direction)->coord = -1;
    wn_make_vect(&((*psearch_direction)->dir_vect),c->num_vars);
  }

  reset_search_direction(c,*psearch_direction);
}
Esempio n. 4
0
local void initialize(void)
{
  int i,j;

  wn_gpmake("no_free"); 

  wn_make_vect(&solution_vect,SIZE*SIZE);
  wn_make_vect(&grad_vect,SIZE*SIZE);

  wn_make_mat(&solution_mat,SIZE,SIZE);
  wn_make_mat(&grad_mat,SIZE,SIZE);
  wn_make_mat(&cost_mat,SIZE,SIZE);

  for(i=0;i<SIZE;++i)
  for(j=0;j<SIZE;++j)
  {
    cost_mat[i][j] = wn_flat_distribution();
  }
}
Esempio n. 5
0
void wn_make_mat(double ***pmat,int len_i,int len_j)
{
  int i;

  *pmat = (double **)wn_alloc(len_i*sizeof(double *));

  for(i=0;i<len_i;i++)
  {
    wn_make_vect(&((*pmat)[i]),len_j);
  }
}
Esempio n. 6
0
void wn_cdn_make_context
(
  wn_cdn_context_type *pc,
  double (*pfunction)(double vect[],int sequence_number),
  int num_vars
)
{
  int i;

  *pc = (wn_cdn_context_type)wn_zalloc(
               sizeof(struct wn_cdn_context_type_struct));

  (*pc)->num_vars = num_vars;
  (*pc)->pfunction = pfunction;
  (*pc)->pline_eval = &default_line_eval;

  (*pc)->num_search_directions = 0;
  (*pc)->max_num_search_directions = (*pc)->num_vars;
  (*pc)->coord_search_direction_array = 
      (wn_cdn_srchdir_type *)wn_zalloc(
           num_vars*sizeof(wn_cdn_srchdir_type));
  for(i=0;i<num_vars;++i)
  {
    make_search_direction((*pc),&(((*pc)->coord_search_direction_array)[i]),
                          TRUE,i);
  }
  (*pc)->search_direction_array = 
      (wn_cdn_srchdir_type *)wn_zalloc(
           ((*pc)->max_num_search_directions)*sizeof(wn_cdn_srchdir_type));
  for(i=0;i<((*pc)->max_num_search_directions);++i)
  {
    ((*pc)->search_direction_array)[i] = NULL;
  }

  wn_make_vect(&((*pc)->current_vect),num_vars);
  wn_make_vect(&((*pc)->func_call_vect),num_vars);
  wn_make_vect(&((*pc)->old_vect),num_vars);
  wn_make_vect(&((*pc)->coord_direction),num_vars);

  (*pc)->min_num_line_samples = 10;
  (*pc)->num_line_samples = (*pc)->min_num_line_samples;
  wn_make_vect(&((*pc)->xa),(*pc)->num_line_samples);
  wn_make_vect(&((*pc)->fa),(*pc)->num_line_samples);

  (*pc)->ob = WN_FHUGE;
  (*pc)->code = WN_CDN_NOT_STARTED;

  (*pc)->num_func_calls = 0;
  (*pc)->max_num_func_calls = 0;

  (*pc)->force_optimize_stop_flag = FALSE;

  (*pc)->current_group = wn_curgp();
}
Esempio n. 7
0
EXTERN void wn_conj_direction_method
(
  int *pcode,
  double *pval_min,
  double vect[],
  double initial_coord_x0s[],
  int passed_num_vars,
  double (*pfunction)(double vect[]),
  int max_func_calls
)
{
  int i,j,iteration;
  double *old_vect,*coord_direction;
  double *new_search_direction;
  double old_val_min;

  wn_memgp conj_dir_memgp;

  wn_gpmake("no_free");

  force_optimize_stop_flag = FALSE;

  num_vars = passed_num_vars;
  max_num_search_directions = num_vars;

  wn_make_vect(&buffer_vect,num_vars);
  search_directions = (double **)wn_zalloc(
		  max_num_search_directions*sizeof(double *));
  wn_make_vect(&old_vect,num_vars);
  wn_make_vect(&coord_direction,num_vars);
  wn_make_vect(&coord_x0s,num_vars);
  wn_make_vect(&search_direction_x0s,max_num_search_directions);
  wn_make_vect(&coord_as,num_vars);
  wn_make_vect(&search_direction_as,max_num_search_directions);
  if(initial_coord_x0s == NULL)
  {
    wn_zero_vect(coord_x0s,num_vars);
  }
  else
  {
    wn_copy_vect(coord_x0s,initial_coord_x0s,num_vars);
  }
  wn_zero_vect(search_direction_x0s,max_num_search_directions);
  wn_zero_vect(coord_as,num_vars);
  wn_zero_vect(search_direction_as,max_num_search_directions);

  /* name and pop the memory group we created */

  conj_dir_memgp = wn_curgp();
  wn_gppop();

  sqrt_tolerance = sqrt(wn_machine_tolerance());

  num_search_directions = 0;

  num_func_calls = 0;

  last_line_function_x_valid = FALSE;
  *pval_min = wn_clip_f((*pfunction)(vect));
  ++num_func_calls;

  for(iteration=0;;++iteration)
  {
    if(wn_conj_direction_debug >= WN_CONJ_DIR_DBG_PASSES)
    {
      printf("iteration = %d ********************************\n",iteration);
      printf("ob = %lg\n",*pval_min);
      fflush(stdout);
    }
    /*
    (void)getchar();
    */

    old_val_min = *pval_min;
    wn_copy_vect(old_vect,vect,num_vars);

    /* minimize along acceleration search directions */
    if(wn_conj_direction_debug >= WN_CONJ_DIR_DBG_LINESEARCH)
    {
      printf("start acceleration line minimizations ------------------\n");
    }
    for(i=0;i<num_search_directions;++i)
    {
      if(wn_conj_direction_debug >= WN_CONJ_DIR_DBG_LINESEARCH)
      {
        printf("acceleration line search %d\n",i);
      }

      line_minimize(vect,search_directions[i],pval_min,
		    &(search_direction_x0s[i]),&(search_direction_as[i]),
		    pfunction);

      if(
	  ((max_func_calls < WN_IHUGE)&&(num_func_calls > max_func_calls))
	    ||
          force_optimize_stop_flag
	)
      {
        *pcode = WN_SUBOPTIMAL;
	goto finish;
      }
    }

    /* minimize along coordinate directions */
    if(wn_conj_direction_debug >= WN_CONJ_DIR_DBG_LINESEARCH) 
    {
      printf("start coordinate line minimizations ------------------\n");
    }
    for(i=0;i<num_vars;++i)
    {
      if(wn_conj_direction_debug >= WN_CONJ_DIR_DBG_LINESEARCH)
      {
        printf("coord line search %d\n",i);
      }

      coord_direction[i] = 1.0;

      line_minimize(vect,coord_direction,pval_min,
		    &(coord_x0s[i]),&(coord_as[i]),
		    pfunction);

      coord_direction[i] = 0.0;

      if(
	  ((max_func_calls < WN_IHUGE)&&(num_func_calls > max_func_calls))
	    ||
          force_optimize_stop_flag
	)
      {
        *pcode = WN_SUBOPTIMAL;
	goto finish;
      }
    }

    if(*pval_min >= old_val_min)
    {
      wn_assert(*pval_min == old_val_min);

      *pcode = WN_SUCCESS;
      break;
    }

    /* compute new acceleration search direction */
    if(num_search_directions < max_num_search_directions)
    {
      wn_gppush(conj_dir_memgp);
      wn_make_vect(&new_search_direction,num_vars);
      wn_gppop();
      for(i=num_search_directions;i>0;--i)
      {
        search_directions[i] = search_directions[i-1];
	search_direction_x0s[i] = search_direction_x0s[i-1];
	search_direction_as[i] = search_direction_as[i-1];
      }
      search_directions[0] = new_search_direction;
      search_direction_x0s[0] = 1.0;
      search_direction_as[0] = 0.0;

      ++num_search_directions;
    }
    else
    {
      new_search_direction = search_directions[max_num_search_directions-1];
      for(i=max_num_search_directions-1;i>0;--i)
      {
        search_directions[i] = search_directions[i-1];
	search_direction_x0s[i] = search_direction_x0s[i-1];
	search_direction_as[i] = search_direction_as[i-1];
      }
      search_directions[0] = new_search_direction;
      search_direction_x0s[0] = 1.0;
      search_direction_as[0] = 0.0;
    }

    for(j=0;j<num_vars;++j)
    {
      new_search_direction[j] = vect[j] - old_vect[j];
    }
  }

finish: ;

  force_optimize_stop_flag = FALSE;
  last_line_function_x_valid = FALSE;

  wn_gppush(conj_dir_memgp);
  wn_gpfree();
}
Esempio n. 8
0
local void fit_experiments_to_poly2
(
  double poly[3],
  double poly_total_noise[3],
  double poly_random_noise[3],
  double poly_systematic_noise[3],
  double *precommend_widen,
  double x_vect[],
  double f_vect[],
  int n
)
{
  int i,j,code;
  double **m,**mlsinv,**m3lsinv;
  double poly4[4],poly4_random_noise[4];
  double total4_noise2;
  double poly3_2;
  double *x3_vect;

  wn_assert(n > 4);

  wn_gpmake("no_free");

  wn_make_mat(&m,n,4);

  for(i=0;i<n;++i)
  for(j=0;j<4;++j)
  {
    m[i][j] = pow(x_vect[i],(double)j);
  }

  wn_make_vect(&x3_vect,n);

  for(i=0;i<n;++i)
  {
    x3_vect[i] = m[i][3];
  }

  wn_make_mat(&m3lsinv,3,n);
  ls_invert_mat(&code,m3lsinv,m,n,3);
  wn_assert(code == WN_SUCCESS);

  wn_make_mat(&mlsinv,4,n);
  ls_invert_mat(&code,mlsinv,m,n,4);
  wn_assert(code == WN_SUCCESS);

  wn_mult_mat_by_vect(poly,m3lsinv,f_vect,
		      3,n);
  /*
  printf("poly = \n");
  wn_print_vect(poly,3);
  */

  wn_mult_mat_by_vect(poly4,mlsinv,f_vect,
		      4,n);
  compute_noise2(&total4_noise2,m,poly4,f_vect,n,4);
  /*
  printf("total4_noise2 = %lg\n",total4_noise2);
  printf("poly4 = \n");
  wn_print_vect(poly4,4);
  */

  for(i=0;i<4;++i)
  {
    poly4_random_noise[i] = sqrt(total4_noise2*wn_norm2_vect(mlsinv[i],n));
  }
  for(i=0;i<3;++i)
  {
    /* use total4_noise2 because it is the most accurate
       estimate of function noise that we have */
    poly_random_noise[i] = sqrt(total4_noise2*wn_norm2_vect(m3lsinv[i],n));
  }

  poly3_2 = wn_square(poly4[3]) - wn_square(poly4_random_noise[3]);
  /*
  printf("poly3_2(%lg) = wn_square(poly4[3])(%lg) - wn_square(poly4_random_noise[3])(%lg)\n",
         poly3_2,wn_square(poly4[3]),wn_square(poly4_random_noise[3]));
  */
  if(poly3_2 < 0.0)
  {
    poly3_2 = 0.0;
  }

  wn_mult_mat_by_vect(poly_systematic_noise,m3lsinv,x3_vect,
		      3,n);
  for(i=0;i<3;++i)
  {
    poly_systematic_noise[i] = sqrt(poly3_2)*wn_abs(poly_systematic_noise[i]);
  }

  for(i=0;i<3;++i)
  {
    poly_total_noise[i] = 
                sqrt(wn_square(poly_random_noise[i])+
	             wn_square(poly_systematic_noise[i]));
  }

  if(poly_systematic_noise[1] == 0.0)
  {
    *precommend_widen = WN_FHUGE;
  }
  else
  {
    *precommend_widen = pow(0.5*wn_square(poly_random_noise[1])/
			    wn_square(poly_systematic_noise[1]),1.0/6.0);
  }

  wn_gpfree();
}
Esempio n. 9
0
local void mat_test_big_simplex_loop(void)
{
  /* double objective; */
  double val,*solution_vect,*objective_vect,
	 *right_side,*right_side_control,**mat;
  /* int vars,eqs; */
  int len_i,len_j,code,i,j;
  int *zero_vars,*non_zero_vars;

  wn_gpmake("no_free");    /* temp memory group */

  len_j = LEN;
  len_i = 3*len_j;

  wn_make_mat(&mat,len_i,len_j);

  wn_make_vect(&solution_vect,len_j);
  wn_make_vect(&objective_vect,len_j);
  wn_make_vect(&right_side,len_i);
  wn_make_vect(&right_side_control,len_i);

  zero_vars = (int *)wn_zalloc(len_j*sizeof(int));
  non_zero_vars = (int *)wn_zalloc(len_i*sizeof(int));

  for(j=0;j<len_j;++j)
  {
    val = wn_normal_distribution();
    if(val < 0.0)
    {
      val = -val;
    }

    objective_vect[j] = val; 
  }

  for(i=0;i<len_i;++i)
  {
    val = wn_normal_distribution();
    if(val < 0.0)
    {
      val = -val;
    }

    right_side[i] = val; 
    right_side_control[i] = val;
  }

  for(i=0;i<len_i;++i)
  for(j=0;j<len_j;++j)
  {
    val = wn_normal_distribution();
    if(val < 0.0)
    {
      val = -val;
    }

    mat[i][j] = val;
  }

  /*
  printf("mat =\n");
  wn_print_mat(mat,len_i,len_j);
  printf("rhs =\n");
  wn_print_vect(right_side,len_i);
  printf("ob =\n");
  wn_print_vect(objective_vect,len_j);
  */

  wn_simplex_loop(&code,mat,right_side,right_side_control,
		  non_zero_vars,zero_vars,
		  len_i,len_j);
  wn_assert(WN_SUCCESS == code);

  /*
  printf("code = %d\n",code);
  */
  /*
  wn_print_vect(solution_vect,len_j);
  */

  wn_gpfree();
} /* mat_test_big_simplex_loop */
Esempio n. 10
0
local void mat_test_big_simplex()
{
  double val,objective,*solution_vect,*objective_vect,*right_side,**mat;
  int vars,eqs;
  int len_i,len_j,code,i,j;

  wn_gpmake("no_free");    /* temp memory group */

  vars = LEN;
  eqs = LEN;

  len_i = eqs;
  len_j = vars+eqs;

  wn_make_mat(&mat,len_i,len_j);

  wn_make_vect(&solution_vect,len_j);
  wn_make_vect(&objective_vect,len_j);
  wn_make_vect(&right_side,len_i);

  for(j=0;j<len_j;++j)
  {
    val = wn_normal_distribution();
    /*
    if(val < 0.0)
    {
      val = -val;
    }
    */

    objective_vect[j] = val; 
    /*
    objective_vect[j] = 1.0;
    */
  }

  for(i=0;i<len_i;++i)
  {
    val = wn_normal_distribution();
    if(val < 0.0)
    {
      val = -val;
    }

    right_side[i] = val; 
  }

  for(i=0;i<len_i;++i)
  {
    wn_zero_vect(mat[i],len_j);

    for(j=0;j<vars;++j)
    {
      val = wn_normal_distribution();
      if(val < 0.0)
      {
	val = -val;
      }

      mat[i][j] = val;
    }

    mat[i][vars+i] = 1;
  }

  wn_simplex_method(&code,&objective,NULL,solution_vect,
		    objective_vect,mat,right_side,
		    len_i,len_j);
  wn_assert(WN_SUCCESS == code);

  wn_gpfree();
} /* mat_test_big_simplex */
Esempio n. 11
0
local void mat_test_gramm_schmidt(void)
{
  double **mat, **mat_orig;
  int ilen, jlen, code;		/* ilen rows, jlen columns */
  int a, b;
  double *sum_vect, mul, mul2;
  int trial;

  for (trial = 0;  trial < 1000;  ++trial) {
    wn_gpmake("no_free");    /* temp memory group */

      jlen = (unsigned) wn_random_int() % 20    +  1;
      ilen = (unsigned) wn_random_int() % jlen  +  1;

      wn_make_mat(&mat_orig, ilen, jlen);
      wn_random_mat(mat_orig, ilen, jlen);

      wn_make_mat(&mat, ilen, jlen);
      for (a = 0;  a < ilen;  ++a) {
	for (b = 0;  b < jlen;  ++b) {
	  mat[a][b] = mat_orig[a][b];
	}
      }

      wn_gramm_schmidt(&code, mat, ilen, jlen);
      wn_assert(WN_SUCCESS == code);

      /* first ilen vects should be orthogonal */
      for (a = 0;  a < ilen;  ++a) {
	for (b = a+1;  b < ilen;  ++b) {
	  wn_assert(LO_ALMOST_EQUAL(0.0, wn_dot_vects(mat[a], mat[b], jlen)));
	}
      }

      /* let's make the gramm_schmidt be unit vectors, to be simple */
      for (a = 0;  a < ilen;  ++a) {
	mul2 = wn_dot_vects(mat[a], mat[a], jlen);
	mul = 1/sqrt(mul2);
	for (b = 0;  b < jlen;  ++b) {
	  mat[a][b] *= mul;
	}
	wn_assert(LO_ALMOST_EQUAL(1.0, wn_dot_vects(mat[a], mat[a], jlen)));
      }

      /* check again orthogonality */
      for (a = 0;  a < ilen;  ++a) {
	for (b = a+1;  b < ilen;  ++b) {
	  wn_assert(LO_ALMOST_EQUAL(0.0, wn_dot_vects(mat[a], mat[b], jlen)));
	}
      }

      /* make a vector in the space spanned by the original vectors */
      wn_make_vect(&sum_vect, jlen);
      wn_zero_vect(sum_vect, jlen);
      for (a = 0;  a < ilen;  ++a) {
	wn_add_scaled_vect(sum_vect, mat_orig[a], wn_flat_distribution(),
	/**/							jlen);
      }

      /* OK, sum_vect is reached from the original matrix of vectors */
      /* can we reach it from mat? */
      for (a = 0;  a < ilen;  ++a) {
	mul = wn_dot_vects(sum_vect, mat[a], jlen);
	wn_add_scaled_vect(sum_vect, mat[a], -mul, jlen);
      }
      wn_assert(LO_ALMOST_EQUAL(0.0, wn_dot_vects(sum_vect, sum_vect, jlen)));

    wn_gpfree();
  } /* for trial */
} /* mat_test_gramm_schmidt */
Esempio n. 12
0
EXTERN void wn_conj_gradient_method
(
  int *pcode,
  double *pval_min,
  double vect[],
  int len,
  double (*pfunction)(double vect[]),
  void (*pgradient)(double grad[],double vect[]),
  int max_iterations
)
{
  int iteration,no_move_count;
  int stable_satisfy_count;
  double norm2_g,norm2_last_g,g_dot_last_g,val,last_val,beta,
	 jump_len,last_jump_len,alpha;
  double *g,*last_g,*direction;
  bool function_free_method,last_was_function_free_method;
  bool first_parabolic_fit_succeeded;
  double computed_g_dot_dlast;

  old_group = wn_curgp();
  wn_gpmake("no_free");

  wn_force_optimize_stop_flag = FALSE;

  wn_make_vect(&buffer_vect,len);
  wn_make_vect(&g,len);
  wn_make_vect(&last_g,len);
  wn_make_vect(&direction,len);

  function_free_method = FALSE;
  stable_satisfy_count = 0;
  last_was_function_free_method = FALSE;
  jump_len = 1.0;
  no_move_count = 0;
  beta = 0.0;

  wn_gppush(old_group);

  val = (*pfunction)(vect);
  (*pgradient)(g,vect);

  wn_copy_vect(direction,g,len);

  norm2_g = wn_norm2_vect(g,len);

  for(iteration=0;;++iteration)
  {
    last_dy1 = dy1;
    last_jump_len = jump_len;
    wn_swap(last_g,g,double *);  /* move g to last g */
    norm2_last_g = norm2_g;

    if(function_free_method)
    {
      double x2;
      double g0_dot_d0,g1s_dot_d0,dot_diff;

      dy1 = wn_dot_vects(direction,last_g,len);

      last_jump_len = -wn_sign(dy1)*wn_abs(last_jump_len);
      x2 = last_jump_len;

      /*
      printf("last_jump_len = %lg\n",last_jump_len);
      */

      wn_add_vect_and_scaled_vect(buffer_vect,vect,direction,last_jump_len,len);

      (*pgradient)(g,buffer_vect);

      /*
      g0_dot_d0 = wn_dot_vects(last_g,direction,len);
      */
      g0_dot_d0 = dy1;
      g1s_dot_d0 = wn_dot_vects(g,direction,len);
      dot_diff = g0_dot_d0-g1s_dot_d0;
     
      if(!(dot_diff > 0.0))  /* not upward facing parabola */
      {
        stable_satisfy_count = 0;
	goto function_based_method;
      }

      alpha = g0_dot_d0/dot_diff;
      /*
      printf("alpha = %lg\n",alpha);
      */

      if(!(((1.0-10000.0) < alpha)&&(alpha < (1.0+10000.0))))
      {
        stable_satisfy_count = 0;
	goto function_based_method;
      }

      jump_len = alpha*last_jump_len;

      /* g[j] = alpha*g[j] + (1.0-alpha)*last_g[j]; */
      wn_scale_vect(g,alpha,len);
      wn_add_scaled_vect(g,last_g,1.0-alpha,len);

      g_dot_last_g = wn_dot_vects(g,last_g,len);
      if(beta != 0.0)
      {
	computed_g_dot_dlast = -g_dot_last_g/beta;
	/*
	printf("computed_g_dot_dlast=%lg,last_dy1=%lg\n",
	       computed_g_dot_dlast,last_dy1);
	*/
	if(!(wn_abs(computed_g_dot_dlast) < 0.4*wn_abs(last_dy1)))
	{
          stable_satisfy_count = 0;
	  goto function_based_method;
        }
      }

      wn_add_scaled_vect(vect,direction,alpha*last_jump_len,len);

      if(
          wn_force_optimize_stop_flag
	    ||
	  ((max_iterations < WN_IHUGE)&&(iteration >= max_iterations))
        )
      {
        wn_force_optimize_stop_flag = FALSE;
        wn_gppop();
        wn_gpfree();
        val = (*pfunction)(vect);
        *pval_min = val;
        *pcode = WN_SUBOPTIMAL;
        return;
      }
    }
    else  /* function based method */
    {
      function_based_method: 

      if(last_was_function_free_method)
      {
        /* set so that next iteration will succeed */
        val = (*pfunction)(vect);  
      }
      function_free_method = FALSE;

      if(norm2_last_g == 0.0)   /* unlikely */
      {
        wn_gppop();
        wn_gpfree();
        *pval_min = val;
        *pcode = WN_SUCCESS;
        return;
      }

      last_val = val;

      line_minimize(pcode,&first_parabolic_fit_succeeded,&val,&jump_len,
		    vect,
		    direction,last_val,last_g,last_jump_len,len,pfunction);
      if(*pcode != WN_SUCCESS)
      {
        wn_gppop();
        wn_gpfree();
        *pval_min = val;
        return;
      }

      if(
	  wn_force_optimize_stop_flag
	    ||
	  ((max_iterations < WN_IHUGE)&&(iteration >= max_iterations))
        )
      {
        wn_force_optimize_stop_flag = FALSE;
        wn_gppop();
        wn_gpfree();
        *pval_min = val;
        *pcode = WN_SUBOPTIMAL;
        return;
      }
      wn_assert(val <= last_val);
      if(val == last_val)
      {
        if(no_move_count >= 2)
        {
          wn_gppop();
          wn_gpfree();
          *pval_min = val;
          *pcode = WN_SUCCESS;
          return;
        }
        else
        {
	  ++no_move_count;
	  jump_len = last_jump_len;
        }
      }
      else
      {
        no_move_count = 0;
      }

      (*pgradient)(g,vect);
      g_dot_last_g = wn_dot_vects(g,last_g,len);

      if((!first_parabolic_fit_succeeded)||(last_jump_len == 0.0))
      {
        stable_satisfy_count = 0;
	goto no_function_method_test_fail;
      }

      alpha = jump_len/last_jump_len;

      if(!(((1.0-3000.0) < alpha)&&(alpha < (1.0+3000.0))))
      {
        stable_satisfy_count = 0;
	goto no_function_method_test_fail;
      }

      if(beta != 0.0)
      {
	computed_g_dot_dlast = -g_dot_last_g/beta;
	/*
	printf("computed_g_dot_dlast=%lg,last_dy1=%lg\n",
	       computed_g_dot_dlast,last_dy1);
	*/
	if(!(wn_abs(computed_g_dot_dlast) < 0.2*wn_abs(last_dy1)))
	{
          stable_satisfy_count = 0;
	  goto no_function_method_test_fail;
        }
      }

      ++stable_satisfy_count;
      if(stable_satisfy_count > 3)
      {
        function_free_method = TRUE;
      }

      no_function_method_test_fail: ;
    }

    norm2_g = wn_norm2_vect(g,len);
    /*
    g_dot_last_g = wn_dot_vects(g,last_g,len);
    */

    beta = (norm2_g - g_dot_last_g)/norm2_last_g;

    wn_add_vect_and_scaled_vect(direction,g,direction,beta,len);

    /*
    printf("norm(g) = %lg\n",wn_norm_vect(g,len));
    printf("ob = %lg,beta = %lg,numerator=%lg,denom=%lg,norm2(direction)=%lg\n",
	   val,beta,numerator,norm2_last_g,wn_norm2(direction));
    printf("iteration = %d,ob = %lg\n",iteration,val);
    */

    last_was_function_free_method = function_free_method;
  }
}
Esempio n. 13
0
void main(void)
{
  int code,i;
  double val_min,val;
  double *vect;

  wn_gpmake("no_free");

  wn_make_vect(&p,SIZE);
  wn_make_vect(&vect,SIZE);

  for(i=0;i<SIZE;++i)
  {
    val = wn_normal_distribution();
    p[i] = wn_abs(val);
    /*
    p[i] = 1.0;
    */
  }

  for(i=0;i<SIZE-1;++i)
  {
    vect[i] = 1.0/SIZE*(1.0+0.01*wn_normal_distribution());
    /*
    vect[i] = 1.0/SIZE;
    */
  }

  wn_conj_gradient_method(&code,&val_min,
		          vect,SIZE-1,(function),(gradient),
			  WN_IHUGE);
  /*
  wn_conj_direction_method(&code,&val_min,
		          vect,SIZE-1,(function),
			  WN_IHUGE);
  */

  printf("final result: code = %d   ",code);
  printf("    ob = %lf\n",val_min);
  printf("total_count=%d,count=%d.\n",total_count,count);
  /*
  wn_print_vect(p,SIZE);
  {
    int i;
    double val;

    val = 1.0;

    for(i=0;i<SIZE-1;++i)
    {
      val -= vect[i];
    }

    vect[SIZE-1] = val;
  }
  wn_print_vect(vect,SIZE);
  {
    int i;

    for(i=0;i<SIZE-1;++i)
    {
      printf("%lf ",p[i]/(vect[i]*vect[i]));
    }
  }
  */

  wn_gpfree();
}