C# 灰狼算法 旅行商问题
时间: 2023-11-22 15:50:05 浏览: 32
以下是使用C#实现灰狼算法解决旅行商问题的示例代码:
```csharp
using System;
using System.Collections.Generic;
namespace GreyWolfOptimizer
{
class Program
{
static void Main(string[] args)
{
// 城市坐标
double[,] cities = new double[,] { { 0, 0 }, { 1, 1 }, { 1, 2 }, { 2, 2 } };
// 城市数量
int n = cities.GetLength(0);
// 种群数量
int m = 10;
// 迭代次数
int maxIter = 100;
// 初始化种群
List<Wolf> wolves = new List<Wolf>();
for (int i = 0; i < m; i++)
{
wolves.Add(new Wolf(n));
}
// 迭代
for (int iter = 0; iter < maxIter; iter++)
{
// 更新每只狼的位置
foreach (Wolf wolf in wolves)
{
// 计算适应度
wolf.fitness = Fitness(wolf.position, cities);
// 更新α狼的位置
if (wolf.fitness < Alpha.fitness)
{
Alpha.position = wolf.position.Clone() as int[];
Alpha.fitness = wolf.fitness;
}
// 更新β狼的位置
if (wolf.fitness > Alpha.fitness && wolf.fitness < Beta.fitness)
{
Beta.position = wolf.position.Clone() as int[];
Beta.fitness = wolf.fitness;
}
// 更新δ狼的位置
if (wolf.fitness > Alpha.fitness && wolf.fitness > Beta.fitness && wolf.fitness < Delta.fitness)
{
Delta.position = wolf.position.Clone() as int[];
Delta.fitness = wolf.fitness;
}
}
// 更新每只狼的位置
foreach (Wolf wolf in wolves)
{
// 更新ω狼的位置
for (int i = 0; i < n; i++)
{
wolf.position[i] = (int)Math.Round((Alpha.position[i] + 2 * Beta.position[i] + 2 * Delta.position[i] + wolf.position[i]) / 6.0);
// 边界处理
if (wolf.position[i] < 0)
{
wolf.position[i] = 0;
}
if (wolf.position[i] >= n)
{
wolf.position[i] = n - 1;
}
}
}
}
// 输出结果
Console.WriteLine("最短路径长度为:" + Alpha.fitness);
Console.Write("最短路径为:");
for (int i = 0; i < n; i++)
{
Console.Write(Alpha.position[i] + " ");
}
Console.WriteLine();
}
// 适应度函数
static double Fitness(int[] position, double[,] cities)
{
double distance = 0;
for (int i = 0; i < position.Length - 1; i++)
{
distance += Math.Sqrt(Math.Pow(cities[position[i], 0] - cities[position[i + 1], 0], 2) + Math.Pow(cities[position[i], 1] - cities[position[i + 1], 1], 2));
}
distance += Math.Sqrt(Math.Pow(cities[position[position.Length - 1], 0] - cities[position[0], 0], 2) + Math.Pow(cities[position[position.Length - 1], 1] - cities[position[0], 1], 2));
return distance;
}
// α狼
static Wolf Alpha = new Wolf();
// β狼
static Wolf Beta = new Wolf();
// δ狼
static Wolf Delta = new Wolf();
// 狼
class Wolf
{
// 位置
public int[] position;
// 适应度
public double fitness;
// 构造函数
public Wolf(int n)
{
position = new int[n];
for (int i = 0; i < n; i++)
{
position[i] = i;
}
Shuffle(position);
fitness = Fitness(position, new double[,] { { 0, 0 } });
}
// 随机打乱数组
private void Shuffle(int[] array)
{
Random rand = new Random();
for (int i = array.Length - 1; i > 0; i--)
{
int j = rand.Next(i + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// 构造函数
public Wolf()
{
position = new int[] { };
fitness = double.MaxValue;
}
}
}
}
```
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)