-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_cluster.go
295 lines (259 loc) · 7.66 KB
/
model_cluster.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
"github.com/nextmv-io/nextroute/common"
)
// Cluster is both a constraint and an objective that limits/prefers the
// vehicles a plan cluster will be added to. If used as a constraint a plan
// cluster can only be added to a vehicles whose centroid is closer to the plan
// cluster than the centroid of any other vehicle. In case of using it as an
// objective, those vehicles will be preferred.
type Cluster interface {
ConstraintStopDataUpdater
ModelConstraint
ModelObjective
// IncludeFirst returns whether the first stop of the vehicle is included in the
// centroid calculation. The centroid is used to determine the distance
// between a new stop and the cluster.
IncludeFirst() bool
// IncludeLast returns whether the last stop of the vehicle is included in
// the centroid calculation. The centroid is used to determine the distance
// between a new stop and the cluster.
IncludeLast() bool
// SetIncludeFirst sets whether the first stop of the vehicle is included in
// the centroid calculation. The centroid is used to determine the distance
// between a new stop and the cluster.
SetIncludeFirst(includeFirst bool)
// SetIncludeLast sets whether the last stop of the vehicle is included in
// the centroid calculation. The centroid is used to determine the distance
// between a new stop and the cluster.
SetIncludeLast(includeLast bool)
}
// NewCluster creates a new cluster component. It needs to be added as a
// constraint or as an objective to the model to be taken into account.
// By default, the first and last stop of a vehicle are not included in the
// centroid calculation.
func NewCluster() (Cluster, error) {
return &clusterImpl{
modelConstraintImpl: newModelConstraintImpl(
"cluster",
ModelExpressions{},
),
includeFirst: false,
includeLast: false,
}, nil
}
// Implements Cluster.
type clusterImpl struct {
modelConstraintImpl
includeFirst bool
includeLast bool
}
func (l *clusterImpl) IncludeFirst() bool {
return l.includeFirst
}
func (l *clusterImpl) IncludeLast() bool {
return l.includeLast
}
func (l *clusterImpl) SetIncludeFirst(includeFirst bool) {
l.includeFirst = includeFirst
}
func (l *clusterImpl) SetIncludeLast(includeLast bool) {
l.includeLast = includeLast
}
type centroidData struct {
location common.Location
// this will be 0.0 for all stops but the last and only if this is used as
// an objective.
compactness float64
}
func (c *centroidData) Copy() Copier {
return ¢roidData{
location: c.location,
compactness: c.compactness,
}
}
func (c *centroidData) String() string {
return fmt.Sprintf("%v", c.location)
}
func (l *clusterImpl) String() string {
return fmt.Sprintf("%v", l.name)
}
func (l *clusterImpl) EstimationCost() Cost {
return LinearVehicle
}
func (l *clusterImpl) UpdateObjectiveStopData(
solutionStop SolutionStop,
) (Copier, error) {
return l.updateData(solutionStop, true)
}
func (l *clusterImpl) UpdateConstraintStopData(
solutionStop SolutionStop,
) (Copier, error) {
return l.updateData(solutionStop, false)
}
func (l *clusterImpl) updateData(
solutionStop SolutionStop,
asObjective bool,
) (Copier, error) {
if solutionStop.IsFirst() {
location, err := common.NewLocation(0, 0)
if err != nil {
return nil, err
}
return ¢roidData{
location: location,
}, nil
}
if solutionStop.IsLast() {
if asObjective {
centroid := solutionStop.Previous().ObjectiveData(l).(*centroidData)
stops := l.getSolutionStops(solutionStop.vehicle())
compact := compactness(stops, centroid.location, SolutionStop{}, false)
centroid.compactness = compact
return centroid, nil
}
return solutionStop.Previous().ConstraintData(l).(*centroidData), nil
}
nrStops := solutionStop.Position()
var centroid *centroidData
if asObjective {
centroid = solutionStop.Previous().ObjectiveData(l).(*centroidData)
} else {
centroid = solutionStop.Previous().ConstraintData(l).(*centroidData)
}
location, err := common.NewLocation(
centroid.location.Longitude()+
(solutionStop.modelStop().Location().Longitude()-
centroid.location.Longitude())/float64(nrStops),
centroid.location.Latitude()+
(solutionStop.modelStop().Location().Latitude()-
centroid.location.Latitude())/float64(nrStops),
)
if err != nil {
return nil, err
}
return ¢roidData{
location: location,
}, nil
}
func compactness(
stops []SolutionStop,
centroid common.Location,
newStop SolutionStop,
newStopIsSet bool,
) float64 {
if newStopIsSet {
numberOfStops := len(stops)
location := newStop.modelStop().location
newLat := (centroid.Latitude()*float64(numberOfStops) +
location.Latitude()) / float64(numberOfStops+1)
newLong := (centroid.Longitude()*float64(numberOfStops) +
location.Longitude()) / float64(numberOfStops+1)
newLocation, err := common.NewLocation(newLong, newLat)
if err != nil {
panic(err)
}
centroid = newLocation
stops = append(stops, newStop)
}
compactness := 0.0
for _, stop := range stops {
dist := haversineDistance(centroid, stop.modelStop().Location())
compactness += dist.Value(common.Meters) * dist.Value(common.Meters)
}
return compactness
}
func (l *clusterImpl) EstimateDeltaValue(
move SolutionMoveStops,
) float64 {
score, _ := l.estimateDeltaScore(
move,
false,
)
return score
}
func (l *clusterImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
score, hint := l.estimateDeltaScore(
move,
true,
)
return score != 0.0, hint
}
func (l *clusterImpl) estimateDeltaScore(
move SolutionMoveStops,
asConstraint bool,
) (deltaScore float64, stopPositionsHint StopPositionsHint) {
solutionImpl := move.Solution().(*solutionImpl)
moveImpl := move.(*solutionMoveStopsImpl)
stopPositions := moveImpl.stopPositions
deltaScore = 0.0
for _, stopPosition := range stopPositions {
vehicle := moveImpl.vehicle()
if vehicle.IsEmpty() {
return deltaScore, constNoPositionsHint
}
candidate := stopPosition.Stop()
var c *centroidData
if asConstraint {
c = vehicle.Last().ConstraintData(l).(*centroidData)
} else {
c = vehicle.Last().ObjectiveData(l).(*centroidData)
}
centroid := c.location
if asConstraint {
distanceToCentroid := haversineDistance(
centroid,
candidate.modelStop().Location(),
).Value(common.Meters)
for _, otherVehicle := range solutionImpl.vehicles {
if otherVehicle.IsEmpty() ||
otherVehicle.Index() == vehicle.Index() {
continue
}
centroidOtherVehicle := otherVehicle.
Last().
ConstraintData(l).(*centroidData).location
if haversineDistance(
centroidOtherVehicle,
candidate.modelStop().Location(),
).Value(common.Meters) < distanceToCentroid {
return 1.0, constSkipVehiclePositionsHint
}
}
} else {
stops := l.getSolutionStops(vehicle)
deltaScore += compactness(stops, centroid, candidate, true)
// we want to compute the difference in compactness, so we need to
// subtract the compactness of the current route
deltaScore -= c.compactness
}
}
return deltaScore, constNoPositionsHint
}
func (l *clusterImpl) getSolutionStops(vehicle SolutionVehicle) []SolutionStop {
stops := make([]SolutionStop, 0, vehicle.NumberOfStops())
for _, stop := range vehicle.SolutionStops() {
if stop.IsFirst() && !l.includeFirst {
continue
}
if stop.IsLast() && !l.includeLast {
continue
}
stops = append(stops, stop)
}
return stops
}
func (l *clusterImpl) Value(solutionStop Solution) float64 {
sum := 0.0
for _, vehicle := range solutionStop.(*solutionImpl).vehiclesMutable() {
if vehicle.IsEmpty() {
continue
}
sum += vehicle.Last().ObjectiveData(l).(*centroidData).compactness
}
return sum
}