Skip to content

Commit

Permalink
Remove superfluous function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkschumacher committed May 19, 2024
1 parent bda4e1a commit 8acf332
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 104 deletions.
2 changes: 1 addition & 1 deletion model_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (l *clusterImpl) estimateDeltaScore(
return deltaScore, constNoPositionsHint
}

candidate := stopPosition.stop()
candidate := stopPosition.Stop()

var c *centroidData
if asConstraint {
Expand Down
2 changes: 1 addition & 1 deletion model_constraint_maximum_stops.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (l *maximumStopsConstraintImpl) EstimateIsViolated(
stopPositions := moveImpl.stopPositions
nrStopsToBeAddedToSolution := len(stopPositions)

beforeStop := stopPositions[len(stopPositions)-1].next()
beforeStop := stopPositions[len(stopPositions)-1].Next()
vehicle := beforeStop.vehicle()

vehicleType := vehicle.ModelVehicle().VehicleType().Index()
Expand Down
12 changes: 6 additions & 6 deletions model_constraint_no_mix.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,19 @@ func (l *noMixConstraintImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)
_, hasRemoveMixItem := l.remove[moveImpl.stopPositions[0].stop().ModelStop()]
_, hasRemoveMixItem := l.remove[moveImpl.stopPositions[0].Stop().ModelStop()]
if hasRemoveMixItem {
return true, constNoPositionsHint
}

previousStopImp := moveImpl.stopPositions[0].previous()
previousStopImp := moveImpl.stopPositions[0].Previous()
previousNoMixData := previousStopImp.ConstraintData(l).(*noMixSolutionStopData)
contentName := previousNoMixData.content.Name
contentQuantity := previousNoMixData.content.Quantity

deltaQuantity := 0

insertMixItem, hasInsertMixItem := l.insert[moveImpl.stopPositions[0].stop().ModelStop()]
insertMixItem, hasInsertMixItem := l.insert[moveImpl.stopPositions[0].Stop().ModelStop()]
if hasInsertMixItem {
if contentName != insertMixItem.Name && previousNoMixData.content.Quantity != 0 {
return true, constNoPositionsHint
Expand All @@ -377,22 +377,22 @@ func (l *noMixConstraintImpl) EstimateIsViolated(
}

for idx := 1; idx < len(moveImpl.stopPositions); idx++ {
previousStopImp = moveImpl.stopPositions[idx].previous()
previousStopImp = moveImpl.stopPositions[idx].Previous()
if previousStopImp.IsPlanned() {
previousNoMixData = previousStopImp.ConstraintData(l).(*noMixSolutionStopData)
if previousNoMixData.tour != tour || previousNoMixData.content.Name != contentName {
return true, constNoPositionsHint
}
}
insertMixItem, hasInsertMixItem = l.insert[moveImpl.stopPositions[idx].stop().ModelStop()]
insertMixItem, hasInsertMixItem = l.insert[moveImpl.stopPositions[idx].Stop().ModelStop()]
if hasInsertMixItem {
if contentName != insertMixItem.Name {
return true, constNoPositionsHint
}
deltaQuantity += insertMixItem.Quantity
continue
}
removeMixItem, hasRemoveMixItem := l.remove[moveImpl.stopPositions[idx].stop().ModelStop()]
removeMixItem, hasRemoveMixItem := l.remove[moveImpl.stopPositions[idx].Stop().ModelStop()]
if hasRemoveMixItem {
if contentName != removeMixItem.Name || contentQuantity+deltaQuantity < removeMixItem.Quantity {
return true, constNoPositionsHint
Expand Down
2 changes: 1 addition & 1 deletion solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (s *solutionImpl) addInitialSolution(m Model) error {
)
}
for _, position := range move.(*solutionMoveStopsImpl).stopPositions {
position.stop().detach()
position.Stop().detach()
}
infeasiblePlanUnits[solutionPlanUnit] = true
continue
Expand Down
108 changes: 54 additions & 54 deletions solution_move_stops.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ func (m *solutionMoveStopsImpl) Vehicle() SolutionVehicle {
if len(m.stopPositions) == 0 {
return SolutionVehicle{}
}
return m.stopPositions[len(m.stopPositions)-1].next().Vehicle()
return m.stopPositions[len(m.stopPositions)-1].Next().Vehicle()
}

func (m *solutionMoveStopsImpl) vehicle() SolutionVehicle {
return m.stopPositions[len(m.stopPositions)-1].next().vehicle()
return m.stopPositions[len(m.stopPositions)-1].Next().vehicle()
}

func (m *solutionMoveStopsImpl) Next() SolutionStop {
Expand All @@ -150,7 +150,7 @@ func (m *solutionMoveStopsImpl) next() (SolutionStop, bool) {
if len(m.stopPositions) == 0 {
return SolutionStop{}, false
}
return m.stopPositions[len(m.stopPositions)-1].next(), true
return m.stopPositions[len(m.stopPositions)-1].Next(), true
}

func (m *solutionMoveStopsImpl) Previous() SolutionStop {
Expand All @@ -165,7 +165,7 @@ func (m *solutionMoveStopsImpl) previous() (SolutionStop, bool) {
if len(m.stopPositions) == 0 {
return SolutionStop{}, false
}
return m.stopPositions[0].previous(), true
return m.stopPositions[0].Previous(), true
}

func (m *solutionMoveStopsImpl) Execute(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (m *solutionMoveStopsImpl) Execute(_ context.Context) (bool, error) {
}

for _, position := range m.stopPositions {
position.stop().detach()
position.Stop().detach()
}

constraint, _, err := m.planUnit.solution().isFeasible(startPropagate, true)
Expand All @@ -224,9 +224,9 @@ func (m *solutionMoveStopsImpl) attach() (int, error) {
startPropagate := -1
for i := len(m.stopPositions) - 1; i >= 0; i-- {
stopPosition := m.stopPositions[i]
m.planUnit.solutionStops[i] = stopPosition.stop()
beforeStop := stopPosition.next()
if stopPosition.stop().IsPlanned() {
m.planUnit.solutionStops[i] = stopPosition.Stop()
beforeStop := stopPosition.Next()
if stopPosition.Stop().IsPlanned() {
return -1, fmt.Errorf(
"stop %v is already planned",
stopPosition.Stop(),
Expand All @@ -238,7 +238,7 @@ func (m *solutionMoveStopsImpl) attach() (int, error) {
beforeStop,
)
}
startPropagate = stopPosition.stop().attach(
startPropagate = stopPosition.Stop().attach(
beforeStop.PreviousIndex(),
)
}
Expand Down Expand Up @@ -321,20 +321,20 @@ func (m *solutionMoveStopsImpl) TakeBest(that SolutionMove) SolutionMove {
func (m *solutionMoveStopsImpl) deltaStopTravelDurationValue(
vehicleType ModelVehicleType,
) float64 {
if len(m.stopPositions) == 0 || m.stopPositions[0].stop().IsPlanned() {
if len(m.stopPositions) == 0 || m.stopPositions[0].Stop().IsPlanned() {
return 0
}
deltaStopDurationValue := 0.0
travelDuration := 0.0
vehicleTravelDuration := vehicleType.TravelDurationExpression()
vehicleDuration := vehicleType.DurationExpression()
for _, stopPosition := range m.stopPositions {
modelStop := stopPosition.stop().ModelStop()
nextStop := stopPosition.next().ModelStop()
previousStop := stopPosition.previous().ModelStop()
if stopPosition.next().IsPlanned() {
deltaStopDurationValue -= stopPosition.next().DurationValue()
travelDuration -= stopPosition.next().TravelDurationValue()
modelStop := stopPosition.Stop().ModelStop()
nextStop := stopPosition.Next().ModelStop()
previousStop := stopPosition.Previous().ModelStop()
if stopPosition.Next().IsPlanned() {
deltaStopDurationValue -= stopPosition.Next().DurationValue()
travelDuration -= stopPosition.Next().TravelDurationValue()
travelDuration += vehicleTravelDuration.Value(
vehicleType,
modelStop,
Expand All @@ -361,7 +361,7 @@ func (m *solutionMoveStopsImpl) deltaStopTravelDurationValue(
}

func (m *solutionMoveStopsImpl) deltaTravelDurationValue() float64 {
if len(m.stopPositions) == 0 || m.stopPositions[0].stop().IsPlanned() {
if len(m.stopPositions) == 0 || m.stopPositions[0].Stop().IsPlanned() {
return 0
}

Expand All @@ -373,7 +373,7 @@ func (m *solutionMoveStopsImpl) deltaTravelDurationValue() float64 {

if isDependentOnTime {
if len(m.stopPositions) == 1 {
solutionStop := m.stopPositions[0].stop()
solutionStop := m.stopPositions[0].Stop()
previousStop, _ := m.previous()
departure := previousStop.EndValue()
fromDuration, _, _, _ := vehicleType.TemporalValues(
Expand Down Expand Up @@ -422,18 +422,18 @@ func (m *solutionMoveStopsImpl) deltaTravelDurationValue() float64 {
travelDuration := 0.0

for _, stopPosition := range m.stopPositions {
modelStop := stopPosition.stop().ModelStop()
if stopPosition.next().IsPlanned() {
travelDuration -= stopPosition.next().TravelDurationValue()
modelStop := stopPosition.Stop().ModelStop()
if stopPosition.Next().IsPlanned() {
travelDuration -= stopPosition.Next().TravelDurationValue()
travelDuration += vehicleType.TravelDurationExpression().Value(
vehicleType,
modelStop,
stopPosition.next().ModelStop(),
stopPosition.Next().ModelStop(),
)
}
travelDuration += vehicleType.TravelDurationExpression().Value(
vehicleType,
stopPosition.previous().ModelStop(),
stopPosition.Previous().ModelStop(),
modelStop,
)
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func newMoveStops(
)
}

vehicle := stopPositions[0].previous().vehicle()
vehicle := stopPositions[0].Previous().vehicle()

lastPlannedPreviousStop := stopPositions[0].Previous()

Expand All @@ -518,102 +518,102 @@ func newMoveStops(
)
}

if stopPosition.previous().IsPlanned() {
if stopPosition.previous().Position() < position {
if stopPosition.Previous().IsPlanned() {
if stopPosition.Previous().Position() < position {
return nil,
fmt.Errorf("previous stop %s of stop position %v is planned at position %v,"+
" which is before or at the last planned previous stop %s at position %v",
stopPosition.previous().ModelStop().ID(),
stopPosition.Previous().ModelStop().ID(),
index,
stopPosition.previous().Position(),
stopPosition.Previous().Position(),
lastPlannedPreviousStop.ModelStop().ID(),
lastPlannedPreviousStop.Position(),
)
}
position = stopPosition.previous().Position()
position = stopPosition.Previous().Position()

lastPlannedPreviousStop = stopPosition.previous()
lastPlannedPreviousStop = stopPosition.Previous()
}
if stopPosition.next().IsPlanned() {
if stopPosition.next().Position() < position {
if stopPosition.Next().IsPlanned() {
if stopPosition.Next().Position() < position {
return nil,
fmt.Errorf("next stop %s of stop position %v is planned at position %v,"+
" which is before or at the last planned previous stop %s at position %v",
stopPosition.next().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
index,
stopPosition.next().Position(),
stopPosition.Next().Position(),
lastPlannedPreviousStop.ModelStop().ID(),
lastPlannedPreviousStop.Position(),
)
}
position = stopPosition.next().Position()
position = stopPosition.Next().Position()
}
if stopPosition.next().IsPlanned() && !stopPosition.previous().IsPlanned() {
if lastPlannedPreviousStop.Position() != stopPosition.next().Position()-1 {
if stopPosition.Next().IsPlanned() && !stopPosition.Previous().IsPlanned() {
if lastPlannedPreviousStop.Position() != stopPosition.Next().Position()-1 {
return nil,
fmt.Errorf("stop positions are not allowed, planned previous stop %v is not adjacent"+
" to the planned next stop %v of the next stop position",
lastPlannedPreviousStop.ModelStop().ID(),
stopPosition.next().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
)
}
}

if stopPosition.next().IsPlanned() && stopPosition.previous().IsPlanned() {
if stopPosition.next().Position() != stopPosition.previous().Position()+1 {
if stopPosition.Next().IsPlanned() && stopPosition.Previous().IsPlanned() {
if stopPosition.Next().Position() != stopPosition.Previous().Position()+1 {
return nil,
fmt.Errorf("stop positions are not allowed, planned previous stop %v is not adjacent"+
" to the planned next stop %v of stop position %v",
stopPosition.previous().ModelStop().ID(),
stopPosition.next().ModelStop().ID(),
stopPosition.Previous().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
index,
)
}
}

if !stopPosition.previous().IsPlanned() {
if stopPositions[index-1].Stop() != stopPosition.previous() {
if !stopPosition.Previous().IsPlanned() {
if stopPositions[index-1].Stop() != stopPosition.Previous() {
return nil,
fmt.Errorf("the previous stop %s of stop position %v"+
" must be the stop %s of the previous stop position %v if it is unplanned",
stopPosition.previous().ModelStop().ID(),
stopPosition.Previous().ModelStop().ID(),
index,
stopPositions[index-1].Stop().ModelStop().ID(),
index-1,
)
}
}

if !stopPosition.next().IsPlanned() {
if stopPositions[index+1].Stop() != stopPosition.next() {
if !stopPosition.Next().IsPlanned() {
if stopPositions[index+1].Stop() != stopPosition.Next() {
return nil,
fmt.Errorf("the next stop %s of stop position %v"+
" must be the stop %s of the next stop position %v if it is unplanned",
stopPosition.next().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
index,
stopPositions[index+1].Stop().ModelStop().ID(),
index+1,
)
}
}

if stopPosition.previous().IsPlanned() && stopPosition.previous().vehicle().index != vehicle.index {
if stopPosition.Previous().IsPlanned() && stopPosition.Previous().vehicle().index != vehicle.index {
return nil,
fmt.Errorf(
"planned previous stop %v of stop position %v vehicle mismatch: %v != %v",
stopPosition.previous().ModelStop().ID(),
stopPosition.Previous().ModelStop().ID(),
index,
stopPosition.previous().vehicle().ModelVehicle().ID(),
stopPosition.Previous().vehicle().ModelVehicle().ID(),
vehicle.ModelVehicle().ID(),
)
}
if stopPosition.next().IsPlanned() && stopPosition.next().vehicle().index != vehicle.index {
if stopPosition.Next().IsPlanned() && stopPosition.Next().vehicle().index != vehicle.index {
return nil,
fmt.Errorf(
"planned next stop %v of stop position %v vehicle mismatch: %v != %v",
stopPosition.next().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
index,
stopPosition.next().vehicle().ModelVehicle().ID(),
stopPosition.Next().vehicle().ModelVehicle().ID(),
vehicle.ModelVehicle().ID(),
)
}
Expand Down
6 changes: 3 additions & 3 deletions solution_move_stops_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ func generate(
stopPositions[positionIdx-1].nextStopIndex = stopPositions[positionIdx].stopIndex
} else {
stopPositions[positionIdx-1].nextStopIndex = target[combination[positionIdx-1]].index
if mustBeNeighbours(model, stopPositions[positionIdx-1].stop(), stopPositions[positionIdx].stop()) {
if mustBeNeighbours(model, stopPositions[positionIdx-1].Stop(), stopPositions[positionIdx].Stop()) {
break
}
}

if isNotAllowed(model, stopPositions[positionIdx-1].stop(), stopPositions[positionIdx-1].next()) {
if isNotAllowed(model, stopPositions[positionIdx-1].Stop(), stopPositions[positionIdx-1].Next()) {
combination = combination[:positionIdx]
if stopPositions[positionIdx-1].nextStopIndex != stopPositions[positionIdx].previousStopIndex {
break
Expand All @@ -235,7 +235,7 @@ func generate(
}
}

if isNotAllowed(model, stopPositions[positionIdx].previous(), stopPositions[positionIdx].stop()) {
if isNotAllowed(model, stopPositions[positionIdx].Previous(), stopPositions[positionIdx].Stop()) {
combination = combination[:positionIdx]
continue
}
Expand Down
2 changes: 1 addition & 1 deletion solution_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (v SolutionStop) Vehicle() SolutionVehicle {
if v.solution.next[v.index] == v.solution.previous[v.index] {
panic("cannot get route of unplanned visit")
}
return v.solution.solutionVehicles[v.solution.inVehicle[v.index]]
return v.vehicle()
}

func (v SolutionStop) vehicle() SolutionVehicle {
Expand Down
Loading

0 comments on commit 8acf332

Please sign in to comment.