goldensectionsearch.txt

来自「此代码为公司生产用的黄金搜索算法」· 文本 代码 · 共 76 行

TXT
76
字号
void branislav_gold(void)
{
	short	a,b,c,s;
	double 	fa,fb,fc,fs;

	a = bran_x[0];
	b = bran_x[1];
	c = bran_x[2];
	fa = bran_y[0];
	fb = bran_y[1];
	fc = bran_y[2];
	
	// it is assumed here that the case when yy(y) is % less than both yy(x) and yy(z) never happens. 
	// this is the case of an inverted triangle.

	if (fc > fb) 
	{	s = bround(c - (1-golden_section)*(c-b));
		fs = branislav_measure(s);
		bran_x[0]=b;
		bran_x[1]=s;
		bran_x[2]=c;
		bran_y[0]=fb;
		bran_y[1]=fs;
		bran_y[2]=fc;
	}
	else if (fa > fb)               
	{	s = bround(a + (1-golden_section)*(b-a));
		fs = branislav_measure(s);
		bran_x[0]=a;
		bran_x[1]=s;
		bran_x[2]=b;
		bran_y[0]=fa;
		bran_y[1]=fs;
		bran_y[2]=fb;
	}
	else if ((b-a) > (c-b))           	 
	{	s = bround(b - (1-golden_section)*(b-a));
		fs = branislav_measure(s);
	    if (fs >= fb)
		{	bran_x[0]=a;
			bran_x[1]=s;
			bran_x[2]=b;
			bran_y[0]=fa;
			bran_y[1]=fs;
			bran_y[2]=fb;
	    }
		else
		{	bran_x[0]=s;
			bran_x[1]=b;
			bran_x[2]=c;
		 	bran_y[0]=fs;
			bran_y[1]=fb;
			bran_y[2]=fc;
	    }
	 }
	else
	{	s = bround(b + (1-golden_section)*(c-b));
		fs = branislav_measure(s);
	    if (fs >= fb)
		{	bran_x[0]=b;
			bran_x[1]=s;
			bran_x[2]=c;
		 	bran_y[0]=fb;
			bran_y[1]=fs;
			bran_y[2]=fc;
	    }
	    else
		{	bran_x[0]=a;
			bran_x[1]=b;
			bran_x[2]=s;
		 	bran_y[0]=fa;
			bran_y[1]=fb;
			bran_y[2]=fs;
	    }
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?