Esempio n. 1
0
 public int CalculateFitness(Solution solution)
 {
     int fitness = 0;
     for (int i = 0; i < solution.BoardList[solution.BoardList.Count - 1].RectList.Count; i++)
     {
         fitness += solution.BoardList[solution.BoardList.Count - 1].RectList[i].size;
     }
     return fitness;
 }
Esempio n. 2
0
        public Solution CloneSolution(Solution solution)
        {
            Solution solutionClone = new Solution();
            solutionClone.SolutionID = solution.SolutionID;
            solutionClone.usedBenchmarkID = solution.usedBenchmarkID;
            solutionClone.numberOfBoards = solution.numberOfBoards;
            solutionClone.numberOfRects = solution.numberOfRects;
            solutionClone.creationTime = solution.creationTime;
            solutionClone.percentageFilledArea = solution.percentageFilledArea;
            solutionClone.numberRectsPlaced = solution.numberRectsPlaced;
            solutionClone.numberRectsLeft = solution.numberRectsLeft;
            solutionClone.benchmark = solution.benchmark;

            solutionClone.BoardList = new List<Board>();

            for (int i = 0; i < solution.BoardList.Count; i++)
            {
                Board board = new Board();
                board.boardID = solution.BoardList[i].boardID;
                board.height = solution.BoardList[i].height;
                board.width = solution.BoardList[i].width;
                board.size = solution.BoardList[i].size;
                board.isCollectionBoard = solution.BoardList[i].isCollectionBoard;

                board.edgeLeftUp = new MyPoint();
                board.edgeLeftUp.x = (solution.BoardList[i].edgeLeftUp != null)? solution.BoardList[i].edgeLeftUp.x : 0;
                board.edgeLeftUp.y = (solution.BoardList[i].edgeLeftUp != null)? solution.BoardList[i].edgeLeftUp.y : 0;
                board.edgeRightDown = new MyPoint();
                board.edgeRightDown.x = (solution.BoardList[i].edgeRightDown != null) ? solution.BoardList[i].edgeRightDown.x : 0;
                board.edgeRightDown.y = (solution.BoardList[i].edgeRightDown != null) ? solution.BoardList[i].edgeRightDown.y : 0;

                for (int j = 0; j < solution.BoardList[i].RectList.Count; j++)
                {
                    Rect rect = new Rect();
                    rect.rectID = solution.BoardList[i].RectList[j].rectID;
                    rect.width = solution.BoardList[i].RectList[j].width;
                    rect.height = solution.BoardList[i].RectList[j].height;
                    rect.size = solution.BoardList[i].RectList[j].size;

                    rect.edgeLeftUp = new MyPoint();
                    rect.edgeLeftUp.x = (solution.BoardList[i].RectList[j].edgeLeftUp != null)? solution.BoardList[i].RectList[j].edgeLeftUp.x : 0;
                    rect.edgeLeftUp.y = (solution.BoardList[i].RectList[j].edgeLeftUp != null)? solution.BoardList[i].RectList[j].edgeLeftUp.y : 0;
                    rect.edgeRightDown = new MyPoint();
                    rect.edgeRightDown.x = (solution.BoardList[i].RectList[j].edgeRightDown != null)? solution.BoardList[i].RectList[j].edgeRightDown.x : 0;
                    rect.edgeRightDown.y = (solution.BoardList[i].RectList[j].edgeRightDown != null)? solution.BoardList[i].RectList[j].edgeRightDown.y : 0;

                    board.RectList.Add(rect);
                }
                solutionClone.BoardList.Add(board);
            }

            return solutionClone;
        }
        // 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();
        }
Esempio n. 4
0
        private Base()
        {
            this.BoardList = new List<Board>();
            this.boardGap = 20;
            this.contentToShow = "";
            this.runningProcess = new RunningProcess();
            this.solutionStatus = 0;

            this.positionsManaged = new List<Position>();
            this.positionsValid = new List<Position>();
            this.bestPositionSet = false;

            this.chosenGreedies = new List<int>();
            this.populationSmall = new List<PopulationElement>();
            this.populationLarge = new List<PopulationElement>();

            this.changeCounter = 0;
            this.random = new Random();
            this.tournamentPopulation = false;
            this.tournamentGreediesOnly = false;
            this.tournamentGreedyMethods = new List<int>();

            this.bestSolution = null;
            this.evolutionStep = 0;
        }
Esempio n. 5
0
        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;
        }
 public List<int> CreateBoardRanking(Solution solution)
 {
     // list to be sorted from best to worst filled percentage
     List<int> rankedList = new List<int>();
     // list with index of each board
     List<int> listAllBoards = new List<int>();
     for(int i = 0; i < solution.BoardList.Count - 1; i++)
     {
         listAllBoards.Add(i);
     }
     while(listAllBoards.Count > 0)
     {
         int bestIndex = 0;
         int bestIndexSolution = listAllBoards[bestIndex];
         double bestValue = CalculateFilledPercentageBoard(solution.BoardList[0]);
         for (int i = 0; i < listAllBoards.Count; i++)
         {
             if(CalculateFilledPercentageBoard(solution.BoardList[listAllBoards[i]]) > bestValue)
             {
                 bestIndex = i;
                 bestIndexSolution = listAllBoards[bestIndex];
                 bestValue = CalculateFilledPercentageBoard(solution.BoardList[listAllBoards[i]]);
             }
         }
         rankedList.Add(bestIndexSolution);
         listAllBoards.RemoveAt(bestIndex);
     }
     return (rankedList);
 }
Esempio n. 7
0
        public void ShowSolution(Solution solution)
        {
            ShowBoards(solution.BoardList, false);
            ShowRects(solution.BoardList);

            // set solution as contentToShow so it is shown everytime the display changes
            global.contentToShow = "solution";
            global.Verschnittoptimierung.buttonSelectView.Text = "Solution";
            global.Verschnittoptimierung.buttonSelectView.Enabled = true;
            global.Verschnittoptimierung.buttonSelectView.BackColor = Color.LawnGreen;
        }