public void ExecuteStep(int stepType) { Base global = Base.GetInstance(); Show show = new Show(global); Tools tools = new Tools(); global.Verschnittoptimierung.Output.Text = ""; // for testing only if (global.Verschnittoptimierung.comboBox1.Text.Equals("Create Board(s) + Objects")) { } // reset displayed values if (global.runningProcess.existing == false) { global.Verschnittoptimierung.cl_evolutionMue.Text = ""; global.Verschnittoptimierung.cl_evolutionLambda.Text = ""; } switch (global.Verschnittoptimierung.comboBox1.Text) { // old, not existing anymore. Objects+board created when creating benchmark, including an empty solution case "Create Board(s) + Objects": // get board values from user interface float generalBoardHeight = (float)(global.Verschnittoptimierung.boardHeight.Value); float generalBoardWidth = (float)(global.Verschnittoptimierung.boardWidth.Value); if(generalBoardHeight >= generalBoardWidth) { float h = generalBoardWidth; generalBoardWidth = generalBoardHeight; generalBoardHeight = h; } // set global board values in Base/global global.generalBoardHeight = generalBoardHeight; global.generalBoardWidth = generalBoardWidth; CalculateMult(); // global.boardGap = Convert.ToInt32(0.1 * global.generalBoardHeight); // create and draw board(s) Creation creation = new Creation(); creation.CreateBoards(); DrawBoards(); break; case "Create Benchmark": // 1. step: verify benchmark information if(global.Verschnittoptimierung.boardHeight.Value <= global.Verschnittoptimierung.boardWidth.Value && global.Verschnittoptimierung.objectsMinNumber.Value <= global.Verschnittoptimierung.objectsMaxNumber.Value) { // before creating benchmark clear the screen // clear screen global.Verschnittoptimierung.display.Invalidate(); global.bestSolution = null; global.Verschnittoptimierung.button_useBestSolution.Enabled = false; // create benchmark Benchmark benchmark = new Benchmark(); //benchmark.BoardList = new List<Board>(); BenchmarkManagement benchmarkManagement = new BenchmarkManagement(); benchmarkManagement.CreateBenchmark(global, benchmark); benchmarkManagement.CreateRects(Convert.ToInt32(global.Verschnittoptimierung.objectsMinNumber.Value) , Convert.ToInt32(global.Verschnittoptimierung.objectsMaxNumber.Value), benchmark); global.benchmark = benchmark; // info: benchmark (boards with rects that fit exactly) created // also create a basic solution SolutionManagement solutionManagement = new SolutionManagement(); solutionManagement.CreateBasicSolution(global, global.benchmark); show.ShowBenchmark(global.benchmark); } else { // not enough information entered or too much information entered System.Windows.Forms.MessageBox.Show("Not enough information specified or wrong information. Check the min, max fields."); /* FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult result = fbd.ShowDialog(); string[] files = Directory.GetFiles(fbd.SelectedPath); System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message"); */ } break; case "Fill": // lock fill radio buttons tools.LockFillButtons(); // get type from what was entered int type = 0; // check if no process exists, create one if(global.runningProcess.existing == false) { global.runningProcess.type = 0; global.runningProcess.existing = true; global.runningProcess.state = 0; // 0 = single step, 1 = all remaining steps global.runningProcess.stepType = stepType; global.runningProcess.firstStep = true; /* // background worker global.Verschnittoptimierung.backgroundWorker1.RunWorkerAsync(); */ } // if a process exists, but of another process type else if(global.runningProcess.existing == true && global.runningProcess.type != type) { global.Verschnittoptimierung.Output.Text = "Another process is already running. Please complete this process first."; break; } // if a process exists of the same type else if(global.runningProcess.existing == true && global.runningProcess.type == type) { // check if the process is running or waiting // if waiting, do another step or all steps if(global.runningProcess.state == 0) { // set params and reactivate process // single step or all steps global.runningProcess.stepType = stepType; /* // process makes the next step or all remaining steps, depending on stepType global.runningProcess.autoResetEvent.Set(); */ } // if running else if(global.runningProcess.state == 1) { global.Verschnittoptimierung.Output.Text = "The process is already running. Please wait."; break; } } // check if a valid solution + benchmark exist if(global.solution != null && global.benchmark != null && global.solution.benchmark != null && global.solution.BoardList != null && global.solution.BoardList.Count > 1) { show.ShowSolution(global.solution); int selection = 1; // greedy 1 if(selection == 1) { Fill fill = new Fill(); if(global.Verschnittoptimierung.radioButton_BestFit.Checked) { fill.Greedy(false, new Solution()); } if(global.Verschnittoptimierung.radioButton_FirstFit.Checked) { global.Verschnittoptimierung.Output.Text = "@info: \"First Fit\" not implemented"; global.runningProcess.existing = false; } } } else { global.Verschnittoptimierung.Output.Text = "At least one global element is null. Cannot fill."; tools.UnlockFillButtons(); } // unlock fill radio buttons if(global.runningProcess.existing == false) { tools.UnlockFillButtons(); } break; case "Evolutionary Algorithm": // lock EvAlg entry tools.LockEvAlgButtons(); // set entered evAlg values global.mue = Convert.ToInt32(global.Verschnittoptimierung.evAlg_mue.Value); global.multForLambda = Convert.ToInt32(global.Verschnittoptimierung.evAlg_mult.Value); global.lambda = global.mue * global.multForLambda; global.mutationRate = (float)global.Verschnittoptimierung.evAlg_mutationRate.Value; tools.SaveSelectedGreedies(); global.tournamentPopulation = global.Verschnittoptimierung.checkBox_greedyTournamentPopulation.Checked; global.tournamentGreediesOnly = global.Verschnittoptimierung.checkBox_greedyTournamentProceduresOnly.Checked; // 1. verification // check if a valid solution + benchmark exist if (global.solution != null && global.benchmark != null && global.solution.benchmark != null && global.solution.BoardList != null && global.solution.BoardList.Count > 1) { // check if 'emptySolution' exists (the unchanged basic solution) if (global.emptySolution == null) { global.Verschnittoptimierung.Output.Text = "global.emptySolution is null"; break; } // check parameters entered at evolutionary algorithm int numberGreedies = tools.GetNumberSelectedGreedies(); if ((!(numberGreedies > 2) || (numberGreedies < global.mue) || numberGreedies < global.multForLambda) && !(global.tournamentPopulation && global.tournamentGreediesOnly)) { global.Verschnittoptimierung.Output.Text = "You need to select more greedy procedures."; tools.UnlockEvAlgButtons(); break; } if (global.mutationRate > (global.emptySolution.BoardList.Count - 1)) { global.Verschnittoptimierung.Output.Text = "The mutation rate cannot be larger than the number of boards."; tools.UnlockEvAlgButtons(); break; } // check if no process exists, create one if (global.runningProcess.existing == false) { global.runningProcess.type = 1; global.runningProcess.existing = true; global.runningProcess.state = 0; // 0 = single step, 1 = all remaining steps global.runningProcess.stepType = stepType; global.runningProcess.firstStep = true; // set display values global.Verschnittoptimierung.cl_evolutionMue.Text = global.mue.ToString(); global.Verschnittoptimierung.cl_evolutionLambda.Text = global.lambda.ToString(); } // if a process exists, but of another process type else if (global.runningProcess.existing == true && global.runningProcess.type != 1) { global.Verschnittoptimierung.Output.Text = "Another process is already running. Please complete this process first."; break; } // if a process exists of the same type else if (global.runningProcess.existing == true && global.runningProcess.type == 1) { // check if the process is running or waiting // if waiting, do another step or all steps if (global.runningProcess.state == 0) { // set params and reactivate process // single step or all steps global.runningProcess.stepType = stepType; } // if running else if (global.runningProcess.state == 1) { global.Verschnittoptimierung.Output.Text = "The process is already running. Please wait."; break; } } // 2. execute EvolutionaryAlgorithm evolutionaryAlgorithm = new EvolutionaryAlgorithm(); evolutionaryAlgorithm.BombingAlgorithm(); } else { global.Verschnittoptimierung.Output.Text = "At least one global element is null. Cannot use EvAlg."; tools.UnlockEvAlgButtons(); } // unlock EvAlg entry if (global.runningProcess.existing == false) { tools.UnlockEvAlgButtons(); } break; default: break; } /* //global.Verschnittoptimierung.display.; using (Graphics g = global.Verschnittoptimierung.display.CreateGraphics()) { using (Pen pen = new Pen(Color.Black, 2)) { Brush brush = new SolidBrush(Color.DarkBlue); g.TranslateTransform(global.Verschnittoptimierung.display.AutoScrollPosition.X, global.Verschnittoptimierung.display.AutoScrollPosition.Y); g.DrawRectangle(pen, 100, 100, 100, 200); } } */ }
// gets the best 'number's of tournament greedies // number has to be smaller than 16 or 16 (no effect) public List<int> GetTournamentGreedies(int number) { Base global = Base.GetInstance(); List <SingleTournamentResult> singleTournamentResults = new List<SingleTournamentResult>(); Tools tools = new Tools(); for(int i = 1; i <= 16; i++) { global.selectedGreedy = i; SingleTournamentResult singleTournamentResult = new SingleTournamentResult(); singleTournamentResult.greedyNr = i; Fill fill = new Fill(); Solution solutionEmpty = tools.CloneSolution(global.emptySolution); Solution solution = fill.Greedy(true, solutionEmpty); int fitnessValue = tools.CalculateFitness(solution); singleTournamentResult.fitnessValue = fitnessValue; singleTournamentResults.Add(singleTournamentResult); } global.selectedGreedy = 0; List<int> winners = new List<int>(); for(int i = 0; i < number; i++) { int bestIndex = 0; int bestGreedyNr = singleTournamentResults[0].greedyNr; int bestFitnessValue = singleTournamentResults[0].fitnessValue; for(int j = 0; j < singleTournamentResults.Count; j++) { if(singleTournamentResults[j].fitnessValue < bestFitnessValue) { bestIndex = j; bestGreedyNr = singleTournamentResults[j].greedyNr; bestFitnessValue = singleTournamentResults[j].fitnessValue; } } winners.Add(bestGreedyNr); singleTournamentResults.RemoveAt(bestIndex); } return winners; }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { //this.Output.Text = "test1"; //test123(); Fill fill = new Fill(); fill.Greedy(false, new Solution()); }
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; } }