我需要一本字典的替代品,因为需要复本。这样做的原因是:我需要搜索最佳路线。例如,为了做到这一点,我创建了一个有20个个体的种群。每个个体都有自己的路线,并且对每条路线都计算了适应度。为了能够根据适应度对路线进行排序,我创建了一个字典。现在我正在迭代几代,同时对字典进行排序,并在每次迭代中向其添加新的路由。但是,字典正在删除重复项,这不应该这样做,因为代码可能会多次抛出最佳路由。
我已经读过一些关于查找和链表的东西,但对此并不真正了解。或者可能是一个元组?有谁更了解什么可能会有帮助吗?
这是我的代码:这不是空洞代码,只是为了避免误解而显示字典。
List<List<Point3d>> currentGeneration = new List<List<Point3d>>(cGP.Count);
cGP.ForEach((item) => {currentGeneration.Add(new List<Point3d>(item));});
List<double> currentFitness = cGF.ToList();
Dictionary<List<Point3d>, double> dictionary = new Dictionary<List<Point3d>, double>();
foreach(List<Point3d> individual in currentGeneration)
{
foreach(double individualsFitness in currentFitness)
{
if(!dictionary.ContainsKey(individual))
{
if(!dictionary.ContainsValue(individualsFitness))
{
dictionary.Add(individual, individualsFitness);
}
}
}
}发布于 2016-10-08 00:31:17
我需要一个替代字典,因为需要重复的字典
参见Lookup。这本质上是一个允许重复的字典。
从文档中:
表示键的集合,每个键都映射到一个或多个值。
不同之处在于Dictionary将键映射到单个值,而Lookup将键映射到值的集合。
可以通过在实现IEnumerable的对象上调用ToLookup来创建Lookup实例。
发布于 2016-10-09 03:14:47
好了,我已经找到我要找的东西了。
List<KeyValuePair>不会删除重复项,并且它能够按值或键对位置进行排序,这两种方法都是可能的。
下面是我为像我这样的编程新手编写的代码:
List<List<Point3d>> handoverPopulation = createPopulation(pts, p);
List<double> handoverFitness = calculateFitness(handoverPopulation, p0);
List<KeyValuePair<List<Point3d>, double>> list = new List<KeyValuePair<List<Point3d>, double>>();
for(int i = 0; i < handoverFitness.Count; i++)
{
list.Add(new KeyValuePair<List<Point3d>, double>(handoverPopulation[i], handoverFitness[i]));
}
list.Sort((x, y) => x.Value.CompareTo(y.Value));Yeeyy happy :)
https://stackoverflow.com/questions/39921851
复制相似问题