Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/rellermeyer/99tsp
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeryn committed Dec 13, 2016
2 parents 543d4e1 + b3662a8 commit c808420
Show file tree
Hide file tree
Showing 92 changed files with 110,063 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ data
*.class
*~
*.swp
.DS_Store
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ available in this repository.
* Genetic (gen)
* ~~Neural Network (neural)~~ (no one has done 1 yet)
* 2-Opt (2opt)
* ~~Dynamic Programming~~ (pending implementations)

If more implementations are added, this list will be added to as well.
Below are short descriptions of each implementation method. Note that the
Expand Down Expand Up @@ -120,25 +121,54 @@ so the algorithm can potentially be extremely inefficient.
More details can be found here:
https://en.wikipedia.org/wiki/2-opt

Last update: November 28, 2016
### Dynamic Programming

TODO

Last update: November 30, 2016

## Languages

The following languages and implementations are currently available on the
repository.

Bolded languages are pending implementation/examination (Fall 2016).

* R
* Greedy
* C
* Greedy
* Simulated Annealing
* C++
* Greedy
* **Genetic**
* Simulated Annealing
* **2-Opt**
* Clingo
* Greedy
* Groovy
* **Greedy**
* Go
* **Greedy**
* **Simulated Annealing**
* Haskell
* **Greedy**
* Java
* Greedy
* **Simulated Annealing**
* **Dynamic Programming**
* **2-Opt**
* Javascript
* Greedy
* **Genetic**
* Julia
* **Greedy**
* Kotlin
* **Greedy**
* Lisp
* **Greedy**
* Matlab
* **Greedy**
* Objective C
* Greedy
* Prolog
Expand All @@ -147,6 +177,7 @@ repository.
* Greedy
* Genetic
* Simulated Annealing
* **Neural Network**
* 2-Opt
* Ruby
* Greedy
Expand All @@ -155,9 +186,12 @@ repository.
* Scala
* Greedy
* Genetic
* Swift
* **Greedy**
* **Genetic**
* Verilog
* Simulated Annealing
* Visual Basic
* Greedy

Last update: November 22, 2016
Last update: December 9, 2016
20 changes: 20 additions & 0 deletions c/greedy/tsp.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.tsp</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Binary file added c/greedy/tsp.dSYM/Contents/Resources/DWARF/tsp
Binary file not shown.
29 changes: 29 additions & 0 deletions clingo/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Charles Incorvia (Ci2923)

The traveling salesman problem implemented in Clingo.

Clingo is a declarative programming languaged descened from Prolog.

The clingo implementation is declarative, and thus not exactly the greedy algorithm, but in the background gringo is using greedy algorithms.

In order to run this program, clingo 4.5.4 or later is required.

It's available free here : http://potassco.sourceforge.net/

The command to run the program is:

clingo tsp.lp

Clingo does not take inputs from I/O, so all of the information regarding the traveling salesman problem is within the file.

The path() terms indicate the paths from city to city available to the salesman.
The place() terms are the cities and their names.
The const t is the number of cities the salesman is planning to visit.
The first location variable indicates the salesman's starting city.

The program will output UNSATISFIABLE if there's no way to visit all of the cities and return to the home city without visiting any cities twice.

Otherwise, it will output SATISFIABLE and list the steps to the smallest possible path in the form of:

travel(City, Time).

67 changes: 67 additions & 0 deletions clingo/tsp.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
% THE TRAVELING SALESMAN

% All of the paths from city to city
path(austin, houston, 238; houston, dallas, 163; dallas, austin, 212).

%Paths are bidirectional
path(City1, City2) :- path(City2, City1).

% A list of all of the cities
place(austin; houston; dallas).

% number of cities on tour
#const t=3.

%start location
location(austin, 0).

% The salesman must end in his home city
:- not location(City, t), location(City, 0).







% choose where to travel at each timestep
% there are exactly as many cities as there are timesteps
{ travel(City, Time) : place(City) } 1 :- Time = 0..t-1.

%if the salesman travels TO somewhere, he's already been there
%this way, we can travel to our start city at the end
visited(City, Time) :- travel(City, Time).

%we can't travel to a city we've been to before
:- travel(City, Time1), visited(City, Time2), Time1 > Time2.

%you can't go anywhere there is not a path to
:- travel(City_end, Time), location(City_start, Time), not path(City_start, City_end, _).

%if you travel from somewhere too somewhere, distance happens
distance(D) :- travel(City_end, Time), location(City_start, Time), path(City_start, City_end, D).


%can't travel to where you already are
:- travel(City, Time), location(City, Time).

%Traveling gets you places
location(City, Time + 1) :- travel(City, Time).


%uniqueness of location
-location(City2, T) :- location(City1, T), place(City2), City1 != City2.

%commonsense intertia
location(City, T + 1) :- location(City, T), not -location(City, T + 1), place(City), T = 0..t-1.


% every city must be visited by the end
:- place(City), not visited(City, _).

% tell clingo to keep trying until it fins a solution with optimal distance.
total(N) :- N = #sum{L : distance(L)}.
#minimize{N : total(N)}.

#show total/1.
#show travel/2.
File renamed without changes.
123 changes: 123 additions & 0 deletions go/greedy/99tsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"fmt"
"bufio"
"os"
"strings"
"strconv"
"math"
"math/rand"
)

// Run the program.
func main() {
fmt.Println("99 Traveling Salespeople Problem - go version")
parseInput()
}

// Read information from the input file.
func parseInput() {

// Open the input file.
f, _ := os.Open(os.Args[1])
defer f.Close()

// Use a scanner to read lines from the input file.
scanner := bufio.NewScanner(f)
arraySize := 0
j := 0

// Skip first few lines and get the number of dimension.
for (scanner.Scan()) {
line := scanner.Text()
str := strings.Fields(line)

// Get the number of dimension.
if (strings.Contains(str[0], "DIMENSION")) {
for i := 1; i < len(str); i++ {
nextToken, _ := strconv.Atoi(str[i])
if (nextToken != 0) {
arraySize = nextToken
}
}
break
}
}

// Create two arrays to store x and y coordinates respectively.
x := make([]float64, arraySize + 1)
y := make([]float64, arraySize + 1)

// Read all x, y coordinates of all points.
for (scanner.Scan()) {
line := scanner.Text()
str := strings.Fields(line)
firstToken, _ := strconv.Atoi(str[0])
if (firstToken != 0 && line != "EOF") {
j++
x[j], _ = strconv.ParseFloat(str[1], 64)
y[j], _ = strconv.ParseFloat(str[2], 64)
}
if (str[0] == "EOF") {
break
}
}
findRoute(x, y, arraySize)
}

// Find the closest point to this point.
func findNearNeighbor(x []float64, y []float64, points map[int]int, start int) int {
neighbor := 0
smDist := 10000000.0

// Loop through all yet visited points to find out the cloesest point to current point.
for i := 1; i < len(points); i++ {
if (i != start) && (points[i] != 0) {
sqSum := math.Pow(x[i] - x[start], 2) + math.Pow(y[i] - y[start], 2)
dist := math.Sqrt(sqSum)
if dist < smDist {
neighbor = i
smDist = dist
}
}
}
return neighbor
}

// Use greedy algorithm to find the solution route.
func findRoute(x []float64, y []float64, arraySize int) {

// Randomly generate a start point.
startpoint := rand.Intn(arraySize) + 1

// Create an array to save the order of points to visit as the solution.
solutionRoute := make([]int, arraySize + 1)
solutionRoute[1] = startpoint

// Create a map to store the points which have not been visited yet.
unvisitedPoints := make(map[int]int)
for l := 0; l <= arraySize; l++ {
unvisitedPoints[l] = l
}
unvisitedPoints[startpoint] = 0

// Find the cloesest point to the current one and go to that point, and repeat.
for k := 2; k <= arraySize; k++ {
nearNeighbor := findNearNeighbor(x, y, unvisitedPoints, startpoint)
solutionRoute[k] = nearNeighbor
startpoint = nearNeighbor
unvisitedPoints[nearNeighbor] = 0
}
printResult(solutionRoute)
}

// Print out the solution route, in the format of "LocationNumber-LocationNumber-LocationNumber-...".
func printResult(solutionRoute []int) {
for z := 1; z < len(solutionRoute); z++ {
fmt.Print(solutionRoute[z], "-")
}

// Prints out the start point because it requires to return to the city started.
fmt.Println(solutionRoute[1])
}
2 changes: 2 additions & 0 deletions go/greedy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
go run 99tsp.go input/a280.tsp
8 changes: 7 additions & 1 deletion go/greedy/README.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Go
Chuqi Zhou (cz4792)

This "99 Traveling Salespeople Problem - go version" program finds out the shortest path using greedy algorithm in Go language. It visits every city exactly once and returns to the city you started from.

To run this "99 Traveling Salespeople Problem - go version" program, just enter "make run" in the command line, the default input is "a280.tsp". You can also enter "go run 99tsp.go input/<filename>"

The output solution route will be in the format of "LocationNumber-LocationNumber-LocationNumber-...". For example, the solution is from location1 to location2 and to location3 finally, the output will be "1-2-3".
Loading

0 comments on commit c808420

Please sign in to comment.