Greedy : Selection not immutable -> calculate order when needed.

improve readability
master 11
Selebrator 7 years ago
parent c1e409d82c
commit 6063ca4b76

@ -17,16 +17,18 @@ public class KnapsackDynamic extends Knapsack {
return this.pack(new Selection(), new HashMap<>()); return this.pack(new Selection(), new HashMap<>());
} }
private Selection pack(Selection me, Map<Integer, Selection> mem) { private Selection pack(Selection parent, Map<Integer, Selection> memory) {
return mem.computeIfAbsent(me.getWeight(), weight -> { return memory.computeIfAbsent(parent.getWeight(), weight -> {
Collection<Selection> children = new ArrayList<>(); final Collection<Selection> children = new ArrayList<>();
for(Item candidate : this.candidates) { for(Item candidate : this.candidates) {
if(candidate.getWeight() + me.getWeight() <= this.capacity) { if(parent.getWeight() + candidate.getWeight() <= this.capacity) {
children.add(pack(new Selection(me, candidate), mem)); 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);
}); });
} }

@ -11,24 +11,33 @@ import java.util.List;
public class KnapsackGreedy extends Knapsack { public class KnapsackGreedy extends Knapsack {
// descending private static final Comparator<Item> DESCENDING_ITEM_VALUE_COMPARATOR
protected final List<Item> orderedCandidates; = Comparator.comparing(Item::getValue).reversed();
public KnapsackGreedy(int capacity, Collection<Item> candidates) { public KnapsackGreedy(int capacity, Collection<Item> candidates) {
super(capacity, candidates); super(capacity, candidates);
this.orderedCandidates = new ArrayList<>(this.candidates);
this.orderedCandidates.sort(Comparator.comparing(Item::getValue).reversed());
} }
@Override @Override
public Selection pack() { public Selection pack() {
final List<Item> 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(); Selection selection = new Selection();
for(Item candidate : this.orderedCandidates) { for(Item candidate : orderedCandidates) {
while(candidate.getWeight() + selection.getWeight() <= this.capacity) { while(selection.getWeight() + candidate.getWeight() <= this.capacity) {
selection = new Selection(selection, candidate); selection = new Selection(selection, candidate);
/*
* Der Speicher leidet hier sehr,
* weil Selection#add(Item) private ist.
*/
} }
} }
return selection; return selection;
} }
} }

@ -19,15 +19,17 @@ public class KnapsackRecursive extends Knapsack {
return this.pack(new Selection()); return this.pack(new Selection());
} }
private Selection pack(Selection me) { private Selection pack(Selection parent) {
Collection<Selection> children = new ArrayList<>(); final Collection<Selection> children = new ArrayList<>();
for(Item candidate : this.candidates) { for(Item candidate : this.candidates) {
if(candidate.getWeight() + me.getWeight() <= this.capacity) { if(parent.getWeight() + candidate.getWeight() <= this.capacity) {
children.add(pack(new Selection(me, candidate))); 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);
} }
} }

Loading…
Cancel
Save