diff --git a/src/main/java/provided/_11/_3/Item.java b/src/main/java/provided/_11/_3/Item.java new file mode 100644 index 0000000..08c08bf --- /dev/null +++ b/src/main/java/provided/_11/_3/Item.java @@ -0,0 +1,74 @@ +public class Item { + + private String name; + private int value; + private int weight; + + public Item(String name, int value, int weight) { + super(); + this.name = name; + this.value = value; + this.weight = weight; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + @Override + public String toString() { + return name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + value; + result = prime * result + weight; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Item other = (Item) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (value != other.value) + return false; + if (weight != other.weight) + return false; + return true; + } + +} diff --git a/src/main/java/provided/_11/_3/Knapsack.java b/src/main/java/provided/_11/_3/Knapsack.java new file mode 100644 index 0000000..23dd2cc --- /dev/null +++ b/src/main/java/provided/_11/_3/Knapsack.java @@ -0,0 +1,61 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public abstract class Knapsack { + + protected List candidates = new ArrayList<>(); + protected int capacity; + + public Knapsack(int capacity, Collection candidates) { + super(); + this.capacity = capacity; + this.candidates.addAll(candidates); + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public List getCandidates() { + return candidates; + } + + public abstract Selection pack(); + + private static final int REPETITIONS = 5; + private static final int CAPACITY = 49; + + public static void main(String[] args) { + List items = new ArrayList<>(); + items.add(new Item("Banknote", 100, 1)); + items.add(new Item("Goldbar", 1000, 30)); + items.add(new Item("Diamond", 750, 5)); + test("Recursive", new KnapsackRecursive(CAPACITY, items)); + test("Greedy", new KnapsackGreedy(CAPACITY, items)); + test("Dynamic Programming", new KnapsackDynamic(CAPACITY, items)); + } + + private static void test(String title, Knapsack knapsack) { + System.out.print(title); + Selection result = null; + long totalNs = 0; + for (int i = 0; i < REPETITIONS; i++) { + long start = System.nanoTime(); + result = knapsack.pack(); + long stop = System.nanoTime(); + totalNs += stop - start; + System.out.print("."); + } + System.out.println("\n\t" + result); + totalNs /= REPETITIONS; + long totalMs = totalNs / 1000000L; + System.out.println("\tTime required: " + totalNs + " ns (~ " + totalMs + " ms)"); + System.out.println(); + } + +} diff --git a/src/main/java/provided/_11/_3/KnapsackDynamic.java b/src/main/java/provided/_11/_3/KnapsackDynamic.java new file mode 100644 index 0000000..56084da --- /dev/null +++ b/src/main/java/provided/_11/_3/KnapsackDynamic.java @@ -0,0 +1,17 @@ +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class KnapsackDynamic extends Knapsack { + + public KnapsackDynamic(int capacity, Collection candidates) { + super(capacity, candidates); + } + + @Override + public Selection pack() { + //TODO: implement this + return new Selection(); + } + +} diff --git a/src/main/java/provided/_11/_3/KnapsackGreedy.java b/src/main/java/provided/_11/_3/KnapsackGreedy.java new file mode 100644 index 0000000..76a51ae --- /dev/null +++ b/src/main/java/provided/_11/_3/KnapsackGreedy.java @@ -0,0 +1,15 @@ +import java.util.Collection; + +public class KnapsackGreedy extends Knapsack { + + public KnapsackGreedy(int capacity, Collection candidates) { + super(capacity, candidates); + } + + @Override + public Selection pack() { + //TODO: implement this + return new Selection(); + } + +} diff --git a/src/main/java/provided/_11/_3/KnapsackRecursive.java b/src/main/java/provided/_11/_3/KnapsackRecursive.java new file mode 100644 index 0000000..577ab0d --- /dev/null +++ b/src/main/java/provided/_11/_3/KnapsackRecursive.java @@ -0,0 +1,15 @@ +import java.util.Collection; + +public class KnapsackRecursive extends Knapsack { + + public KnapsackRecursive(int capacity, Collection candidates) { + super(capacity, candidates); + } + + @Override + public Selection pack() { + //TODO: implement this + return new Selection(); + } + +} diff --git a/src/main/java/provided/_11/_3/Selection.java b/src/main/java/provided/_11/_3/Selection.java new file mode 100644 index 0000000..4e6f275 --- /dev/null +++ b/src/main/java/provided/_11/_3/Selection.java @@ -0,0 +1,112 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Selection { + + private Map items = new HashMap<>(); + private int value; + private int weight; + + public Selection() { + super(); + } + + public Selection(Selection previous) { + super(); + items.putAll(previous.items); + value = previous.value; + weight = previous.weight; + } + + public Selection(Selection previous, Item item) { + super(); + items.putAll(previous.items); + value = previous.value; + weight = previous.weight; + add(item); + } + + private void add(Item item) { + items.put(item, getCount(item) + 1); + value += item.getValue(); + weight += item.getWeight(); + } + + public int getCount(Item item) { + Integer result = items.get(item); + if (result == null) { + result = 0; + } + return result; + } + + public Collection getItems() { + return items.keySet(); + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + @Override + public String toString() { + StringBuilder b = new StringBuilder(); + b.append("Value: "); + b.append(value); + b.append(", weight: "); + b.append(weight); + b.append(", items: "); + List list = new ArrayList<>(items.keySet()); + list.sort((i1, i2) -> i1.getName().compareTo(i2.getName())); + for (int i = 0; i < list.size(); i++) { + b.append(items.get(list.get(i))); + b.append("x "); + b.append(list.get(i).getName()); + if (i < list.size() - 1) { + b.append(", "); + } + } + return b.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((items == null) ? 0 : items.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Selection other = (Selection) obj; + if (items == null) { + if (other.items != null) + return false; + } else if (!items.equals(other.items)) + return false; + return true; + } + +}