-
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 github.com:rellermeyer/99tsp
- Loading branch information
Showing
133 changed files
with
276,776 additions
and
9 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
data | ||
*.class |
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,29 @@ | ||
.SUFFIXES: .R | ||
|
||
|
||
TSPLIB_MIRROR:=http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95 | ||
TSPLIB:=$(TSPLIB_MIRROR)/tsp | ||
TSPLIB_XML:=$(TSPLIB_MIRROR)/XML-TSPLIB/instances | ||
TSPLIB_280:=a280 | ||
|
||
DATA_DIR:=data | ||
|
||
TEST_FILE:=$(CURDIR)/$(DATA_DIR)/$(TSPLIB_280) | ||
|
||
|
||
MAIN = master | ||
RFILES = $(RNWINCLUDES:=.R) | ||
|
||
R: $(RFILES) | ||
|
||
view: all | ||
acroread $(MAIN).pdf & | ||
|
||
.Rnw.R: | ||
R CMD Stangle $< | ||
|
||
clean: | ||
rm -fv $(RFILES) | ||
rm -fv *.aux *.dvi *.log *.toc *.bak *~ *.blg *.bbl *.lot *.lof | ||
rm -fv *.nav *.snm *.out *.pyc \#*\# _region_* _tmp.* *.vrb | ||
rm -fv Rplots.pdf *.RData |
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,96 @@ | ||
|
||
#Name: Shuoyi Yang | ||
#UTEID: sy6955 | ||
|
||
#CS 345 | ||
#Greed algorithm for TSP. | ||
|
||
|
||
#reading data from the input path | ||
read.data<-function(){ | ||
filepath = readline("Enter data file:") | ||
data = read.table(filepath,skip =6, header=FALSE,nrow=length(readLines(filepath))-7) | ||
return(data) | ||
} | ||
|
||
#calculate the euclidean distance between two node | ||
distance <- function(x1,x2){ | ||
return (sqrt(sum((x1-x2)^2))) | ||
} | ||
|
||
|
||
#find the nearest node from the remaining list | ||
get.nearest<- function(remain.nodes,current){ | ||
i = 0 | ||
shortest.dist = 0 | ||
min = c() | ||
current=current[2:3] | ||
rec = numeric(nrow(remain.nodes)) | ||
id = remain.nodes[,1] | ||
cor = remain.nodes[,2:3] | ||
for(i in 1:nrow(remain.nodes)){ | ||
#store the distance of nodes into a vector | ||
cur.dist = distance(current,cor[i,]) | ||
min = rbind(min,c(i,id[i],cur.dist)) | ||
} | ||
min.node = min[which.min(min[,3]),] | ||
return (min.node) | ||
} | ||
|
||
|
||
#get the list of nodeId and minimum distance by greedy algorithm | ||
|
||
|
||
#find the shortest path for any start point | ||
any.start.shortest.path<-function(data){ | ||
min.total.dist = 0 | ||
best.result = c() | ||
for (i in 1:nrow(data)){ | ||
total.dist = 0 | ||
visit.nodes = c() | ||
current = data[i,] | ||
visit.nodes = current | ||
#remain.nodes = subset(data,V1!=i) | ||
remain.nodes = data[-i,] | ||
|
||
while(nrow(remain.nodes)>0) { | ||
current = tail(visit.nodes,n=1) | ||
next.node = get.nearest(remain.nodes,current) | ||
visit.nodes = rbind(visit.nodes,data[next.node[2],]) | ||
remain.nodes = remain.nodes[-next.node[1],] | ||
total.dist = total.dist + next.node[3] | ||
} | ||
if(total.dist < min.total.dist){ | ||
min.total.dist = total.dist | ||
best.result = visit.nodes[,1] | ||
} | ||
} | ||
print(paste("minimum distance:",min.total.dist)) | ||
print(best.result) | ||
} | ||
|
||
#find the shortest path from node 1 | ||
shortest.path<-function(data){ | ||
first.node = data[1,] | ||
remain.nodes = data[-1,] | ||
total.dist = 0 | ||
visit.nodes=first.node | ||
while(nrow(remain.nodes)>0) { | ||
current = tail(visit.nodes,n=1) | ||
next.node = get.nearest(remain.nodes,current) | ||
visit.nodes = rbind(visit.nodes,data[next.node[2],]) | ||
remain.nodes = remain.nodes[-next.node[1],] | ||
total.dist = total.dist + next.node[3] | ||
} | ||
print(paste("minimum distance :",total.dist)) | ||
print(visit.nodes[,1]) | ||
} | ||
|
||
#main function | ||
main<-function(){ | ||
shortest.path(read.data()) | ||
#any.start.shortest.path(read.data()) | ||
} | ||
|
||
|
||
if(interactive()) main() |
Empty file.
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,4 @@ | ||
all: tsp.c | ||
gcc -g -std=c99 -w -o tsp tsp.c -lm | ||
clean: | ||
$(RM) 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Greedy solution to TSP written in c. | ||
Only works with EUC_2D inputs in a .tsp format. |
Oops, something went wrong.