public NonameReturn(NfpKey key, NFP[] nfp) { this.key = key; this.nfp = nfp; }
/** * 一次迭代计算 * @param tree 板件列表(去掉了带孔的多边形内孔的点集) * @param binPolygon 底板 * @param config 设置 * @return */ public Result launchWorkers(List <NestPath> tree, NestPath binPolygon, Config config) { launchcount++; if (GA == null) { List <NestPath> adam = new List <NestPath>(); foreach (var nestPath in tree) { var clone = new NestPath(nestPath); adam.Add(clone); } foreach (NestPath nestPath in adam) { nestPath.area = GeometryUtil.polygonArea(nestPath); } //按零件的面积由大到小排序 adam.Sort((x, y) => x.area.CompareTo(y.area)); //Collections.sort(adam); GA = new GeneticAlgorithm(adam, binPolygon, config); } Individual individual = null; for (int i = 0; i < GA.population.Count; i++) { if (GA.population[i].getFitness() < 0) { individual = GA.population[i]; break; } } // if(individual == null ){ // GA.generation(); // individual = GA.population.get(1); // } if (launchcount > 1 && individual == null) { GA.generation(); individual = GA.population[1]; } // 以上为GA List <NestPath> placelist = individual.getPlacement(); List <double> rotations = individual.getRotation(); List <int> ids = new List <int>(); for (int i = 0; i < placelist.Count; i++) { ids.Add(placelist[i].getId()); placelist[i].setRotation(rotations[i]); } List <NfpPair> nfpPairs = new List <NfpPair>(); NfpKey key = null; /** * 如果在nfpCache里没找到nfpKey 则添加进nfpPairs */ for (int i = 0; i < placelist.Count; i++) { NestPath part = placelist[i]; //这个是零件和底板之间形成的nfp,所以inside这个参数为true key = new NfpKey(binPolygon.getId(), part.getId(), true, 0, part.getRotation()); if (!nfpCache.ContainsKey(serialize.Serialize(key))) { nfpPairs.Add(new NfpPair(binPolygon, part, key)); } else { } //这个是零件之间相互形成的nfp,所以inside这个参数为false for (int j = 0; j < i; j++) { NestPath placed = placelist[j]; NfpKey keyed = new NfpKey(placed.getId(), part.getId(), false, rotations[j], rotations[i]); nfpPairs.Add(new NfpPair(placed, part, keyed)); } } /** * 第一次nfpCache为空 ,nfpCache存的是nfpKey所对应的两个polygon所形成的Nfp( List<NestPath> ) */ List <ParallelData> generatedNfp = new List <ParallelData>(); foreach (NfpPair nfpPair in nfpPairs) { ParallelData dataTemp = NfpUtil.nfpGenerator(nfpPair, config); generatedNfp.Add(dataTemp); } for (int i = 0; i < generatedNfp.Count; i++) { ParallelData Nfp = generatedNfp[i]; //TODO remove gson & generate a new key algorithm String tkey = serialize.Serialize(Nfp.getKey()); //gson.toJson(Nfp.getKey()); if (!nfpCache.ContainsKey(tkey)) { nfpCache.Add(tkey, Nfp.value); } else { } } PlacementWorker worker = new PlacementWorker(binPolygon, config, nfpCache); List <NestPath> placeListSlice = new List <NestPath>(); for (int i = 0; i < placelist.Count; i++) { placeListSlice.Add(new NestPath(placelist[i])); } List <List <NestPath> > data = new List <List <NestPath> >(); data.Add(placeListSlice); List <Result> placements = new List <Result>(); for (int i = 0; i < data.Count; i++) { Result result = worker.placePaths(data[i]); placements.Add(result); } if (placements.Count == 0) { return(null); } individual.fitness = placements[0].fitness; Result bestResult = placements[0]; for (int i = 1; i < placements.Count; i++) { if (placements[i].fitness < bestResult.fitness) { bestResult = placements[i]; } } return(bestResult); }