【算法】遗传算法

news2024/9/22 17:43:01

一、引言

        遗传算法(Genetic Algorithm, GA)是一种模拟生物进化过程的启发式搜索算法,它通过模拟自然选择、遗传、交叉和突变等生物学机制来优化问题的解决方案。遗传算法因其通用性、高效性和鲁棒性,在多个领域中得到了广泛应用,如工程、科研、经济和艺术等。

二、算法原理

遗传算法的核心原理包括以下几个方面:

  • 编码:将问题的解编码为染色体(通常为一串数字或符号序列)。
  • 初始种群:随机生成一组解作为初始种群。
  • 适应度函数:定义一个适应度函数来评估每个个体的性能。
  • 选择:根据适应度选择个体进行繁殖,高适应度的个体有更高的被选择概率。
  • 交叉:选中的个体通过交叉操作生成新的后代,模拟基因重组。
  • 突变:以一定概率随机改变个体的某些基因,增加种群的多样性。
  • 新一代种群:形成新的种群,重复上述过程直到满足终止条件。

三、数据结构

遗传算法中常用的数据结构包括:

  • 染色体:表示问题的解,通常为一串数字或符号序列。
  • 适应度数组:存储每个个体适应度值的数组。
  • 个体(Individual):表示一个解。通常用一个染色体(Chromosome)来表示,染色体由基因(Gene)组成。
  • 种群(Population):由多个个体组成,是算法的基础单元。
  • 适应度函数(Fitness Function):用于评估个体的优劣。
  • 选择策略(Selection Strategy):确定哪些个体会被选择进行繁殖。常见的策略包括轮盘赌选择、锦标赛选择等。
  • 交叉策略(Crossover Strategy):决定如何将两个父母个体的基因组合成子代个体。常见的策略包括单点交叉、两点交叉等。
  • 变异策略(Mutation Strategy):在个体中引入随机变异,以增加种群的多样性。

四、算法使用场景

遗传算法适用于解决以下类型的优化问题:

  • 组合优化问题:如旅行商问题(TSP)、车辆路径问题(VRP)等。

  • 参数优化问题:如神经网络权重优化、机器学习模型参数调优等。

  • 调度问题:如作业调度、任务调度等。
  • 设计问题:如结构设计、网络设计等。
  • 数据挖掘:特征选择、聚类分析。

五、算法实现

  • 初始化种群:随机生成一组个体,每个个体代表一个可能的解。
  • 评估适应度:根据目标函数评估每个个体的适应度。
  • 选择操作:根据适应度选择较优的个体进行繁殖。
  • 交叉操作:将选择出来的个体配对,通过交叉生成新个体。
  • 变异操作:对新个体进行随机变异,以保持种群的多样性。
  • 替代操作:用新生成的个体替代旧种群中的个体,形成新的种群。
  • 终止条件:当达到预定的终止条件(如最大代数或适应度阈值)时,算法停止。
import numpy as np

def initialize_population(pop_size, gene_length):
    return np.random.randint(2, size=(pop_size, gene_length))

def fitness_function(individual):
    # 示例:适应度函数为个体基因的汉明重量
    return np.sum(individual)

def select(population, fitness_values):
    # 示例:轮盘赌选择
    probabilities = fitness_values / np.sum(fitness_values)
    indices = np.random.choice(range(len(population)), size=len(population), p=probabilities)
    return population[indices]

def crossover(parent1, parent2):
    # 示例:单点交叉
    point = np.random.randint(1, len(parent1))
    child1 = np.concatenate((parent1[:point], parent2[point:]))
    child2 = np.concatenate((parent2[:point], parent1[point:]))
    return child1, child2

def mutate(individual, mutation_rate):
    # 示例:基因突变
    for i in range(len(individual)):
        if np.random.rand() < mutation_rate:
            individual[i] = 1 - individual[i]
    return individual

def genetic_algorithm(population_size, gene_length, num_generations):
    population = initialize_population(population_size, gene_length)
    for _ in range(num_generations):
        fitness_values = np.array([fitness_function(ind) for ind in population])
        population = select(population, fitness_values)
        next_generation = []
        while len(next_generation) < population_size:
            parent1, parent2 = np.random.choice(population, size=2, replace=False)
            child1, child2 = crossover(parent1, parent2)
            child1 = mutate(child1, 0.01)
            child2 = mutate(child2, 0.01)
            next_generation.extend([child1, child2])
        population = np.array(next_generation)
    best_individual = population[np.argmax(fitness_values)]
    return best_individual

# 运行遗传算法
best_solution = genetic_algorithm(100, 10, 50)
print("Best solution:", best_solution)

六、同类型算法对比

        粒子群优化(PSO):基于个体与群体之间的信息共享,收敛速度较快,但容易陷入局部最优。
        蚁群算法(ACO):模拟蚂蚁觅食行为,适用于路径优化问题,但计算量较大。
        模拟退火(SA):借鉴物理退火过程,适用于大规模问题,容易避免局部最优但计算复杂度较高。

遗传算法与其他优化算法(如粒子群优化、模拟退火、蚁群算法等)相比,具有以下特点:

  • 全局搜索能力强:遗传算法通过模拟自然进化过程,具有较强的全局搜索能力。

  • 鲁棒性:遗传算法对初始种群和参数设置不敏感,具有较强的鲁棒性。

  • 适用于多种优化问题:遗传算法适用于连续、离散及混合类型的优化问题。

  • 编码简单:遗传算法的编码方式较为简单,易于实现。

七、多语言代码实现

Java

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

class Individual {
    List<Integer> genes;
    double fitness;

    public Individual(int geneLength) {
        genes = new ArrayList<>(Collections.nCopies(geneLength, 0));
        Random rand = new Random();
        for (int i = 0; i < geneLength; i++) {
            genes.set(i, rand.nextInt(2)); // Binary genes
        }
    }

    public void calculateFitness() {
        // Example fitness function: sum of genes
        fitness = genes.stream().mapToInt(Integer::intValue).sum();
    }
}

class GeneticAlgorithm {
    private List<Individual> population;
    private int geneLength;
    private int populationSize;
    private double mutationRate;
    private int generations;

    public GeneticAlgorithm(int geneLength, int populationSize, double mutationRate, int generations) {
        this.geneLength = geneLength;
        this.populationSize = populationSize;
        this.mutationRate = mutationRate;
        this.generations = generations;
        population = new ArrayList<>();
        for (int i = 0; i < populationSize; i++) {
            population.add(new Individual(geneLength));
        }
    }

    public void evolve() {
        for (int generation = 0; generation < generations; generation++) {
            evaluateFitness();
            List<Individual> newPopulation = new ArrayList<>();
            while (newPopulation.size() < populationSize) {
                Individual parent1 = selectParent();
                Individual parent2 = selectParent();
                Individual child = crossover(parent1, parent2);
                mutate(child);
                newPopulation.add(child);
            }
            population = newPopulation;
        }
    }

    private void evaluateFitness() {
        population.forEach(Individual::calculateFitness);
    }

    private Individual selectParent() {
        // Simple roulette wheel selection
        double totalFitness = population.stream().mapToDouble(i -> i.fitness).sum();
        double rand = new Random().nextDouble() * totalFitness;
        double sum = 0;
        for (Individual individual : population) {
            sum += individual.fitness;
            if (sum >= rand) return individual;
        }
        return population.get(population.size() - 1); // Should not reach here
    }

    private Individual crossover(Individual parent1, Individual parent2) {
        Individual child = new Individual(geneLength);
        int crossoverPoint = new Random().nextInt(geneLength);
        for (int i = 0; i < geneLength; i++) {
            child.genes.set(i, i < crossoverPoint ? parent1.genes.get(i) : parent2.genes.get(i));
        }
        return child;
    }

    private void mutate(Individual individual) {
        for (int i = 0; i < geneLength; i++) {
            if (new Random().nextDouble() < mutationRate) {
                individual.genes.set(i, 1 - individual.genes.get(i));
            }
        }
    }
}

Python

import random

class Individual:
    def __init__(self, gene_length):
        self.genes = [random.randint(0, 1) for _ in range(gene_length)]
        self.fitness = 0

    def calculate_fitness(self):
        self.fitness = sum(self.genes)

class GeneticAlgorithm:
    def __init__(self, gene_length, population_size, mutation_rate, generations):
        self.gene_length = gene_length
        self.population_size = population_size
        self.mutation_rate = mutation_rate
        self.generations = generations
        self.population = [Individual(gene_length) for _ in range(population_size)]

    def evolve(self):
        for _ in range(self.generations):
            self.evaluate_fitness()
            new_population = []
            while len(new_population) < self.population_size:
                parent1 = self.select_parent()
                parent2 = self.select_parent()
                child = self.crossover(parent1, parent2)
                self.mutate(child)
                new_population.append(child)
            self.population = new_population

    def evaluate_fitness(self):
        for individual in self.population:
            individual.calculate_fitness()

    def select_parent(self):
        total_fitness = sum(individual.fitness for individual in self.population)
        rand = random.uniform(0, total_fitness)
        sum_ = 0
        for individual in self.population:
            sum_ += individual.fitness
            if sum_ >= rand:
                return individual
        return self.population[-1]

    def crossover(self, parent1, parent2):
        crossover_point = random.randint(0, self.gene_length - 1)
        child = Individual(self.gene_length)
        child.genes = parent1.genes[:crossover_point] + parent2.genes[crossover_point:]
        return child

    def mutate(self, individual):
        for i in range(self.gene_length):
            if random.random() < self.mutation_rate:
                individual.genes[i] = 1 - individual.genes[i]

C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>

class Individual {
public:
    std::vector<int> genes;
    double fitness;

    Individual(int geneLength) : genes(geneLength), fitness(0) {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(0, 1);
        for (int &gene : genes) {
            gene = dis(gen);
        }
    }

    void calculateFitness() {
        fitness = std::accumulate(genes.begin(), genes.end(), 0.0);
    }
};

class GeneticAlgorithm {
    std::vector<Individual> population;
    int geneLength;
    int populationSize;
    double mutationRate;
    int generations;

public:
    GeneticAlgorithm(int geneLength, int populationSize, double mutationRate, int generations)
        : geneLength(geneLength), populationSize(populationSize), mutationRate(mutationRate), generations(generations) {
        for (int i = 0; i < populationSize; ++i) {
            population.emplace_back(geneLength);
        }
    }

    void evolve() {
        for (int generation = 0; generation < generations; ++generation) {
            evaluateFitness();
            std::vector<Individual> newPopulation;
            while (newPopulation.size() < populationSize) {
                Individual parent1 = selectParent();
                Individual parent2 = selectParent();
                Individual child = crossover(parent1, parent2);
                mutate(child);
                newPopulation.push_back(child);
            }
            population = newPopulation;
        }
    }

private:
    void evaluateFitness() {
        for (auto& individual : population) {
            individual.calculateFitness();
        }
    }

    Individual selectParent() {
        double totalFitness = 0;
        for (const auto& individual : population) {
            totalFitness += individual.fitness;
        }
        std::uniform_real_distribution<> dis(0, totalFitness);
        std::random_device rd;
        std::mt19937 gen(rd());
        double rand = dis(gen);
        double sum = 0;
        for (const auto& individual : population) {
            sum += individual.fitness;
            if (sum >= rand) {
                return individual;
            }
        }
        return population.back(); // Should not reach here
    }

    Individual crossover(const Individual& parent1, const Individual& parent2) {
        std::uniform_int_distribution<> dis(0, geneLength - 1);
        std::random_device rd;
        std::mt19937 gen(rd());
        int crossoverPoint = dis(gen);
        Individual child(geneLength);
        std::copy(parent1.genes.begin(), parent1.genes.begin() + crossoverPoint, child.genes.begin());
        std::copy(parent2.genes.begin() + crossoverPoint, parent2.genes.end(), child.genes.begin() + crossoverPoint);
        return child;
    }

    void mutate(Individual& individual) {
        std::uniform_real_distribution<> dis(0, 1);
        std::random_device rd;
        std::mt19937 gen(rd());
        for (int i = 0; i < geneLength; ++i) {
            if (dis(gen) < mutationRate) {
                individual.genes[i] = 1 - individual.genes[i];
            }
        }
    }
};

Go

package main

import (
    "math/rand"
    "time"
)

type Individual struct {
    Genes   []int
    Fitness float64
}

func NewIndividual(geneLength int) *Individual {
    genes := make([]int, geneLength)
    for i := range genes {
        genes[i] = rand.Intn(2)
    }
    return &Individual{Genes: genes}
}

func (ind *Individual) CalculateFitness() {
    sum := 0
    for _, gene := range ind.Genes {
        sum += gene
    }
    ind.Fitness = float64(sum)
}

type GeneticAlgorithm struct {
    Population    []*Individual
    GeneLength    int
    PopulationSize int
    MutationRate  float64
    Generations   int
}

func NewGeneticAlgorithm(geneLength, populationSize int, mutationRate float64, generations int) *GeneticAlgorithm {
    population := make([]*Individual, populationSize)
    for i := 0; i < populationSize; i++ {
        population[i] = NewIndividual(geneLength)
    }
    return &GeneticAlgorithm{
        Population:   population,
        GeneLength:   geneLength,
        PopulationSize: populationSize,
        MutationRate: mutationRate,
        Generations:  generations,
    }
}

func (ga *GeneticAlgorithm) Evolve() {
    for i := 0; i < ga.Generations; i++ {
        ga.EvaluateFitness()
        newPopulation := make([]*Individual, ga.PopulationSize)
        for j := 0; j < ga.PopulationSize; j++ {
            parent1 := ga.SelectParent()
            parent2 := ga.SelectParent()
            child := ga.Crossover(parent1, parent2)
            ga.Mutate(child)
            newPopulation[j] = child
        }
        ga.Population = newPopulation
    }
}

func (ga *GeneticAlgorithm) EvaluateFitness() {
    for _, ind := range ga.Population {
        ind.CalculateFitness()
    }
}

func (ga *GeneticAlgorithm) SelectParent() *Individual {
    totalFitness := 0.0
    for _, ind := range ga.Population {
        totalFitness += ind.Fitness
    }
    randValue := rand.Float64() * totalFitness
    sum := 0.0
    for _, ind := range ga.Population {
        sum += ind.Fitness
        if sum >= randValue {
            return ind
        }
    }
    return ga.Population[len(ga.Population)-1] // Should not reach here
}

func (ga *GeneticAlgorithm) Crossover(parent1, parent2 *Individual) *Individual {
    crossoverPoint := rand.Intn(ga.GeneLength)
    child := NewIndividual(ga.GeneLength)
    copy(child.Genes[:crossoverPoint], parent1.Genes[:crossoverPoint])
    copy(child.Genes[crossoverPoint:], parent2.Genes[crossoverPoint:])
    return child
}

func (ga *GeneticAlgorithm) Mutate(ind *Individual) {
    for i := range ind.Genes {
        if rand.Float64() < ga.MutationRate {
            ind.Genes[i] = 1 - ind.Genes[i]
        }
    }
}

func main() {
    rand.Seed(time.Now().UnixNano())
    ga := NewGeneticAlgorithm(10, 100, 0.01, 50)
    ga.Evolve()
}

八、应用场景的整个代码框架

用遗传算法进行超参数调优,可构建如下的项目结构:

project/
    ├── main.py
    ├── ga.py
    ├── objective.py
    ├── utils.py
    ├── requirements.txt
    └── README.md

main.py

from ga import GeneticAlgorithm
from objective import objective_function

def main():
    ga = GeneticAlgorithm(objective_function, pop_size=100, gene_length=5)
    best_solution, best_fitness = ga.run(generations=200)
    print(f"Optimal parameters: {best_solution}, Maximum fitness: {best_fitness}")

if __name__ == '__main__':
    main()

ga.py

import numpy as np
import random

class GeneticAlgorithm:
    def __init__(self, objective_function, pop_size=50, gene_length=10, mutation_rate=0.01):
        self.objective_function = objective_function
        self.pop_size = pop_size
        self.gene_length = gene_length
        self.mutation_rate = mutation_rate
        self.population = self.initialize_population()

    def initialize_population(self):
        return [np.random.rand(self.gene_length) for _ in range(self.pop_size)]

    def calculate_fitness(self):
        return [self.objective_function(ind) for ind in self.population]

    def selection(self, fitness):
        idx = np.random.choice(range(len(self.population)), size=len(self.population), p=fitness/np.sum(fitness))
        return [self.population[i] for i in idx]

    def crossover(self, parent1, parent2):
        point = random.randint(1, len(parent1)-1)
        return np.concatenate((parent1[:point], parent2[point:]))

    def mutate(self, individual):
        for i in range(len(individual)):
            if random.random() < self.mutation_rate:
                individual[i] = random.random()
        return individual

    def run(self, generations):
        for generation in range(generations):
            fitness = self.calculate_fitness()
            self.population = self.selection(fitness)
            next_population = []

            while len(next_population) < self.pop_size:
                parent1, parent2 = random.sample(self.population, 2)
                child = self.crossover(parent1, parent2)
                child = self.mutate(child)
                next_population.append(child)

            self.population = next_population
        
        best_individual = self.population[np.argmax(self.calculate_fitness())]
        return best_individual, self.objective_function(best_individual)

objective.py

def objective_function(x):
    return -(x[0]**2 + x[1]**2) + 10  # Example objective function

utils.py

import numpy as np
import random
import matplotlib.pyplot as plt

def set_random_seed(seed):
    """
    Set the random seed for reproducibility.
    
    Parameters:
        seed (int): The seed value to use.
    """
    random.seed(seed)
    np.random.seed(seed)

def initialize_population(pop_size, gene_length):
    """
    Initialize a population with random values.
    
    Parameters:
        pop_size (int): The number of individuals in the population.
        gene_length (int): The length of each individual (chromosome).
    
    Returns:
        List[np.ndarray]: A list containing the initialized population.
    """
    return [np.random.rand(gene_length) for _ in range(pop_size)]

def plot_fitness_progress(fitness_history):
    """
    Plot the progress of fitness over generations.
    
    Parameters:
        fitness_history (List[float]): A list of fitness values for each generation.
    """
    plt.figure(figsize=(10, 5))
    plt.plot(fitness_history, label='Fitness', color='blue')
    plt.title('Fitness Progress Over Generations')
    plt.xlabel('Generation')
    plt.ylabel('Fitness')
    plt.legend()
    plt.grid()
    plt.show()

def save_results_to_file(results, filename):
    """
    Save the results to a text file.
    
    Parameters:
        results (dict): The results to save (e.g., best solution, fitness).
        filename (str): The name of the file where results will be saved.
    """
    with open(filename, 'w') as f:
        for key, value in results.items():

requirements.txt

numpy>=1.21.0
matplotlib>=3.4.0
scikit-learn>=0.24.0  # 如果需要用于机器学习相关的库
pandas>=1.2.0  # 如果你想处理数据集

        遗传算法是一种灵活强大的优化工具,适用于多个领域。通过不断演化和选择,可以找到较优的解。在具体实现时,需综合考虑问题的实际需求,合理设计适应度函数和遗传操作。由于遗传算法的随机性,可能需要多次运行以找到较优解。希望这篇博文能帮助你更好地理解和实现遗传算法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2144052.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)

&#x1f308;个人主页&#xff1a;努力学编程’ ⛅个人推荐&#xff1a; c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构&#xff0c;刷题刻不容缓&#xff1a;点击一起刷题 &#x1f319;心灵鸡汤&#xff1a;总有人要赢&#xff0c;为什么不能是我呢 &#x1f354…

Nest.js

Nestjs中文文档链接 TypeORM 中文文档 小满视频 1. 安装Nest.js 安装脚手架 npm i -g nestjs/cli创建nestjs工程 nest new工程目录 app.module.ts 根模块用于处理其他类的引用与共享。app.controller.ts 常见功能是用来处理http请求&#xff08;处理请求的路径&#xff09…

.net core8 使用JWT鉴权(附当前源码)

说明 该文章是属于OverallAuth2.0系列文章&#xff0c;每周更新一篇该系列文章&#xff08;从0到1完成系统开发&#xff09;。 该系统文章&#xff0c;我会尽量说的非常详细&#xff0c;做到不管新手、老手都能看懂。 说明&#xff1a;OverallAuth2.0 是一个简单、易懂、功能强…

焦虑拜拜!这些维生素是你的情绪小太阳✨,焦虑星人必看!

&#x1f33f; ‌维生素B群&#xff1a;情绪的调节大师‌ &#x1f3af; 说到缓解焦虑&#xff0c;怎能不提维生素B群&#xff1f;它可是个大家庭&#xff0c;包括B1、B2、B6、B12等&#xff0c;每一个都是调节神经系统的关键角色。维生素B群能够促进神经递质的合成&#xff0…

Prometheus监控k8s环境构建

传统架构中比较流行的监控工具有 Zabbix、Nagios 等&#xff0c;这些监控工具对于 Kubernetes 这类云平台的监控不是很友好&#xff0c;特别是当 Kubernetes 集群中有了成千上万的容器后更是如此&#xff0c;本章节学习下一代的云原生监控平台---Prometheus。 一、基于kuberne…

DNS解析域名详解

你有没有想过&#xff0c;当一个url传过来网络对它进行了哪些操作~DNS又是怎样对域名进行解析的~或者我们为什么要用到域名&#xff0c;为什么不直接使用ip地址~ 对于我们而言&#xff0c;面对长串的ip地址&#xff0c;我们更喜欢记忆较短的域名&#xff0c;但是对于路由器来说…

第二证券:降息升温!资金涌入港股,行情还能持续多久?

在美联储行将打开降息影响下&#xff0c;多国股指改写高点。 当时&#xff0c;商场环绕美联储是25个基点仍是50个基点的降息展开预期买卖&#xff0c;资金流向风险财物规划扩大显着。17日&#xff0c;澳大利亚S&P/ASX 200指数股指、印度孟买SENSEX30指数、新加坡富时海峡指…

MySQL函数:日期函数

先贴一张黑马程序员的听课截图 1.返回当前日期 CURDATE(); select CURDATE(); //获取当前日期2. 返回当前时间 CURTIME(); select CURTIME(); //获取当前时间3.返回当前日期和时间NOW() select NOW(); //获取当前日期和时间 4.获取指定date的年份YEAR(date) select YEAR…

力扣(LeetCode)每日一题 2848. 与车相交的点

题目链接https://leetcode.cn/problems/points-that-intersect-with-cars/description/?envTypedaily-question&envId2024-09-15 给你一个下标从 0 开始的二维整数数组 nums 表示汽车停放在数轴上的坐标。对于任意下标 i&#xff0c;nums[i] [starti, endi] &#xff0c;…

[Python]一、Python基础编程

F:\BaiduNetdiskDownload\2023人工智能开发学习路线图\1、人工智能开发入门\1、零基础Python编程 1. Python简介 Python优点: 学习成本低开源适应人群广泛应用领域广泛1.1 Python解释器 下载地址:Download Python | Python.org 1.2 Python开发IDE -- Pycharm 2. 基础语法…

人工智能(AI)的影响下人类的生活样子

讨论在人工智能(AI)的影响下人类的生活是什么样子 在21世纪的今天&#xff0c;人工智能&#xff08;AI&#xff09;已经不再是遥不可及的未来科技&#xff0c;而是悄然渗透到我们日常生活的每一个角落&#xff0c;以一种前所未有的方式改变着我们的生活方式、工作模式乃至社会…

使用 Python 绘制 BTC 期权的波动率曲面

波动率曲面&#xff08;Volatility Surface&#xff09;是期权交易中展示隐含波动率随行权价&#xff08;strike price&#xff09;和到期时间&#xff08;expiry time&#xff09;变化的一种三维图形。 本文尝试通过 Python&#xff0c;通过 ccxt 基于从交易所获取期权的指标…

远程连接MySQL并操作

配置MySQL开发环境 如果你使用的是基于Debian的系统&#xff08;如Ubuntu&#xff09;&#xff0c;可以在终端通过如下步骤安装MySQL开发包。 更新软件包列表 运行以下命令以确保你拥有最新的软件包列表。 sudo apt-get update安装libmysqlclient-dev开发包 执行以下命令以…

python-字符排列问题

题目描述 有 n 个字母&#xff0c;列出由该字母组成的字符串的全排列&#xff08;相同的排列只计一次&#xff09;。输入格式 第一行输入是字母个数 n 。 接下来一行输入的是待排列的 n 个字母。输出格式 计算出的 n 个字母的所有不同排列总数。样例输入输出样例输入 4 aacc样例…

道路驾驶视角人车检测数据集 16000张 带标注 voc yolo

随着智能驾驶技术和车辆辅助系统的快速发展&#xff0c;道路驾驶视角下的多目标检测成为了保障行车安全的关键技术之一。为了提高自动驾驶车辆以及辅助驾驶系统的性能&#xff0c;需要大量的高质量标注数据来训练这些系统。本数据集旨在为道路驾驶视角下的人车检测提供高质量的…

python画图|中秋到了,尝试画个月亮(球体画法)

学习了一段时间的画图&#xff0c;已经掌握了一些3D图的画法&#xff0c;部分链接如下&#xff1a; python画图|极坐标下的3D surface-CSDN博客 python画图|3D参数化图形输出-CSDN博客 我们今天尝试一下月亮的画法。 【1】官网教程 首先还是到达官网教程学习&#xff1a; …

java: 警告: 源发行版 17 需要目标发行版 17(100% 解决)

1. 问题说明 Idea启动Springboot服务报错&#xff1a;java: 警告: 源发行版 17 需要目标发行版 17 2. 解决方案 Project Structure指定jdk版本为我们当前使用的版本&#xff1b; Java Compiler指定jdk为我们当前使用的版本&#xff1b; Invalidate Caches重启Idea。 如果还…

NTC温度电阻--100K 10K

100K温度电阻分度表 粗精度直接用公式计算 细精度用厂家的传感器参数来计算 Y C1 C2* y C3 * y * y*y static float get_ntc_temp(double Rt) { double y log(Rt);//122.6us48M double val; static const double c1 0.0008314; static const double c2 0.00026178;…

Linux下vscode配置C++和python编译调试环境

Visual Studio Code (简称 VSCode) 是由微软开发的一款免费、开源、跨平台的代码编辑器。它支持 Windows、macOS 和 Linux 操作系统&#xff0c;并且内置对多种编程语言的支持&#xff0c;包括但不限于 C/C、Python、JavaScript、TypeScript、Java 和 Go 等。VSCode 主要用于编…

【C语言进阶】动态内存与柔性数组:C语言开发者必须知道的陷阱与技巧

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ ⏩收录专栏⏪&#xff1a;C语言 “ 登神长阶 ” &#x1f921;往期回顾&#x1f921;&#xff1a;C语言动态内存管理 &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀C语言动态内存管理 &…