模拟退火算法解多元函数

2024-08-28 22:32

本文主要是介绍模拟退火算法解多元函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

模拟退火算法解多元函数

题目:
F ( x ) = 11.16386 − 0.0903 x 1 − 0.1487 x 2 − 0.0664 x 3 + 0.09074 x 4 − 2.452 ∗ 1 0 − 4 x 1 x 2 + 6.228 ∗ 1 0 − 5 x 1 x 3 + 2.457 ∗ 1 0 − 3 x 1 x 4 + 3.8688 ∗ 1 0 − 3 x 2 x 3 − 6.471 ∗ 1 0 − 3 x 2 x 4 − 1.451 ∗ 1 0 − 3 x 3 x 4 F(x)=11.16386-0.0903x_1-0.1487x_2-0.0664x_3+0.09074x_4-2.452*10^{-4}x_1x_2+6.228*10^{-5}x_1x_3+2.457*10^{-3}x_1x_4+3.8688*10^{-3}x_2x_3-6.471*10^{-3}x_2x_4-1.451*10^{-3}x_3x_4 F(x)=11.163860.0903x10.1487x20.0664x3+0.09074x42.452104x1x2+6.228105x1x3+2.457103x1x4+3.8688103x2x36.471103x2x41.451103x3x4

约束条件:

36.163 < x 1 < 65.0934 36.163<x_1<65.0934 36.163<x1<65.0934

0.45 < 1 − 3 x 2 2 x 1 < 0.5 0.45<1-\frac{3x_2}{2x_1}<0.5 0.45<12x13x2<0.5

12.0543 < x 2 < 21.699 12.0543<x_2<21.699 12.0543<x2<21.699

27.75 < x 3 < 36.075 27.75<x_3<36.075 27.75<x3<36.075

10 < x 4 − 2 x 3 10<x_4-2x_3 10<x42x3

x 4 − 3.2 x 3 < 16 x_4-3.2x_3<16 x43.2x3<16

48.02 < x 4 48.02<x_4 48.02<x4

求多目标函数 F ( x ) F(x) F(x)的最小值?

资源链接

https://download.csdn.net/download/u013095333/12585474

解法1:暴力解法

思路,设置 x 1 , x 2 , x 3 , x 4 x_1,x_2,x_3,x_4 x1,x2,x3,x4的范围和精度,依次计算每一个 F ( x ) F(x) F(x)的值,取最小的 F ( x ) F(x) F(x)对应的 x 1 , x 2 , x 3 , x 4 x_1,x_2,x_3,x_4 x1,x2,x3,x4

#include <bits/stdc++.h>
using namespace std;double f(double x1, double x2, double x3, double x4){double ans = 0;ans = 11.16386 - 0.0903*x1 - 0.1487*x2 - 0.0664*x3 + 0.0907*x4;ans = ans - 2.452*0.0001*x1*x2;ans = ans + 6.228*0.00001*x1*x3;ans = ans + 2.457*0.001*x1*x4;ans = ans + 3.8688*0.001*x2*x3;ans = ans - 6.471*0.001*x2*x4;ans = ans - 1.451*0.001*x3*x4;return ans;
}int main()
{double x1, x2, x3, x4;double step = 1;x1 = 36.16;x2 = 12.05;x3 = 27.75;x4 = 48.02;double ftemp = 10000, x1temp = x1, x2temp = x2, x3temp = x3, x4temp = x4;while(x1 < 65.10){x2 = 12.05;while(x2 < 21.7){x3 = 27.75;while(x3 < 36.10){x4 = 48.02;while(x4 < 1000){double fx = 10000;double a = 1 - 3*x2/1.0/(2*x1);double b = x4 - 2*x3;double c = x4 - 3.2*x3;if(a>0.45&&a<0.5&&b>10&&c<16){fx = f(x1, x2, x3, x4);}if(ftemp > fx){ftemp = fx;x1temp = x1;x2temp = x2;x3temp = x3;x4temp = x4;}x4 = x4 + step;}x3 = x3 + step;cout << ftemp << " " << x1 << " " << x2 << " " << x3 << endl;}x2 = x2 + step;}x1 = x1 + step;}cout << ftemp << endl;cout << x1temp << " " << x2temp << " " << x3temp << " " << x4temp << endl;return 0;
} 

结果:
在这里插入图片描述

解法2:模拟退火

参考:
模拟退火算法
用模拟退火算法求解带约束的二元函数极值问题(Java实现)

#include <bits/stdc++.h>
using namespace std;double f(double x1, double x2, double x3, double x4){double ans = 0;ans = 11.16386 - 0.0903*x1 - 0.1487*x2 - 0.0664*x3 + 0.0907*x4;ans = ans - 2.452*0.0001*x1*x2;ans = ans + 6.228*0.00001*x1*x3;ans = ans + 2.457*0.001*x1*x4;ans = ans + 3.8688*0.001*x2*x3;ans = ans - 6.471*0.001*x2*x4;ans = ans - 1.451*0.001*x3*x4;return ans;
}double getRandom(){                       // 获取-1到1区间的随机数 return (rand()%200 - 100)/100.0;
}double getRangeRandom(double a, double b){  // 获取a到b范围内的随机数 int step = (a-b)*1000;return rand()%step/1000.0+a; 
}int main()
{srand((int)time(0));double T = 100, t = 100;double T_min = 1e-8;double step = 0.99;int k = 10;double x1[k], x2[k], x3[k], x4[k];double x1_min = 36.16;double x1_max = 65.10;double x2_min = 12.05;double x2_max = 21.7;double x3_min = 27.75;double x3_max = 36.10;double x4_min = 48.02;double x4_max = 100;double ftemp = 10000, ftemp_new, x1temp, x2temp, x3temp, x4temp;// 随机化初始值for(int i = 0; i < k; i++){x1[i] = getRangeRandom(x1_min, x1_max);x2[i] = getRangeRandom(x2_min, x2_max);x3[i] = getRangeRandom(x3_min, x3_max);x4[i] = getRangeRandom(x4_min, x4_max);double a = 1 - 3*x2[i]/1.0/(2*x1[i]);double b = x4[i] - 2*x3[i];double c = x4[i] - 3.2*x3[i];if(!(a>0.45&&a<0.5&&b>10&&c<16)){i--;}} // 模拟退火 int time = 0;while(t > T_min){ for(int i = 0; i < k; i++){ftemp = f(x1[i], x2[i], x3[i], x4[i]);// 在领域内产生新的解 double x1_new = x1[i] + getRandom();double x2_new = x2[i] + getRandom();double x3_new = x3[i] + getRandom();double x4_new = x4[i] + getRandom();double a = 1 - 3*x2_new/1.0/(2*x1_new);double b = x4_new - 2*x3_new;double c = x4_new - 3.2*x3_new;if(x1_new>x1_min&&x1_new<x1_max&&\x2_new>x2_min&&x2_new<x2_max&&\x3_new>x3_min&&x3_new<x3_max&&\x4_new>x4_min&&x4_new<x4_max&&\a>0.45&&a<0.5&&b>10&&c<16){ftemp_new = f(x1_new, x2_new, x3_new, x4_new);if(ftemp_new < ftemp){   // 有优化,直接替换x1[i] = x1_new;x2[i] = x2_new;x3[i] = x3_new;x4[i] = x4_new;}else{                   // 无优化,以一定概率接受较差的结果 if((t - T_min) > (rand()%100)){x1[i] = x1_new;x2[i] = x2_new;x3[i] = x3_new;x4[i] = x4_new;}} }}t = t * step;// 输出每一轮迭代得到的最小值ftemp = 10000;for(int i = 0; i < k; i++){ftemp_new = f(x1[i], x2[i], x3[i], x4[i]);if(ftemp_new < ftemp){ftemp = ftemp_new;x1temp = x1[i];x2temp = x2[i];x3temp = x3[i];x4temp = x4[i];}} if(time%100==0){cout << time << endl;cout << ftemp << endl;cout << x1temp << " " << x2temp << " " << x3temp << " " << x4temp << endl << endl; }time++;}// 取k个中最小的ftemp = 10000;for(int i = 0; i < k; i++){ftemp_new = f(x1[i], x2[i], x3[i], x4[i]);if(ftemp_new < ftemp){ftemp = ftemp_new;x1temp = x1[i];x2temp = x2[i];x3temp = x3[i];x4temp = x4[i];}} cout << "ans: " << endl;cout << ftemp << endl;cout << x1temp << " " << x2temp << " " << x3temp << " " << x4temp << endl;return 0;
} 

结果:
在这里插入图片描述
与暴力解法得到的基本一致,说明该附近确为最小值对应的解

特点:速度比暴力算法快太多了,确实有用。

迭代过程图:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

绘图代码:

import os
import numpy as np
import matplotlib.pyplot as plti = 0
time = []
fx = []
x1 = []
x2 = []
x3 = []
x4 = []
with open('out.txt', 'r') as file:context = file.read()context = context.split()for i in range(len(context)):if(i%6==0):time.append(int(context[i]))if(i%6==1):fx.append(float(context[i]))if(i%6==2):x1.append(float(context[i]))if(i%6==3):x2.append(float(context[i]))if(i%6==4):x3.append(float(context[i]))if(i%6==5):x4.append(float(context[i]))i = i + 1# 数据清洗干净,下面绘图
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文标签
plt.rcParams['axes.unicode_minus']=Falseplt.plot(time, fx, marker = 'o', c = 'r', label = 'a=0.3')
plt.xlabel('迭代次数', fontsize = 18)
plt.ylabel('目标函数F(x)', fontsize = 18)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.savefig("fx.svg",bbox_inches='tight')

这篇关于模拟退火算法解多元函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1116069

相关文章

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.