用共轭梯度法求最优解

 

题目:

      已知 f(x) = (x1-1)2+5(x2-5)2+(x3-1)2+5(x4-5)  用快速下降法、牛顿法或共轭梯度法求 minf(x) 

 

共轭梯度法代码:

 

 

//共轭梯度法
//请根据具体题目,修改本程序“//@”所在行的下一行代码。
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
//@ 题目中方程是几元的,此处将LEN设为几
#define LEN 4
#define TYPE float

TYPE fAnswer(TYPE *x) {    //求f(x)的值,并返回
	TYPE f;
	//@ 题目中的方程写与此
	f = (x[0] - 1) * (x[0] - 1) + 5 * (x[1] - 5) * (x[1] - 5) + (x[2] - 1) * (x[2] - 1) + 5 * (x[3] - 5) * (x[3] - 5);
	return f;
}

void vectorMultiply(TYPE *x, TYPE e) {    //常数e乘x向量,赋值给x向量 【若求负梯度,可用梯度乘-1】
	int i;
	for(i = 0; i < LEN; i++) {
		x[i] = e * x[i];
	}
}

void vectorAdd(TYPE *x, TYPE *y, TYPE *z) {    //向量加法操作
	int i;
	for(i = 0; i < LEN; i++) {
		z[i] = x[i] + y[i];
	}
}

void vectorSub(TYPE *x, TYPE *y, TYPE *z) {    //向量减法操作
	int i;
	for(i = 0; i < LEN; i++) {
		z[i] = x[i] - y[i];
	}
}

void vectoreEqual(TYPE *x, TYPE *y) {    //向量赋值操作
	int i;
	for(i = 0; i < LEN; i++) {
		y[i] = x[i];
	}
}

TYPE norm2_graded(TYPE *x) {    //负梯度模的平方
	int i;
	TYPE norm2 = 0.0;

	for(i = 0; i < LEN; i++) {
		norm2 += x[i] * x[i];
	}

	return norm2;
}

//@ 对题目的xi分别求偏倒,赋值给stepLength[i]
void setStepLength(TYPE *stepLength, TYPE *x0) {
	stepLength[0] = 2 * (1 - x0[0]);
    stepLength[1] = 10 * (5 - x0[1]);
	stepLength[2] = 2 * (1 - x0[2]);
    stepLength[3] = 10 * (5 - x0[3]);
}

void DSC(TYPE *x0, TYPE *stepLength) {    //用D.S.C.法求下一个x落点
	TYPE x1[LEN];
	TYPE x2[LEN];
	TYPE x3[LEN];
	TYPE x4[LEN];
	TYPE x5[LEN];
	TYPE xa[LEN];
	TYPE xb[LEN];
	TYPE xc[LEN];
	vectorAdd(x0, stepLength, x1);
	if(fAnswer(x1) > fAnswer(x0))
		vectorMultiply(stepLength, -1);

	vectorAdd(x0, stepLength, x1);
	while(fAnswer(x1) <= fAnswer(x0)) {
		vectoreEqual(x1, x0);
		vectorAdd(stepLength, stepLength, stepLength);
		vectorAdd(x1, stepLength, x1);
	}

	vectorMultiply(stepLength, 0.5);
	vectorSub(x0, stepLength, x2);
	vectoreEqual(x1, x4);
	vectoreEqual(x0, x3);
	vectorSub(x1, stepLength, x5);

	if(fAnswer(x5) > fAnswer(x3))
		vectoreEqual(x3, xb);
	else
		vectoreEqual(x5, xb);

	vectorSub(xb, stepLength, xa);
	vectorAdd(xb, stepLength, xc);

	vectorMultiply(stepLength, (fAnswer(xa) - fAnswer(xc)) / (2 * (fAnswer(xa) - 2 * fAnswer(xb) + fAnswer(xc))));
	vectorAdd(xb, stepLength, x0);
	setStepLength(stepLength, xb);
}

void main() {    //方法主体
	TYPE x0[LEN];    //初始点
	TYPE stepLength[LEN];    //步长
	TYPE stepLength2[LEN];
	TYPE e = 0.001;    //误差
	TYPE a = 0.00;

	int i;    //用于循环计数
	int n = LEN;
	int count = 0;

    printf("请输入x的初始值:\n");
	for(i = 0; i < LEN; i++) {    //初始化x0数组
		printf("x%d = ", i+1);
		scanf("%f", &x0[i]);
	}

	setStepLength(stepLength, x0);

   	for(i = 1; norm2_graded(stepLength) > e * e; i++) {    //共轭梯度法的实现主体
		DSC(x0, stepLength);
		if(i != n) {
			if(norm2_graded(stepLength) <= e * e) break;
			setStepLength(stepLength2, x0);
			a = norm2_graded(stepLength2) / norm2_graded(stepLength);
			vectorMultiply(stepLength, a);
			vectorAdd(stepLength2, stepLength, stepLength); 
			i = i + 1;
		} else {
			i = 1;
		}
		count = count + 1;
	}

	printf("共轭梯度法循环结束,共循环%d次\n", count);

	printf("使用共轭梯度法获得的最优点为:\n");
	for(i = 0; i < LEN; i++) {
		printf("x%d = %f\n", i+1, x0[i]);
	}
	printf("minf(x) = %f\n", fAnswer(x0));

	printf("共轭梯度法程序结束!!!\n");
}

 

总结

        本解法所使用的一维搜索算法仍是改进的D.S.C.法,将步长 的初始值改为负梯度向量,D.S.C.算法中的其它一维参量也采用向量形式替换,使此D.S.C.法可适用于多维搜索。

        本解法仅通过将我发表的最速下降法中的main()函数中最速下降法的实现主体替换为共轭梯度法的实现主体而得到。

        经测试验证,此方法无误。

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值