tspsolver.go 4.21 KB
Newer Older
1
2
3
4
5
6
7
// =======================================================
// Author Cedric Pump, Student, Technische Hochschule Lübeck
// =======================================================

package tspsolver

import (
CedricPump's avatar
CedricPump committed
8
	"errors"
9
	"fmt"
CedricPump's avatar
CedricPump committed
10
11
12
	"log"
	"math"
	"math/rand"
13
	"pvs_tsp/tspreader"
CedricPump's avatar
CedricPump committed
14
	"time"
15
16
17
18
19
)

type TspSolution struct {
	path []int
	length float64
CedricPump's avatar
CedricPump committed
20
	strategy string
21
22
23
24
25
26
27
}

// NewTspSolution
// ------------------------------------
// Constructs empty TSP Solution
// ------------------------------------
func NewTspSolution() TspSolution{
CedricPump's avatar
CedricPump committed
28
	return TspSolution{path: []int{}, length: 0, strategy: ""}
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}

// String
// ------------------------------------
// returns string representation of solution
// ------------------------------------
func (t TspSolution) String() string {
	return fmt.Sprintf("solution - length: %f", t.length)
}

// StringFull
// ------------------------------------
// returns full string representation of solution
// ------------------------------------
func (t TspSolution) StringFull() string {
CedricPump's avatar
CedricPump committed
44
	return fmt.Sprintf("solution - length: %f path: [%d, %d, ... %d] strategy: %s", t.length, t.path[0], t.path[1], t.path[len(t.path)-1], t.strategy)
45
46
}

CedricPump's avatar
CedricPump committed
47

48
49
50
51
52
53
54
// Solve
// ------------------------------------
// solve TSP
// ------------------------------------
func Solve(spec tspreader.TspSpec) TspSolution {
	// do tsp magic here

CedricPump's avatar
CedricPump committed
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	// strategies:
	// - Random
	// - Nearest Neighbor
	// - Ant Colony Optimization

	solrand := SolveRandom(spec, 1000000)
	solnear := SolveNearest(spec)

	log.Println("done")

	if solrand.length < solnear.length {
		return solrand
	} else {
		return solnear
	}


}


// SolveRandom
// ------------------------------------
// solve TSP
// ------------------------------------
func SolveRandom(spec tspreader.TspSpec, turns int) TspSolution {

	bestsol := TspSolution{[]int{}, math.MaxFloat64, ""}

	for turn := 0; turn < turns; turn++ {

		sol := NewTspSolution()
		sol.strategy = "Random"

		var locs []int
		for i := 0; i < spec.Dimension; i++ {
			locs = append(locs, i)
		}

		// select random start
		rand.Seed(time.Now().Unix())
		n := rand.Int() % len(locs)
		node := locs[n];
		sol.path = append(sol.path, node)
		locs = removeCopy(locs,n)

		// until all node are taken
		for len(sol.path) < spec.Dimension {

			dists := (*spec.Data.Values)[node]

			n := rand.Int() % len(locs)
			node = locs[n]
			index := indexOf(locs,node)
			dist := dists[node]
			sol.path = append(sol.path, node)
			sol.length = sol.length + dist
			locs = removeElementCopy(locs,index)
		}

		if sol.length < bestsol.length {
			bestsol = sol
		}

		log.Println(bestsol.length)
	}

	return bestsol
}


// SolveNearest
// ------------------------------------
// solve TSP
// ------------------------------------
func SolveNearest(spec tspreader.TspSpec) TspSolution {

	bestsol := TspSolution{[]int{}, math.MaxFloat64, ""}

	for turn := 0; turn < spec.Dimension; turn++ {

		sol := NewTspSolution()
		sol.strategy = "NearestNeighbor"

		var locs []int
		for i := 0; i < spec.Dimension; i++ {
			locs = append(locs, i)
		}

		// start at 0
		n := turn
		node := locs[n];
		sol.path = append(sol.path, node)
		locs = removeCopy(locs,n)

		// until all node are taken
		for len(sol.path) < spec.Dimension {

			dists := (*spec.Data.Values)[node]

			var err error
			var dist float64
			node, dist, err = minNotZero(dists,locs)
			if err != nil {
				panic(err)
			}
			sol.path = append(sol.path, node)
			sol.length = sol.length + dist
			index := indexOf(locs,node)
			locs = removeElementCopy(locs,index)
		}

		if sol.length < bestsol.length {
			bestsol = sol
		}

		log.Println(bestsol.length)
	}

	return bestsol
}

func removeCopy(slice []int, i int) []int {
	copy(slice[i:], slice[i+1:])
	return slice[:len(slice)-1]
}

func removeElementCopy(slice []int, i int) []int {
	copy(slice[i:], slice[i+1:])
	return slice[:len(slice)-1]
}

func minNotZero(slice []float64, locs []int) (int, float64, error) {
	if len(slice) == 0 {
		return 0, 0, errors.New("slice is empty")
	}
	min := math.MaxFloat64
	minIndex := -1
	for i, e := range slice {
		if indexOf(locs,i) > -1 {
			if e < min && e != 0 {
				min = e
				minIndex = i
			}
		}
	}
	return minIndex, min, nil
}

func indexOf(slice []int, item int) int {
	for i, _ := range slice {
		if slice[i] == item {
			return i
		}
	}
	return -1
210
}