参考书目:机械最优设计技术,孟兆明 常德功
人工智能的学习里头,我们经常会去找极值,很多都是和步长相关的,我们慢慢接近他,前面bpnet中步长全部使用0.5,为什么?先熟悉吧!熟悉了,就知道了,先看介绍:
这一节主题:极值存在区间搜索,里边有了步长最基础的印象
c#代码实现:
// min f(X)
//f(x)=x^2-10*x+8;
我们尝试找上面函数存在极值的区间
第一,声明全局变量
PointF glob = new PointF();//ji`lu`jie'guo*
float x0 = -10;//chu-shi*zhi'
float step = 1;//bu`chang'
第二,搞定
private void 找极值区间(object sender, EventArgs e)
{
float a = x0;
float b = x0 + step;//默认b大于a
float FX0 = func(x0);
float FB = func(b);
if (FX0 == FB) { a = x0; b = x0 + step; return; }
if (FX0 < FB)
{
//1,x0,b互换
huhuanhanshu(ref x0, ref b);
//2,fx0,fb互换
huhuanhanshu(ref FX0, ref FB);
//3,h=-h;
step = -step;
}
//只有这一条路向下走
迭代goto(ref FX0, ref FB, ref b);
a = glob.X;//fan*hui'qu*jian-jie'guo*(a,b)
b = glob.Y;
}
float func(float X)
{
return X*X - 10 * X + 8;
}
void huhuanhanshu(ref float a,ref float b)
{
float temp = 0;
temp = a;
a = b;
b = temp;
}
void 迭代goto(ref float fx0, ref float fb, ref float 位置b)
{//方向正确
//x0 = b;
x0 = 位置b;
fx0 = fb; step = 2 * step;
//b = x0 + step;
位置b = x0 + step;
//fb = func(b);
fb = func(位置b);
if (fx0 <= fb)//终止条件
{
if (fx0 == fb)
glob = new PointF(x0, 位置b);
else
glob = new PointF(x0 - step / 2f, 位置b);
}
else
{
迭代goto(ref fx0, ref fb, ref 位置b);
}
}
看到原书有basic代码,goto语句跳来跳去的,c#实现后,发现这个迭代goto函数很像goto语句。
里边的步长很容易理解。