-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/rellermeyer/99tsp
- Loading branch information
Showing
92 changed files
with
110,063 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ data | |
*.class | ||
*~ | ||
*.swp | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
0
java/sa/fakefile.java → cpp/gen/README.txt
100644 → 100755
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
run: | ||
go run 99tsp.go input/a280.tsp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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". |
Oops, something went wrong.