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<>());
}
private Selection pack(Selection me, Map<Integer, Selection> mem) {
return mem.computeIfAbsent(me.getWeight(), weight -> {
Collection<Selection> children = new ArrayList<>();
private Selection pack(Selection parent, Map<Integer, Selection> memory) {
return memory.computeIfAbsent(parent.getWeight(), weight -> {
final Collection<Selection> 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);
});
}

@ -11,24 +11,33 @@ import java.util.List;
public class KnapsackGreedy extends Knapsack {
// descending
protected final List<Item> orderedCandidates;
private static final Comparator<Item> DESCENDING_ITEM_VALUE_COMPARATOR
= Comparator.comparing(Item::getValue).reversed();
public KnapsackGreedy(int capacity, Collection<Item> candidates) {
super(capacity, candidates);
this.orderedCandidates = new ArrayList<>(this.candidates);
this.orderedCandidates.sort(Comparator.comparing(Item::getValue).reversed());
}
@Override
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();
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;
}
}

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

Loading…
Cancel
Save