tspreader.go 2.36 KB
Newer Older
CedricPump's avatar
- init    
CedricPump committed
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
package tspreader

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

type TspSpec struct {
	name           string
	problemType    string
	comment        string
	dimension      int
	edgeWeightType string
	edgeWeightFormat string
	data           *TspData
}

type TspData struct {
	datatype string
	values*[][] float64
}

func NewTspData() *TspSpec {
	return &TspSpec{name: "", problemType: "TSP", comment: "", dimension: 0, edgeWeightType: "", data: NewTspDataArray()}
}

func NewTspDataArray() *TspData {
	return &TspData{datatype: "EDGE_WEIGHT_SECTION", values: &[][]float64{}}
}

func (t TspSpec) String() string {
37
38
39
40
41
	return fmt.Sprintf("%s [%s] - %s %s %d", t.name, t.problemType, t.edgeWeightType, t.edgeWeightFormat, t.dimension)
}

func (t TspData) String() string {
	return fmt.Sprintf("Datatype: %s", t.datatype)
CedricPump's avatar
- init    
CedricPump committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}

// Read Reads TSP data from .tsp file
func Read(path string) (*TspSpec, error) {
	file, err := os.Open(path)
	if(err != nil) {
		log.Fatal(err)
		return nil, errors.New("unable to open file")
	}
	scanner := bufio.NewScanner(file)

	spec := NewTspData()
	dataLine := -1
	arr := [][]float64{}

	for scanner.Scan() {
		line := scanner.Text()
59
60

		// handle Specification Headers
CedricPump's avatar
- init    
CedricPump committed
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
		if strings.Contains(line,":") {
			strs := strings.Split(line, ":")
			switch strings.ReplaceAll(strs[0]," ","") {
			case "NAME": {
				spec.name = strings.ReplaceAll(strs[1]," ","")
			}
			case "TYPE": {
				spec.problemType = strings.ReplaceAll(strs[1]," ","")
			}
			case "COMMENT": {
				spec.comment = strings.ReplaceAll(strs[1]," ","")
			}
			case "DIMENSION": {
				i, e := strconv.ParseInt(strings.ReplaceAll(strs[1]," ",""),10,64)
				if(e == nil) {
					spec.dimension = int(i)
				}
			}
			case "EDGE_WEIGHT_TYPE": {
				spec.edgeWeightType = strings.ReplaceAll(strs[1]," ","")
			}
			case "EDGE_WEIGHT_FORMAT": {
				spec.edgeWeightFormat = strings.ReplaceAll(strs[1]," ","")
			}
			}
		} else {
87
			// Sace Data as Multi Dimensional Array
CedricPump's avatar
- init    
CedricPump committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
			if dataLine == -1 {
				spec.data.datatype = strings.ReplaceAll(line," ","")
				dataLine++
			} else {
				entries := strings.Split(line, " ")

				arr = append(arr, []float64{})

				for _, e := range entries{
					f, err := strconv.ParseFloat(e, 64)
					if err == nil {
						arr[dataLine] = append(arr[dataLine], f)
					}
				}
				dataLine++
			}
		}

	}
	spec.data.values = &arr
108
109

	// return specification
CedricPump's avatar
- init    
CedricPump committed
110
111
	return spec, nil
}