Initial commit for 11.3
parent
f9288aa4d5
commit
c1e409d82c
@ -0,0 +1,33 @@
|
||||
package _11._3;
|
||||
|
||||
import provided._11._3.Item;
|
||||
import provided._11._3.Knapsack;
|
||||
import provided._11._3.Selection;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class KnapsackDynamic extends Knapsack {
|
||||
|
||||
public KnapsackDynamic(int capacity, Collection<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
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<>();
|
||||
for(Item candidate : this.candidates) {
|
||||
if(candidate.getWeight() + me.getWeight() <= this.capacity) {
|
||||
children.add(pack(new Selection(me, candidate), mem));
|
||||
}
|
||||
}
|
||||
|
||||
return children.stream().max(Comparator.comparingDouble(Selection::getValue)).orElse(me);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package _11._3;
|
||||
|
||||
import provided._11._3.Item;
|
||||
import provided._11._3.Knapsack;
|
||||
import provided._11._3.Selection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class KnapsackGreedy extends Knapsack {
|
||||
|
||||
// descending
|
||||
protected final List<Item> orderedCandidates;
|
||||
|
||||
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() {
|
||||
Selection selection = new Selection();
|
||||
for(Item candidate : this.orderedCandidates) {
|
||||
while(candidate.getWeight() + selection.getWeight() <= this.capacity) {
|
||||
selection = new Selection(selection, candidate);
|
||||
}
|
||||
}
|
||||
return selection;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package _11._3;
|
||||
|
||||
import provided._11._3.Item;
|
||||
import provided._11._3.Knapsack;
|
||||
import provided._11._3.Selection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class KnapsackRecursive extends Knapsack {
|
||||
|
||||
public KnapsackRecursive(int capacity, Collection<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
return this.pack(new Selection());
|
||||
}
|
||||
|
||||
private Selection pack(Selection me) {
|
||||
Collection<Selection> children = new ArrayList<>();
|
||||
for(Item candidate : this.candidates) {
|
||||
if(candidate.getWeight() + me.getWeight() <= this.capacity) {
|
||||
children.add(pack(new Selection(me, candidate)));
|
||||
}
|
||||
}
|
||||
|
||||
return children.stream().max(Comparator.comparingDouble(Selection::getValue)).orElse(me);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class KnapsackDynamic extends Knapsack {
|
||||
|
||||
public KnapsackDynamic(int capacity, Collection<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
//TODO: implement this
|
||||
return new Selection();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
import java.util.Collection;
|
||||
|
||||
public class KnapsackGreedy extends Knapsack {
|
||||
|
||||
public KnapsackGreedy(int capacity, Collection<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
//TODO: implement this
|
||||
return new Selection();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
import java.util.Collection;
|
||||
|
||||
public class KnapsackRecursive extends Knapsack {
|
||||
|
||||
public KnapsackRecursive(int capacity, Collection<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
//TODO: implement this
|
||||
return new Selection();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue