Skip to content

Commit

Permalink
ACE: Add Maximum Rectangles packing algorithm implementation. Some cl…
Browse files Browse the repository at this point in the history
…ean-up and refactor.
  • Loading branch information
achaussende committed May 10, 2015
1 parent f66ea4a commit a8e0d82
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 374 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public class Main {
*/
public static void main(String[] args) {
logger.info("STARTED");
logger.info("Parameters - File : " + args[0]);
Resolution resolution = new Resolution(args[0]);
resolution.solve(Integer.parseInt(args[1]));
logger.info("Parameters - Number of iterations : " + args[1]);
if (args.length == 3) {
resolution.solve(Integer.parseInt(args[1]), Integer.valueOf(args[2]));
} else {
resolution.solve(Integer.parseInt(args[1]), 1);
}
logger.info("STOPPED");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
package com.polytech4A.cuttingstock.core.packing;

import com.polytech4A.cuttingstock.core.model.*;
import com.polytech4A.cuttingstock.core.model.Vector;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.ListIterator;
import java.util.*;

/**
* Created by Antoine CARON on 13/03/2015.
Expand All @@ -41,13 +39,13 @@
* Jukka Jylänki
* February 27, 2010
*/
public class GuillotineSortBFF extends Packer {
public class GuillotineSortBAF extends Packer {

private static final Logger logger = Logger.getLogger(GuillotineSortBFF.class);
private static final Logger logger = Logger.getLogger(GuillotineSortBAF.class);

private Solution solution;

public GuillotineSortBFF(ArrayList<Comparator<Box>> boxComparators) {
public GuillotineSortBAF(ArrayList<Comparator<Box>> boxComparators) {
super(boxComparators);
}

Expand Down Expand Up @@ -140,19 +138,21 @@ public ArrayList<PlacedBox> generatePlacedBoxes(ArrayList<Box> boxes, ArrayList<
* @return The box that is now placed. Return null if it can't be placed.
*/
public PlacedBox generatePlacedBox(ArrayList<Bin> bins, Box box) {
for (Bin bin : bins) {
if (bin.isActive() && (isBinCompatible(bin.getSize(), box.getSize()) || isBinCompatible(bin.getSize(), box.getSize().getInvertedVector()))) {
PlacedBox placedBox = generatePlaceBoxForBin(bin, box);
if (placedBox != null) {
//mark Current Bin
bin.setActive(false);
//Disable bin borthers from other cutting (Vertical or horizontal
bin.disableSubBinFromBin(bins);
//Generate subBins and adds it
generateSubBins(bin, placedBox, bins);
return placedBox;
}
}
Optional<Bin> test = bins.parallelStream()
.filter(b -> b.isActive() && (isBinCompatible(b.getSize(), box.getSize()) || isBinCompatible(b.getSize(), box.getSize().getInvertedVector())))
.reduce((b1, b2) -> b1.getSize().isSmallerThan(b2.getSize()) ? b1 : b2);
PlacedBox placedBox = null;
if (test.isPresent()) {
placedBox = generatePlaceBoxForBin(test.get(), box);
}
if (placedBox != null) {
//mark Current Bin
test.get().setActive(false);
//Disable bin borthers from other cutting (Vertical or horizontal
test.get().disableSubBinFromBin(bins);
//Generate subBins and adds it
generateSubBins(test.get(), placedBox, bins);
return placedBox;
}
return null;
}
Expand Down Expand Up @@ -189,6 +189,7 @@ public void generateSubBins(Bin bin, PlacedBox placedBox, ArrayList<Bin> bins) {
} else {
sizeBox = placedBox.getSize();
}

//Vertical cut.
//First bin.
Vector sizeBin = new Vector(bin.getSize().getX() - sizeBox.getX(), bin.getSize().getY());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Project to resolve 2D cutting stock problem for Discreet Optimizations course at Polytech Lyon
* Copyright (C) 2015. CARON Antoine and CHAUSSENDE Adrien
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.polytech4A.cuttingstock.core.packing;

import com.polytech4A.cuttingstock.core.model.Box;
import com.polytech4A.cuttingstock.core.model.Pattern;
import com.polytech4A.cuttingstock.core.model.PlacedBox;
import com.polytech4A.cuttingstock.core.model.Vector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
* Created by Adrien CHAUSSENDE on 09/05/2015.
*
* @author Adrien CHAUSSENDE
* @version 1.0
*/
public class MaxRectsBAF extends GuillotineSortBAF {

public MaxRectsBAF(ArrayList<Comparator<Box>> boxComparators) {
super(boxComparators);
}

@Override
public void generateSubBins(Bin bin, PlacedBox placedBox, ArrayList<Bin> bins) {
Vector sizeBox;
Bin bintoCreate;
if (placedBox.isRotation()) {
sizeBox = placedBox.getSize().getInvertedVector();
} else {
sizeBox = placedBox.getSize();
}
//Horizontal cut
Vector sizeBin = new Vector(bin.getSize().getX(), bin.getSize().getY() - sizeBox.getY());
Vector originBin = new Vector(bin.getOrigin().getX(), bin.getOrigin().getY() + sizeBox.getY());
if (!sizeBin.isDimensionNull()) {
bintoCreate = new MaxRectsBin(sizeBin, bin.getPattern(), originBin);
bin.getHorizontalsubBin().add(bintoCreate);
bins.add(bintoCreate);
}

//Vertical cut
sizeBin = new Vector(bin.getSize().getX() - sizeBox.getX(), bin.getSize().getY());
originBin = new Vector(bin.getOrigin().getX() + sizeBox.getX(), bin.getOrigin().getY());
if (!sizeBin.isDimensionNull()) {
bintoCreate = new MaxRectsBin(sizeBin, bin.getPattern(), originBin);
bin.getVerticalsubBin().add(bintoCreate);
bins.add(bintoCreate);
}
}

@Override
public Pattern generatePattern(Pattern p, Comparator<Box> comparator) {
ArrayList<Box> boxes = p.getBoxes();
Collections.sort(boxes, Collections.reverseOrder(comparator));
Bin bin = new MaxRectsBin(p.getSize(), p, new Vector(0, 0));
ArrayList<Bin> bins = new ArrayList<>();
bins.add(bin);
ArrayList<PlacedBox> placedBoxes = generatePlacedBoxes(boxes, bins);
if (placedBoxes != null) {
Pattern retPattern = new Pattern(p.getSize(), p.getAmounts());
retPattern.setPlacedBoxes(placedBoxes);
return retPattern;
}
return p;
}
}
Loading

0 comments on commit a8e0d82

Please sign in to comment.