-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (171 loc) · 4.31 KB
/
main.go
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
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
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime/pprof"
"time"
"github.com/rhartert/srte-ls/parser"
"github.com/rhartert/srte-ls/solver"
"github.com/rhartert/srte-ls/srte"
)
var flagNetworkFile = flag.String(
"network",
"examples/synth100.graph",
"Path to the network file",
)
var flagDemandFile = flag.String(
"demands",
"examples/synth100.demands",
"Path to the demand file",
)
var flagUseUnaryWeights = flag.Bool(
"unary_weights",
true,
"Use unary weights for the network",
)
var flagScaling = flag.Int64(
"scaling",
1000,
"Scaling factor applied on load/capacity to reduce rounding errors",
)
var flagMaxNodesPerPath = flag.Int(
"max_nodes",
2,
"Maximum number of intermediate nodes per path",
)
var flagMaxIterations = flag.Int(
"max_iterations",
10000,
"Maximum number of iterations for the algorithm",
)
var flagSeed = flag.Int64(
"seed",
42,
"Seed value for the random number generator",
)
var flagAlpha = flag.Float64(
"alpha",
8.0,
"Alpha parameter for edge selection",
)
var flagBeta = flag.Float64(
"beta",
2.0,
"Beta parameter for demand selection",
)
var flagCPUProfile = flag.Bool(
"cpuprof",
false,
"save cpu pprof profile in cpuprof",
)
func parseAndValidateFlags() error {
flag.Parse()
if *flagNetworkFile == "" {
return fmt.Errorf("missing network file")
}
if *flagDemandFile == "" {
return fmt.Errorf("missing demands file")
}
if n := *flagScaling; n <= 0 {
return fmt.Errorf("scaling must be greater than 0, got %d", n)
}
if n := *flagMaxNodesPerPath; n < 0 {
return fmt.Errorf("number of intermediate nodes must be non-negative, got: %d", n)
}
if n := *flagMaxIterations; n < 0 {
return fmt.Errorf("number of iterations must be non-negative, got: %d", n)
}
if n := *flagAlpha; n < 0 {
return fmt.Errorf("parameter alpha must be non-negative, got: %f", n)
}
if n := *flagBeta; n < 0 {
return fmt.Errorf("parameter beta must be non-negative, got: %f", n)
}
return nil
}
func srteState() *srte.SRTE {
network, capacities, err := parser.ParseNetwork(*flagNetworkFile)
if err != nil {
log.Fatalf("Error reading graph file: %s", err)
}
demands, err := parser.ParseDemands(*flagDemandFile)
if err != nil {
log.Fatalf("Error reading demand file: %s", err)
}
if s := *flagScaling; s > 1 {
for i := range demands {
demands[i].Bandwidth *= s
}
for i := range capacities {
capacities[i] *= s
}
}
if *flagUseUnaryWeights {
for i := range network.Edges {
network.Edges[i].Cost = 1
}
}
fgs, err := srte.NewFGraphs(network)
if err != nil {
log.Fatal(err)
}
state, err := srte.NewSRTE(&srte.SRTEInstance{
Graph: network,
FGraphs: fgs,
MaxPathNodes: *flagMaxNodesPerPath + 2, // + source and destination
Demands: demands,
LinkCapacities: capacities,
})
if err != nil {
log.Fatal(err)
}
return state
}
func main() {
if err := parseAndValidateFlags(); err != nil {
log.Fatalf("Error validating flags: %s", err)
}
parseStart := time.Now()
rng := rand.New(rand.NewSource(*flagSeed))
lgs := solver.NewLinkGuidedSolver(srteState(), solver.Config{
Alpha: *flagAlpha,
Beta: *flagBeta,
})
if *flagCPUProfile {
f, err := os.Create("cpuprof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
startUtil := lgs.MaxUtilization()
optStart := time.Now()
for iter := 0; iter < *flagMaxIterations; iter++ {
// Randomly select the next edge to improve. The more utilized an edge
// is, the more likely it is to be selected.
e := lgs.SelectEdge(rng.Float64())
// Select the demand to move. The more a demand contributes to the
// edge's load, the more likely it is to be selected.
d := lgs.SelectDemand(e, rng.Float64())
if d == -1 {
continue // no demand on the edge
}
// Search for a move that reduces the load of the selected edge and does
// not increase the maximum utilization of the network.
move, found := lgs.Search(e, d, lgs.MaxUtilization())
if !found {
continue
}
lgs.ApplyMove(move)
}
totalTime := time.Since(parseStart)
optTime := time.Since(optStart)
fmt.Printf("total time (ms): %v\n", totalTime.Milliseconds())
fmt.Printf("optimization time (ms): %v\n", optTime.Milliseconds())
fmt.Printf("utilization (before): %f\n", startUtil)
fmt.Printf("utilization (after): %f\n", lgs.MaxUtilization())
}