2016-08-23 12 views
0

한 조건에서 목록의 항목을 다른 목록으로 복사하려고합니다. 세 가지 목록이 있습니다. 첫 번째 목록에는 예를 들어 10 개의 점 목록이 포함되고 두 번째 목록에는 각 목록의 총 거리 (비용 또는 적합성)가 포함됩니다 (10 개의 목록 -> 10 개의 총 거리). 여기에 사진 한 조건의 목록에서 항목 복사

: - 두 번째 목록 '피트니스' enter image description here 세 번째 목록이 비어 있고 하나 개의 조건의 항목으로 채워 져야 첫 번째 목록 10 개 목록 (각 목록 포인트를 포함)가 포함되어 있습니다. 먼저 두 번째 목록에 모든 값을 더했습니다. = 5324 + 5153 + 5577 + 4847 ...

제 3 목록 제리스트 지적 목록을 추가 조건 totalFitness : 위 숫자 예 예 ------ ----> (피트니스 [0]/totalFitness) < = 비율.

가 작동하지 않습니다하지만, 여기 당신은 내가 노력 코드를 볼 수 있습니다

class RunGA 
{ 
    public static List<List<Point3d>> createGenerations(List<List<Point3d>> firstGeneration, List<int> firstFitness, int generationSize) 
    { 
    List<List<Point3d>> currentGeneration = new List<List<Point3d>>(); 

    int totalFitness; 
    int actualFitness; 
    totalFitness = firstFitness[0] + firstFitness[1]; 
    double ratio = 1/10; 

    for(int k = 2; k < firstFitness.Count; k++) 
    { 
    actualFitness = firstFitness[k]; 
    totalFitness += actualFitness; 
    } 

    for(int i = 0; i < firstFitness.Count; i++) 
    { 
    double selected = firstFitness[i]/totalFitness; 
    if(selected < ratio) 
    { 
    currentGeneration.Add(firstGeneration[i]); 
    } 
    } 
    return currentGeneration; 
    } 
} 

세 번째 목록은 여전히 ​​비어 있습니다. 조건을 if(selected <= ratio) 으로 변경하면 첫 번째 목록의 전체 지점 목록이 세 번째 목록에 복사됩니다. 그러나 내가 복사하고자하는 것은 : '최고의'적합성을 가진 점수의 목록입니다.

내가 잘못하고있는 것은 무엇입니까? 나는 단서가없고 이미 몇 가지 변화를 시도했지만 아직 작동하지 않습니다. 내가 초보자라고 생각하면 고맙겠습니다.

+1

디버거를 사용하여 코드를 단계별로 실행하는 방법을 배워야 할 수도 있습니다. 좋은 디버거를 사용하면 한 줄씩 프로그램을 실행하고 예상 한 곳에서 벗어난 곳을 볼 수 있습니다. 프로그래밍을 할 때 필수적인 도구입니다. 추가 읽기 : ** [작은 프로그램을 디버깅하는 방법] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ** –

+2

LINQ Union, Where, Sum, Aggregate methods 확실히 이것을 달성하는 데 도움이 될 것입니다. – Aybe

답변

0

이 문제에 대한 다른 해결책을 찾았습니다.

목록 1 :

  1. ListOfPoints는
  2. ListOfPoints = B
  3. ListOfPoints = C를
  4. ListOfPoints = D

목록 2를 =

는 여전히 이들 데이터를 :

최고의 피트니스을 가지고 넣어 그 ListOfPoints을 가지고 : I이었다 달성하고 싶었다 무엇 D

의 C의 B의

  • 피트니스의 16,
    1. 피트니스는
    2. 피트니스
    3. 피트니스 List3에 추가하십시오. 나머지 모든 ListOfPoints를 다른 List4에 놓습니다.

      이것은 내가 생각한 해결책입니다. List1을 키로, List2를 값으로 사전에 넣고 LINQ를 통해 정렬합니다. 이제 정렬 된 키를 List3으로 전송하십시오. for-Loop을 사용하면 정렬 된 List의 전반부를 List4에 배치하고 후반부를 List5에 배치합니다.이 같은 문제에 직면 다른 사람을 도움이

      List<List<Point3d>> currentGeneration = handoverPopulation.ToList(); 
      List<double> currentFitness = handoverFitness.ToList(); 
      Dictionary<List<Point3d>, double> dict = new Dictionary<List<Point3d>, double>(); 
      foreach(List<Point3d> key in currentGeneration) 
      { 
          foreach(double valuee in currentFitness) 
          { 
          if(!dict.ContainsKey(key)) 
          { 
           if(!dict.ContainsValue(valuee)) 
           {dict.Add(key, valuee);} 
          } 
          } 
      } 
      var item = from pair in dict orderby pair.Value ascending select pair; 
      List<List<Point3d>> currentGenerationSorted = new List<List<Point3d>>(); 
      currentGenerationSorted = item.Select(kvp => kvp.Key).ToList(); 
      
      List<List<Point3d>> newGeneration = new List<List<Point3d>>(); 
      List<List<Point3d>> newGenerationExtra = new List<List<Point3d>>(); 
      
      int p = currentGenerationSorted.Count/2; 
      for(int i = 0; i < p; i++) 
      {newGeneration.Add(currentGenerationSorted[i]);} 
      
      for(int j = p; j < currentGenerationSorted.Count; j++) 
      {newGenerationExtra.Add(currentGenerationSorted[j]);} 
      

      희망 :

      여기 내 코드입니다.