public void CreateBenchmark(Base global, Benchmark benchmark)
        {
            // set benchmark ID
            string path = Environment.CurrentDirectory;
            // without bin\Debug
            path = path.Substring(0, path.Length - 9) + "Resources\\BenchmarkNr.txt";
            // global.Verschnittoptimierung.Output.Text = path;
            benchmark.benchmarkID = Convert.ToInt32(System.IO.File.ReadAllText(path));

            // set creationTime
            benchmark.creationTime = DateTime.Now;
            // add boards to benchmark
            for (int i = 0; i < global.Verschnittoptimierung.numberBoards.Value; i++)
            {
                Board board = new Board();
                board.boardID = i + 1;
                board.height = Convert.ToInt32(global.Verschnittoptimierung.boardHeight.Value);
                board.width = Convert.ToInt32(global.Verschnittoptimierung.boardWidth.Value);
                board.isCollectionBoard = false;
                board.size = board.height * board.width;

                benchmark.boardList.Add(board);
            }
            // no collection board in benchmark. code can still be useful for creating a solution
            /*
            // add collection board to benchmark
            Board collectionBoard = new Board();
            collectionBoard.boardID = BoardList.Count() + 1;
            collectionBoard.height = Convert.ToInt32(global.Verschnittoptimierung.boardHeight.Value);
            collectionBoard.width = Convert.ToInt32(global.Verschnittoptimierung.boardWidth.Value);
            collectionBoard.isCollectionBoard = false;
            collectionBoard.size = collectionBoard.height * collectionBoard.width;
            BoardList.Add(collectionBoard);
            */
        }
예제 #2
0
 public void ShowBenchmark(Benchmark benchmark)
 {
     ShowBoards(benchmark.boardList, true);
     ShowRects(benchmark.boardList);
     // set benchmark as contentToShow so it is shown everytime the display changes
     global.contentToShow = "benchmark";
     global.Verschnittoptimierung.buttonSelectView.Text = "Benchmark";
     global.Verschnittoptimierung.buttonSelectView.Enabled = true;
     global.Verschnittoptimierung.buttonSelectView.BackColor = Color.CornflowerBlue;
 }
        public void CreateRects(int minRects, int maxRects, Benchmark benchmark)
        {
            int rectID = 1;
            Random random = new Random();
            for (int i = 0; i < benchmark.boardList.Count; i++)
            {
                int numberOfRequiredRects = random.Next(minRects, maxRects + 1);

                // "convert" the board to a rect, meaning adding a rect to the board with the size of the board
                Rect rect = new Rect();
                rect.height = benchmark.boardList[i].height;
                rect.width = benchmark.boardList[i].width;
                rect.size = rect.height * rect.width;
                rect.edgeLeftUp = new MyPoint(0, rect.height);
                rect.edgeRightDown = new MyPoint(rect.width, 0);
                benchmark.boardList[i].RectList.Add(rect);

                // split the largest rect of the rectList until number of rects = required number of rects
                for (int j = 1; j < numberOfRequiredRects; j++)
                {
                    Rect largestRect = new Rect();
                    Rect rectOne = new Rect();
                    Rect rectTwo = new Rect();

                    Boolean nullCoordsOccured = true;
                    while(nullCoordsOccured == true)
                    {
                        // 1. find largest rect
                        largestRect = benchmark.boardList[i].RectList[0];
                        for (int k = 0; k < benchmark.boardList[i].RectList.Count; k++)
                        {
                            if (benchmark.boardList[i].RectList[k].size > largestRect.size)
                            {
                                largestRect = benchmark.boardList[i].RectList[k];
                            }
                        }
                        // 2. decide if to split horizontal or vertical
                        Boolean splitVertical;

                        if (random.Next(0, 2) == 0)
                        {
                            splitVertical = false;
                        }
                        else
                        {
                            splitVertical = true;
                        }
                        // 3. create two new rects out of the largest one and add them to the rect list
                        rectOne = new Rect();
                        rectTwo = new Rect();

                        if (splitVertical)
                        {
                            double largestRectWidthDouble = Convert.ToDouble(largestRect.width);
                            // new width between 10% and 90% of largestRect's width
                            // several conversions have to be done to use Ceiling and random for integer
                            int newWidth = Convert.ToInt32(Math.Ceiling(largestRectWidthDouble * (Convert.ToDouble(random.Next(1, 10)) / 10)));
                            if (newWidth == largestRect.width)
                            {
                                // this would not create 2 new rects. retry
                                nullCoordsOccured = true;
                            }
                            else
                            {
                                rectOne.height = largestRect.height;
                                rectTwo.height = largestRect.height;
                                rectOne.width = newWidth;
                                rectTwo.width = largestRect.width - newWidth;

                                rectOne.edgeLeftUp = largestRect.edgeLeftUp;
                                rectOne.edgeRightDown = new MyPoint(largestRect.edgeLeftUp.x + rectOne.width, largestRect.edgeRightDown.y);

                                rectTwo.edgeLeftUp = new MyPoint(rectOne.edgeRightDown.x, rectOne.edgeLeftUp.y);
                                rectTwo.edgeRightDown = largestRect.edgeRightDown;

                                nullCoordsOccured = false;

                            }
                        }
                        // if split horizontal
                        else
                        {
                            double largestRectHeightDouble = Convert.ToDouble(largestRect.height);
                            // new height between 10% and 90% of largestRect's height
                            // several conversions have to be done to use Ceiling and random for integer
                            int newHeight = Convert.ToInt32(Math.Ceiling(largestRectHeightDouble * (Convert.ToDouble(random.Next(1, 10)) / 10)));
                            if (newHeight == largestRect.height)
                            {
                                // this would not create 2 new rects. retry
                                nullCoordsOccured = true;
                            }
                            else
                            {
                                rectOne.height = newHeight;
                                rectTwo.height = largestRect.height - newHeight;
                                rectOne.width = largestRect.width;
                                rectTwo.width = largestRect.width;

                                rectOne.edgeLeftUp = largestRect.edgeLeftUp;
                                rectOne.edgeRightDown = new MyPoint(largestRect.edgeRightDown.x, largestRect.edgeLeftUp.y - rectOne.height);

                                rectTwo.edgeLeftUp = new MyPoint(rectOne.edgeLeftUp.x, rectOne.edgeRightDown.y);
                                rectTwo.edgeRightDown = largestRect.edgeRightDown;

                                nullCoordsOccured = false;
                            }
                        }
                    }

                    // add rect data
                    rectOne.size = rectOne.height * rectOne.width;
                    rectTwo.size = rectTwo.height * rectTwo.width;

                    // add rects to list
                    benchmark.boardList[i].RectList.Add(rectOne);
                    benchmark.boardList[i].RectList.Add(rectTwo);

                    // 4. remove the largest rect from the rect list
                    benchmark.boardList[i].RectList.Remove(largestRect);
                }

                // add rectIDs
                for (int j = 0; j < benchmark.boardList[i].RectList.Count; j++)
                {
                    benchmark.boardList[i].RectList[j].rectID = rectID;
                    rectID++;
                }

                // add number of rects and number of boards
                benchmark.numberOfRects = 0;
                for(int l = 0; l < benchmark.boardList.Count; l++)
                {
                    benchmark.numberOfRects += benchmark.boardList[i].RectList.Count;
                }
                benchmark.numberOfBoards = benchmark.boardList.Count;

            }
            // for testing:
            //1. size test
            String success = "success";
            for (int i = 0; i < benchmark.boardList.Count; i++)
            {
                int size = benchmark.boardList[i].size;
                for (int j = 0; j < benchmark.boardList[i].RectList.Count; j++)
                {
                    size -= benchmark.boardList[i].RectList[j].size;
                }
                if (size != 0)
                {
                    success = "fail";
                }
            }
            // 2. > 236 test
            for (int i = 0; i < benchmark.boardList.Count; i++)
            {
                for (int j = 0; j < benchmark.boardList[i].RectList.Count; j++)
                {
                    /*
                    if (benchmark.boardList[i].RectList[j].edgeLeftUp.y > 236)
                    {
                        int s = 1;
                    }
                    */
                }
            }
        }
        // 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();
        }
        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);
                }
            }
            */
        }