-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
99 lines (90 loc) · 2.02 KB
/
Copy pathinput.go
File metadata and controls
99 lines (90 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type TSPProbelmEUC struct {
name string
comment string
probType string
dimension int
edgeWeightType string
initCities []City
}
func convertFloatXY(x string, y string) (float64, float64) {
xf, err1 := strconv.ParseFloat(x, 64)
if err1 != nil {
panic(err1)
}
yf, err2 := strconv.ParseFloat(y, 64)
if err2 != nil {
panic(err2)
}
return xf, yf
}
func convInt(x string) int {
xi, err1 := strconv.Atoi(x)
if err1 != nil {
panic(err1)
}
return xi
}
func readEucTSPFile(fp string) TSPProbelmEUC {
f, err := os.Open(fp)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var pname, pcomment, ptype, pedgewtype string
var pdimension int
scanner := bufio.NewScanner(f)
var path []City
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "NAME") {
parts := strings.Split(line, ":")
pname = parts[1]
} else if strings.Contains(line, "COMMENT") {
parts := strings.Split(line, ":")
pcomment = parts[1]
} else if strings.Contains(line, "DIMENSION") {
parts := strings.Split(line, ":")
pdimension = convInt(strings.ReplaceAll(parts[1], " ", "")) //need to convert
} else if strings.Contains(line, "TYPE") {
parts := strings.Split(line, ":")
ptype = parts[1] //need to convert
} else if strings.Contains(line, "EDGE_WEIGHT_TYPE") {
parts := strings.Split(line, ":")
pedgewtype = parts[1]
} else if strings.Contains(line, "NODE_COORD_SECTION") {
fmt.Print("")
} else if strings.Contains(line, "EOF") {
break
} else {
parts := strings.Split(line, " ")
x, y := convertFloatXY(parts[1], parts[2])
newCity := City{
id: parts[0],
x: x,
y: y,
}
path = append(path, newCity)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
data := TSPProbelmEUC{
name: pname,
comment: pcomment,
probType: ptype,
dimension: pdimension,
edgeWeightType: pedgewtype,
initCities: path,
}
return data
}