public void LoadSolution() { System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog(); dialog.ShowDialog(); string pathToLoad = dialog.FileName; try { XmlSerializer serializer = new XmlSerializer(typeof(Solution)); FileStream filestream = new FileStream(pathToLoad, FileMode.Open, FileAccess.Read, FileShare.Read); Solution solution = (Solution)serializer.Deserialize(filestream); filestream.Close(); // before setting solution and benchmark, clear the screen // clear screen global.Verschnittoptimierung.display.Invalidate(); global.solution = solution; // also set global benchmark to the benchmark of the solution global.benchmark = solution.benchmark; // cl values ClassificationNumbers clNumbers = new ClassificationNumbers(global); clNumbers.GetAndShowAllClassificationNumbers(); // show solution Show show = new Show(global); show.ShowSolution(global.solution); System.Windows.Forms.MessageBox.Show("Solution was loaded successfully."); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Solution could not be loaded. Please make sure to select the correct file path."); } }
// creates a basic solution without rects placed, according to the benchmark public void CreateBasicSolution(Base global, Benchmark benchmark) { Solution solution = new Solution(); // set solution ID string path = Environment.CurrentDirectory; // without bin\Debug path = path.Substring(0, path.Length - 9) + "Resources\\SolutionNr.txt"; solution.SolutionID = Convert.ToInt32(System.IO.File.ReadAllText(path)); // set creationTime solution.creationTime = DateTime.Now; // set Benchmark to solution solution.benchmark = benchmark; solution.usedBenchmarkID = benchmark.benchmarkID; // add boards solution.BoardList = new List<Board>(); for (int i = 0; i < benchmark.boardList.Count; i++) { Board board = new Board(); board.boardID = i + 1; board.height = benchmark.boardList[0].height; board.width = benchmark.boardList[0].width; board.isCollectionBoard = false; board.size = board.height * board.width; solution.BoardList.Add(board); } // add collection board Board collectionBoard = new Board(); collectionBoard.boardID = solution.BoardList.Count + 1; collectionBoard.height = benchmark.boardList[0].height; collectionBoard.width = benchmark.boardList[0].width * 2; collectionBoard.isCollectionBoard = true; collectionBoard.size = collectionBoard.height * collectionBoard.width; solution.BoardList.Add(collectionBoard); // add rects to collection board, sorted from largest to smallest and height > width (change these parameters if necessary) // also remove the coordinates from the rects for(int i = 0; i < benchmark.boardList.Count; i++) { for(int j = 0; j < benchmark.boardList[i].RectList.Count; j++) { Rect rect = new Rect(); rect.rectID = benchmark.boardList[i].RectList[j].rectID; rect.size = benchmark.boardList[i].RectList[j].size; if(benchmark.boardList[i].RectList[j].height < benchmark.boardList[i].RectList[j].width) { rect.height = benchmark.boardList[i].RectList[j].width; rect.width = benchmark.boardList[i].RectList[j].height; } else { rect.height = benchmark.boardList[i].RectList[j].height; rect.width = benchmark.boardList[i].RectList[j].width; } rect.edgeLeftUp = new MyPoint(0,0); rect.edgeRightDown = new MyPoint(0,0); collectionBoard.RectList.Add(rect); } } // sort the rects in rect list from largest size to smallest size (new version; old version below) Tools tools = new Tools(); tools.QuickSortRectBySizeDec(0, collectionBoard.RectList.Count - 1, collectionBoard.RectList); // old version, new version = using quicksort /* // sort the rect list List<Rect> rectList = new List<Rect>(); // as long as rects exist while (collectionBoard.RectList.Count > 0) { // search the largest rect (by size), then add to rectList and remove from collectionBoard List Rect largestRect = collectionBoard.RectList[0]; for (int i = 1; i < collectionBoard.RectList.Count; i++) { if(collectionBoard.RectList[i].size > largestRect.size) { largestRect = collectionBoard.RectList[i]; } } rectList.Add(largestRect); collectionBoard.RectList.Remove(largestRect); } collectionBoard.RectList = rectList; */ // add the remaining parameters to solution solution.numberOfRects = collectionBoard.RectList.Count; solution.percentageFilledArea = 0; global.solution = solution; global.emptySolution = tools.CloneSolution(solution); // cl values ClassificationNumbers clNumbers = new ClassificationNumbers(global); clNumbers.GetAndShowAllClassificationNumbers(); }
private void button_useBestSolution_Click(object sender, EventArgs e) { Base global = Base.GetInstance(); if(global.runningProcess.existing == true) { global.Verschnittoptimierung.Output.Text = "Process running. Please finish this process first."; } else if(global.solution != null && global.bestSolution != null) { Show show = new Show(global); Tools tools = new Tools(); global.solution = tools.CloneSolution(global.bestSolution); show.ShowSolution(global.solution); ClassificationNumbers classificationNumbers = new ClassificationNumbers(global); classificationNumbers.GetAndShowAllClassificationNumbers(); button_useBestSolution.Enabled = false; } }
public Solution Greedy(Boolean evolution, Solution solutionEvo) { Base global = Base.GetInstance(); Solution solution; if(evolution) { solution = solutionEvo; } else { solution = global.solution; } ClassificationNumbers classificationNumbers = new ClassificationNumbers(global); // preparations if(!evolution) { global.runningProcess.state = 1; } Tools tools = new Tools(); // START one time: if (evolution || global.runningProcess.firstStep) { // next rect to pick of the list: the first one (default) if(!evolution) { global.runningProcess.nextStep = 0; } else { global.nextStepGreedyEvo = 0; } global.positionsManaged = new List<Position>(); global.positionsValid = new List<Position>(); if((global.Verschnittoptimierung.radioButton_largestSideInc.Checked && !evolution) || (evolution && global.selectedGreedy == 1) || (evolution && global.selectedGreedy == 2) || (evolution && global.selectedGreedy == 9) || (evolution && global.selectedGreedy == 10) ) { // sort rects from min largest side to max largest side tools.QuickSortRectByLargestSizeInc(0, solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1, solution.BoardList[solution.BoardList.Count - 1].RectList); } else if((global.Verschnittoptimierung.radioButton_largestSideDec.Checked && !evolution) || (evolution && global.selectedGreedy == 3) || (evolution && global.selectedGreedy == 4) || (evolution && global.selectedGreedy == 11) || (evolution && global.selectedGreedy == 12) ) { // sort rects from min largest side to max largest side tools.QuickSortRectByLargestSideDec(0, solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1, solution.BoardList[solution.BoardList.Count - 1].RectList); } else if((global.Verschnittoptimierung.radioButton_sizeInc.Checked) || (evolution && global.selectedGreedy == 5) || (evolution && global.selectedGreedy == 6) || (evolution && global.selectedGreedy == 13) || (evolution && global.selectedGreedy == 14) ) { // sort rects from min size to max size tools.QuickSortRectBySizeInc(0, solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1, solution.BoardList[solution.BoardList.Count - 1].RectList); } else if((global.Verschnittoptimierung.radioButton_sizeDec.Checked) || (evolution && global.selectedGreedy == 7) || (evolution && global.selectedGreedy == 8) || (evolution && global.selectedGreedy == 15) || (evolution && global.selectedGreedy == 16) ) { // sort rects from max size to min size tools.QuickSortRectBySizeDec(0, solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1, solution.BoardList[solution.BoardList.Count - 1].RectList); } if(!evolution) { global.runningProcess.firstStep = false; } } // END one time // for each rect on collectionBoard for (int i = (evolution ? global.nextStepGreedyEvo : global.runningProcess.nextStep); i < solution.BoardList[solution.BoardList.Count - 1].RectList.Count;) { if(!evolution) { global.runningProcess.state = 1; } // sort boards from max free space to least free space List<int> boardNrSorted = tools.SortBoardsBySize(solution.BoardList); // for each board try to place the rect for (int j = 0; j < boardNrSorted.Count - 1; j++) { // try place the rect // 1. is the space as area enough for the board? (without finding a specific place) // calculate boardSizeEffective int boardSizeEffective = solution.BoardList[boardNrSorted[j]].size; for (int k = 0; k < solution.BoardList[boardNrSorted[j]].RectList.Count; k++) { boardSizeEffective -= solution.BoardList[boardNrSorted[j]].RectList[k].size; } if (boardSizeEffective < solution.BoardList[solution.BoardList.Count - 1].RectList[i].size) { // rect is too large, cannot be placed on any of the boards. // has to remain on the collectionBoard if(!evolution) { classificationNumbers.GetAndShowAllClassificationNumbers(); } break; } // 2. try to place the rect in the selected board // clone the rect Rect rect = new Rect(); rect.edgeLeftUp = new MyPoint(); rect.edgeRightDown = new MyPoint(); rect.rectID = solution.BoardList[solution.BoardList.Count - 1].RectList[i].rectID; rect.size = solution.BoardList[solution.BoardList.Count - 1].RectList[i].size; rect.width = solution.BoardList[solution.BoardList.Count - 1].RectList[i].width; rect.height = solution.BoardList[solution.BoardList.Count - 1].RectList[i].height; /* rect.edgeLeftUp.x = solution.BoardList[solution.BoardList.Count - 1].RectList[i].edgeLeftUp.x; rect.edgeLeftUp.y = solution.BoardList[solution.BoardList.Count - 1].RectList[i].edgeLeftUp.y; rect.edgeRightDown.x = solution.BoardList[solution.BoardList.Count - 1].RectList[i].edgeRightDown.x; rect.edgeRightDown.y = solution.BoardList[solution.BoardList.Count - 1].RectList[i].edgeRightDown.y; */ // reset values in global global.positionsManaged = new List<Position>(); global.positionsValid = new List<Position>(); global.bestPositionSet = false; // TODO: this part(vertical, then horizontal / or only one if equal) should be moved to GetValidPos() // first vertical, then horizontal if (rect.width == rect.height) { rect.edgeLeftUp.x = 0; rect.edgeLeftUp.y = rect.height; rect.edgeRightDown.x = rect.width; rect.edgeRightDown.y = 0; GetValidPositions(solution.BoardList[boardNrSorted[j]].RectList, rect, evolution); } else { // 1. vertical if (rect.width > rect.height) { int helper = rect.width; rect.width = rect.height; rect.height = helper; } rect.edgeLeftUp.x = 0; rect.edgeLeftUp.y = rect.height; rect.edgeRightDown.x = rect.width; rect.edgeRightDown.y = 0; GetValidPositions(solution.BoardList[boardNrSorted[j]].RectList, rect, evolution); // 2. horizontal if (rect.height > rect.width) { int helper = rect.width; rect.width = rect.height; rect.height = helper; } rect.edgeLeftUp.x = 0; rect.edgeLeftUp.y = rect.height; rect.edgeRightDown.x = rect.width; rect.edgeRightDown.y = 0; GetValidPositions(solution.BoardList[boardNrSorted[j]].RectList, rect, evolution); } // select the best position of the valid ones SelectBestPosition(evolution); Boolean rectPlaced = false; if (global.bestPositionSet == true) { // 1. show valid positions // 2. show best position // 3. place best position rect.edgeLeftUp.x = global.bestPosition.edgeLeftUp.x; rect.edgeLeftUp.y = global.bestPosition.edgeLeftUp.y; rect.edgeRightDown.x = global.bestPosition.edgeRightDown.x; rect.edgeRightDown.y = global.bestPosition.edgeRightDown.y; solution.BoardList[boardNrSorted[j]].RectList.Add(rect); //solution.BoardList[solution.BoardList.Count - 1].RectList.Remove(rect); solution.BoardList[solution.BoardList.Count - 1].RectList.RemoveAt(i); // .... rectPlaced = true; // show can't be called from here if(!evolution) { // check for best solution and set if necessary tools.CheckForBestSolution(); // show solution Show show = new Show(global); show.ShowSolution(global.solution); } } // last rect tried? if (solution.BoardList[solution.BoardList.Count - 1].RectList.Count == 0 || (solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1) < (evolution ? global.nextStepGreedyEvo : global.runningProcess.nextStep)) { if(!evolution) { global.runningProcess.existing = false; global.runningProcess.state = 0; classificationNumbers.GetAndShowAllClassificationNumbers(); } break; } if (rectPlaced) { if(!evolution) { global.runningProcess.state = 0; classificationNumbers.GetAndShowAllClassificationNumbers(); } break; } if (!rectPlaced) { if(!evolution) { global.runningProcess.nextStep++; } else { global.nextStepGreedyEvo++; } i++; if ((solution.BoardList[solution.BoardList.Count - 1].RectList.Count - 1) < (evolution ? global.nextStepGreedyEvo : global.runningProcess.nextStep)) { if(!evolution) { global.runningProcess.existing = false; global.runningProcess.state = 0; classificationNumbers.GetAndShowAllClassificationNumbers(); } break; } } } if(!evolution && global.runningProcess.stepType == 0) { global.runningProcess.state = 0; classificationNumbers.GetAndShowAllClassificationNumbers(); break; } } if(!evolution) { global.runningProcess.state = 0; classificationNumbers.GetAndShowAllClassificationNumbers(); } return solution; }
// used after the first step and after each regular step public void EndStepBombingAlgorithm() { Base global = Base.GetInstance(); // set new population and delete the rest global.populationSmall = SelectBestXElements(); global.populationLarge = new List<PopulationElement>(); // set best population element of the new population global.bestPopulationElement = SelectBestElement(false); if (global.solution != global.bestPopulationElement.solution) { global.changeCounter = 0; } else { global.changeCounter++; } global.solution = global.bestPopulationElement.solution; // check for best solution and set if necessary Tools tools = new Tools(); tools.CheckForBestSolution(); ClassificationNumbers classificationNumbers = new ClassificationNumbers(global); classificationNumbers.GetAndShowAllClassificationNumbers(); Show show = new Show(global); show.ShowSolution(global.solution); // show/update fitness chart global.evolutionStep++; global.Verschnittoptimierung.fitnessChart.Series["best"].Points.AddXY(global.evolutionStep, global.populationSmall[0].fitnessValue); global.Verschnittoptimierung.fitnessChart.Series["worst"].Points.AddXY(global.evolutionStep, global.populationSmall[global.populationSmall.Count - 1].fitnessValue); // hide running process display global.Verschnittoptimierung.processRunning_gear.Visible = false; global.Verschnittoptimierung.processRunning_label.Visible = false; }
public void BombingAlgorithm() { Base global = Base.GetInstance(); ClassificationNumbers classificationNumbers = new ClassificationNumbers(global); if(global.runningProcess.firstStep) { global.changeCounter = 0; } // preparations global.runningProcess.state = 1; Tools tools = new Tools(); // display running process display global.Verschnittoptimierung.processRunning_gear.Visible = true; global.Verschnittoptimierung.processRunning_label.Visible = true; // change "true" to an abort requirement, for example "best solution better than 95%" // best solution = global.solution (is set after each step/evolutionary step) int rim = 20; try { rim = Convert.ToInt32(global.Verschnittoptimierung.evAlg_numberMaxIterations.Value); } catch(Exception ex) { rim = 20; } while (rim > 0 && global.changeCounter < 10) { // creating a basic population if (global.runningProcess.firstStep) { global.changeCounter = 0; global.evolutionStep = 0; tools.CleanFitnessChart(); global.populationSmall = new List<PopulationElement>(); List<int> greediesForRand = new List<int>(); if (!global.tournamentPopulation) { greediesForRand = tools.CloneList(global.chosenGreedies); } else { greediesForRand = GetTournamentGreedies(global.mue); } // set tournament methods too if(global.tournamentGreediesOnly) { global.tournamentGreedyMethods = new List<int>(); int number = (global.mue > global.multForLambda) ? global.multForLambda : global.multForLambda; global.tournamentGreedyMethods = GetTournamentGreedies(number); } for(int i = 0; i < global.mue; i++) { PopulationElement element = new PopulationElement(); Solution solution = tools.CloneSolution(global.emptySolution); // select random greedy from the selectedGreedies and set its identifier, i.e. "1" for greedy1, in global int selectedGreedyPosition = global.random.Next(0, greediesForRand.Count); int selectedGreedy = greediesForRand[selectedGreedyPosition]; greediesForRand.RemoveAt(selectedGreedyPosition); global.selectedGreedy = selectedGreedy; // execute the greedy Fill fill = new Fill(); element.solution = fill.Greedy(true, solution); element.fitnessValue = tools.CalculateFitness(element.solution); global.populationSmall.Add(element); } EndStepBombingAlgorithm(); global.runningProcess.firstStep = false; if (global.runningProcess.stepType == 0) { global.runningProcess.state = 0; break; } } // creating a new population (lambda) for(int i = 0; i < global.populationSmall.Count; i ++) { // clone the population element's solution Solution newSolutionBase = tools.CloneSolution(global.populationSmall[i].solution); // create a ranking of the boards of this solution List<int> rankedBoards = CreateBoardRanking(newSolutionBase); // remove rects according to the mutation rate float mutationRate = global.mutationRate; while(mutationRate >= 1) { // remove all rects from the worst board and add to collectionBoard // while worst board isn't empty while(newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.Count > 0) { Rect rectToRemove = newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList[0]; newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.RemoveAt(0); newSolutionBase.BoardList[newSolutionBase.BoardList.Count - 1].RectList.Add(rectToRemove); } // remove board which was emptied from the ranked boards (so that the new last element is the weakest board again) rankedBoards.RemoveAt(rankedBoards.Count - 1); mutationRate -= 1; } if(mutationRate > 0) { // calculate how many have to be removed int nrRectsOnBoard = newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.Count; int numbersToRemove = Convert.ToInt32(Math.Floor(nrRectsOnBoard * mutationRate)); // remove the rects left to remove (starting from the last added rect) while(numbersToRemove > 0) { Rect rectToRemove = newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList[ newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.Count - 1]; newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.RemoveAt( newSolutionBase.BoardList[rankedBoards[rankedBoards.Count - 1]].RectList.Count - 1); newSolutionBase.BoardList[newSolutionBase.BoardList.Count - 1].RectList.Add(rectToRemove); numbersToRemove--; } } // fill with a random greedy from the selectedGreedies list // and add to large population for (int j = 0; j < global.multForLambda; j++) { Solution newSolution = tools.CloneSolution(newSolutionBase); List<int> greediesForRand = new List<int>(); if (!global.tournamentGreediesOnly) { greediesForRand = tools.CloneList(global.chosenGreedies); } else { greediesForRand = tools.CloneList(global.tournamentGreedyMethods); } // select random greedy from the selectedGreedies and set its identifier, i.e. "1" for greedy1, in global int selectedGreedyPosition = global.random.Next(0, greediesForRand.Count); int selectedGreedy = greediesForRand[selectedGreedyPosition]; greediesForRand.RemoveAt(selectedGreedyPosition); global.selectedGreedy = selectedGreedy; // execute the greedy PopulationElement element = new PopulationElement(); Fill fill = new Fill(); element.solution = fill.Greedy(true, newSolution); element.fitnessValue = tools.CalculateFitness(element.solution); global.populationLarge.Add(element); } } // old (small) population still existing, new (large) population (lambda) created // -> end step if singleStep EndStepBombingAlgorithm(); if(global.runningProcess.stepType == 0) { global.runningProcess.state = 0; rim--; break; } rim--; } if (global.changeCounter == 10 || rim == 0) { global.runningProcess.existing = false; global.mue = 0; global.lambda = 0; } }
public void Reset() { Base global = Base.GetInstance(); // reset all rects for(int i=0; i<global.solution.BoardList.Count -1; i++) { while(global.solution.BoardList[i].RectList.Count != 0) { global.solution.BoardList[global.solution.BoardList.Count - 1].RectList.Add(global.solution.BoardList[i].RectList[0]); global.solution.BoardList[i].RectList.RemoveAt(0); } } // reset running process/fill values global.runningProcess.existing = false; global.positionsManaged = new List<Position>(); global.positionsValid = new List<Position>(); global.bestPositionSet = false; Show show = new Show(global); show.ShowSolution(global.solution); global.mue = 0; global.lambda = 0; global.Verschnittoptimierung.fitnessValue.Text = ""; ClassificationNumbers classificationNumbers = new ClassificationNumbers(global); classificationNumbers.GetAndShowAllClassificationNumbers(); UnlockFillButtons(); UnlockEvAlgButtons(); Tools tools = new Tools(); tools.CleanFitnessChart(); global.Verschnittoptimierung.processRunning_gear.Visible = false; global.Verschnittoptimierung.processRunning_label.Visible = false; }