C# 遗传算法求函数最大值
时间: 2023-12-01 20:42:11 浏览: 96
简单的c#遗传算法求函数最大值
5星 · 资源好评率100%
以下是使用C#实现遗传算法求解函数最大值的示例代码:
```csharp
using System;
namespace GeneticAlgorithm
{
class Program
{
static void Main(string[] args)
{
// 定义常量
const float CROSSOVER_RATE = 0.7F; // 交叉概率
const float MUTATION_RATE = 0.001F; // 变异概率
const int POP_SIZE = 100; // 种群大小
const int DELTA_LENGTH = 5; // 解小数点后的位数
const int X_LENGTH = DELTA_LENGTH + 2; // 整体长度
// 定义函数
float Function(float x)
{
return (float)(-1 * Math.Pow(x - 2,2) + 2);
}
// 定义个体类
class Individual
{
public float[] genes = new float[X_LENGTH];
public float fitness;
public Individual()
{
// 随机初始化基因
for (int i = 0; i < X_LENGTH; i++)
{
genes[i] = (float)(-1 + 3 * new Random(Guid.NewGuid().GetHashCode()).NextDouble());
}
}
// 计算适应度
public void CalculateFitness()
{
float x = genes[0] * 10 + genes[1];
float y = Function(x);
fitness = y;
}
// 交叉
public Individual Crossover(Individual partner)
{
Individual child = new Individual();
for (int i = 0; i < X_LENGTH; i++)
{
if (new Random(Guid.NewGuid().GetHashCode()).NextDouble() < CROSSOVER_RATE)
{
child.genes[i] = genes[i];
}
else
{
child.genes[i] = partner.genes[i];
}
}
return child;
}
// 变异
public void Mutate()
{
for (int i = 0; i < X_LENGTH; i++)
{
if (new Random(Guid.NewGuid().GetHashCode()).NextDouble() < MUTATION_RATE)
{
genes[i] = (float)(-1 + 3 * new Random(Guid.NewGuid().GetHashCode()).NextDouble());
}
}
}
}
// 定义种群类
class Population
{
public Individual[] individuals = new Individual[POP_SIZE];
public Population()
{
// 初始化种群
for (int i = 0; i < POP_SIZE; i++)
{
individuals[i] = new Individual();
}
}
// 计算种群中每个个体的适应度
public void CalculateFitness()
{
for (int i = 0; i < POP_SIZE; i++)
{
individuals[i].CalculateFitness();
}
}
// 选择
public Individual Selection()
{
float sumFitness = 0;
for (int i = 0; i < POP_SIZE; i++)
{
sumFitness += individuals[i].fitness;
}
float rand = (float)new Random(Guid.NewGuid().GetHashCode()).NextDouble() * sumFitness;
float tempSum = 0;
for (int i = 0; i < POP_SIZE; i++)
{
tempSum += individuals[i].fitness;
if (tempSum > rand)
{
return individuals[i];
}
}
return individuals[POP_SIZE - 1];
}
// 进化
public void Evolve()
{
Population newPopulation = new Population();
for (int i = 0; i < POP_SIZE; i++)
{
Individual parent1 = Selection();
Individual parent2 = Selection();
Individual child = parent1.Crossover(parent2);
child.Mutate();
newPopulation.individuals[i] = child;
}
individuals = newPopulation.individuals;
}
// 获取最优解
public Individual GetBestIndividual()
{
Individual bestIndividual = individuals[0];
for (int i = 1; i < POP_SIZE; i++)
{
if (individuals[i].fitness > bestIndividual.fitness)
{
bestIndividual = individuals[i];
}
}
return bestIndividual;
}
}
// 初始化种群
Population population = new Population();
// 进化
for (int i = 0; i < 1000; i++)
{
population.CalculateFitness();
population.Evolve();
}
// 获取最优解
Individual bestIndividual = population.GetBestIndividual();
float xBest = bestIndividual.genes[0] * 10 + bestIndividual.genes[1];
float yBest = Function(xBest);
// 输出结果
Console.WriteLine("x = " + xBest.ToString("F5") + ", y = " + yBest.ToString("F5"));
}
}
}
```
阅读全文