常用的启发式算法

启发式算法是一类基于直观或经验规则设计的策略,用于求解复杂问题,特别是那些传统方法(如精确算法)在计算时间和空间上难以处理的问题。它们通常不保证找到全局最优解,但能在合理的计算资源内提供近似最优解。以下是几种常用的启发式算法及其简要介绍:

1、模拟退火算法 (Simulated Annealing, SA):

        模拟退火算法模拟了金属冷却过程中的退火现象,通过允许算法接受可能劣质的解以跳出局部最优,从而增加找到全局最优解的概率。算法通过控制“温度”参数来决定接受劣质解的概率,随着迭代过程逐渐降低温度,算法趋于收敛到稳定状态。

应用示例

  • 物流配送路线优化
  • 电路布线问题

        这里给出一个简化的Java代码框架示例,展示模拟退火算法的基本结构及其在解决物流配送路线优化问题中的应用。我们假设已经实现了计算配送路径成本(即目标函数值)的calculateRouteCost方法,以及用于生成邻域解(即路径微调)的generateNeighborSolution方法。简单示例,我们将具体的降温策略、初始温度设置、温度下降比例等参数已设定为常数,实际应用中可能需要根据具体问题进行调整。

import java.util.Random;

public class SimulatedAnnealing {
    private static final double INITIAL_TEMPERATURE = 1000.0; // 初始温度
    private static final double TEMPERATURE_DECREASE_RATE = 0.95; // 温度下降比例
    private static final double ACCEPTANCE_PROBABILITY_THRESHOLD = 0.05; // 接受概率阈值

    // 假设已有以下实现:
    // double calculateRouteCost(Route route); // 计算给定配送路径的成本
    // Route generateNeighborSolution(Route currentRoute); // 生成当前路径的一个邻域解(微调后的路径)

    public Route solve(Route initialRoute) {
        Route currentRoute = initialRoute;
        double currentCost = calculateRouteCost(currentRoute);
        double temperature = INITIAL_TEMPERATURE;

        while (temperature > ACCEPTANCE_PROBABILITY_THRESHOLD) {
            Route neighborRoute = generateNeighborSolution(currentRoute);
            double neighborCost = calculateRouteCost(neighborRoute);

            double deltaE = neighborCost - currentCost; // 新旧解的成本差
            double acceptanceProbability = Math.exp(-deltaE / temperature); // Metropolis-Hastings接受概率公式

            if (deltaE < 0 || acceptanceProbability > new Random()..nextDouble()) {
                // 接受新解
                currentRoute = neighborRoute;
                currentCost = neighborCost;
            }

            // 降温
            temperature *= TEMPERATURE_DECREASE_RATE;
        }

        return currentRoute; // 返回最终找到的近似最优解
    }
}

2、遗传算法 (Genetic Algorithm, GA):

        遗传算法借鉴生物进化理论,使用种群、遗传、交叉、突变等概念来搜索解空间。初始种群由一组随机生成的解(个体)构成,通过适应度函数评估每个个体的好坏。然后进行选择、交叉(类似于生物的基因重组)和突变操作,生成下一代种群。此过程反复迭代,期望最终进化出具有高适应度的解。

        这里给出一个简化的Java代码框架示例,展示了遗传算法的基本结构及其在解决参数优化问题中的应用。我们假设已经实现了编码解(个体)为染色体(如二进制串)的Chromosome类,以及计算个体适应度的FitnessFunction接口。为了简化示例,具体的种群规模、交叉概率、变异概率等参数已设定为常数,实际应用中可能需要根据具体问题进行调整。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GeneticAlgorithm {
    private static final int POPULATION_SIZE = 50; // 种群大小
    private static final double CROSSOVER_PROBABILITY = 0.8; // 交叉概率
    private static final double MUTATION_PROBABILITY = 0.1; // 变异概率

    private FitnessFunction fitnessFunction; // 已实现的适应度函数接口

    public GeneticAlgorithm(FitnessFunction fitnessFunction) {
        this.fitnessFunction = fitnessFunction;
    }

    public Chromosome solve(int maxGenerations) {
        List<Chromosome> population = initializePopulation(POPULATION_SIZE);

        for (int generation = 0; generation < maxGenerations; generation++) {
            evaluateFitness(population);
            population = selection(population);
            population = crossover(population);
            mutate(population);
        }

        return getBestIndividual(population);
    }

    private List<Chromosome> initializePopulation(int size) {
        List<Chromosome> population = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            population.add(new Chromosome()); // 假设已实现Chromosome类的无参构造函数,用于随机初始化个体
        }
        return population;
    }

    private void evaluateFitness(List<Chromosome> population) {
        for (Chromosome individual : population) {
            individual.setFitness(fitnessFunction.calculateFitness(individual));
        }
    }

    private List<Chromosome> selection(List<Chromosome> population) {
        // 实现基于适应度的选择策略,如轮盘赌选择等
        // ...
        return selectedIndividuals;
    }

    private List<Chromosome> crossover(List<Chromosome> population) {
        List<Chromosome> offspring = new ArrayList<>(population.size());
        Random random = new Random();

        for (int i = 0; i < population.size(); i += 2) {
            Chromosome parent1 = population.get(i);
            Chromosome parent2 = population.get(i + 1);

            if (random.nextDouble() < CROSSOVER_PROBABILITY) {
                Chromosome child1 = parent1.crossover(parent2); // 假设Chromosome类已实现crossover方法
                Chromosome child2 = parent2.crossover(parent1);

                offspring.add(child1);
                offspring.add(child2);
            } else {
                offspring.add(parent1.clone());
                offspring.add(parent2.clone());
            }
        }

        return offspring;
    }

    private void mutate(List<Chromosome> population) {
        Random random = new Random();

        for (Chromosome individual : population) {
            if (random.nextDouble() < MUTATION_PROBABILITY) {
                individual.mutate(); // 假设Chromosome类已实现mutate方法
            }
        }
    }

    private Chromosome getBestIndividual(List<Chromosome> population) {
        Chromosome bestIndividual = population.get(0);
        for (Chromosome individual : population) {
            if (individual.getFitness() > bestIndividual.getFitness()) {
                bestIndividual = individual;
            }
        }
        return bestIndividual;
    }
}

// 定义适应度函数接口
interface FitnessFunction {
    double calculateFitness(Chromosome chromosome);
}

// 假设已实现Chromosome类,包含染色体表示、适应度设置、交叉、变异等方法
class Chromosome {
    // ...
}

 注意:

在这个简单示例中:

  1. GeneticAlgorithm类接收一个实现了FitnessFunction接口的对象作为构造参数,用于计算个体适应度。
  2. solve方法是遗传算法的主要执行逻辑,它接收最大迭代代数作为输入。
  3. 初始化种群,创建一组随机个体。
  4. 对每一代种群进行以下操作: a. 评估每个个体的适应度。 b. 根据适应度进行选择操作,保留较优个体进入下一代。 c. 对选中的个体进行交叉操作,生成新的子代。 d. 对子代进行变异操作,引入随机扰动。
  5. 重复以上步骤直到达到最大代数。
  6. 返回当前种群中适应度最高的个体作为问题的近似最优解。

当然,上述代码仅为算法示例,实际应用中需要根据具体问题实现Chromosome类、FitnessFunction接口以及选择、交叉、变异等操作的具体细节,并可能需要对算法参数(如种群规模、交叉概率、变异概率等)进行细致调整以优化搜索性能。

3、粒子群算法 (Particle Swarm Optimization, PSO):

        粒子群算法模拟鸟群或鱼群的群体智能,每个粒子代表一个解,并具有速度和位置属性。粒子根据自身的最佳位置(个体极值)和整个种群的最佳位置(全局极值)调整速度,从而在解空间中移动。通过群体间的通信和协作,粒子群逐步收敛到问题的最优或近似最优解。

        这里给出一个简化的Java代码框架示例,用于解决一维或多维函数优化问题。我们实现了一个通用的ParticleSwarmOptimizer类,它包含粒子类Particle、初始化方法、速度与位置更新逻辑以及主要的迭代过程。我们将使用一个简单的平方函数作为目标函数进行优化,但实际应用中可以替换为任何自定义的目标函数。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ParticleSwarmOptimizer {

    private static final Random RANDOM = new Random();

    private final int numParticles;
    private final int numDimensions;
    private final int maxIterations;
    private final double w; // 惯性权重
    private final double c1; // 局部学习因子
    private final double c2; // 全局学习因子
    private final double minVelocity, maxVelocity; // 速度限制
    private final Function objectiveFunction; // 目标函数

    public ParticleSwarmOptimizer(int numParticles, int numDimensions, int maxIterations,
                                  double w, double c1, double c2,
                                  double minVelocity, double maxVelocity,
                                  Function objectiveFunction) {
        this.numParticles = numParticles;
        this.numDimensions = numDimensions;
        this.maxIterations = maxIterations;
        this.w = w;
        this.c1 = c1;
        this.c2 = c2;
        this.minVelocity = minVelocity;
        this.maxVelocity = maxVelocity;
        this.objectiveFunction = objectiveFunction;
    }

    public double[] optimize() {
        List<Particle> particles = initializeParticles();
        double[] gBestPosition = particles.get(0).getPosition().clone(); // 初始全局最佳位置
        double gBestValue = objectiveFunction.evaluate(gBestPosition); // 初始全局最佳值

        for (int iteration = 0; iteration < maxIterations; iteration++) {
            for (Particle particle : particles) {
                double[] pBestPosition = particle.getPBestPosition();
                double pBestValue = particle.getPBestValue();

                // 更新速度
                double[] velocity = updateVelocity(particle.getVelocity(), particle.getPosition(),
                                                   pBestPosition, gBestPosition);

                // 更新位置
                double[] newPosition = updatePosition(particle.getPosition(), velocity);

                // 评估新位置的函数值
                double newValue = objectiveFunction.evaluate(newPosition);

                // 更新个人最佳位置与全局最佳位置
                if (newValue < pBestValue) {
                    particle.setPBestPosition(newPosition.clone());
                    particle.setPBestValue(newValue);
                }
                if (newValue < gBestValue) {
                    gBestPosition = newPosition.clone();
                    gBestValue = newValue;
                }

                particle.setPosition(newPosition);
                particle.setVelocity(velocity);
            }
        }

        return gBestPosition;
    }

    private List<Particle> initializeParticles() {
        List<Particle> particles = new ArrayList<>();
        for (int i = 0; i < numParticles; i++) {
            double[] position = generateRandomPosition();
            double[] velocity = generateRandomVelocity();
            particles.add(new Particle(position, velocity));
        }
        return particles;
    }

    private double[] generateRandomPosition() {
        double[] position = new double[numDimensions];
        for (int i = 0; i < numDimensions; i++) {
            position[i] = RANDOM.nextDouble();
        }
        return position;
    }

    private double[] generateRandomVelocity() {
        double[] velocity = new double[numDimensions];
        for (int i = 0; i < numDimensions; i++) {
            velocity[i] = RANDOM.nextDouble() * (maxVelocity - minVelocity) + minVelocity;
        }
        return velocity;
    }

    private double[] updateVelocity(double[] currentVelocity, double[] currentPosition,
                                   double[] pBestPosition, double[] gBestPosition) {
        double[] newVelocity = new double[currentVelocity.length];

        for (int i = 0; i < newVelocity.length; i++) {
            newVelocity[i] = w * currentVelocity[i]
                            + c1 * RANDOM.nextDouble() * (pBestPosition[i] - currentPosition[i])
                            + c2 * RANDOM.nextDouble() * (gBestPosition[i] - currentPosition[i]);

            // 限制速度范围
            newVelocity[i] = Math.max(minVelocity, Math.min(maxVelocity, newVelocity[i]));
        }

        return newVelocity;
    }

    private double[] updatePosition(double[] currentPosition, double[] velocity) {
        double[] newPosition = new double[currentPosition.length];

        for (int i = 0; i < newPosition.length; i++) {
            newPosition[i] = currentPosition[i] + velocity[i];
        }

        return newPosition;
    }

    interface Function {
        double evaluate(double[] x);
    }

    static class Particle {
        private double[] position;
        private double[] velocity;
        private double[] pBestPosition;
        private double pBestValue;

        Particle(double[] position, double[] velocity) {
            this.position = position;
            this.velocity = velocity;
            this.pBestPosition = position.clone();
            this.pBestValue = Double.POSITIVE_INFINITY;
        }

        double[] getPosition() {
            return position;
        }

        void setPosition(double[] position) {
            this.position = position;
        }

        double[] getVelocity() {
            return velocity;
        }

        void setVelocity(double[] velocity) {
            this.velocity = velocity;
        }

        double[] getPBestPosition() {
            return pBestPosition;
        }

        void setPBestPosition(double[] pBestPosition) {
            this.pBestPosition = pBestPosition;
        }

        double getPBestValue() {
            return pBestValue;
        }

        void setPBestValue(double pBestValue) {
            this.pBestValue = pBestValue;
        }
    }

    // 使用示例
    public static void main(String[] args) {
        Function objectiveFunction = x -> x[0] * x[0] + x[1] * x[1]; // 目标函数:二维空间中的平方函数

        ParticleSwarmOptimizer optimizer = new ParticleSwarmOptimizer(
                50, // 粒子数量
                2, // 维度数
                100, // 最大迭代次数
                0.¾, // 惯性权重
                1.49, // 局部学习因子
                1.49, // 全局学习因子
                -1.0, // 最小速度
                1.0, // 最大速度
                objectiveFunction);

        double[] optimalPosition = optimizer.optimize();
        System.out.println("Optimal Position: [" + optimalPosition[0] + ", " + optimalPosition[1] + "]");
    }
}

 

在示例中:

  1. 定义一个ParticleSwarmOptimizer类,包含粒子类Particle、初始化方法、速度与位置更新逻辑以及主要的迭代过程。
  2. optimize方法负责执行PSO算法: a. 初始化粒子群,为每个粒子赋予随机位置和速度,并设置初始个人最佳位置与全局最佳位置。 b. 循环执行以下操作直至达到最大迭代次数: i. 对每个粒子,根据当前速度、个人最佳位置和全局最佳位置更新其速度。 ii. 根据更新后的速度计算新的位置,并评估新位置的函数值。 iii. 更新该粒子的个人最佳位置和全局最佳位置(如果新位置对应的函数值更优)。 iv. 保存粒子的新位置和速度。
  3. 定义一个Function接口,用户可以提供自定义的目标函数实现。
  4. main方法中创建ParticleSwarmOptimizer实例,指定参数并运行优化过程,最后输出最优位置。

注意:这个示例使用了固定的惯性权重、学习因子、速度范围和目标函数。实际应用中,这些参数可以根据具体问题进行调整,目标函数也可以替换为需要优化的实际函数。

4、贪心算法:

        贪心算法在每一步决策时都采取当前状态下看起来最优的选择,不考虑未来步骤对整体最优性的影响。这种算法在某些特定问题中能产生全局最优解,如霍夫曼编码、Prim's最小生成树算法等,前提是问题具备贪心选择性质和最优子结构。

        以Prim算法求解最小生成树为例,我们将使用一个简单的邻接矩阵表示图结构,并使用优先队列(PriorityQueue)来辅助找到当前未加入最小生成树中但与已加入节点相连的边中权值最小的那条边。为了简化代码,我们假设图是无向的,且不存在自环和重边

import java.util.*;

public class PrimMinimumSpanningTree {
    private final int[][] graph; // 邻接矩阵表示的无向图
    private final int n; // 图中顶点数

    public PrimMinimumSpanningTree(int[][] graph) {
        this.graph = graph;
        this.n = graph.length;
    }

    public int primMST() {
        boolean[] visited = new boolean[n]; // 记录已访问节点
        int[] parent = new int[n]; // 记录每个节点的父节点,用于构建最小生成树
        int[] key = new int[n]; // 记录从起点到各个节点的已知最小边的权值

        Arrays.fill(key, Integer.MAX_VALUE);
        key[0] = 0; // 从任意节点(这里取第一个节点)开始

        PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));

        // 将与起始节点相邻的边加入优先队列
        for (int i = 1; i < n; i++) {
            if (graph[0][i] != 0) {
                pq.offer(new Edge(i, 0, graph[0][i])); // 边的终点、起点和权值
            }
        }

        int mstWeight = 0;

        while (!pq.isEmpty()) {
            Edge edge = pq.poll();

            int u = edge.u;
            int v = edge.v;

            if (!visited[u]) {
                visited[u] = true;
                mstWeight += edge.weight;

                for (int i = 0; i < n; i++) {
                    if (graph[u][i] != 0 && !visited[i] && graph[u][i] < key[i]) {
                        key[i] = graph[u][i];
                        parent[i] = u;
                        pq.offer(new Edge(i, u, key[i]));
                    }
                }
            }
        }

        return mstWeight; // 最小生成树的总权值
    }

    static class Edge {
        int u, v, weight;

        Edge(int u, int v, int weight) {
            this.u = u;
            this.v = v;
            this.weight = weight;
        }
    }
}

// 使用示例
public class Main {
    public static void main(String[] args) {
        int[][] graph = {
                {0, 2, 0, 6, 0},
                {2, 0, 3, 8, 5},
                {0, 3, 0, 0, 7},
                {6, 8, 0, 0, 9},
                {0, 5, 7, 9, 0}
        };

        PrimMinimumSpanningTree prim = new PrimMinimumSpanningTree(graph);
        int mstWeight = prim.primMST();
        System.out.println("Minimum Spanning Tree weight: " + mstWeight);
    }
}

在示例中:

  1. 定义一个PrimMinimumSpanningTree类,接收一个邻接矩阵表示的无向图作为输入。
  2. primMST方法负责执行Prim算法: a. 初始化已访问节点标记数组visited、父节点数组parent以及记录最小边权值的数组key。 b. 创建一个优先队列pq,用于存放待处理的边,按照边的权值进行排序。 c. 从任意节点(此处取第一个节点)开始,将其标记为已访问,并将与其相邻的所有边加入优先队列。 d. 当优先队列非空时,循环执行以下操作: i. 取出队首边(权值最小的边)。 ii. 如果该边的一个端点未被访问,则将其标记为已访问,累加最小生成树的总权值,并更新其邻居节点的最小边权值(若有更小边,则替换)。将新发现的更小边加入优先队列。
  3. 返回最小生成树的总权值。
  4. Main类的示例中,创建了一个实例并调用primMST方法计算给定图的最小生成树的权值,最后输出结果。       

5、A*搜索算法:

A*搜索是一种启发式图搜索算法,结合了启发式函数(评估当前节点到目标节点的估计代价)和实际代价(从起点到当前节点的实际代价),以确定下一个要扩展的节点。它确保了在搜索过程中始终沿着预期总代价最低的路径前进,有效地减少了搜索范围,适用于求解最短路径等问题。

        这里给出一个简化的Java代码示例,展示了A*搜索算法的基本结构及其在解决网格地图上的路径规划问题中的应用。我们假设已实现了一个表示地图节点的Node类,以及计算实际代价g(n)和启发式代价h(n)的相应方法。为了简化,仅考虑了二维网格地图,并假设节点间移动代价是恒定的。实际应用中可能需要根据具体问题进行扩展和调整。

import java.util.*;

public class AStarSearch {
    private final Node startNode;
    private final Node targetNode;
    private final Map<Node, Node> cameFrom; // 记录到达当前节点的前驱节点
    private final Map<Node, Double> gScore; // 记录从起点到各节点的实际代价
    private final Map<Node, Double> fScore; // 记录节点的总代价(g(n) + h(n))
    private PriorityQueue<Node> openSet; // 未处理节点集,按f(n)值排序

    public AStarSearch(Node startNode, Node targetNode) {
        this.startNode = startNode;
        this.targetNode = targetNode;
        this.cameFrom = new HashMap<>();
        this.gScore = new HashMap<>();
        this.fScore = new HashMap<>();
        this.openSet = new PriorityQueue<>(Comparator.comparingDouble(this::getFScore));

        gScore.put(startNode, 0.0);
        fScore.put(startNode, startNode.heuristic(targetNode));
        openSet.add(startNode);
    }

    public Optional<List<Node>> findPath() {
        while (!openSet.isEmpty()) {
            Node current = openSet.poll();
            if (current.equals(targetNode)) {
                return Optional.of(reconstructPath(cameFrom, current));
            }

            for (Node neighbor : current.neighbors()) {
                double tentativeGScore = gScore.get(current) + current.costTo(neighbor);
                if (tentativeGScore < gScore.getOrDefault(neighbor, Double.MAX_VALUE)) {
                    cameFrom.put(neighbor, current);
                    gScore.put(neighbor, tentativeGScore);
                    fScore.put(neighbor, tentativeGScore + neighbor.heuristic(targetNode));
                    if (!openSet.contains(neighbor)) {
                        openSet.add(neighbor);
                    }
                }
            }
        }

        return Optional.empty(); // 没有找到路径
    }

    private List<Node> reconstructPath(Map<Node, Node> cameFrom, Node current) {
        List<Node> path = new ArrayList<>();
        path.add(current);

        while (cameFrom.containsKey(current)) {
            current = cameFrom.get(current);
            path.add(0, current); // 将当前节点添加到路径列表的开头
        }

        return path;
    }

    private double getFScore(Node node) {
        return fScore.get(node);
    }
}

// 定义节点类,包含邻居访问、实际代价计算、启发式代价计算等方法
class Node {
    // ...

    public List<Node> neighbors() {
        // 返回当前节点的所有邻居节点列表
    }

    public double costTo(Node neighbor) {
        // 返回从当前节点到指定邻居节点的代价
    }

    public double heuristic(Node target) {
        // 返回从当前节点到目标节点的启发式估计代价
    }
}

 

在这个代码中:

  1. AStarSearch类接收起始节点和目标节点作为构造参数。
  2. findPath方法是A*搜索的主要执行逻辑,它返回一个可选的路径列表(Optional<List<Node>>),表示从起始节点到目标节点的最优路径。如果没有找到路径,则返回空可选对象。
  3. 初始化数据结构,包括前驱节点映射cameFrom、实际代价映射gScore、总代价映射fScore和优先级队列openSet,用于存储待处理节点。
  4. openSet非空时,循环执行以下操作: a. 从openSet中取出具有最小f(n)值的节点current。 b. 如果current等于目标节点,结束搜索并返回重建的路径。 c. 对current的每个邻居节点neighbor: i. 计算从起点到neighbor的新的实际代价tentativeGScore。 ii. 如果新代价小于已记录的代价,更新cameFromgScorefScore,并将neighbor加入或更新在openSet中。
  5. 使用reconstructPath方法从cameFrom映射中逆向追溯路径,构建从起始节点到目标节点的实际路径列表。

注意:上述代码仅提供了A*算法的核心逻辑,实际应用中需要根据具体问题实现Node类及其相关方法,如邻居访问、节点间代价计算、启发式代价计算等。此外,还可以根据需求调整启发式函数以适应不同的搜索场景。

6、蚁群算法 (Ant Colony Optimization, ACO):

        蚁群算法模拟蚂蚁寻找食物路径的行为,通过信息素的正反馈机制在解空间中分布和更新信息。蚂蚁在寻找路径时,会倾向于选择信息素浓度高的路径,并在走过后留下更多信息素。随着时间推移,信息素浓度高的路径更可能被其他蚂蚁选择,从而形成最优或近似最优路径。

        这个算法示例,大家可以来试试,给出一个算法示例!!!

蚁群算法示例,后续更新补上

  • 37
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值