c# 多容器 二维矩形优化排版 遗传算法代码
时间: 2023-09-06 12:07:07 浏览: 133
以下是一个简单的C#遗传算法实现多容器二维矩形优化排版的示例代码:
```csharp
using System;
using System.Collections.Generic;
namespace GeneticAlgorithm
{
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public Rectangle(int width, int height)
{
Width = width;
Height = height;
}
}
class Container
{
public int Width { get; set; }
public int Height { get; set; }
public List<Rectangle> Rectangles { get; set; }
public Container(int width, int height)
{
Width = width;
Height = height;
Rectangles = new List<Rectangle>();
}
}
class Individual
{
public List<Container> Containers { get; set; }
public int Fitness { get; set; }
public Individual(List<Container> containers)
{
Containers = containers;
Fitness = CalculateFitness();
}
private int CalculateFitness()
{
// 计算适应度函数的值
int fitness = 0;
foreach (var container in Containers)
{
int maxHeight = 0;
foreach (var rectangle in container.Rectangles)
{
maxHeight = Math.Max(maxHeight, rectangle.Height);
}
fitness += maxHeight;
}
return fitness;
}
}
class GeneticAlgorithm
{
private Random random;
private int populationSize;
private double crossoverProbability;
private double mutationProbability;
private int maxGenerations;
private List<Container> containers;
private List<Individual> population;
private int currentGeneration;
public GeneticAlgorithm(int populationSize, double crossoverProbability, double mutationProbability, int maxGenerations, List<Container> containers)
{
random = new Random();
this.populationSize = populationSize;
this.crossoverProbability = crossoverProbability;
this.mutationProbability = mutationProbability;
this.maxGenerations = maxGenerations;
this.containers = containers;
population = new List<Individual>();
currentGeneration = 0;
}
private void InitializePopulation()
{
// 初始化种群
for (int i = 0; i < populationSize; i++)
{
List<Container> containersCopy = new List<Container>();
foreach (var container in containers)
{
Container containerCopy = new Container(container.Width, container.Height);
containersCopy.Add(containerCopy);
}
Individual individual = new Individual(containersCopy);
population.Add(individual);
}
}
private Individual SelectParent()
{
// 选择父代个体
int index1 = random.Next(populationSize);
int index2 = random.Next(populationSize);
return population[index1].Fitness < population[index2].Fitness ? population[index1] : population[index2];
}
private void Crossover(Individual parent1, Individual parent2, out Individual child1, out Individual child2)
{
// 交叉操作
child1 = new Individual(new List<Container>(parent1.Containers));
child2 = new Individual(new List<Container>(parent2.Containers));
if (random.NextDouble() < crossoverProbability)
{
int containerIndex = random.Next(containers.Count);
Container container1 = child1.Containers[containerIndex];
Container container2 = child2.Containers[containerIndex];
for (int i = 0; i < container1.Rectangles.Count; i++)
{
if (random.NextDouble() < 0.5)
{
Rectangle rectangle = container1.Rectangles[i];
container1.Rectangles.RemoveAt(i);
container2.Rectangles.Add(rectangle);
}
}
}
}
private void Mutate(Individual individual)
{
// 变异操作
if (random.NextDouble() < mutationProbability)
{
int containerIndex = random.Next(containers.Count);
Container container = individual.Containers[containerIndex];
int rectangleIndex = random.Next(container.Rectangles.Count);
Rectangle rectangle = container.Rectangles[rectangleIndex];
int newX = random.Next(container.Width - rectangle.Width);
int newY = random.Next(container.Height - rectangle.Height);
rectangle.Width = newX;
rectangle.Height = newY;
}
}
private void Reproduce()
{
// 繁殖操作,生成新一代
List<Individual> newPopulation = new List<Individual>();
while (newPopulation.Count < populationSize)
{
Individual parent1 = SelectParent();
Individual parent2 = SelectParent();
Individual child1, child2;
Crossover(parent1, parent2, out child1, out child2);
Mutate(child1);
Mutate(child2);
newPopulation.Add(child1);
newPopulation.Add(child2);
}
population = newPopulation;
}
private Individual GetBestIndividual()
{
// 获取适应度函数值最小的个体
Individual bestIndividual = population[0];
foreach (var individual in population)
{
if (individual.Fitness < bestIndividual.Fitness)
{
bestIndividual = individual;
}
}
return bestIndividual;
}
public Individual Run()
{
InitializePopulation();
while (currentGeneration < maxGenerations)
{
Reproduce();
currentGeneration++;
}
return GetBestIndividual();
}
}
class Program
{
static void Main(string[] args)
{
// 创建容器
Container container1 = new Container(100, 100);
Container container2 = new Container(200, 200);
Container container3 = new Container(300, 300);
// 创建矩形
Rectangle rectangle1 = new Rectangle(50, 50);
Rectangle rectangle2 = new Rectangle(100, 100);
Rectangle rectangle3 = new Rectangle(150, 150);
// 将矩形添加到容器中
container1.Rectangles.Add(rectangle1);
container1.Rectangles.Add(rectangle2);
container2.Rectangles.Add(rectangle1);
container2.Rectangles.Add(rectangle2);
container2.Rectangles.Add(rectangle3);
container3.Rectangles.Add(rectangle1);
container3.Rectangles.Add(rectangle2);
container3.Rectangles.Add(rectangle3);
// 创建容器列表
List<Container> containers = new List<Container>();
containers.Add(container1);
containers.Add(container2);
containers.Add(container3);
// 运行遗传算法
GeneticAlgorithm ga = new GeneticAlgorithm(100, 0.8, 0.1, 1000, containers);
Individual bestIndividual = ga.Run();
// 输出结果
Console.WriteLine("最优解:");
foreach (var container in bestIndividual.Containers)
{
Console.WriteLine($"容器({container.Width}, {container.Height}):");
foreach (var rectangle in container.Rectangles)
{
Console.WriteLine($"矩形({rectangle.Width}, {rectangle.Height})");
}
}
Console.WriteLine($"适应度函数值:{bestIndividual.Fitness}");
Console.ReadLine();
}
}
}
```
在上面的示例代码中,我们定义了三个类:`Rectangle`、`Container`和`Individual`。`Rectangle`表示矩形,包含宽度和高度两个属性。`Container`表示容器,包含宽度、高度和矩形列表三个属性。`Individual`表示个体,包含容器列表和适应度函数值两个属性。
在`GeneticAlgorithm`类中,我们定义了遗传算法的各种操作,包括初始化种群、选择父代个体、交叉操作、变异操作、繁殖操作和获取适应度函数值最小的个体等。在`Main`函数中,我们首先创建了三个容器和三个矩形,并将矩形添加到容器中。然后,我们将容器列表传入遗传算法中,并指定种群大小、交叉概率、变异概率和最大迭代次数等参数。最后,我们运行遗传算法,并输出结果。
请注意,这只是一个简单的示例代码,需要根据具体情况进行修改和优化。
阅读全文