实现简单的差分进化算法(DE),用MATLAB和java实现

avatar 2024年05月28日22:55:58 0 594 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

一、MATLAB实现

clc; clear;

% 定义常量
max_generations = 1000;
population_size = 100;
dimensions = 2;
CR = 0.8;
F = 0.5;
bounds = [-10, 10];

% 目标函数
target_function = @(x) x(1)^2 + (x(2) - 2)^2;

% 初始化种群
populations = rand(population_size, dimensions) * (bounds(2) - bounds(1)) + bounds(1);
fitness = arrayfun(@(i) target_function(populations(i, :)), 1:population_size);

% 进化过程
for gen = 1:max_generations

    for i = 1:population_size
        % 变异操作
        candidates = randperm(population_size, 3);
        a = populations(candidates(1), :);
        b = populations(candidates(2), :);
        c = populations(candidates(3), :);
        mutation = a + F * (b - c);
        
        % 交叉操作
        cross_points = randi(dimensions);
        cross_mask = rand(1, dimensions) < CR;
        while sum(cross_mask) == 0
            cross_mask(cross_points) = true;
        end
        trial = populations(i, :);
        trial(cross_mask) = mutation(cross_mask);
        trial = min(max(trial, bounds(1)), bounds(2));
        
        % 选择操作
        trial_fitness = target_function(trial);
        if trial_fitness < fitness(i)
            fitness(i) = trial_fitness;
            populations(i, :) = trial;
        end
    end

    % 获取当前最优解
    [best_fitness, best_index] = min(fitness);
    best_solution = populations(best_index, :);
    fprintf('%d次,fitness:%d, x1:%d, x2:%d\n', gen, best_fitness, best_solution(1), best_solution(2));
end

 

二、Java实现

package com.liuyanzhao.de;

import java.util.Random;

/**
 * 差分进化算法示例
 * 目标函数:f(x1, x2) = x1^2 + (x2 - 2)^2
 * 作者:言曌
 * 日期:2024/5/28 10:25
 */
public class MyDE2 {

    // 目标函数
    public static double targetFunction(double[] x) {
        return Math.pow(x[0], 2) + Math.pow((x[1] - 2), 2);
    }

    // 生成count个0到n-1的随机整数
    public static int[] getRandomNum(int n, int count) {
        Random random = new Random();
        int[] result = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = random.nextInt(n);
        }
        return result;
    }

    // 获取最小值索引
    public static int getMinIndex(double[] fitness) {
        int minIndex = 0;
        for (int i = 1; i < fitness.length; i++) {
            if (fitness[i] < fitness[minIndex]) {
                minIndex = i;
            }
        }
        return minIndex;
    }

    public static void main(String[] args) {

        // 常量定义
        final int populationSize = 100;
        final int dim = 2;
        final int maxGenerations = 1000;
        final double CR = 0.8; // 交叉率
        final double F = 0.5; // 缩放因子
        final double[] bounds = {-10, 10};
        Random random = new Random();

        // 初始化种群
        double[][] population = new double[populationSize][dim];
        double[] fitness = new double[populationSize];
        for (int i = 0; i < populationSize; i++) {
            for (int j = 0; j < dim; j++) {
                population[i][j] = random.nextDouble() * (bounds[1] - bounds[0]) + bounds[0];
            }
            fitness[i] = targetFunction(population[i]);
        }

        // 进化过程
        for (int generation = 0; generation < maxGenerations; generation++) {
            for (int i = 0; i < populationSize; i++) {
                // 变异操作:vi = F * (a - b) + c
                int[] candidates = getRandomNum(populationSize, 3);
                double[] a = population[candidates[0]];
                double[] b = population[candidates[1]];
                double[] c = population[candidates[2]];
                double[] mutation = new double[dim];
                for (int j = 0; j < dim; j++) {
                    mutation[j] = F * (a[j] - b[j]) + c[j];
                }

                // 交叉操作
                double[] trial = new double[dim];
                boolean crossFlag = false;
                for (int j = 0; j < dim; j++) {
                    if (random.nextDouble() <= CR) {
                        crossFlag = true;
                        trial[j] = mutation[j];
                    } else {
                        trial[j] = population[i][j];
                    }
                }
                if (!crossFlag) {
                    int randomDimension = random.nextInt(dim);
                    trial[randomDimension] = mutation[randomDimension];
                }
                // 将解限定在边界内
                for (int j = 0; j < dim; j++) {
                    trial[j] = Math.min(Math.max(trial[j], bounds[0]), bounds[1]);
                }
                

                // 选择操作
                double trialFitness = targetFunction(trial);
                if (trialFitness < fitness[i]) {
                    fitness[i] = trialFitness;
                    for (int j = 0; j < dim; j++) {
                        population[i][j] = trial[j];
                    }
                }
            }

            // 记录当前最优解
            int minIndex = getMinIndex(fitness);
            double[] bestSolution = population[minIndex];
            System.out.printf("第%d代,最优适应度: %s, x1: %s, x2: %s\n", generation+1, fitness[minIndex], bestSolution[0], bestSolution[1]);
        }
    }
}

 

  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0