2014-12-21 3 views
0

TSP 문제가 해결되었으므로 출력 파일이 2 개 있습니다.x, y 그래프는 R을 사용하여 점을 그리며 점을 그립니다.

File1. 모든 도시의 (x, y) 값을 포함합니다.

I이 다음과 같은 코드를 사용하여 포인트 플롯 가지고

d = read.table('city_data.txt', sep=" ", col.names=c("cityX", "cityY")); 

plot(d$cityX,d$cityY); 

파일 2> 22-> 3-> 25- (21)을 가정 접속 노드 N의 시퀀스를 포함 .......... .

city_data.txt ROW1의 X가, 노드 1의 Y 값 ROW2 노드의 베일을 나타내 되 파일

.....

제가 25-> 22-> 3-> (21) 사이의 선을 그릴 수있는 방법 ...............

+0

'플롯 (cityY ~ cityX는 D [파일 2 $으로 노드, = "B 입력 ")'는'file2 $ node'의 순서로 점과 선을 그려야합니다. [이 게시물] (http://stackoverflow.com/questions/27363653/find-shortest-path-from-x-y-coordinates/27386611#27386611)을 참조하십시오. – jlhoward

답변

0

다음은 작동하는 전자 메일입니다. xample :

### Create a sample input data.frame 
d <- read.csv(text= 
'cityX,cityY 
1.0,1.0 
2.0,3.0 
3.0,2.0 
2.0,1.0 
') 

# this line just add a row to close the TSP "tour" 
# (i.e. the salesman returning to the first city) 
# if you don't want it, just remove this line 
d <- rbind(d,d[1,]) 
### 

### PLOT 1 - LINES BETWEEN CITIES 
# plot the cities and the lines connecting them 
plot(x=d$cityX,y=d$cityY,type='b',xlab='X',ylab='Y') 
### 


### PLOT 2 - ARROWS BETWEEN CITIES 
# plot the cities 
plot(x=d$cityX,y=d$cityY,type='p',xlab='X',ylab='Y') 
# plot the arrows connecting them 
arrows(x0=head(d$cityX,-1), 
     y0=head(d$cityY,-1), 
     x1=tail(d$cityX,-1), 
     y1=tail(d$cityY,-1)) 
### 

PLOT 1

PLOT 1

PLOT 2

PLOT 2

관련 문제