Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rellermeyer/99tsp
Browse files Browse the repository at this point in the history
  • Loading branch information
giladoved committed May 11, 2016
2 parents 9f8107a + 9d2cbe0 commit ff3bcfa
Show file tree
Hide file tree
Showing 133 changed files with 276,776 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
data
*.class
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
MODULES:=java/dummy
#cpp/greedy
#rust/2opt

TSPLIB_MIRROR:=http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95
TSPLIB:=$(TSPLIB_MIRROR)/tsp
Expand All @@ -7,7 +9,7 @@ TSPLIB_280:=a280

DATA_DIR:=data

TEST_FILE:=$(CURDIR)/$(DATA_DIR)/TSPLIB_280
TEST_FILE:=$(CURDIR)/$(DATA_DIR)/$(TSPLIB_280)

.PHONY: compile run clean depclean $(MODULES)

Expand Down
29 changes: 29 additions & 0 deletions R/greedy/Makefile
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
96 changes: 96 additions & 0 deletions R/greedy/TSP.R
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 removed c/greedy/Brandon Post.txt
Empty file.
4 changes: 4 additions & 0 deletions c/greedy/Makefile
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
2 changes: 2 additions & 0 deletions c/greedy/README.txt
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.
Loading

0 comments on commit ff3bcfa

Please sign in to comment.