c# - copy items from a list on one condition -
i trying copy items list list on 1 condition. have 3 lists. first list contains example 10 lists of points, second list contains total distance (cost or fitness) of each list (10 lists -> 10 total distances).
here picture: first list contains 10 lists (each list contains points) - second list 'fitness' third list empty , should filled items on 1 condition. first added values in second list. example numbers above: totalfitness = 4847 + 5153 + 5577 + 5324...
the condition add list of points out of first list third list is: example ----------> (fitness[0] / totalfitness) <= ratio.
but not working, here can see code tried:
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 = 0; < firstfitness.count; i++) { double selected = firstfitness[i] / totalfitness; if(selected < ratio) { currentgeneration.add(firstgeneration[i]); } } return currentgeneration; } }
the third list still empty. if change condition to: if(selected <= ratio)
whole list of points in first list being copied third list. want copy is: list of points has 'best' fitness.
what i'm doing wrong? have absolutely no clue , tried few changes, still not working. appreciate if can consider i'm beginner.
i found solution problem.
i still have these data:
list1:
- listofpoints = a
- listofpoints = b
- listofpoints = c
- listofpoints = d
list2:
- fitness of a
- fitness of b
- fitness of c
- fitness of d
what wanted achieve was: take listofpoints, has best fitness , put them list3. rest listofpoints, put them list4.
this solution thought of: put list1 keys , list2 values dictionary , sort via linq. transfer sorted keys list3. for- loop put first half of sorted list list4 , second half list5.
here code:
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 = 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 = 0; < p; i++) {newgeneration.add(currentgenerationsorted[i]);} for(int j = p; j < currentgenerationsorted.count; j++) {newgenerationextra.add(currentgenerationsorted[j]);}
hope helps others face same problem.
Comments
Post a Comment