Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Sep 15, 2024
1 parent 9a65672 commit 896059e
Show file tree
Hide file tree
Showing 22 changed files with 1,616 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ public void decrement(int value) {
this.count = this.count - value;
}

public StackableItem clone() {
return new StackableItem(stackable, count);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.skjolber.packing.api;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -44,7 +45,8 @@ public StackableItem get(int i) {
return items.get(i);
}

public int loadableItemsCount() {

public int stackableItemsCount() {
int count = 0;
for (StackableItem loadableItem : items) {
count += loadableItem.getCount();
Expand Down Expand Up @@ -72,4 +74,15 @@ public void removeEmpty() {
}
}
}

public StackableItemGroup clone() {
List<StackableItem> items = new ArrayList<>();

for (StackableItem stackableItem : this.items) {
items.add(stackableItem.clone());
}

return new StackableItemGroup(id, items);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* "https://www.sitepoint.com/self-types-with-javas-generics/">https://www.sitepoint.com/self-types-with-javas-generics/</a>
*/

public abstract class AbstractLoadableItemGroupIteratorBuilder<B extends AbstractLoadableItemGroupIteratorBuilder<B>> {
public abstract class AbstractStackableItemGroupIteratorBuilder<B extends AbstractStackableItemGroupIteratorBuilder<B>> {

protected int maxLoadWeight = -1;
protected Predicate<Stackable> filter;
Expand Down Expand Up @@ -103,6 +103,6 @@ protected List<StackableItemGroup> toMatrix() {
return results;
}

public abstract LoadableItemPermutationRotationIterator build();
public abstract StackableItemPermutationRotationIterator build();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.github.skjolber.packing.iterator;

import java.util.List;

import com.github.skjolber.packing.api.StackableItem;
import com.github.skjolber.packing.api.StackableItemGroup;

public abstract class AbstractStackableItemGroupPermutationRotationIterator extends AbstractStackableItemPermutationRotationIterator {

protected List<StackableItemGroup> groups;

public AbstractStackableItemGroupPermutationRotationIterator(IndexedStackableItem[] matrix, List<StackableItemGroup> groups) {
super(matrix);
this.groups = groups;
}

/**
* Return number of permutations for boxes which fit within this container.
*
* @return permutation count
*/

public long countPermutations() {
// reduce permutations for boxes which are duplicated

// could be further bounded by looking at how many boxes (i.e. n x the smallest) which actually
// fit within the container volume
long n = 1;

for (StackableItemGroup loadableItemGroup : groups) {

List<StackableItem> items = loadableItemGroup.getItems();

int count = loadableItemGroup.stackableItemsCount();

int maxCount = 0;
for (StackableItem value : items) {
if(value != null) {
if(maxCount < value.getCount()) {
maxCount = value.getCount();
}
}
}

if(maxCount > 1) {
int[] factors = new int[maxCount];
for (StackableItem value : items) {
if(value != null) {
for (int k = 0; k < value.getCount(); k++) {
factors[k]++;
}
}
}

for (long i = 0; i < count; i++) {
if(Long.MAX_VALUE / (i + 1) <= n) {
return -1L;
}

n = n * (i + 1);

for (int k = 1; k < maxCount; k++) {
while (factors[k] > 0 && n % (k + 1) == 0) {
n = n / (k + 1);

factors[k]--;
}
}
}

for (int k = 1; k < maxCount; k++) {
while (factors[k] > 0) {
n = n / (k + 1);

factors[k]--;
}
}
} else {
for (long i = 0; i < count; i++) {
if(Long.MAX_VALUE / (i + 1) <= n) {
return -1L;
}
n = n * (i + 1);
}
}
}
return n;
}


@Override
public void removePermutations(List<Integer> removed) {

for (Integer i : removed) {
IndexedStackableItem loadableItem = stackableItems[i];

loadableItem.decrement();

if(loadableItem.isEmpty()) {
stackableItems[i] = null;
}
}

// go through all groups and clean up
for(int i = 0; i < groups.size(); i++) {
StackableItemGroup group = groups.get(i);

group.removeEmpty();
if(group.isEmpty()) {
groups.remove(i);
i--;
}
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* "https://www.sitepoint.com/self-types-with-javas-generics/">https://www.sitepoint.com/self-types-with-javas-generics/</a>
*/

public abstract class AbstractLoadableIteratorBuilder<B extends AbstractLoadableIteratorBuilder<B>> {
public abstract class AbstractStackableItemIteratorBuilder<B extends AbstractStackableItemIteratorBuilder<B>> {

protected int maxLoadWeight = -1;
protected Predicate<Stackable> filter;
Expand Down Expand Up @@ -89,6 +89,6 @@ protected IndexedStackableItem[] toMatrix() {
return results;
}

public abstract LoadableItemPermutationRotationIterator build();
public abstract StackableItemPermutationRotationIterator build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import com.github.skjolber.packing.api.StackValue;
import com.github.skjolber.packing.api.StackableItem;

public abstract class AbstractLoadablePermutationRotationIterator implements LoadableItemPermutationRotationIterator {
public abstract class AbstractStackableItemPermutationRotationIterator implements StackableItemPermutationRotationIterator {

protected final IndexedStackableItem[] loadableItems; // by index
protected final IndexedStackableItem[] stackableItems; // by index
protected int[] reset;

public AbstractLoadablePermutationRotationIterator(IndexedStackableItem[] matrix) {
this.loadableItems = matrix;
public AbstractStackableItemPermutationRotationIterator(IndexedStackableItem[] matrix) {
this.stackableItems = matrix;
}

public IndexedStackableItem[] getMatrix() {
return loadableItems;
return stackableItems;
}

/**
Expand All @@ -26,7 +26,7 @@ public IndexedStackableItem[] getMatrix() {
*/

public int boxItemLength() {
return loadableItems.length;
return stackableItems.length;
}

public long getMinStackableArea(int offset) {
Expand Down Expand Up @@ -62,12 +62,25 @@ public List<StackValue> get(PermutationRotationState state, int length) {

List<StackValue> results = new ArrayList<StackValue>(length);
for (int i = 0; i < length; i++) {
results.add(loadableItems[permutations[i]].getStackable().getStackValue(rotations[i]));
results.add(stackableItems[permutations[i]].getStackable().getStackValue(rotations[i]));
}
return results;
}

public abstract int length();

public abstract StackValue getStackValue(int index);


protected int[] getFrequencies() {
int[] frequencies = new int[stackableItems.length];

for (int i = 0; i < stackableItems.length; i++) {
if(stackableItems[i] != null) {
frequencies[i] = stackableItems[i].getCount();
}
}
return frequencies;
}

}
Loading

0 comments on commit 896059e

Please sign in to comment.