diff --git a/src/main/java/_11/_3/KnapsackDynamic.java b/src/main/java/_11/_3/KnapsackDynamic.java index 90529e3..5395d32 100644 --- a/src/main/java/_11/_3/KnapsackDynamic.java +++ b/src/main/java/_11/_3/KnapsackDynamic.java @@ -17,16 +17,18 @@ public class KnapsackDynamic extends Knapsack { return this.pack(new Selection(), new HashMap<>()); } - private Selection pack(Selection me, Map mem) { - return mem.computeIfAbsent(me.getWeight(), weight -> { - Collection children = new ArrayList<>(); + private Selection pack(Selection parent, Map memory) { + return memory.computeIfAbsent(parent.getWeight(), weight -> { + final Collection children = new ArrayList<>(); for(Item candidate : this.candidates) { - if(candidate.getWeight() + me.getWeight() <= this.capacity) { - children.add(pack(new Selection(me, candidate), mem)); + if(parent.getWeight() + candidate.getWeight() <= this.capacity) { + children.add(pack(new Selection(parent, candidate), memory)); } } - return children.stream().max(Comparator.comparingDouble(Selection::getValue)).orElse(me); + return children.stream() + .max(Comparator.comparingDouble(Selection::getValue)) + .orElse(parent); }); } diff --git a/src/main/java/_11/_3/KnapsackGreedy.java b/src/main/java/_11/_3/KnapsackGreedy.java index 938a0ef..998198e 100644 --- a/src/main/java/_11/_3/KnapsackGreedy.java +++ b/src/main/java/_11/_3/KnapsackGreedy.java @@ -11,24 +11,33 @@ import java.util.List; public class KnapsackGreedy extends Knapsack { - // descending - protected final List orderedCandidates; + private static final Comparator DESCENDING_ITEM_VALUE_COMPARATOR + = Comparator.comparing(Item::getValue).reversed(); public KnapsackGreedy(int capacity, Collection candidates) { super(capacity, candidates); - this.orderedCandidates = new ArrayList<>(this.candidates); - this.orderedCandidates.sort(Comparator.comparing(Item::getValue).reversed()); } @Override public Selection pack() { + final List orderedCandidates = new ArrayList<>(this.candidates); + orderedCandidates.sort(DESCENDING_ITEM_VALUE_COMPARATOR); + + /* + * Fügt das wertvollste Item hinzu, solange es noch passt, geht + * dann zum nächst wertvolleren Item über und wiederholt den Vorgang + * so lange bis kein Item mehr passt. + */ Selection selection = new Selection(); - for(Item candidate : this.orderedCandidates) { - while(candidate.getWeight() + selection.getWeight() <= this.capacity) { + for(Item candidate : orderedCandidates) { + while(selection.getWeight() + candidate.getWeight() <= this.capacity) { selection = new Selection(selection, candidate); + /* + * Der Speicher leidet hier sehr, + * weil Selection#add(Item) private ist. + */ } } return selection; } - } diff --git a/src/main/java/_11/_3/KnapsackRecursive.java b/src/main/java/_11/_3/KnapsackRecursive.java index fdf2c59..6adae7b 100644 --- a/src/main/java/_11/_3/KnapsackRecursive.java +++ b/src/main/java/_11/_3/KnapsackRecursive.java @@ -19,15 +19,17 @@ public class KnapsackRecursive extends Knapsack { return this.pack(new Selection()); } - private Selection pack(Selection me) { - Collection children = new ArrayList<>(); + private Selection pack(Selection parent) { + final Collection children = new ArrayList<>(); for(Item candidate : this.candidates) { - if(candidate.getWeight() + me.getWeight() <= this.capacity) { - children.add(pack(new Selection(me, candidate))); + if(parent.getWeight() + candidate.getWeight() <= this.capacity) { + children.add(pack(new Selection(parent, candidate))); } } - return children.stream().max(Comparator.comparingDouble(Selection::getValue)).orElse(me); + return children.stream() + .max(Comparator.comparingDouble(Selection::getValue)) + .orElse(parent); } }