Example #1
0
/*
 * 辞書順
 */
void generate_perm2(){
	printf("\n");
	printf("\n");
	int i;
	int p[5];
	for(i = 1;i <= 4; i++){
		p[i] = i;
	}
	perm2(1, p);
}
Example #2
0
void perm2(int i, int p[]){
	int t, j, k;
	if(i < 4){
		for(j = i; j <= 4; j++){
			//p[i] から p[j]の右ローテイト
			t = p[j];
			for(k = j; k > i; k--) p[k] = p[k - 1];
			p[i] = t;
			perm2(i + 1, p);
			//戻す
			for(k = i; k < j; k++) p[k] = p[k + 1];
			p[j] = t;
		}
	}else{
		for(j = 1; j <= 4; j++) printf("%d ", p[j]);
		printf("\n");
	}
}
// Brackets a minimum of function f :
static void minenc(double *ax, double *bx, double *cx, double f(double))
{
  // Given initial bracketing, magnifies the interval so that actual bracketing is achieved
  const double PHI=1.618034; // Default maginifying constant
  const double RLIMIT=200.0; // Limit of parabolic interpolation
  const double EPS=1.0e-20; // Precision
  double ulim, u, r, q, fa, fb, fc, fu;

  // Searches minimum in downhill direction defined by ax and bx.
  // Stops when starting going back uphill.
  fa=f(*ax);
  fb=f(*bx);
  if(fb>fa){
    perm2(ax,bx);
    perm2(&fb,&fa); // Downhill direction defined to be from a to b.
  }
  *cx=*bx+PHI*(*bx - *ax); // Magnifying interval : going further downhill
  fc=f(*cx);

  while(fb > fc){ // Third point not high enough : still going downhill
    // Tries parabolic interpolation
    r=(*bx-*ax)*(fb-fc); 
    q=(*bx-*cx)*(fb-fa);
    // Optimum of the interpolated parabol located at u :
    u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/(2*sgne(MAX(fabs(q-r),EPS),q-r));
    // Limit parabolic interpolation
    ulim=*bx+RLIMIT*(*cx-*bx);

    if((*bx-u)*(u-*cx) > 0){ // u is between bx and cx
      fu=f(u);
      if(fu < fc){ // Minimum between bx and cx
        *ax=*bx;
        *bx=u; // Bracketing triplet is (bx,u,cx)
        return;
      }
      else if(fu > fb){ // Minimum between ax and u
        *cx=u; // Bracketing triplet is (ax,bx,u)
        return;
      }
      u=*cx+PHI*(*cx-*bx); // Parabolic interpolation was useless
      fu=f(u);
    }

    else if((*cx-u)*(u-ulim) > 0){ // u is between cx and ulimit
      fu=f(u);
      if(fu < fc){ 
        chang3(bx,cx,&u,u+PHI*(u-*cx)); // Further downhill AND default magnification
        chang3(&fb,&fc,&fu,f(u));
      }
    }

    else if((u-ulim)*(ulim-*cx) >= 0){ // Limits u to its maximum value
      u=ulim;
      fu=f(u);
    }

    else {
      u=*cx+PHI*(*cx-*bx); // Default magnification
      fu=f(u);
    }

    chang3(ax,bx,cx,u); // Continues further on downhill
    chang3(&fa,&fb,&fc,fu);
  }
}