@[toc]
前言
本文以求解二元函数最小值为例,如果需要求解多元函数,只需要修改以下变量即可:
- varNum:变量维度数
- ub和lb:变量的上下界
- vMaxArr:每个维度的搜索速度限制
优化目标
目标:在变量区间范围最小化 Z = x^2 + y^2 - xy - 10x - 4y +60
求解结果
变量取值为:[7.9993905972136385, 5.999758300133049]
最优解为:8.000000282498
进化过程可视化
Java算法代码
import java.util.Arrays;
import java.util.Random;
/**
* @Author:WSKH
* @ClassName:DE_Solve
* @ClassType:
* @Description:
* @Date:2022/6/8/18:26
* @Email:1187560563@qq.com
* @Blog:https://blog.csdn.net/weixin_51545953?type=blog
*/
public class DE_Solve {
// 个体对象
class Individual {
// 当前个体的坐标(自变量数组)
double[] curVars;
// 当前自变量对应的目标函数值
double curObjValue;
// 适应度(解决最小化问题,所以适应度为目标函数值的倒数)
double fit;
// 全参构造
public Individual(double[] curVars, double curObjValue, double fit) {
this.curVars = curVars;
this.curObjValue = curObjValue;
this.fit = fit;
}
}
// 算法参数
// 变量个数
int varNum = 2;
// 最大迭代次数
int maxGen = 500;
// 个体群中个体的个数
int individualNum = 200;
// 常数因子(用来控制差分变化的放大率)
double F = 0.4;
// 贪婪系数(用来控制向当前最优解变异的程度,设置太大会容易陷入局部最优,设置太小,会导致收敛较慢)
double greedyRate = 0.25;
// 局部变异次数
int variationCnt = 10;
// 局部交叉概率
double crossRate = 0.5;
// 步长数组(各个维度的步长)
double[] stepArr = new double[]{1.2, 1.2};
// 变量的上下界
double[] ub = new double[]{1000, 1000};
double[] lb = new double[]{-1000, -1000};
// 随机数对象
Random random = new Random();
// 个体群
Individual[] individuals;
// 最佳的个体
Individual bestIndividual;
// 记录迭代过程
public double[][][] positionArr;
// 当前记录的行数
int r;
// 求解主函数
public void solve() {
// 初始化个体群
initIndividuals();
// 开始迭代
for (int i = 0; i < maxGen; i++) {
variation();
report();
cross();
report();
}
// 输出最好的结果
System.out.println("变量取值为:" + Arrays.toString(bestIndividual.curVars));
System.out.println("最优解为:" + bestIndividual.curObjValue);
}
// 变异
void variation() {
for (int r1 = 0; r1 < individuals.length; r1++) {
for (int j = 0; j < variationCnt; j++) {
// 随机选择两个其他个体
int r2 = random.nextInt(individualNum);
int r3 = random.nextInt(individualNum);
while (r1 == r2 || r1 == r3 || r2 == r3) {
r2 = random.nextInt(individualNum);
r3 = random.nextInt(individualNum);
}
for (int m = 0; m < varNum; m++) {
double move = F * (individuals[r2].curVars[m] - individuals[r3].curVars[m]) + greedyRate * (bestIndividual.curVars[m] - individuals[r1].curVars[m]);
moveIndividual(individuals[r1], m, move);
}
updateIndividual(individuals[r1]);
if (bestIndividual.fit < individuals[r1].fit) {
bestIndividual = copyIndividual(individuals[r1]);
}
}
}
}
// 交叉
void cross() {
for (int r1 = 0; r1 < individualNum; r1++) {
if(random.nextDouble() <= crossRate){
int r2 = random.nextInt(individualNum);
while (r1 == r2){
r2 = random.nextInt(individualNum);
}
int m1 = random.nextInt(varNum);
int m2 = random.nextInt(varNum);
double temp = individuals[r1].curVars[m1];
individuals[r1].curVars[m1] = individuals[r2].curVars[m2];
individuals[r2].curVars[m2] = temp;
updateIndividual(individuals[r1]);
updateIndividual(individuals[r2]);
if(individuals[r1].fit > bestIndividual.fit){
bestIndividual = copyIndividual(individuals[r1]);
}
if(individuals[r2].fit > bestIndividual.fit){
bestIndividual = copyIndividual(individuals[r2]);
}
}
}
}
// 记录
void report() {
for (int i = 0; i < individuals.length; i++) {
for (int j = 0; j < varNum; j++) {
positionArr[r][i][j] = individuals[i].curVars[j];
}
}
r++;
}
// 求两个个体之间的距离
double getDistance(Individual f1, Individual f2) {
double dis = 0d;
for (int i = 0; i < varNum; i++) {
dis += Math.pow(f1.curVars[i] - f2.curVars[i], 2);
}
return Math.sqrt(dis);
}
// 初始化个体群
private void initIndividuals() {
positionArr = new double[2*maxGen][individualNum][varNum];
individuals = new Individual[individualNum];
for (int i = 0; i < individualNum; i++) {
individuals[i] = getRandomIndividual();
if (i == 0 || bestIndividual.fit < individuals[i].fit) {
bestIndividual = copyIndividual(individuals[i]);
}
}
}
// 控制个体在第m个维度上移动n个距离
public void moveIndividual(Individual individual, int m, double n) {
// 移动
individual.curVars[m] += n;
// 超出定义域的判断
if (individual.curVars[m] < lb[m]) {
individual.curVars[m] = lb[m];
}
if (individual.curVars[m] > ub[m]) {
individual.curVars[m] = ub[m];
}
}
// 更新个体信息
void updateIndividual(Individual individual) {
double objValue = getObjValue(individual.curVars);
individual.curObjValue = objValue;
individual.fit = 1 / objValue;
}
// 获取一个随机生成的个体
Individual getRandomIndividual() {
double[] vars = new double[varNum];
for (int j = 0; j < vars.length; j++) {
vars[j] = lb[j] + random.nextDouble() * (ub[j] - lb[j]);
}
double objValue = getObjValue(vars);
return new Individual(vars.clone(), objValue, 1 / objValue);
}
/**
* @param vars 自变量数组
* @return 返回目标函数值
*/
public double getObjValue(double[] vars) {
//目标:在变量区间范围最小化 Z = x^2 + y^2 - xy - 10x - 4y +60
return Math.pow(vars[0], 2) + Math.pow(vars[1], 2) - vars[0] * vars[1] - 10 * vars[0] - 4 * vars[1] + 60;
}
// 复制个体
Individual copyIndividual(Individual old) {
return new Individual(old.curVars.clone(), old.curObjValue, old.fit);
}
}
可视化代码
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* @Author:WSKH
* @ClassName:PlotUtil
* @ClassType:
* @Description:
* @Date:2022/6/6/18:31
* @Email:1187560563@qq.com
* @Blog:https://blog.csdn.net/weixin_51545953?type=blog
*/
public class PlotUtil extends Application {
//当前的时间轴
private Timeline nowTimeline;
//绘图位置坐标
private double[][][] positionArr;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// 调用算法获取绘图数据
DE_Solve solver = new DE_Solve();
solver.solve();
positionArr = solver.positionArr;
// 画图
try {
BorderPane root = new BorderPane();
root.setStyle("-fx-padding: 20;");
Scene scene = new Scene(root, 1600, 900);
double canvasWid = 800;
double canvasHei = 800;
//根据画布大小缩放坐标值
this.fixPosition(canvasWid - 100, canvasHei - 100);
//画布和画笔
HBox canvasHbox = new HBox();
Canvas canvas = new Canvas();
canvas.setWidth(canvasWid);
canvas.setHeight(canvasHei);
canvasHbox.setPrefWidth(canvasWid);
canvasHbox.getChildren().add(canvas);
canvasHbox.setAlignment(Pos.CENTER);
canvasHbox.setStyle("-fx-spacing: 20;" +
"-fx-background-color: #87e775;");
root.setTop(canvasHbox);
GraphicsContext paintBrush = canvas.getGraphicsContext2D();
//启动
HBox hBox2 = new HBox();
Button beginButton = new Button("播放迭代过程");
hBox2.getChildren().add(beginButton);
root.setBottom(hBox2);
hBox2.setAlignment(Pos.CENTER);
//启动仿真以及暂停仿真
beginButton.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
nowTimeline.play();
});
//创建扫描线连接动画
nowTimeline = new Timeline();
createAnimation(paintBrush);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修正cityPositionArr的坐标,让画出来的点在画布内
*
* @param width
* @param height
*/
private void fixPosition(double width, double height) {
double minX = Double.MAX_VALUE;
double maxX = -Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
double maxY = -Double.MAX_VALUE;
for (int i = 0; i < this.positionArr.length; i++) {
for (int j = 0; j < this.positionArr[0].length; j++) {
minX = Math.min(minX, this.positionArr[i][j][0]);
maxX = Math.max(maxX, this.positionArr[i][j][0]);
minY = Math.min(minY, this.positionArr[i][j][1]);
maxY = Math.max(maxY, this.positionArr[i][j][1]);
}
}
double multiple = Math.max((maxX - minX) / width, (maxY - minY) / height);
//转化为正数数
for (int i = 0; i < this.positionArr.length; i++) {
for (int j = 0; j < this.positionArr[0].length; j++) {
if (minX < 0) {
this.positionArr[i][j][0] = this.positionArr[i][j][0] - minX;
}
if (minY < 0) {
this.positionArr[i][j][1] = this.positionArr[i][j][1] - minY;
}
}
}
for (int i = 0; i < this.positionArr.length; i++) {
for (int j = 0; j < this.positionArr[0].length; j++) {
this.positionArr[i][j][0] = this.positionArr[i][j][0] / multiple;
this.positionArr[i][j][1] = this.positionArr[i][j][1] / multiple;
}
}
}
/**
* 用画笔在画布上画出所有的孔
* 画第i代的所有粒子
*/
private void drawAllCircle(GraphicsContext paintBrush, int i) {
paintBrush.clearRect(0, 0, 2000, 2000);
paintBrush.setFill(Color.RED);
for (int j = 0; j < this.positionArr[i].length; j++) {
drawCircle(paintBrush, i, j);
}
}
/**
* 用画笔在画布上画出一个孔
* 画第i代的第j个粒子
*/
private void drawCircle(GraphicsContext paintBrush, int i, int j) {
double x = this.positionArr[i][j][0];
double y = this.positionArr[i][j][1];
double radius = 2;
// 圆的直径
double diameter = radius * 2;
paintBrush.fillOval(x, y, diameter, diameter);
}
/**
* 创建动画
*/
private void createAnimation(GraphicsContext paintBrush) {
for (int i = 0; i < this.positionArr[0].length; i++) {
int finalI = i;
KeyFrame keyFrame = new KeyFrame(Duration.seconds(i * 0.05), event -> drawAllCircle(paintBrush, finalI));
nowTimeline.getKeyFrames().add(keyFrame);
}
}
}