Commit of the provided classed for task number 3
parent
de32d5fd23
commit
f9288aa4d5
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Knapsack {
|
||||
|
||||
protected List<Item> candidates = new ArrayList<>();
|
||||
protected int capacity;
|
||||
|
||||
public Knapsack(int capacity, Collection<Item> candidates) {
|
||||
super();
|
||||
this.capacity = capacity;
|
||||
this.candidates.addAll(candidates);
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public void setCapacity(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public List<Item> 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<Item> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<Item> candidates) {
|
||||
super(capacity, candidates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selection pack() {
|
||||
//TODO: implement this
|
||||
return new Selection();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<Item, Integer> 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<Item> 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<Item> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue