diff --git a/groovy/greedy/Makefile b/groovy/greedy/Makefile new file mode 100644 index 0000000..5c7b476 --- /dev/null +++ b/groovy/greedy/Makefile @@ -0,0 +1 @@ +run: ; groovy TSP.groovy a280.xml \ No newline at end of file diff --git a/groovy/greedy/README.md b/groovy/greedy/README.md index d6c4557..cd4843e 100644 --- a/groovy/greedy/README.md +++ b/groovy/greedy/README.md @@ -1 +1,5 @@ -Tirey Morris +Solution to Traveling Salesman Problem using Greedy Algorithm + +Author: Tirey Morris +Usage: $ make run + (depends on file a280.xml being in groovy/greedy directory. This can be modified in the makefile.) diff --git a/groovy/greedy/TSP.groovy b/groovy/greedy/TSP.groovy new file mode 100644 index 0000000..58c33e2 --- /dev/null +++ b/groovy/greedy/TSP.groovy @@ -0,0 +1,87 @@ +/** + * Tirey Morris (tam3354) + * CS 345 (Rellermeyer) + * December 5th, 2016 + */ + +class TSP { + + static final BigDecimal MAX = BigDecimal.valueOf(Double.MAX_VALUE) + + static void main(String... args) { + if (args.size() < 1 || !args[0].endsWith(".xml")) { + throw new IllegalArgumentException("Must provide .xml input file") + } + + def graph = parseGraph(new File(args[0])) + def results = solve(graph) + println("path = ${results[0]}\n\ntotal cost = ${results[1]}") + } + + static parseGraph(File xmlFile) { + def tspXml = new XmlParser().parse(xmlFile) + def graph = tspXml.graph.vertex + } + + static getNodes(graph) { + def nodes = [:] + graph.eachWithIndex { vertex, i -> + nodes[i] = vertex.edge + } + nodes + } + + static Integer getNumFromEdge(edge) { + edge.value().text().toInteger() + } + + static BigDecimal getCostFromEdge(edge) { + new BigDecimal(edge.attributes().get("cost")) + } + + static List solve(graph) { + def nodes = getNodes(graph) + def path = [] + def total = 0 + int start = 0 + int current = 0 + + while (nodes.size() > 0) { + path << current + def edges = nodes[current] + nodes.remove(current) + + def min = MAX + + edges.each { edge -> + Integer num = getNumFromEdge(edge) + BigDecimal cost = getCostFromEdge(edge) + + if (cost < min && nodes[num]) { + min = cost + current = num + } + } + + if (min != MAX) { + total += min + } + + if (nodes.size() == 1) { + edges.each { edge -> + Integer num = getNumFromEdge(edge) + BigDecimal cost = getCostFromEdge(edge) + + if (num == start) { + total += cost + } + + } + } + } + + path << start + + [path, total] + } +} \ No newline at end of file