Java 模拟退火算法

模拟退火算法(Simulated Annealing, SA)是一种用于全局优化的启发式搜索算法,它模仿了物理学中金属退火的过程。该算法在搜索空间中逐步降低“温度”,以寻找全局最优解。下面是一个用Java实现模拟退火算法的简单示例。

假设我们要解决的是一个简单的函数优化问题,例如最小化函数 f(x) = x^2

import java.util.Random;  
  
public class SimulatedAnnealing {  
  
    // 定义目标函数(要最小化的函数)  
    public static double objectiveFunction(double x) {  
        return x * x;  // 例如 f(x) = x^2  
    }  
  
    // 初始化当前解  
    public static double initializeSolution(double lowerBound, double upperBound) {  
        Random rand = new Random();  
        return lowerBound + rand.nextDouble() * (upperBound - lowerBound);  
    }  
  
    // 生成一个新的解(当前解的邻域解)  
    public static double generateNeighbor(double currentSolution, double stepSize) {  
        Random rand = new Random();  
        return currentSolution + (rand.nextDouble() - 0.5) * 2 * stepSize;  
    }  
  
    // 模拟退火算法  
    public static double simulatedAnnealing(double lowerBound, double upperBound, double initialTemp, double coolingRate, int maxIterations) {  
        double currentSolution = initializeSolution(lowerBound, upperBound);  
        double currentEnergy = objectiveFunction(currentSolution);  
        double temperature = initialTemp;  
  
        for (int i = 0; i < maxIterations; i++) {  
            // 生成一个新的解  
            double neighborSolution = generateNeighbor(currentSolution, temperature);  
            double neighborEnergy = objectiveFunction(neighborSolution);  
  
            // 接受新解的概率  
            if (neighborEnergy < currentEnergy || Math.exp((currentEnergy - neighborEnergy) / temperature) > Math.random()) {  
                currentSolution = neighborSolution;  
                currentEnergy = neighborEnergy;  
            }  
  
            // 降低温度  
            temperature *= coolingRate;  
  
            // 输出当前状态(可选)  
            System.out.println("Iteration " + i + ": Solution = " + currentSolution + ", Energy = " + currentEnergy + ", Temperature = " + temperature);  
        }  
  
        return currentSolution;  
    }  
  
    public static void main(String[] args) {  
        double lowerBound = -100.0;  
        double upperBound = 100.0;  
        double initialTemp = 100.0;  
        double coolingRate = 0.99;  
        int maxIterations = 1000;  
  
        double bestSolution = simulatedAnnealing(lowerBound, upperBound, initialTemp, coolingRate, maxIterations);  
  
        System.out.println("Best Solution Found: " + bestSolution);  
        System.out.println("Objective Function Value: " + objectiveFunction(bestSolution));  
    }  
}

代码解释

  1. 目标函数
    • objectiveFunction(double x):定义了我们要最小化的目标函数 f(x) = x^2
  2. 初始化当前解
    • initializeSolution(double lowerBound, double upperBound):在给定范围内随机生成一个初始解。
  3. 生成新的解
    • generateNeighbor(double currentSolution, double stepSize):生成当前解的邻域解,即在当前解的基础上添加一个随机扰动。
  4. 模拟退火算法
    • simulatedAnnealing(double lowerBound, double upperBound, double initialTemp, double coolingRate, int maxIterations):实现了模拟退火算法的主要逻辑。
      • 初始化当前解和当前能量。
      • 在每次迭代中,生成一个新的解,并计算其能量。
      • 根据接受新解的概率(基于当前能量和邻域解的能量以及当前温度)决定是否接受新解。
      • 降低温度。
      • 输出当前状态(可选)。
  5. 主函数
    • 在主函数中,设置了算法的参数(如边界、初始温度、冷却率和最大迭代次数),并调用 simulatedAnnealing 方法。
    • 输出找到的最佳解及其目标函数值。

这个示例展示了一个基本的模拟退火算法的实现。实际应用中,你可能需要根据具体问题调整目标函数、初始解生成方式、邻域解生成方式、温度初始值和冷却率等参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值