This blog is about Matlab智能算法、规划问题、灰度预测
You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
Welcome to see my homepage and contact me
: NicholasYe’s Homepage.
1.退火算法
%% help simulannealbnd
clc, clear all, close all
%% 用遗传算法求解最小值点和最小值
% 定义匿名函数
fun = @(x)exp(-0.1*x).*sin(x).^2-0.5*(x+0.1).*sin(x);
% 决策变量下限
lb = -10;
% 决策变量上限
ub = 10;
% 初始值
x0 = 0;
% 全局最优解
[x, fval] = simulannealbnd(fun, x0, lb, ub)
2.遗传算法
%% help ga
clc, clear all, close all
%% 用遗传算法求解最小值点和最小值
% 定义匿名函数
fun = @(x)exp(-0.1*x).*sin(x).^2-0.5*(x+0.1).*sin(x);
% 决策变量下限
lb = -10;
% 决策变量上限
ub = 10;
% 初始值
x0 = 0;
% 全局最优解
[x1, fval1] = ga(fun, 1, [], [], [], [], lb, ub);
3.粒子群算法
%% help particleswarm
% particleswarm attempts to solve problems of the form:
% min F(X) subject to LB <= X <= UB
clc, clear all, close all
%% 用粒子群算法求解非线性优化问题
fun = @(x)exp(-0.1*x).*sin(x).^2-0.5*(x+0.1).*sin(x);
nvars = 1; % 决策变量个数
lb = -10;
ub = 10;
[x, fval] = particleswarm(fun, nvars, lb, ub)
4.线性规划
% help linprog
clc, clear all, close all
%% 求优化问题
% min z =-5*x1-4*x2-6*x3
% x1-x2+x3<=20
% 3*x1+2*x2+4*x3<=42
% 3*x1+2*x2<=30
% 0<=x1, 0<=x2, 0<=x3
% x1+x2+x3=18
% 线性规划系数
f = [-5, -4, -6];
% 条件数组 a*x<=b
a = [1, -1, 1; 3, 2, 4; 3, 2, 0];
b = [20; 42; 30];
% 条件数组 aeq*x=beq
aeq = [1, 1, 1];
beq = 18;
% x1,x2,x3下限
lb = [0; 0; 0];
% 注意,如果是求最大值,f改为-f
[x, fval, exitflag, output] = linprog(f, a, b, aeq, beq, lb, []);
5.非线性规划
% help fmincon
clc; clear all; close all;
%% 求优化问题
% min z = -(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)))
x0 = [1; 1; 1; 1];
lb = [0; 0; 0; 0];
ub = [];
A = [];
b = [];
Aeq = [];
beq = [];
[x, fval, exitflag, output] = fmincon(@fun44, x0, A, b, Aeq, beq, lb, ub, @mycon1)
%% 定义目标函数
function f = fun44(x)
f = -(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)));
end
%% 定义自变量范围
function [g, ceq] = mycon1(x)
g(1) = x(1) - 400;
g(2) = 1.1*x(1) + x(2) - 440;
g(3) = 1.21*x(1) + 1.1*x(2) + x(3) - 484;
g(4) = 1.331*x(1) + 1.21*x(2) + 1.1*x(3) + x(4) - 532.4;
ceq = 0;
end
6.整数规划
% help intlinprog
clc, clear all, close all
%% 0-1 规划是指自变量Xj仅取值0或者1
% max z = 3*x1-2*x2+5*x3
% x1+2*x2-x3<=2
% x1+4*x2+x3<=4
% x1+x2<=3
% 4*x2+x3<=6
% x1,x2,x3=0,1
% 设定x1~x3约束为整数
intcon = [1, 3];
% 线性规划系数
f = [-3, 2, -5];
% 条件数组 a*x<=b
a = [1, 2, -1; 1, 4, 1; 1, 1, 0; 0, 4, 1];
b = [2; 4; 3; 6];
% 自变量x的上下边界
lb = [0; 0; 0];
ub = [1; 1; 1];
% 条件数组 aeq*x=beq
aeq = [0, 0, 0];
beq = 0;
[x, fval, exitflag, output] = intlinprog(f, intcon, a, b, aeq, beq, lb, ub)
7.最大最小规划
%% help fminimax
clc; clear all; close all;
%% 以匿名函数形式编写目标函数
minimaxMyfun = @(x)sqrt([(x(1)-1.5)^2 + (x(2)-6.8)^2;
(x(1)-6)^2 + (x(2)-7.0)^2;
(x(1)-8.9)^2 + (x(2)-6.9)^2;
(x(1)-3.5)^2 + (x(2)-4)^2;
(x(1)-7.4)^2 + (x(2)-3.1)^2]);
%% 定义自变量范围
x0 = [0.0 ; 0.0];
A = [];
b = [];
Aeq = [1, -1];
beq = 2.5;
%% 函数求解
[x, fval] = fminimax(minimaxMyfun, x0, A, b, Aeq, beq)
8.灰度预测
%% 填充原有数据
clc, clear all, close all
syms a b;
c=[a b]';
A=[89677,99215,109655,120333,135823,159878,182321,209407,246619,300670,350032];
B=cumsum(A); % 原始数据累加
n=length(A); % 原始数据量测量
for i=1:(n-1)
C(i)=(B(i)+B(i+1))/2; % 生成累加矩阵
end
%% 计算待定参数的值
D=A;D(1)=[];
D=D';
E=[-C;ones(1,n-1)];
c=inv(E*E')*E*D;
c=c';
a=c(1);b=c(2);
%% 预测后续数据
F=[];F(1)=A(1);
for i=2:(n+10)
F(i)=(A(1)-b/a)/exp(a*(i-1))+b/a ;
end
G=[];G(1)=A(1);
for i=2:(n+10)
G(i)=F(i)-F(i-1); %得到预测出来的数据
end
Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!